1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.plugins;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Dimension;
|
---|
7 | import java.awt.GridBagLayout;
|
---|
8 | import java.io.BufferedReader;
|
---|
9 | import java.io.ByteArrayInputStream;
|
---|
10 | import java.io.File;
|
---|
11 | import java.io.FileOutputStream;
|
---|
12 | import java.io.FilenameFilter;
|
---|
13 | import java.io.IOException;
|
---|
14 | import java.io.InputStream;
|
---|
15 | import java.io.InputStreamReader;
|
---|
16 | import java.io.OutputStream;
|
---|
17 | import java.io.OutputStreamWriter;
|
---|
18 | import java.io.PrintWriter;
|
---|
19 | import java.net.HttpURLConnection;
|
---|
20 | import java.net.MalformedURLException;
|
---|
21 | import java.net.URL;
|
---|
22 | import java.nio.charset.StandardCharsets;
|
---|
23 | import java.util.ArrayList;
|
---|
24 | import java.util.Arrays;
|
---|
25 | import java.util.Collection;
|
---|
26 | import java.util.Collections;
|
---|
27 | import java.util.HashSet;
|
---|
28 | import java.util.LinkedList;
|
---|
29 | import java.util.List;
|
---|
30 |
|
---|
31 | import javax.swing.JLabel;
|
---|
32 | import javax.swing.JOptionPane;
|
---|
33 | import javax.swing.JPanel;
|
---|
34 | import javax.swing.JScrollPane;
|
---|
35 |
|
---|
36 | import org.openstreetmap.josm.Main;
|
---|
37 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
38 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
39 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
40 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
41 | import org.openstreetmap.josm.gui.widgets.JosmTextArea;
|
---|
42 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
43 | import org.openstreetmap.josm.tools.GBC;
|
---|
44 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
45 | import org.openstreetmap.josm.tools.Utils;
|
---|
46 | import org.xml.sax.SAXException;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * An asynchronous task for downloading plugin lists from the configured plugin download sites.
|
---|
50 | * @since 2817
|
---|
51 | */
|
---|
52 | public class ReadRemotePluginInformationTask extends PleaseWaitRunnable {
|
---|
53 |
|
---|
54 | private Collection<String> sites;
|
---|
55 | private boolean canceled;
|
---|
56 | private HttpURLConnection connection;
|
---|
57 | private List<PluginInformation> availablePlugins;
|
---|
58 | private boolean displayErrMsg;
|
---|
59 |
|
---|
60 | protected enum CacheType {PLUGIN_LIST, ICON_LIST}
|
---|
61 |
|
---|
62 | protected final void init(Collection<String> sites, boolean displayErrMsg){
|
---|
63 | this.sites = sites;
|
---|
64 | if (sites == null) {
|
---|
65 | this.sites = Collections.emptySet();
|
---|
66 | }
|
---|
67 | this.availablePlugins = new LinkedList<>();
|
---|
68 | this.displayErrMsg = displayErrMsg;
|
---|
69 | }
|
---|
70 | /**
|
---|
71 | * Creates the task
|
---|
72 | *
|
---|
73 | * @param sites the collection of download sites. Defaults to the empty collection if null.
|
---|
74 | */
|
---|
75 | public ReadRemotePluginInformationTask(Collection<String> sites) {
|
---|
76 | super(tr("Download plugin list..."), false /* don't ignore exceptions */);
|
---|
77 | init(sites, true);
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Creates the task
|
---|
82 | *
|
---|
83 | * @param monitor the progress monitor. Defaults to {@link NullProgressMonitor#INSTANCE} if null
|
---|
84 | * @param sites the collection of download sites. Defaults to the empty collection if null.
|
---|
85 | * @param displayErrMsg if {@code true}, a blocking error message is displayed in case of I/O exception.
|
---|
86 | */
|
---|
87 | public ReadRemotePluginInformationTask(ProgressMonitor monitor, Collection<String> sites, boolean displayErrMsg) {
|
---|
88 | super(tr("Download plugin list..."), monitor == null ? NullProgressMonitor.INSTANCE: monitor, false /* don't ignore exceptions */);
|
---|
89 | init(sites, displayErrMsg);
|
---|
90 | }
|
---|
91 |
|
---|
92 | @Override
|
---|
93 | protected void cancel() {
|
---|
94 | canceled = true;
|
---|
95 | synchronized(this) {
|
---|
96 | if (connection != null) {
|
---|
97 | connection.disconnect();
|
---|
98 | }
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | @Override
|
---|
103 | protected void finish() {}
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Creates the file name for the cached plugin list and the icon cache
|
---|
107 | * file.
|
---|
108 | *
|
---|
109 | * @param site the name of the site
|
---|
110 | * @param type icon cache or plugin list cache
|
---|
111 | * @return the file name for the cache file
|
---|
112 | */
|
---|
113 | protected File createSiteCacheFile(File pluginDir, String site, CacheType type) {
|
---|
114 | String name;
|
---|
115 | try {
|
---|
116 | site = site.replaceAll("%<(.*)>", "");
|
---|
117 | URL url = new URL(site);
|
---|
118 | StringBuilder sb = new StringBuilder();
|
---|
119 | sb.append("site-");
|
---|
120 | sb.append(url.getHost()).append("-");
|
---|
121 | if (url.getPort() != -1) {
|
---|
122 | sb.append(url.getPort()).append("-");
|
---|
123 | }
|
---|
124 | String path = url.getPath();
|
---|
125 | for (int i =0;i<path.length(); i++) {
|
---|
126 | char c = path.charAt(i);
|
---|
127 | if (Character.isLetterOrDigit(c)) {
|
---|
128 | sb.append(c);
|
---|
129 | } else {
|
---|
130 | sb.append("_");
|
---|
131 | }
|
---|
132 | }
|
---|
133 | switch (type) {
|
---|
134 | case PLUGIN_LIST:
|
---|
135 | sb.append(".txt");
|
---|
136 | break;
|
---|
137 | case ICON_LIST:
|
---|
138 | sb.append("-icons.zip");
|
---|
139 | break;
|
---|
140 | }
|
---|
141 | name = sb.toString();
|
---|
142 | } catch(MalformedURLException e) {
|
---|
143 | name = "site-unknown.txt";
|
---|
144 | }
|
---|
145 | return new File(pluginDir, name);
|
---|
146 | }
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Downloads the list from a remote location
|
---|
150 | *
|
---|
151 | * @param site the site URL
|
---|
152 | * @param monitor a progress monitor
|
---|
153 | * @return the downloaded list
|
---|
154 | */
|
---|
155 | protected String downloadPluginList(String site, final ProgressMonitor monitor) {
|
---|
156 | /* replace %<x> with empty string or x=plugins (separated with comma) */
|
---|
157 | String pl = Utils.join(",", Main.pref.getCollection("plugins"));
|
---|
158 | String printsite = site.replaceAll("%<(.*)>", "");
|
---|
159 | if (pl != null && pl.length() != 0) {
|
---|
160 | site = site.replaceAll("%<(.*)>", "$1"+pl);
|
---|
161 | } else {
|
---|
162 | site = printsite;
|
---|
163 | }
|
---|
164 |
|
---|
165 | try {
|
---|
166 | monitor.beginTask("");
|
---|
167 | monitor.indeterminateSubTask(tr("Downloading plugin list from ''{0}''", printsite));
|
---|
168 |
|
---|
169 | URL url = new URL(site);
|
---|
170 | synchronized(this) {
|
---|
171 | connection = Utils.openHttpConnection(url);
|
---|
172 | connection.setRequestProperty("Cache-Control", "no-cache");
|
---|
173 | connection.setRequestProperty("Accept-Charset", "utf-8");
|
---|
174 | }
|
---|
175 | try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
|
---|
176 | StringBuilder sb = new StringBuilder();
|
---|
177 | String line;
|
---|
178 | while ((line = in.readLine()) != null) {
|
---|
179 | sb.append(line).append("\n");
|
---|
180 | }
|
---|
181 | return sb.toString();
|
---|
182 | }
|
---|
183 | } catch (MalformedURLException e) {
|
---|
184 | if (canceled) return null;
|
---|
185 | Main.error(e);
|
---|
186 | return null;
|
---|
187 | } catch (IOException e) {
|
---|
188 | if (canceled) return null;
|
---|
189 | Main.addNetworkError(site, e);
|
---|
190 | handleIOException(monitor, e, tr("Plugin list download error"), tr("JOSM failed to download plugin list:"), displayErrMsg);
|
---|
191 | return null;
|
---|
192 | } finally {
|
---|
193 | synchronized(this) {
|
---|
194 | if (connection != null) {
|
---|
195 | connection.disconnect();
|
---|
196 | }
|
---|
197 | connection = null;
|
---|
198 | }
|
---|
199 | monitor.finishTask();
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | private void handleIOException(final ProgressMonitor monitor, IOException e, final String title, final String firstMessage, boolean displayMsg) {
|
---|
204 | StringBuilder sb = new StringBuilder();
|
---|
205 | try (InputStream errStream = connection.getErrorStream()) {
|
---|
206 | if (errStream != null) {
|
---|
207 | try (BufferedReader err = new BufferedReader(new InputStreamReader(errStream, StandardCharsets.UTF_8))) {
|
---|
208 | String line;
|
---|
209 | while ((line = err.readLine()) != null) {
|
---|
210 | sb.append(line).append("\n");
|
---|
211 | }
|
---|
212 | } catch (Exception ex) {
|
---|
213 | Main.error(e);
|
---|
214 | Main.error(ex);
|
---|
215 | }
|
---|
216 | }
|
---|
217 | } catch (IOException ex) {
|
---|
218 | Main.warn(ex);
|
---|
219 | }
|
---|
220 | final String msg = e.getMessage();
|
---|
221 | final String details = sb.toString();
|
---|
222 | if (details.isEmpty()) {
|
---|
223 | Main.error(e.getClass().getSimpleName()+": " + msg);
|
---|
224 | } else {
|
---|
225 | Main.error(msg + " - Details:\n" + details);
|
---|
226 | }
|
---|
227 |
|
---|
228 | if (displayMsg) {
|
---|
229 | displayErrorMessage(monitor, msg, details, title, firstMessage);
|
---|
230 | }
|
---|
231 | }
|
---|
232 |
|
---|
233 | private void displayErrorMessage(final ProgressMonitor monitor, final String msg, final String details, final String title, final String firstMessage) {
|
---|
234 | GuiHelper.runInEDTAndWait(new Runnable() {
|
---|
235 | @Override public void run() {
|
---|
236 | JPanel panel = new JPanel(new GridBagLayout());
|
---|
237 | panel.add(new JLabel(firstMessage), GBC.eol().insets(0, 0, 0, 10));
|
---|
238 | StringBuilder b = new StringBuilder();
|
---|
239 | for (String part : msg.split("(?<=\\G.{200})")) {
|
---|
240 | b.append(part).append("\n");
|
---|
241 | }
|
---|
242 | panel.add(new JLabel("<html><body width=\"500\"><b>"+b.toString().trim()+"</b></body></html>"), GBC.eol().insets(0, 0, 0, 10));
|
---|
243 | if (!details.isEmpty()) {
|
---|
244 | panel.add(new JLabel(tr("Details:")), GBC.eol().insets(0, 0, 0, 10));
|
---|
245 | JosmTextArea area = new JosmTextArea(details);
|
---|
246 | area.setEditable(false);
|
---|
247 | area.setLineWrap(true);
|
---|
248 | area.setWrapStyleWord(true);
|
---|
249 | JScrollPane scrollPane = new JScrollPane(area);
|
---|
250 | scrollPane.setPreferredSize(new Dimension(500, 300));
|
---|
251 | panel.add(scrollPane, GBC.eol().fill());
|
---|
252 | }
|
---|
253 | JOptionPane.showMessageDialog(monitor.getWindowParent(), panel, title, JOptionPane.ERROR_MESSAGE);
|
---|
254 | }
|
---|
255 | });
|
---|
256 | }
|
---|
257 |
|
---|
258 | /**
|
---|
259 | * Downloads the icon archive from a remote location
|
---|
260 | *
|
---|
261 | * @param site the site URL
|
---|
262 | * @param monitor a progress monitor
|
---|
263 | */
|
---|
264 | protected void downloadPluginIcons(String site, File destFile, ProgressMonitor monitor) {
|
---|
265 | try {
|
---|
266 | site = site.replaceAll("%<(.*)>", "");
|
---|
267 |
|
---|
268 | monitor.beginTask("");
|
---|
269 | monitor.indeterminateSubTask(tr("Downloading plugin list from ''{0}''", site));
|
---|
270 |
|
---|
271 | URL url = new URL(site);
|
---|
272 | synchronized(this) {
|
---|
273 | connection = Utils.openHttpConnection(url);
|
---|
274 | connection.setRequestProperty("Cache-Control", "no-cache");
|
---|
275 | }
|
---|
276 | try (
|
---|
277 | InputStream in = connection.getInputStream();
|
---|
278 | OutputStream out = new FileOutputStream(destFile)
|
---|
279 | ) {
|
---|
280 | byte[] buffer = new byte[8192];
|
---|
281 | for (int read = in.read(buffer); read != -1; read = in.read(buffer)) {
|
---|
282 | out.write(buffer, 0, read);
|
---|
283 | }
|
---|
284 | }
|
---|
285 | } catch (MalformedURLException e) {
|
---|
286 | if (canceled) return;
|
---|
287 | Main.error(e);
|
---|
288 | return;
|
---|
289 | } catch (IOException e) {
|
---|
290 | if (canceled) return;
|
---|
291 | handleIOException(monitor, e, tr("Plugin icons download error"), tr("JOSM failed to download plugin icons:"), displayErrMsg);
|
---|
292 | return;
|
---|
293 | } finally {
|
---|
294 | synchronized(this) {
|
---|
295 | if (connection != null) {
|
---|
296 | connection.disconnect();
|
---|
297 | }
|
---|
298 | connection = null;
|
---|
299 | }
|
---|
300 | monitor.finishTask();
|
---|
301 | }
|
---|
302 | for (PluginInformation pi : availablePlugins) {
|
---|
303 | if (pi.icon == null && pi.iconPath != null) {
|
---|
304 | pi.icon = new ImageProvider(pi.name+".jar/"+pi.iconPath)
|
---|
305 | .setArchive(destFile)
|
---|
306 | .setMaxWidth(24)
|
---|
307 | .setMaxHeight(24)
|
---|
308 | .setOptional(true).get();
|
---|
309 | }
|
---|
310 | }
|
---|
311 | }
|
---|
312 |
|
---|
313 | /**
|
---|
314 | * Writes the list of plugins to a cache file
|
---|
315 | *
|
---|
316 | * @param site the site from where the list was downloaded
|
---|
317 | * @param list the downloaded list
|
---|
318 | */
|
---|
319 | protected void cachePluginList(String site, String list) {
|
---|
320 | File pluginDir = Main.pref.getPluginsDirectory();
|
---|
321 | if (!pluginDir.exists() && !pluginDir.mkdirs()) {
|
---|
322 | Main.warn(tr("Failed to create plugin directory ''{0}''. Cannot cache plugin list from plugin site ''{1}''.", pluginDir.toString(), site));
|
---|
323 | }
|
---|
324 | File cacheFile = createSiteCacheFile(pluginDir, site, CacheType.PLUGIN_LIST);
|
---|
325 | getProgressMonitor().subTask(tr("Writing plugin list to local cache ''{0}''", cacheFile.toString()));
|
---|
326 | try (PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(cacheFile), StandardCharsets.UTF_8))) {
|
---|
327 | writer.write(list);
|
---|
328 | writer.flush();
|
---|
329 | } catch(IOException e) {
|
---|
330 | // just failed to write the cache file. No big deal, but log the exception anyway
|
---|
331 | Main.error(e);
|
---|
332 | }
|
---|
333 | }
|
---|
334 |
|
---|
335 | /**
|
---|
336 | * Filter information about deprecated plugins from the list of downloaded
|
---|
337 | * plugins
|
---|
338 | *
|
---|
339 | * @param plugins the plugin informations
|
---|
340 | * @return the plugin informations, without deprecated plugins
|
---|
341 | */
|
---|
342 | protected List<PluginInformation> filterDeprecatedPlugins(List<PluginInformation> plugins) {
|
---|
343 | List<PluginInformation> ret = new ArrayList<>(plugins.size());
|
---|
344 | HashSet<String> deprecatedPluginNames = new HashSet<>();
|
---|
345 | for (PluginHandler.DeprecatedPlugin p : PluginHandler.DEPRECATED_PLUGINS) {
|
---|
346 | deprecatedPluginNames.add(p.name);
|
---|
347 | }
|
---|
348 | for (PluginInformation plugin: plugins) {
|
---|
349 | if (deprecatedPluginNames.contains(plugin.name)) {
|
---|
350 | continue;
|
---|
351 | }
|
---|
352 | ret.add(plugin);
|
---|
353 | }
|
---|
354 | return ret;
|
---|
355 | }
|
---|
356 |
|
---|
357 | /**
|
---|
358 | * Parses the plugin list
|
---|
359 | *
|
---|
360 | * @param site the site from where the list was downloaded
|
---|
361 | * @param doc the document with the plugin list
|
---|
362 | */
|
---|
363 | protected void parsePluginListDocument(String site, String doc) {
|
---|
364 | try {
|
---|
365 | getProgressMonitor().subTask(tr("Parsing plugin list from site ''{0}''", site));
|
---|
366 | InputStream in = new ByteArrayInputStream(doc.getBytes(StandardCharsets.UTF_8));
|
---|
367 | List<PluginInformation> pis = new PluginListParser().parse(in);
|
---|
368 | availablePlugins.addAll(filterDeprecatedPlugins(pis));
|
---|
369 | } catch (PluginListParseException e) {
|
---|
370 | Main.error(tr("Failed to parse plugin list document from site ''{0}''. Skipping site. Exception was: {1}", site, e.toString()));
|
---|
371 | Main.error(e);
|
---|
372 | }
|
---|
373 | }
|
---|
374 |
|
---|
375 | @Override
|
---|
376 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
377 | if (sites == null) return;
|
---|
378 | getProgressMonitor().setTicksCount(sites.size() * 3);
|
---|
379 | File pluginDir = Main.pref.getPluginsDirectory();
|
---|
380 |
|
---|
381 | // collect old cache files and remove if no longer in use
|
---|
382 | List<File> siteCacheFiles = new LinkedList<>();
|
---|
383 | for (String location : PluginInformation.getPluginLocations()) {
|
---|
384 | File [] f = new File(location).listFiles(
|
---|
385 | new FilenameFilter() {
|
---|
386 | @Override
|
---|
387 | public boolean accept(File dir, String name) {
|
---|
388 | return name.matches("^([0-9]+-)?site.*\\.txt$") ||
|
---|
389 | name.matches("^([0-9]+-)?site.*-icons\\.zip$");
|
---|
390 | }
|
---|
391 | }
|
---|
392 | );
|
---|
393 | if(f != null && f.length > 0) {
|
---|
394 | siteCacheFiles.addAll(Arrays.asList(f));
|
---|
395 | }
|
---|
396 | }
|
---|
397 |
|
---|
398 | for (String site: sites) {
|
---|
399 | String printsite = site.replaceAll("%<(.*)>", "");
|
---|
400 | getProgressMonitor().subTask(tr("Processing plugin list from site ''{0}''", printsite));
|
---|
401 | String list = downloadPluginList(site, getProgressMonitor().createSubTaskMonitor(0, false));
|
---|
402 | if (canceled) return;
|
---|
403 | siteCacheFiles.remove(createSiteCacheFile(pluginDir, site, CacheType.PLUGIN_LIST));
|
---|
404 | siteCacheFiles.remove(createSiteCacheFile(pluginDir, site, CacheType.ICON_LIST));
|
---|
405 | if(list != null)
|
---|
406 | {
|
---|
407 | getProgressMonitor().worked(1);
|
---|
408 | cachePluginList(site, list);
|
---|
409 | if (canceled) return;
|
---|
410 | getProgressMonitor().worked(1);
|
---|
411 | parsePluginListDocument(site, list);
|
---|
412 | if (canceled) return;
|
---|
413 | getProgressMonitor().worked(1);
|
---|
414 | if (canceled) return;
|
---|
415 | }
|
---|
416 | downloadPluginIcons(site+"-icons.zip", createSiteCacheFile(pluginDir, site, CacheType.ICON_LIST), getProgressMonitor().createSubTaskMonitor(0, false));
|
---|
417 | }
|
---|
418 | for (File file: siteCacheFiles) /* remove old stuff or whole update process is broken */
|
---|
419 | {
|
---|
420 | file.delete();
|
---|
421 | }
|
---|
422 | }
|
---|
423 |
|
---|
424 | /**
|
---|
425 | * Replies true if the task was canceled
|
---|
426 | * @return <code>true</code> if the task was stopped by the user
|
---|
427 | */
|
---|
428 | public boolean isCanceled() {
|
---|
429 | return canceled;
|
---|
430 | }
|
---|
431 |
|
---|
432 | /**
|
---|
433 | * Replies the list of plugins described in the downloaded plugin lists
|
---|
434 | *
|
---|
435 | * @return the list of plugins
|
---|
436 | * @since 5601
|
---|
437 | */
|
---|
438 | public List<PluginInformation> getAvailablePlugins() {
|
---|
439 | return availablePlugins;
|
---|
440 | }
|
---|
441 | }
|
---|