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 , 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 9 9 import java.io.InputStream; 10 10 import java.net.MalformedURLException; 11 11 import java.net.URL; 12 import java.net.URLConnection; 12 13 import java.nio.file.Files; 13 14 import java.nio.file.StandardCopyOption; 14 15 import java.util.Collection; … … 20 21 import org.openstreetmap.josm.gui.PleaseWaitRunnable; 21 22 import org.openstreetmap.josm.gui.progress.NullProgressMonitor; 22 23 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 24 import org.openstreetmap.josm.io.ProgressInputStream; 23 25 import org.openstreetmap.josm.tools.CheckParameterUtil; 24 26 import org.openstreetmap.josm.tools.HttpClient; 25 27 import org.openstreetmap.josm.tools.Logging; … … 102 104 // Do nothing. Error/success feedback is managed in PluginPreference.notifyDownloadResults() 103 105 } 104 106 105 protected void download(PluginInformation pi, File file ) throws PluginDownloadException {107 protected void download(PluginInformation pi, File file, ProgressMonitor monitor) throws PluginDownloadException { 106 108 if (pi.mainversion > Version.getInstance().getVersion()) { 107 109 ExtendedDialog dialog = new ExtendedDialog( 108 110 progressMonitor.getWindowParent(), … … 125 127 if ("https".equals(url.getProtocol()) || "http".equals(url.getProtocol())) { 126 128 synchronized (this) { 127 129 downloadConnection = HttpClient.create(url).setAccept(PLUGIN_MIME_TYPES); 128 downloadConnection.connect( );130 downloadConnection.connect(monitor); 129 131 } 130 132 try (InputStream in = downloadConnection.getResponse().getContent()) { 131 133 Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING); 132 134 } 133 135 } else { 134 136 // 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)) { 136 140 Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING); 137 141 } 138 142 } … … 168 172 return; 169 173 String message = tr("Downloading Plugin {0}...", d.name); 170 174 Logging.info(message); 171 progressMonitor.subTask(message);172 p rogressMonitor.worked(1);175 ProgressMonitor pluginMonitor = progressMonitor.createSubTaskMonitor(1, false); 176 pluginMonitor.setCustomText(message); 173 177 File pluginFile = new File(pluginDir, d.name + ".jar.new"); 174 178 try { 175 download(d, pluginFile );179 download(d, pluginFile, pluginMonitor); 176 180 } catch (PluginDownloadException e) { 177 181 lastException = e; 178 182 Logging.error(e); 179 183 failed.add(d); 180 184 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(); 181 190 } 182 191 downloaded.add(d); 183 192 } -
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 65 65 private long ifModifiedSince; 66 66 private final Map<String, String> headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); 67 67 private int maxRedirects = Config.getPref().getInt("socket.maxredirects", 5); 68 private boolean redirected; 68 69 private boolean useCache = true; 69 70 private String reasonForRequest; 70 71 private String outputMessage = tr("Uploading data ..."); … … 205 206 oldUrl.getHost(), url.getHost())); 206 207 this.headers.remove("Authorization"); 207 208 } 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 } 209 217 successfulConnection = true; 210 218 return response; 211 219 } else if (maxRedirects == 0) { … … 233 241 protected abstract Response buildResponse(ProgressMonitor progressMonitor) throws IOException; 234 242 235 243 protected final void notifyConnect(ProgressMonitor progressMonitor) { 244 if (redirected) { 245 return; 246 } 236 247 progressMonitor.beginTask(tr("Contacting Server..."), 1); 237 248 progressMonitor.indeterminateSubTask(null); 238 249 } -
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 234 234 assertThat(response.getHeaderField("foo"), is("bar")); 235 235 } 236 236 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 237 257 /** 238 258 * Test maximum number of redirections. 239 259 * @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 1 1 // License: GPL. For details, see LICENSE file. 2 2 package org.openstreetmap.josm.actions.downloadtasks; 3 3 4 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; 5 import static com.github.tomakehurst.wiremock.client.WireMock.get; 6 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; 4 7 import static org.junit.jupiter.api.Assertions.assertArrayEquals; 5 8 import static org.junit.jupiter.api.Assertions.assertFalse; 6 9 import static org.junit.jupiter.api.Assertions.assertTrue; 7 10 11 import java.awt.Component; 8 12 import java.io.File; 9 13 import java.io.FileInputStream; 10 14 import java.io.FileOutputStream; 11 15 import java.nio.charset.StandardCharsets; 16 import java.util.ArrayList; 17 import java.util.Arrays; 12 18 import java.util.Collections; 19 import java.util.List; 13 20 14 21 import org.junit.jupiter.api.Test; 15 22 import org.openstreetmap.josm.TestUtils; 16 23 import org.openstreetmap.josm.data.Preferences; 24 import org.openstreetmap.josm.gui.progress.AbstractProgressMonitor; 25 import org.openstreetmap.josm.gui.progress.CancelHandler; 17 26 import org.openstreetmap.josm.gui.progress.NullProgressMonitor; 27 import org.openstreetmap.josm.gui.progress.ProgressTaskId; 18 28 import org.openstreetmap.josm.plugins.PluginDownloadTask; 19 29 import org.openstreetmap.josm.plugins.PluginInformation; 20 30 import org.openstreetmap.josm.testutils.annotations.AssumeRevision; … … 78 88 TestUtils.assertFileContentsEqual(pluginFile, srcPluginFile); 79 89 } 80 90 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 81 135 /** 82 136 * Test download task when updating a plugin that we already have in our plugins directory 83 137 * and the downloaded file is not a valid jar file. … … 131 185 ); 132 186 } 133 187 } 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 } 134 245 }
