source: josm/trunk/src/org/openstreetmap/josm/plugins/ReadRemotePluginInformationTask.java@ 8855

Last change on this file since 8855 was 8540, checked in by Don-vip, 9 years ago

fix remaining checkstyle issues

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