Skip to content

Commit

Permalink
#525: Added online state check before downloading (#610)
Browse files Browse the repository at this point in the history
  • Loading branch information
WorkingAmeise authored Oct 1, 2024
1 parent c55cec0 commit a729a9a
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ Release with new features and bugfixes:
* https://github.com/devonfw/IDEasy/issues/52[#52]: Adjusting Intellij settings in ide-settings
* https://github.com/devonfw/IDEasy/issues/588[#588]: ide create installs wrong Java version
* https://github.com/devonfw/IDEasy/issues/650[#650]: Improve default success message of step
* https://github.com/devonfw/IDEasy/issues/593[#593]: Tool error reporting still buggy
* https://github.com/devonfw/IDEasy/issues/593[#593]: Tool error reporting still buggy
* https://github.com/devonfw/IDEasy/issues/651[#651]: IDE not started in background anymore
* https://github.com/devonfw/IDEasy/issues/439[#439]: Refactor and improve tool-dependencies and tomcat
* https://github.com/devonfw/IDEasy/issues/356[#356]: Eclipse plugin installation opens an Eclipse window for each plugin installed
* https://github.com/devonfw/IDEasy/issues/655[#655]: CVE-2024-26308 and library updates
* https://github.com/devonfw/IDEasy/issues/627[#627]: Still log messages break processable command output
* https://github.com/devonfw/IDEasy/issues/525[#525]: Added online state check before downloading
* https://github.com/devonfw/IDEasy/issues/663[#663]: Endless loop when installing Eclipse in force mode
* https://github.com/devonfw/IDEasy/issues/657[#657]: Cannot install Java 8

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.devonfw.tools.ide.cli;

import java.net.URL;
import java.nio.file.Path;

import com.devonfw.tools.ide.process.ProcessResult;
import com.devonfw.tools.ide.version.VersionIdentifier;

/**
* {@link CliException} that is thrown if further processing requires network but the user if offline.
Expand All @@ -20,9 +24,51 @@ public CliOfflineException() {
*
* @param message the {@link #getMessage() message}.
*/
public CliOfflineException(String message) {

private CliOfflineException(String message) {
super(message, ProcessResult.OFFLINE);
}

/**
* Factory method, which is called, when trying to download via a URL
*
* @param url the url, which the software should be downloaded from.
* @return A {@link CliOfflineException} with an informative message.
*/
public static CliOfflineException ofDownloadViaUrl(String url) {
return new CliOfflineException("You are offline and cannot download from URL " + url);
}

/**
* Factory method, which is called, when trying to download via tool name, edition and version
*
* @param tool the name of the tool, we want to download.
* @param edition the edition of the tool, we want to download.
* @param version the {@link VersionIdentifier} of the tool, we want to download.
* @return A {@link CliOfflineException} with an informative message.
*/
public static CliOfflineException ofDownloadOfTool(String tool, String edition, VersionIdentifier version) {
return new CliOfflineException("Not able to download tool " + tool + " in edition " + edition + " with version " + version + " because we are offline");
}

/**
* Factory method, which is called, when just a purpose is given.
*
* @param purpose the purpose, which the internet connection serves.
* @return A {@link CliOfflineException} with an informative message.
*/
public static CliOfflineException ofPurpose(String purpose) {
return new CliOfflineException("You are offline but Internet access is required for " + purpose);
}

/**
* Factory method, which is called, when a clone is performed in offline mode
*
* @param url the url, in which the clone should be executed.
* @param repository the path, where the repository should be cloned to.
* @return A {@link CliOfflineException} with an informative message.
*/
public static CliOfflineException ofClone(URL url, Path repository) {
return new CliOfflineException("Could not clone " + url + " to " + repository + " because you are offline.");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public void clone(GitUrl gitRepoUrl, Path targetRepository) {
}
}
} else {
throw new CliOfflineException("Could not clone " + parsedUrl + " to " + targetRepository + " because you are offline.");
throw CliOfflineException.ofClone(parsedUrl, targetRepository);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ default void askToContinue(String question) {
default void requireOnline(String purpose) {

if (isOfflineMode()) {
throw new CliOfflineException("You are offline but Internet access is required for " + purpose);
throw CliOfflineException.ofPurpose(purpose);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;

import com.devonfw.tools.ide.cli.CliException;
import com.devonfw.tools.ide.cli.CliOfflineException;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.os.SystemInfoImpl;
import com.devonfw.tools.ide.process.ProcessContext;
Expand Down Expand Up @@ -96,6 +97,9 @@ public void download(String url, Path target) {
this.context.info("Trying to download {} from {}", target.getFileName(), url);
mkdirs(target.getParent());
try {
if (this.context.isOffline()) {
throw CliOfflineException.ofDownloadViaUrl(url);
}
if (url.startsWith("http")) {

HttpRequest request = HttpRequest.newBuilder().uri(URI.create(url)).GET().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Set;

import com.devonfw.tools.ide.cli.CliException;
import com.devonfw.tools.ide.cli.CliOfflineException;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.log.IdeLogLevel;
import com.devonfw.tools.ide.os.OperatingSystem;
Expand Down Expand Up @@ -49,6 +50,9 @@ public Path download(String tool, String edition, VersionIdentifier version) {
UrlDownloadFileMetadata metadata = getMetadata(tool, edition, version);
VersionIdentifier resolvedVersion = metadata.getVersion();
Set<String> urlCollection = metadata.getUrls();
if (context.isOffline()) {
throw CliOfflineException.ofDownloadOfTool(tool, edition, version);
}
if (urlCollection.isEmpty()) {
throw new IllegalStateException("Invalid download metadata with empty urls file for " + metadata);
}
Expand Down

0 comments on commit a729a9a

Please sign in to comment.