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

Last change on this file since 9569 was 9309, checked in by simon04, 8 years ago

see #12292 - Allow to disconnect HttpClient in connecting phase

  • Property svn:eol-style set to native
File size: 7.7 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.IOException;
9import java.io.InputStream;
10import java.net.MalformedURLException;
11import java.net.URL;
12import java.nio.file.Files;
13import java.nio.file.StandardCopyOption;
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 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 downloadConnection.connect();
128 }
129 try (InputStream in = downloadConnection.getResponse().getContent()) {
130 Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
131 }
132 } catch (MalformedURLException e) {
133 String msg = tr("Cannot download plugin ''{0}''. Its download link ''{1}'' is not a valid URL. Skipping download.",
134 pi.name, pi.downloadlink);
135 Main.warn(msg);
136 throw new PluginDownloadException(msg, e);
137 } catch (IOException e) {
138 if (canceled)
139 return;
140 throw new PluginDownloadException(e);
141 } finally {
142 synchronized (this) {
143 downloadConnection = null;
144 }
145 }
146 }
147
148 @Override
149 protected void realRun() throws SAXException, IOException {
150 File pluginDir = Main.pref.getPluginsDirectory();
151 if (!pluginDir.exists() && !pluginDir.mkdirs()) {
152 /*lastException =*/ new PluginDownloadException(tr("Failed to create plugin directory ''{0}''", pluginDir.toString()));
153 failed.addAll(toUpdate);
154 return;
155 }
156 getProgressMonitor().setTicksCount(toUpdate.size());
157 for (PluginInformation d : toUpdate) {
158 if (canceled) return;
159 String message = tr("Downloading Plugin {0}...", d.name);
160 Main.info(message);
161 progressMonitor.subTask(message);
162 progressMonitor.worked(1);
163 File pluginFile = new File(pluginDir, d.name + ".jar.new");
164 try {
165 download(d, pluginFile);
166 } catch (PluginDownloadException e) {
167 Main.error(e);
168 failed.add(d);
169 continue;
170 }
171 downloaded.add(d);
172 }
173 PluginHandler.installDownloadedPlugins(false);
174 }
175
176 /**
177 * Replies true if the task was canceled by the user
178 *
179 * @return <code>true</code> if the task was stopped by the user
180 */
181 public boolean isCanceled() {
182 return canceled;
183 }
184
185 /**
186 * Replies the list of plugins whose download has failed.
187 *
188 * @return the list of plugins whose download has failed
189 */
190 public Collection<PluginInformation> getFailedPlugins() {
191 return failed;
192 }
193
194 /**
195 * Replies the list of successfully downloaded plugins.
196 *
197 * @return the list of successfully downloaded plugins
198 */
199 public Collection<PluginInformation> getDownloadedPlugins() {
200 return downloaded;
201 }
202}
Note: See TracBrowser for help on using the repository browser.