Subject: [PATCH] Fix #23007: Show download progress while a plugin is downloaded
---
Index: src/org/openstreetmap/josm/plugins/PluginDownloadTask.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/org/openstreetmap/josm/plugins/PluginDownloadTask.java b/src/org/openstreetmap/josm/plugins/PluginDownloadTask.java
--- a/src/org/openstreetmap/josm/plugins/PluginDownloadTask.java	(revision 7ac4c43e068c4ec2a657b18641fa690f8d48430b)
+++ b/src/org/openstreetmap/josm/plugins/PluginDownloadTask.java	(revision ef31dd2338fc2da578c4f4fad7fda7b8f4cee9eb)
@@ -9,6 +9,7 @@
 import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.net.URLConnection;
 import java.nio.file.Files;
 import java.nio.file.StandardCopyOption;
 import java.util.Collection;
@@ -20,6 +21,7 @@
 import org.openstreetmap.josm.gui.PleaseWaitRunnable;
 import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
 import org.openstreetmap.josm.gui.progress.ProgressMonitor;
+import org.openstreetmap.josm.io.ProgressInputStream;
 import org.openstreetmap.josm.tools.CheckParameterUtil;
 import org.openstreetmap.josm.tools.HttpClient;
 import org.openstreetmap.josm.tools.Logging;
@@ -102,7 +104,7 @@
         // Do nothing. Error/success feedback is managed in PluginPreference.notifyDownloadResults()
     }
 
-    protected void download(PluginInformation pi, File file) throws PluginDownloadException {
+    protected void download(PluginInformation pi, File file, ProgressMonitor monitor) throws PluginDownloadException {
         if (pi.mainversion > Version.getInstance().getVersion()) {
             ExtendedDialog dialog = new ExtendedDialog(
                     progressMonitor.getWindowParent(),
@@ -125,14 +127,16 @@
             if ("https".equals(url.getProtocol()) || "http".equals(url.getProtocol())) {
                 synchronized (this) {
                     downloadConnection = HttpClient.create(url).setAccept(PLUGIN_MIME_TYPES);
-                    downloadConnection.connect();
+                    downloadConnection.connect(monitor);
                 }
                 try (InputStream in = downloadConnection.getResponse().getContent()) {
                     Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
                 }
             } else {
                 // this is an alternative for e.g. file:// URLs where HttpClient doesn't work
-                try (InputStream in = url.openConnection().getInputStream()) {
+                URLConnection connection = url.openConnection();
+                monitor.beginTask(tr("Contacting Server..."));
+                try (InputStream in = new ProgressInputStream(connection.getInputStream(), connection.getContentLengthLong(), monitor)) {
                     Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
                 }
             }
@@ -168,16 +172,21 @@
                 return;
             String message = tr("Downloading Plugin {0}...", d.name);
             Logging.info(message);
-            progressMonitor.subTask(message);
-            progressMonitor.worked(1);
+            ProgressMonitor pluginMonitor = progressMonitor.createSubTaskMonitor(1, false);
+            pluginMonitor.setCustomText(message);
             File pluginFile = new File(pluginDir, d.name + ".jar.new");
             try {
-                download(d, pluginFile);
+                download(d, pluginFile, pluginMonitor);
             } catch (PluginDownloadException e) {
                 lastException = e;
                 Logging.error(e);
                 failed.add(d);
                 continue;
+            } finally {
+                // invalidate() covers a task that never started, finishTask() one that failed midway;
+                // both are no-ops once the stream closed
+                pluginMonitor.invalidate();
+                pluginMonitor.finishTask();
             }
             downloaded.add(d);
         }
Index: src/org/openstreetmap/josm/tools/HttpClient.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/org/openstreetmap/josm/tools/HttpClient.java b/src/org/openstreetmap/josm/tools/HttpClient.java
--- a/src/org/openstreetmap/josm/tools/HttpClient.java	(revision 7ac4c43e068c4ec2a657b18641fa690f8d48430b)
+++ b/src/org/openstreetmap/josm/tools/HttpClient.java	(revision ef31dd2338fc2da578c4f4fad7fda7b8f4cee9eb)
@@ -65,6 +65,7 @@
     private long ifModifiedSince;
     private final Map<String, String> headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
     private int maxRedirects = Config.getPref().getInt("socket.maxredirects", 5);
+    private boolean redirected;
     private boolean useCache = true;
     private String reasonForRequest;
     private String outputMessage = tr("Uploading data ...");
@@ -205,7 +206,14 @@
                                 oldUrl.getHost(), url.getHost()));
                         this.headers.remove("Authorization");
                     }
-                    response = connect();
+                    redirected = true;
+                    try {
+                        // the task of the monitor is already started; uploading the body again would finish it
+                        response = connect(requiresBody() ? NullProgressMonitor.INSTANCE : progressMonitor,
+                                authRedirectLocation, authRequestProperty);
+                    } finally {
+                        redirected = false;
+                    }
                     successfulConnection = true;
                     return response;
                 } else if (maxRedirects == 0) {
@@ -233,6 +241,9 @@
     protected abstract Response buildResponse(ProgressMonitor progressMonitor) throws IOException;
 
     protected final void notifyConnect(ProgressMonitor progressMonitor) {
+        if (redirected) {
+            return;
+        }
         progressMonitor.beginTask(tr("Contacting Server..."), 1);
         progressMonitor.indeterminateSubTask(null);
     }
Index: test/functional/org/openstreetmap/josm/tools/HttpClientTest.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/test/functional/org/openstreetmap/josm/tools/HttpClientTest.java b/test/functional/org/openstreetmap/josm/tools/HttpClientTest.java
--- a/test/functional/org/openstreetmap/josm/tools/HttpClientTest.java	(revision 7ac4c43e068c4ec2a657b18641fa690f8d48430b)
+++ b/test/functional/org/openstreetmap/josm/tools/HttpClientTest.java	(revision ef31dd2338fc2da578c4f4fad7fda7b8f4cee9eb)
@@ -234,6 +234,26 @@
         assertThat(response.getHeaderField("foo"), is("bar"));
     }
 
+    /**
+     * A client that followed a redirect must start the task of the next monitor it connects with.
+     * @throws IOException if an I/O error occurs
+     */
+    @Test
+    void testConnectAgainAfterRedirect() throws IOException {
+        mockRedirects(false, 1);
+        final HttpClient client = HttpClient.create(url("/relative-redirect/1"));
+        try {
+            client.connect(progress).fetchContent();
+            final ProgressMonitor secondProgress = TestUtils.newTestProgressMonitor();
+            final Response response = client.connect(secondProgress);
+            assertThat(response.getResponseCode(), is(200));
+            assertThat(response.getHeaderField("foo"), is("bar"));
+            response.fetchContent();
+        } finally {
+            client.disconnect();
+        }
+    }
+
     /**
      * Test maximum number of redirections.
      * @throws IOException if an I/O error occurs
Index: test/unit/org/openstreetmap/josm/actions/downloadtasks/PluginDownloadTaskTest.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/test/unit/org/openstreetmap/josm/actions/downloadtasks/PluginDownloadTaskTest.java b/test/unit/org/openstreetmap/josm/actions/downloadtasks/PluginDownloadTaskTest.java
--- a/test/unit/org/openstreetmap/josm/actions/downloadtasks/PluginDownloadTaskTest.java	(revision 7ac4c43e068c4ec2a657b18641fa690f8d48430b)
+++ b/test/unit/org/openstreetmap/josm/actions/downloadtasks/PluginDownloadTaskTest.java	(revision ef31dd2338fc2da578c4f4fad7fda7b8f4cee9eb)
@@ -1,20 +1,30 @@
 // License: GPL. For details, see LICENSE file.
 package org.openstreetmap.josm.actions.downloadtasks;
 
+import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
+import static com.github.tomakehurst.wiremock.client.WireMock.get;
+import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
+import java.awt.Component;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
+import java.util.List;
 
 import org.junit.jupiter.api.Test;
 import org.openstreetmap.josm.TestUtils;
 import org.openstreetmap.josm.data.Preferences;
+import org.openstreetmap.josm.gui.progress.AbstractProgressMonitor;
+import org.openstreetmap.josm.gui.progress.CancelHandler;
 import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
+import org.openstreetmap.josm.gui.progress.ProgressTaskId;
 import org.openstreetmap.josm.plugins.PluginDownloadTask;
 import org.openstreetmap.josm.plugins.PluginInformation;
 import org.openstreetmap.josm.testutils.annotations.AssumeRevision;
@@ -78,6 +88,50 @@
         TestUtils.assertFileContentsEqual(pluginFile, srcPluginFile);
     }
 
+    /**
+     * Test that the progress monitor is updated while a plugin is downloaded
+     * @throws Exception if an error occurs
+     */
+    @Test
+    void testDownloadReportsProgress() throws Exception {
+        this.pluginPath = "plugin/dummy_plugin.v31772.jar";
+        this.mockHttp();
+        assertDownloadReportsProgress(this.getRemoteFileUrl());
+    }
+
+    /**
+     * Test that the progress monitor is updated when the download link redirects to the plugin,
+     * as release URLs of code hosting sites do
+     * @throws Exception if an error occurs
+     */
+    @Test
+    void testDownloadReportsProgressAfterRedirect() throws Exception {
+        this.pluginPath = "plugin/dummy_plugin.v31772.jar";
+        this.mockHttp();
+        wireMockServer.getWireMock().register(get(urlEqualTo("/releases/latest/dummy_plugin.jar"))
+                .willReturn(aResponse().withStatus(302).withHeader("Location", this.getRemoteFileUrl())));
+        assertDownloadReportsProgress(wireMockServer.getHttpBaseUrl() + "/releases/latest/dummy_plugin.jar");
+    }
+
+    private void assertDownloadReportsProgress(String downloadLink) throws Exception {
+        final File srcPluginFile = new File(
+            new File(TestUtils.getTestDataRoot()), "__files/" + this.pluginPath
+        );
+        final PluginInformation pluginInformation = new PluginInformation(srcPluginFile, "dummy_plugin");
+        pluginInformation.downloadlink = downloadLink;
+
+        final RecordingProgressMonitor monitor = new RecordingProgressMonitor();
+        final PluginInformation secondPluginInformation = new PluginInformation(srcPluginFile, "dummy_plugin");
+        secondPluginInformation.downloadlink = downloadLink;
+        new PluginDownloadTask(monitor, Arrays.asList(pluginInformation, secondPluginInformation), null).run();
+
+        assertTrue(monitor.customTexts.stream().anyMatch(text -> text.contains("dummy_plugin")));
+        assertTrue(monitor.titles.contains("Downloading data..."));
+        // the first of two plugins is complete, so half of the task is done
+        assertTrue(monitor.progress.contains(0.5), "progress should advance during the download");
+        assertTrue(monitor.finished);
+    }
+
     /**
      * Test download task when updating a plugin that we already have in our plugins directory
      * and the downloaded file is not a valid jar file.
@@ -131,4 +185,61 @@
             );
         }
     }
+
+    private static class RecordingProgressMonitor extends AbstractProgressMonitor {
+        final List<String> titles = new ArrayList<>();
+        final List<String> customTexts = new ArrayList<>();
+        final List<Double> progress = new ArrayList<>();
+        boolean finished;
+        private ProgressTaskId taskId;
+
+        RecordingProgressMonitor() {
+            super(new CancelHandler());
+        }
+
+        @Override
+        protected void doBeginTask() {
+            // nothing to record
+        }
+
+        @Override
+        protected void doFinishTask() {
+            finished = true;
+        }
+
+        @Override
+        protected void doSetIntermediate(boolean value) {
+            // nothing to record
+        }
+
+        @Override
+        protected void doSetTitle(String title) {
+            titles.add(title);
+        }
+
+        @Override
+        protected void doSetCustomText(String title) {
+            customTexts.add(title);
+        }
+
+        @Override
+        protected void updateProgress(double value) {
+            progress.add(value);
+        }
+
+        @Override
+        public void setProgressTaskId(ProgressTaskId taskId) {
+            this.taskId = taskId;
+        }
+
+        @Override
+        public ProgressTaskId getProgressTaskId() {
+            return taskId;
+        }
+
+        @Override
+        public Component getWindowParent() {
+            return null;
+        }
+    }
 }
