From a729a9a0286ee7ab5083b639f38916d4f671d08c Mon Sep 17 00:00:00 2001 From: JanAmeis <162981643+WorkingAmeise@users.noreply.github.com> Date: Tue, 1 Oct 2024 15:02:24 +0200 Subject: [PATCH] #525: Added online state check before downloading (#610) --- CHANGELOG.adoc | 3 +- .../tools/ide/cli/CliOfflineException.java | 50 ++++++++++++++++++- .../tools/ide/context/GitContextImpl.java | 2 +- .../devonfw/tools/ide/context/IdeContext.java | 2 +- .../devonfw/tools/ide/io/FileAccessImpl.java | 4 ++ .../ide/repo/AbstractToolRepository.java | 4 ++ 6 files changed, 60 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index f4c38b27a..8f9fb9c07 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -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 diff --git a/cli/src/main/java/com/devonfw/tools/ide/cli/CliOfflineException.java b/cli/src/main/java/com/devonfw/tools/ide/cli/CliOfflineException.java index 56be0abd5..e288d420a 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/cli/CliOfflineException.java +++ b/cli/src/main/java/com/devonfw/tools/ide/cli/CliOfflineException.java @@ -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. @@ -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."); + } + } diff --git a/cli/src/main/java/com/devonfw/tools/ide/context/GitContextImpl.java b/cli/src/main/java/com/devonfw/tools/ide/context/GitContextImpl.java index f485f9600..5cc27bcb1 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/context/GitContextImpl.java +++ b/cli/src/main/java/com/devonfw/tools/ide/context/GitContextImpl.java @@ -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); } } diff --git a/cli/src/main/java/com/devonfw/tools/ide/context/IdeContext.java b/cli/src/main/java/com/devonfw/tools/ide/context/IdeContext.java index f0410fa3a..b0c527c4e 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/context/IdeContext.java +++ b/cli/src/main/java/com/devonfw/tools/ide/context/IdeContext.java @@ -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); } } diff --git a/cli/src/main/java/com/devonfw/tools/ide/io/FileAccessImpl.java b/cli/src/main/java/com/devonfw/tools/ide/io/FileAccessImpl.java index 7f386bb0a..9860df712 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/io/FileAccessImpl.java +++ b/cli/src/main/java/com/devonfw/tools/ide/io/FileAccessImpl.java @@ -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; @@ -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(); diff --git a/cli/src/main/java/com/devonfw/tools/ide/repo/AbstractToolRepository.java b/cli/src/main/java/com/devonfw/tools/ide/repo/AbstractToolRepository.java index a5718d6b9..95955aa29 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/repo/AbstractToolRepository.java +++ b/cli/src/main/java/com/devonfw/tools/ide/repo/AbstractToolRepository.java @@ -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; @@ -49,6 +50,9 @@ public Path download(String tool, String edition, VersionIdentifier version) { UrlDownloadFileMetadata metadata = getMetadata(tool, edition, version); VersionIdentifier resolvedVersion = metadata.getVersion(); Set 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); }