[3083] | 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 |
|
---|
[6257] | 6 | import java.awt.Dimension;
|
---|
| 7 | import java.awt.GridBagLayout;
|
---|
[3083] | 8 | import java.io.BufferedReader;
|
---|
| 9 | import java.io.ByteArrayInputStream;
|
---|
| 10 | import java.io.File;
|
---|
[3479] | 11 | import java.io.FileOutputStream;
|
---|
[3287] | 12 | import java.io.FilenameFilter;
|
---|
[3083] | 13 | import java.io.IOException;
|
---|
| 14 | import java.io.InputStream;
|
---|
| 15 | import java.io.InputStreamReader;
|
---|
[3084] | 16 | import java.io.OutputStreamWriter;
|
---|
[3083] | 17 | import java.io.PrintWriter;
|
---|
| 18 | import java.net.HttpURLConnection;
|
---|
| 19 | import java.net.MalformedURLException;
|
---|
| 20 | import java.net.URL;
|
---|
[7082] | 21 | import java.nio.charset.StandardCharsets;
|
---|
[3083] | 22 | import java.util.ArrayList;
|
---|
| 23 | import java.util.Arrays;
|
---|
| 24 | import java.util.Collection;
|
---|
| 25 | import java.util.Collections;
|
---|
| 26 | import java.util.HashSet;
|
---|
| 27 | import java.util.LinkedList;
|
---|
| 28 | import java.util.List;
|
---|
[8338] | 29 | import java.util.Set;
|
---|
[3083] | 30 |
|
---|
[6257] | 31 | import javax.swing.JLabel;
|
---|
| 32 | import javax.swing.JOptionPane;
|
---|
| 33 | import javax.swing.JPanel;
|
---|
| 34 | import javax.swing.JScrollPane;
|
---|
| 35 |
|
---|
[3083] | 36 | import org.openstreetmap.josm.Main;
|
---|
| 37 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
[3086] | 38 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
[3083] | 39 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
[6257] | 40 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
| 41 | import org.openstreetmap.josm.gui.widgets.JosmTextArea;
|
---|
[3083] | 42 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
[6257] | 43 | import org.openstreetmap.josm.tools.GBC;
|
---|
[4087] | 44 | import org.openstreetmap.josm.tools.Utils;
|
---|
[3083] | 45 | import org.xml.sax.SAXException;
|
---|
| 46 |
|
---|
| 47 | /**
|
---|
[6257] | 48 | * An asynchronous task for downloading plugin lists from the configured plugin download sites.
|
---|
| 49 | * @since 2817
|
---|
[3083] | 50 | */
|
---|
[6257] | 51 | public class ReadRemotePluginInformationTask extends PleaseWaitRunnable {
|
---|
[3083] | 52 |
|
---|
| 53 | private Collection<String> sites;
|
---|
| 54 | private boolean canceled;
|
---|
| 55 | private HttpURLConnection connection;
|
---|
[3287] | 56 | private List<PluginInformation> availablePlugins;
|
---|
[6839] | 57 | private boolean displayErrMsg;
|
---|
[3083] | 58 |
|
---|
[7434] | 59 | protected final void init(Collection<String> sites, boolean displayErrMsg) {
|
---|
[3086] | 60 | this.sites = sites;
|
---|
| 61 | if (sites == null) {
|
---|
| 62 | this.sites = Collections.emptySet();
|
---|
| 63 | }
|
---|
[7005] | 64 | this.availablePlugins = new LinkedList<>();
|
---|
[6839] | 65 | this.displayErrMsg = displayErrMsg;
|
---|
[3086] | 66 | }
|
---|
[7434] | 67 |
|
---|
[3083] | 68 | /**
|
---|
[7434] | 69 | * Constructs a new {@code ReadRemotePluginInformationTask}.
|
---|
[3479] | 70 | *
|
---|
[3083] | 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 */);
|
---|
[6839] | 75 | init(sites, true);
|
---|
[3083] | 76 | }
|
---|
| 77 |
|
---|
[3086] | 78 | /**
|
---|
[7434] | 79 | * Constructs a new {@code ReadRemotePluginInformationTask}.
|
---|
[3479] | 80 | *
|
---|
[5266] | 81 | * @param monitor the progress monitor. Defaults to {@link NullProgressMonitor#INSTANCE} if null
|
---|
[3086] | 82 | * @param sites the collection of download sites. Defaults to the empty collection if null.
|
---|
[6839] | 83 | * @param displayErrMsg if {@code true}, a blocking error message is displayed in case of I/O exception.
|
---|
[3086] | 84 | */
|
---|
[6839] | 85 | public ReadRemotePluginInformationTask(ProgressMonitor monitor, Collection<String> sites, boolean displayErrMsg) {
|
---|
[8510] | 86 | super(tr("Download plugin list..."), monitor == null ? NullProgressMonitor.INSTANCE : monitor, false /* don't ignore exceptions */);
|
---|
[6839] | 87 | init(sites, displayErrMsg);
|
---|
[3086] | 88 | }
|
---|
| 89 |
|
---|
[3083] | 90 | @Override
|
---|
| 91 | protected void cancel() {
|
---|
| 92 | canceled = true;
|
---|
[8510] | 93 | synchronized (this) {
|
---|
[3083] | 94 | if (connection != null) {
|
---|
| 95 | connection.disconnect();
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | @Override
|
---|
| 101 | protected void finish() {}
|
---|
| 102 |
|
---|
| 103 | /**
|
---|
[7434] | 104 | * Creates the file name for the cached plugin list and the icon cache file.
|
---|
[3479] | 105 | *
|
---|
[8093] | 106 | * @param pluginDir directory of plugin for data storage
|
---|
[3083] | 107 | * @param site the name of the site
|
---|
| 108 | * @return the file name for the cache file
|
---|
| 109 | */
|
---|
[7655] | 110 | protected File createSiteCacheFile(File pluginDir, String site) {
|
---|
[3287] | 111 | String name;
|
---|
[3083] | 112 | try {
|
---|
[3380] | 113 | site = site.replaceAll("%<(.*)>", "");
|
---|
[3083] | 114 | URL url = new URL(site);
|
---|
| 115 | StringBuilder sb = new StringBuilder();
|
---|
[8379] | 116 | sb.append("site-")
|
---|
[8390] | 117 | .append(url.getHost()).append('-');
|
---|
[3083] | 118 | if (url.getPort() != -1) {
|
---|
[8390] | 119 | sb.append(url.getPort()).append('-');
|
---|
[3083] | 120 | }
|
---|
| 121 | String path = url.getPath();
|
---|
[8510] | 122 | for (int i = 0; i < path.length(); i++) {
|
---|
[3083] | 123 | char c = path.charAt(i);
|
---|
| 124 | if (Character.isLetterOrDigit(c)) {
|
---|
| 125 | sb.append(c);
|
---|
| 126 | } else {
|
---|
[8390] | 127 | sb.append('_');
|
---|
[3083] | 128 | }
|
---|
| 129 | }
|
---|
[7655] | 130 | sb.append(".txt");
|
---|
[3287] | 131 | name = sb.toString();
|
---|
[8510] | 132 | } catch (MalformedURLException e) {
|
---|
[3287] | 133 | name = "site-unknown.txt";
|
---|
[3083] | 134 | }
|
---|
[3287] | 135 | return new File(pluginDir, name);
|
---|
[3083] | 136 | }
|
---|
| 137 |
|
---|
| 138 | /**
|
---|
| 139 | * Downloads the list from a remote location
|
---|
[3479] | 140 | *
|
---|
[3083] | 141 | * @param site the site URL
|
---|
| 142 | * @param monitor a progress monitor
|
---|
| 143 | * @return the downloaded list
|
---|
| 144 | */
|
---|
[6257] | 145 | protected String downloadPluginList(String site, final ProgressMonitor monitor) {
|
---|
[6839] | 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("%<(.*)>", "");
|
---|
[8461] | 149 | if (pl != null && !pl.isEmpty()) {
|
---|
[6839] | 150 | site = site.replaceAll("%<(.*)>", "$1"+pl);
|
---|
| 151 | } else {
|
---|
| 152 | site = printsite;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
[3083] | 155 | try {
|
---|
| 156 | monitor.beginTask("");
|
---|
[3380] | 157 | monitor.indeterminateSubTask(tr("Downloading plugin list from ''{0}''", printsite));
|
---|
[3083] | 158 |
|
---|
| 159 | URL url = new URL(site);
|
---|
[8510] | 160 | synchronized (this) {
|
---|
[5587] | 161 | connection = Utils.openHttpConnection(url);
|
---|
[3083] | 162 | connection.setRequestProperty("Cache-Control", "no-cache");
|
---|
[3084] | 163 | connection.setRequestProperty("Accept-Charset", "utf-8");
|
---|
[3083] | 164 | }
|
---|
[7082] | 165 | try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
|
---|
[7037] | 166 | StringBuilder sb = new StringBuilder();
|
---|
| 167 | String line;
|
---|
| 168 | while ((line = in.readLine()) != null) {
|
---|
[8390] | 169 | sb.append(line).append('\n');
|
---|
[7037] | 170 | }
|
---|
| 171 | return sb.toString();
|
---|
[3083] | 172 | }
|
---|
[6257] | 173 | } catch (MalformedURLException e) {
|
---|
[3083] | 174 | if (canceled) return null;
|
---|
[6643] | 175 | Main.error(e);
|
---|
[3083] | 176 | return null;
|
---|
[6257] | 177 | } catch (IOException e) {
|
---|
[3083] | 178 | if (canceled) return null;
|
---|
[6839] | 179 | Main.addNetworkError(site, e);
|
---|
| 180 | handleIOException(monitor, e, tr("Plugin list download error"), tr("JOSM failed to download plugin list:"), displayErrMsg);
|
---|
[3083] | 181 | return null;
|
---|
| 182 | } finally {
|
---|
[8510] | 183 | synchronized (this) {
|
---|
[3083] | 184 | if (connection != null) {
|
---|
| 185 | connection.disconnect();
|
---|
| 186 | }
|
---|
| 187 | connection = null;
|
---|
| 188 | }
|
---|
| 189 | monitor.finishTask();
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
[6839] | 192 |
|
---|
[8540] | 193 | private void handleIOException(final ProgressMonitor monitor, IOException e, final String title, final String firstMessage,
|
---|
| 194 | boolean displayMsg) {
|
---|
[6257] | 195 | StringBuilder sb = new StringBuilder();
|
---|
[7033] | 196 | try (InputStream errStream = connection.getErrorStream()) {
|
---|
| 197 | if (errStream != null) {
|
---|
[7082] | 198 | try (BufferedReader err = new BufferedReader(new InputStreamReader(errStream, StandardCharsets.UTF_8))) {
|
---|
[7033] | 199 | String line;
|
---|
| 200 | while ((line = err.readLine()) != null) {
|
---|
[8390] | 201 | sb.append(line).append('\n');
|
---|
[7033] | 202 | }
|
---|
| 203 | } catch (Exception ex) {
|
---|
| 204 | Main.error(e);
|
---|
| 205 | Main.error(ex);
|
---|
[6257] | 206 | }
|
---|
| 207 | }
|
---|
[7033] | 208 | } catch (IOException ex) {
|
---|
| 209 | Main.warn(ex);
|
---|
[6257] | 210 | }
|
---|
| 211 | final String msg = e.getMessage();
|
---|
| 212 | final String details = sb.toString();
|
---|
[6839] | 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 |
|
---|
[8509] | 224 | private void displayErrorMessage(final ProgressMonitor monitor, final String msg, final String details, final String title,
|
---|
| 225 | final String firstMessage) {
|
---|
[6257] | 226 | GuiHelper.runInEDTAndWait(new Runnable() {
|
---|
| 227 | @Override public void run() {
|
---|
| 228 | JPanel panel = new JPanel(new GridBagLayout());
|
---|
[6839] | 229 | panel.add(new JLabel(firstMessage), GBC.eol().insets(0, 0, 0, 10));
|
---|
[6257] | 230 | StringBuilder b = new StringBuilder();
|
---|
| 231 | for (String part : msg.split("(?<=\\G.{200})")) {
|
---|
[8390] | 232 | b.append(part).append('\n');
|
---|
[6257] | 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);
|
---|
[6643] | 239 | area.setLineWrap(true);
|
---|
| 240 | area.setWrapStyleWord(true);
|
---|
[6257] | 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 | }
|
---|
[3083] | 249 |
|
---|
| 250 | /**
|
---|
| 251 | * Writes the list of plugins to a cache file
|
---|
[3479] | 252 | *
|
---|
[3083] | 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) {
|
---|
[7033] | 257 | File pluginDir = Main.pref.getPluginsDirectory();
|
---|
| 258 | if (!pluginDir.exists() && !pluginDir.mkdirs()) {
|
---|
[8509] | 259 | Main.warn(tr("Failed to create plugin directory ''{0}''. Cannot cache plugin list from plugin site ''{1}''.",
|
---|
| 260 | pluginDir.toString(), site));
|
---|
[7033] | 261 | }
|
---|
[7655] | 262 | File cacheFile = createSiteCacheFile(pluginDir, site);
|
---|
[7033] | 263 | getProgressMonitor().subTask(tr("Writing plugin list to local cache ''{0}''", cacheFile.toString()));
|
---|
[7082] | 264 | try (PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(cacheFile), StandardCharsets.UTF_8))) {
|
---|
[3084] | 265 | writer.write(list);
|
---|
[7033] | 266 | writer.flush();
|
---|
[8510] | 267 | } catch (IOException e) {
|
---|
[3083] | 268 | // just failed to write the cache file. No big deal, but log the exception anyway
|
---|
[6643] | 269 | Main.error(e);
|
---|
[3083] | 270 | }
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | /**
|
---|
| 274 | * Filter information about deprecated plugins from the list of downloaded
|
---|
| 275 | * plugins
|
---|
[3479] | 276 | *
|
---|
[3083] | 277 | * @param plugins the plugin informations
|
---|
| 278 | * @return the plugin informations, without deprecated plugins
|
---|
| 279 | */
|
---|
| 280 | protected List<PluginInformation> filterDeprecatedPlugins(List<PluginInformation> plugins) {
|
---|
[7005] | 281 | List<PluginInformation> ret = new ArrayList<>(plugins.size());
|
---|
[8338] | 282 | Set<String> deprecatedPluginNames = new HashSet<>();
|
---|
[3730] | 283 | for (PluginHandler.DeprecatedPlugin p : PluginHandler.DEPRECATED_PLUGINS) {
|
---|
| 284 | deprecatedPluginNames.add(p.name);
|
---|
| 285 | }
|
---|
[3083] | 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
|
---|
[3479] | 297 | *
|
---|
[3083] | 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));
|
---|
[7082] | 304 | InputStream in = new ByteArrayInputStream(doc.getBytes(StandardCharsets.UTF_8));
|
---|
[3083] | 305 | List<PluginInformation> pis = new PluginListParser().parse(in);
|
---|
[3287] | 306 | availablePlugins.addAll(filterDeprecatedPlugins(pis));
|
---|
[6248] | 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()));
|
---|
[6643] | 309 | Main.error(e);
|
---|
[3083] | 310 | }
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | @Override
|
---|
| 314 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
| 315 | if (sites == null) return;
|
---|
| 316 | getProgressMonitor().setTicksCount(sites.size() * 3);
|
---|
[3287] | 317 | File pluginDir = Main.pref.getPluginsDirectory();
|
---|
[3330] | 318 |
|
---|
| 319 | // collect old cache files and remove if no longer in use
|
---|
[7005] | 320 | List<File> siteCacheFiles = new LinkedList<>();
|
---|
[3287] | 321 | for (String location : PluginInformation.getPluginLocations()) {
|
---|
[8443] | 322 | File[] f = new File(location).listFiles(
|
---|
[3479] | 323 | new FilenameFilter() {
|
---|
[6084] | 324 | @Override
|
---|
[3479] | 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 | }
|
---|
[3287] | 329 | }
|
---|
| 330 | );
|
---|
[8510] | 331 | if (f != null && f.length > 0) {
|
---|
[3287] | 332 | siteCacheFiles.addAll(Arrays.asList(f));
|
---|
[3479] | 333 | }
|
---|
[3287] | 334 | }
|
---|
| 335 |
|
---|
[3083] | 336 | for (String site: sites) {
|
---|
[3380] | 337 | String printsite = site.replaceAll("%<(.*)>", "");
|
---|
| 338 | getProgressMonitor().subTask(tr("Processing plugin list from site ''{0}''", printsite));
|
---|
[3083] | 339 | String list = downloadPluginList(site, getProgressMonitor().createSubTaskMonitor(0, false));
|
---|
| 340 | if (canceled) return;
|
---|
[7655] | 341 | siteCacheFiles.remove(createSiteCacheFile(pluginDir, site));
|
---|
[7434] | 342 | if (list != null) {
|
---|
[3287] | 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 | }
|
---|
[3083] | 352 | }
|
---|
[7434] | 353 | // remove old stuff or whole update process is broken
|
---|
| 354 | for (File file: siteCacheFiles) {
|
---|
[3287] | 355 | file.delete();
|
---|
| 356 | }
|
---|
[3083] | 357 | }
|
---|
| 358 |
|
---|
| 359 | /**
|
---|
| 360 | * Replies true if the task was canceled
|
---|
[5836] | 361 | * @return <code>true</code> if the task was stopped by the user
|
---|
[3083] | 362 | */
|
---|
| 363 | public boolean isCanceled() {
|
---|
| 364 | return canceled;
|
---|
| 365 | }
|
---|
| 366 |
|
---|
| 367 | /**
|
---|
| 368 | * Replies the list of plugins described in the downloaded plugin lists
|
---|
[3479] | 369 | *
|
---|
[3083] | 370 | * @return the list of plugins
|
---|
[5601] | 371 | * @since 5601
|
---|
[3083] | 372 | */
|
---|
[5601] | 373 | public List<PluginInformation> getAvailablePlugins() {
|
---|
[3287] | 374 | return availablePlugins;
|
---|
[3083] | 375 | }
|
---|
| 376 | }
|
---|