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.Component;
|
---|
7 | import java.io.File;
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.io.InputStream;
|
---|
10 | import java.net.MalformedURLException;
|
---|
11 | import java.net.URL;
|
---|
12 | import java.nio.file.Files;
|
---|
13 | import java.nio.file.StandardCopyOption;
|
---|
14 | import java.util.Collection;
|
---|
15 | import java.util.LinkedList;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.data.Preferences;
|
---|
18 | import org.openstreetmap.josm.data.Version;
|
---|
19 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
20 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
21 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
22 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
23 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
24 | import org.openstreetmap.josm.tools.HttpClient;
|
---|
25 | import org.openstreetmap.josm.tools.Logging;
|
---|
26 | import org.xml.sax.SAXException;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Asynchronous task for downloading a collection of plugins.
|
---|
30 | *
|
---|
31 | * When the task is finished {@link #getDownloadedPlugins()} replies the list of downloaded plugins
|
---|
32 | * and {@link #getFailedPlugins()} replies the list of failed plugins.
|
---|
33 | * @since 2817
|
---|
34 | */
|
---|
35 | public class PluginDownloadTask extends PleaseWaitRunnable {
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * The accepted MIME types sent in the HTTP Accept header.
|
---|
39 | * @since 6867
|
---|
40 | */
|
---|
41 | public static final String PLUGIN_MIME_TYPES = "application/java-archive, application/zip; q=0.9, application/octet-stream; q=0.5";
|
---|
42 |
|
---|
43 | private final Collection<PluginInformation> toUpdate = new LinkedList<>();
|
---|
44 | private final Collection<PluginInformation> failed = new LinkedList<>();
|
---|
45 | private final Collection<PluginInformation> downloaded = new LinkedList<>();
|
---|
46 | private Exception lastException;
|
---|
47 | private boolean canceled;
|
---|
48 | private HttpClient downloadConnection;
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Creates the download task
|
---|
52 | *
|
---|
53 | * @param parent the parent component relative to which the {@link org.openstreetmap.josm.gui.PleaseWaitDialog} is displayed
|
---|
54 | * @param toUpdate a collection of plugin descriptions for plugins to update/download. Must not be null.
|
---|
55 | * @param title the title to display in the {@link org.openstreetmap.josm.gui.PleaseWaitDialog}
|
---|
56 | * @throws IllegalArgumentException if toUpdate is null
|
---|
57 | */
|
---|
58 | public PluginDownloadTask(Component parent, Collection<PluginInformation> toUpdate, String title) {
|
---|
59 | super(parent, title == null ? "" : title, false /* don't ignore exceptions */);
|
---|
60 | CheckParameterUtil.ensureParameterNotNull(toUpdate, "toUpdate");
|
---|
61 | this.toUpdate.addAll(toUpdate);
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Creates the task
|
---|
66 | *
|
---|
67 | * @param monitor a progress monitor. Defaults to {@link NullProgressMonitor#INSTANCE} if null
|
---|
68 | * @param toUpdate a collection of plugin descriptions for plugins to update/download. Must not be null.
|
---|
69 | * @param title the title to display in the {@link org.openstreetmap.josm.gui.PleaseWaitDialog}
|
---|
70 | * @throws IllegalArgumentException if toUpdate is null
|
---|
71 | */
|
---|
72 | public PluginDownloadTask(ProgressMonitor monitor, Collection<PluginInformation> toUpdate, String title) {
|
---|
73 | super(title, monitor == null ? NullProgressMonitor.INSTANCE : monitor, false /* don't ignore exceptions */);
|
---|
74 | CheckParameterUtil.ensureParameterNotNull(toUpdate, "toUpdate");
|
---|
75 | this.toUpdate.addAll(toUpdate);
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Sets the collection of plugins to update.
|
---|
80 | *
|
---|
81 | * @param toUpdate the collection of plugins to update. Must not be null.
|
---|
82 | * @throws IllegalArgumentException if toUpdate is null
|
---|
83 | */
|
---|
84 | public void setPluginsToDownload(Collection<PluginInformation> toUpdate) {
|
---|
85 | CheckParameterUtil.ensureParameterNotNull(toUpdate, "toUpdate");
|
---|
86 | this.toUpdate.clear();
|
---|
87 | this.toUpdate.addAll(toUpdate);
|
---|
88 | }
|
---|
89 |
|
---|
90 | @Override
|
---|
91 | protected void cancel() {
|
---|
92 | this.canceled = true;
|
---|
93 | synchronized (this) {
|
---|
94 | if (downloadConnection != null) {
|
---|
95 | downloadConnection.disconnect();
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | @Override
|
---|
101 | protected void finish() {
|
---|
102 | // Do nothing. Error/success feedback is managed in PluginPreference.notifyDownloadResults()
|
---|
103 | }
|
---|
104 |
|
---|
105 | protected void download(PluginInformation pi, File file) throws PluginDownloadException {
|
---|
106 | if (pi.mainversion > Version.getInstance().getVersion()) {
|
---|
107 | ExtendedDialog dialog = new ExtendedDialog(
|
---|
108 | progressMonitor.getWindowParent(),
|
---|
109 | tr("Skip Download"),
|
---|
110 | tr("Download Plugin"), tr("Skip Download")
|
---|
111 | );
|
---|
112 | dialog.setContent(tr("JOSM version {0} required for plugin {1}.", pi.mainversion, pi.name));
|
---|
113 | dialog.setButtonIcons("download", "cancel");
|
---|
114 | if (dialog.showDialog().getValue() != 1)
|
---|
115 | throw new PluginDownloadException(tr("Download skipped"));
|
---|
116 | }
|
---|
117 | try {
|
---|
118 | if (pi.downloadlink == null) {
|
---|
119 | String msg = tr("Cannot download plugin ''{0}''. Its download link is not known. Skipping download.", pi.name);
|
---|
120 | Logging.warn(msg);
|
---|
121 | throw new PluginDownloadException(msg);
|
---|
122 | }
|
---|
123 | URL url = new URL(pi.downloadlink);
|
---|
124 | Logging.debug("Download plugin {0} from {1}...", pi.name, url);
|
---|
125 | if ("https".equals(url.getProtocol()) || "http".equals(url.getProtocol())) {
|
---|
126 | synchronized (this) {
|
---|
127 | downloadConnection = HttpClient.create(url).setAccept(PLUGIN_MIME_TYPES);
|
---|
128 | downloadConnection.connect();
|
---|
129 | }
|
---|
130 | try (InputStream in = downloadConnection.getResponse().getContent()) {
|
---|
131 | Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
---|
132 | }
|
---|
133 | } else {
|
---|
134 | // this is an alternative for e.g. file:// URLs where HttpClient doesn't work
|
---|
135 | try (InputStream in = url.openConnection().getInputStream()) {
|
---|
136 | Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
---|
137 | }
|
---|
138 | }
|
---|
139 | } catch (MalformedURLException e) {
|
---|
140 | String msg = tr("Cannot download plugin ''{0}''. Its download link ''{1}'' is not a valid URL. Skipping download.",
|
---|
141 | pi.name, pi.downloadlink);
|
---|
142 | Logging.warn(msg);
|
---|
143 | throw new PluginDownloadException(msg, e);
|
---|
144 | } catch (IOException e) {
|
---|
145 | if (canceled)
|
---|
146 | return;
|
---|
147 | throw new PluginDownloadException(e);
|
---|
148 | } finally {
|
---|
149 | synchronized (this) {
|
---|
150 | downloadConnection = null;
|
---|
151 | }
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | @Override
|
---|
156 | protected void realRun() throws SAXException, IOException {
|
---|
157 | File pluginDir = Preferences.main().getPluginsDirectory();
|
---|
158 | if (!pluginDir.exists() && !pluginDir.mkdirs()) {
|
---|
159 | String message = tr("Failed to create plugin directory ''{0}''", pluginDir.toString());
|
---|
160 | lastException = new PluginDownloadException(message);
|
---|
161 | Logging.error(message);
|
---|
162 | failed.addAll(toUpdate);
|
---|
163 | return;
|
---|
164 | }
|
---|
165 | getProgressMonitor().setTicksCount(toUpdate.size());
|
---|
166 | for (PluginInformation d : toUpdate) {
|
---|
167 | if (canceled)
|
---|
168 | return;
|
---|
169 | String message = tr("Downloading Plugin {0}...", d.name);
|
---|
170 | Logging.info(message);
|
---|
171 | progressMonitor.subTask(message);
|
---|
172 | progressMonitor.worked(1);
|
---|
173 | File pluginFile = new File(pluginDir, d.name + ".jar.new");
|
---|
174 | try {
|
---|
175 | download(d, pluginFile);
|
---|
176 | } catch (PluginDownloadException e) {
|
---|
177 | lastException = e;
|
---|
178 | Logging.error(e);
|
---|
179 | failed.add(d);
|
---|
180 | continue;
|
---|
181 | }
|
---|
182 | downloaded.add(d);
|
---|
183 | }
|
---|
184 | PluginHandler.installDownloadedPlugins(toUpdate, false);
|
---|
185 | }
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * Replies true if the task was canceled by the user
|
---|
189 | *
|
---|
190 | * @return <code>true</code> if the task was stopped by the user
|
---|
191 | */
|
---|
192 | public boolean isCanceled() {
|
---|
193 | return canceled;
|
---|
194 | }
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * Replies the list of plugins whose download has failed.
|
---|
198 | *
|
---|
199 | * @return the list of plugins whose download has failed
|
---|
200 | */
|
---|
201 | public Collection<PluginInformation> getFailedPlugins() {
|
---|
202 | return failed;
|
---|
203 | }
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * Replies the list of successfully downloaded plugins.
|
---|
207 | *
|
---|
208 | * @return the list of successfully downloaded plugins
|
---|
209 | */
|
---|
210 | public Collection<PluginInformation> getDownloadedPlugins() {
|
---|
211 | return downloaded;
|
---|
212 | }
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * Replies the last exception that occured during download, or {@code null}.
|
---|
216 | * @return the last exception that occured during download, or {@code null}
|
---|
217 | * @since 9621
|
---|
218 | */
|
---|
219 | public Exception getLastException() {
|
---|
220 | return lastException;
|
---|
221 | }
|
---|
222 | }
|
---|