Ticket #23007: Fix_#23007__Show_download_progress_while_a_plugin_is_downloaded.patch

File Fix_#23007__Show_download_progress_while_a_plugin_is_downloaded.patch, 13.7 KB (added by gaben, 3 weeks ago)
  • src/org/openstreetmap/josm/plugins/PluginDownloadTask.java

    Subject: [PATCH] Fix #23007: Show download progress while a plugin is downloaded
    ---
    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 b  
    99import java.io.InputStream;
    1010import java.net.MalformedURLException;
    1111import java.net.URL;
     12import java.net.URLConnection;
    1213import java.nio.file.Files;
    1314import java.nio.file.StandardCopyOption;
    1415import java.util.Collection;
    … …  
    2021import org.openstreetmap.josm.gui.PleaseWaitRunnable;
    2122import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
    2223import org.openstreetmap.josm.gui.progress.ProgressMonitor;
     24import org.openstreetmap.josm.io.ProgressInputStream;
    2325import org.openstreetmap.josm.tools.CheckParameterUtil;
    2426import org.openstreetmap.josm.tools.HttpClient;
    2527import org.openstreetmap.josm.tools.Logging;
    … …  
    102104        // Do nothing. Error/success feedback is managed in PluginPreference.notifyDownloadResults()
    103105    }
    104106
    105     protected void download(PluginInformation pi, File file) throws PluginDownloadException {
     107    protected void download(PluginInformation pi, File file, ProgressMonitor monitor) throws PluginDownloadException {
    106108        if (pi.mainversion > Version.getInstance().getVersion()) {
    107109            ExtendedDialog dialog = new ExtendedDialog(
    108110                    progressMonitor.getWindowParent(),
    … …  
    125127            if ("https".equals(url.getProtocol()) || "http".equals(url.getProtocol())) {
    126128                synchronized (this) {
    127129                    downloadConnection = HttpClient.create(url).setAccept(PLUGIN_MIME_TYPES);
    128                     downloadConnection.connect();
     130                    downloadConnection.connect(monitor);
    129131                }
    130132                try (InputStream in = downloadConnection.getResponse().getContent()) {
    131133                    Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
    132134                }
    133135            } else {
    134136                // this is an alternative for e.g. file:// URLs where HttpClient doesn't work
    135                 try (InputStream in = url.openConnection().getInputStream()) {
     137                URLConnection connection = url.openConnection();
     138                monitor.beginTask(tr("Contacting Server..."));
     139                try (InputStream in = new ProgressInputStream(connection.getInputStream(), connection.getContentLengthLong(), monitor)) {
    136140                    Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
    137141                }
    138142            }
    … …  
    168172                return;
    169173            String message = tr("Downloading Plugin {0}...", d.name);
    170174            Logging.info(message);
    171             progressMonitor.subTask(message);
    172             progressMonitor.worked(1);
     175            ProgressMonitor pluginMonitor = progressMonitor.createSubTaskMonitor(1, false);
     176            pluginMonitor.setCustomText(message);
    173177            File pluginFile = new File(pluginDir, d.name + ".jar.new");
    174178            try {
    175                 download(d, pluginFile);
     179                download(d, pluginFile, pluginMonitor);
    176180            } catch (PluginDownloadException e) {
    177181                lastException = e;
    178182                Logging.error(e);
    179183                failed.add(d);
    180184                continue;
     185            } finally {
     186                // invalidate() covers a task that never started, finishTask() one that failed midway;
     187                // both are no-ops once the stream closed
     188                pluginMonitor.invalidate();
     189                pluginMonitor.finishTask();
    181190            }
    182191            downloaded.add(d);
    183192        }
  • 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 b  
    6565    private long ifModifiedSince;
    6666    private final Map<String, String> headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
    6767    private int maxRedirects = Config.getPref().getInt("socket.maxredirects", 5);
     68    private boolean redirected;
    6869    private boolean useCache = true;
    6970    private String reasonForRequest;
    7071    private String outputMessage = tr("Uploading data ...");
    … …  
    205206                                oldUrl.getHost(), url.getHost()));
    206207                        this.headers.remove("Authorization");
    207208                    }
    208                     response = connect();
     209                    redirected = true;
     210                    try {
     211                        // the task of the monitor is already started; uploading the body again would finish it
     212                        response = connect(requiresBody() ? NullProgressMonitor.INSTANCE : progressMonitor,
     213                                authRedirectLocation, authRequestProperty);
     214                    } finally {
     215                        redirected = false;
     216                    }
    209217                    successfulConnection = true;
    210218                    return response;
    211219                } else if (maxRedirects == 0) {
    … …  
    233241    protected abstract Response buildResponse(ProgressMonitor progressMonitor) throws IOException;
    234242
    235243    protected final void notifyConnect(ProgressMonitor progressMonitor) {
     244        if (redirected) {
     245            return;
     246        }
    236247        progressMonitor.beginTask(tr("Contacting Server..."), 1);
    237248        progressMonitor.indeterminateSubTask(null);
    238249    }
  • 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 b  
    234234        assertThat(response.getHeaderField("foo"), is("bar"));
    235235    }
    236236
     237    /**
     238     * A client that followed a redirect must start the task of the next monitor it connects with.
     239     * @throws IOException if an I/O error occurs
     240     */
     241    @Test
     242    void testConnectAgainAfterRedirect() throws IOException {
     243        mockRedirects(false, 1);
     244        final HttpClient client = HttpClient.create(url("/relative-redirect/1"));
     245        try {
     246            client.connect(progress).fetchContent();
     247            final ProgressMonitor secondProgress = TestUtils.newTestProgressMonitor();
     248            final Response response = client.connect(secondProgress);
     249            assertThat(response.getResponseCode(), is(200));
     250            assertThat(response.getHeaderField("foo"), is("bar"));
     251            response.fetchContent();
     252        } finally {
     253            client.disconnect();
     254        }
     255    }
     256
    237257    /**
    238258     * Test maximum number of redirections.
    239259     * @throws IOException if an I/O error occurs
  • 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 b  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.actions.downloadtasks;
    33
     4import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
     5import static com.github.tomakehurst.wiremock.client.WireMock.get;
     6import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
    47import static org.junit.jupiter.api.Assertions.assertArrayEquals;
    58import static org.junit.jupiter.api.Assertions.assertFalse;
    69import static org.junit.jupiter.api.Assertions.assertTrue;
    710
     11import java.awt.Component;
    812import java.io.File;
    913import java.io.FileInputStream;
    1014import java.io.FileOutputStream;
    1115import java.nio.charset.StandardCharsets;
     16import java.util.ArrayList;
     17import java.util.Arrays;
    1218import java.util.Collections;
     19import java.util.List;
    1320
    1421import org.junit.jupiter.api.Test;
    1522import org.openstreetmap.josm.TestUtils;
    1623import org.openstreetmap.josm.data.Preferences;
     24import org.openstreetmap.josm.gui.progress.AbstractProgressMonitor;
     25import org.openstreetmap.josm.gui.progress.CancelHandler;
    1726import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
     27import org.openstreetmap.josm.gui.progress.ProgressTaskId;
    1828import org.openstreetmap.josm.plugins.PluginDownloadTask;
    1929import org.openstreetmap.josm.plugins.PluginInformation;
    2030import org.openstreetmap.josm.testutils.annotations.AssumeRevision;
    … …  
    7888        TestUtils.assertFileContentsEqual(pluginFile, srcPluginFile);
    7989    }
    8090
     91    /**
     92     * Test that the progress monitor is updated while a plugin is downloaded
     93     * @throws Exception if an error occurs
     94     */
     95    @Test
     96    void testDownloadReportsProgress() throws Exception {
     97        this.pluginPath = "plugin/dummy_plugin.v31772.jar";
     98        this.mockHttp();
     99        assertDownloadReportsProgress(this.getRemoteFileUrl());
     100    }
     101
     102    /**
     103     * Test that the progress monitor is updated when the download link redirects to the plugin,
     104     * as release URLs of code hosting sites do
     105     * @throws Exception if an error occurs
     106     */
     107    @Test
     108    void testDownloadReportsProgressAfterRedirect() throws Exception {
     109        this.pluginPath = "plugin/dummy_plugin.v31772.jar";
     110        this.mockHttp();
     111        wireMockServer.getWireMock().register(get(urlEqualTo("/releases/latest/dummy_plugin.jar"))
     112                .willReturn(aResponse().withStatus(302).withHeader("Location", this.getRemoteFileUrl())));
     113        assertDownloadReportsProgress(wireMockServer.getHttpBaseUrl() + "/releases/latest/dummy_plugin.jar");
     114    }
     115
     116    private void assertDownloadReportsProgress(String downloadLink) throws Exception {
     117        final File srcPluginFile = new File(
     118            new File(TestUtils.getTestDataRoot()), "__files/" + this.pluginPath
     119        );
     120        final PluginInformation pluginInformation = new PluginInformation(srcPluginFile, "dummy_plugin");
     121        pluginInformation.downloadlink = downloadLink;
     122
     123        final RecordingProgressMonitor monitor = new RecordingProgressMonitor();
     124        final PluginInformation secondPluginInformation = new PluginInformation(srcPluginFile, "dummy_plugin");
     125        secondPluginInformation.downloadlink = downloadLink;
     126        new PluginDownloadTask(monitor, Arrays.asList(pluginInformation, secondPluginInformation), null).run();
     127
     128        assertTrue(monitor.customTexts.stream().anyMatch(text -> text.contains("dummy_plugin")));
     129        assertTrue(monitor.titles.contains("Downloading data..."));
     130        // the first of two plugins is complete, so half of the task is done
     131        assertTrue(monitor.progress.contains(0.5), "progress should advance during the download");
     132        assertTrue(monitor.finished);
     133    }
     134
    81135    /**
    82136     * Test download task when updating a plugin that we already have in our plugins directory
    83137     * and the downloaded file is not a valid jar file.
    … …  
    131185            );
    132186        }
    133187    }
     188
     189    private static class RecordingProgressMonitor extends AbstractProgressMonitor {
     190        final List<String> titles = new ArrayList<>();
     191        final List<String> customTexts = new ArrayList<>();
     192        final List<Double> progress = new ArrayList<>();
     193        boolean finished;
     194        private ProgressTaskId taskId;
     195
     196        RecordingProgressMonitor() {
     197            super(new CancelHandler());
     198        }
     199
     200        @Override
     201        protected void doBeginTask() {
     202            // nothing to record
     203        }
     204
     205        @Override
     206        protected void doFinishTask() {
     207            finished = true;
     208        }
     209
     210        @Override
     211        protected void doSetIntermediate(boolean value) {
     212            // nothing to record
     213        }
     214
     215        @Override
     216        protected void doSetTitle(String title) {
     217            titles.add(title);
     218        }
     219
     220        @Override
     221        protected void doSetCustomText(String title) {
     222            customTexts.add(title);
     223        }
     224
     225        @Override
     226        protected void updateProgress(double value) {
     227            progress.add(value);
     228        }
     229
     230        @Override
     231        public void setProgressTaskId(ProgressTaskId taskId) {
     232            this.taskId = taskId;
     233        }
     234
     235        @Override
     236        public ProgressTaskId getProgressTaskId() {
     237            return taskId;
     238        }
     239
     240        @Override
     241        public Component getWindowParent() {
     242            return null;
     243        }
     244    }
    134245}