source: josm/trunk/src/org/openstreetmap/josm/plugins/PluginDownloadTask.java@ 9231

Last change on this file since 9231 was 9168, checked in by simon04, 8 years ago

see #12231 - Uniform access to HTTP resources

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