001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.plugins;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.Component;
007import java.io.File;
008import java.io.FileOutputStream;
009import java.io.IOException;
010import java.io.InputStream;
011import java.io.OutputStream;
012import java.net.HttpURLConnection;
013import java.net.MalformedURLException;
014import java.net.URL;
015import java.util.Collection;
016import java.util.LinkedList;
017
018import org.openstreetmap.josm.Main;
019import org.openstreetmap.josm.data.Version;
020import org.openstreetmap.josm.gui.ExtendedDialog;
021import org.openstreetmap.josm.gui.PleaseWaitRunnable;
022import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
023import org.openstreetmap.josm.gui.progress.ProgressMonitor;
024import org.openstreetmap.josm.io.CachedFile;
025import org.openstreetmap.josm.tools.CheckParameterUtil;
026import org.xml.sax.SAXException;
027
028/**
029 * Asynchronous task for downloading a collection of plugins.
030 *
031 * When the task is finished {@link #getDownloadedPlugins()} replies the list of downloaded plugins
032 * and {@link #getFailedPlugins()} replies the list of failed plugins.
033 *
034 */
035public class PluginDownloadTask extends PleaseWaitRunnable{
036
037    /**
038     * The accepted MIME types sent in the HTTP Accept header.
039     * @since 6867
040     */
041    public static final String PLUGIN_MIME_TYPES = "application/java-archive, application/zip; q=0.9, application/octet-stream; q=0.5";
042    
043    private final Collection<PluginInformation> toUpdate = new LinkedList<>();
044    private final Collection<PluginInformation> failed = new LinkedList<>();
045    private final Collection<PluginInformation> downloaded = new LinkedList<>();
046    private Exception lastException;
047    private boolean canceled;
048    private HttpURLConnection downloadConnection;
049
050    /**
051     * Creates the download task
052     *
053     * @param parent the parent component relative to which the {@link org.openstreetmap.josm.gui.PleaseWaitDialog} is displayed
054     * @param toUpdate a collection of plugin descriptions for plugins to update/download. Must not be null.
055     * @param title the title to display in the {@link org.openstreetmap.josm.gui.PleaseWaitDialog}
056     * @throws IllegalArgumentException thrown if toUpdate is null
057     */
058    public PluginDownloadTask(Component parent, Collection<PluginInformation> toUpdate, String title) throws IllegalArgumentException{
059        super(parent, title == null ? "" : title, false /* don't ignore exceptions */);
060        CheckParameterUtil.ensureParameterNotNull(toUpdate, "toUpdate");
061        this.toUpdate.addAll(toUpdate);
062    }
063
064    /**
065     * Creates the task
066     *
067     * @param monitor a progress monitor. Defaults to {@link NullProgressMonitor#INSTANCE} if null
068     * @param toUpdate a collection of plugin descriptions for plugins to update/download. Must not be null.
069     * @param title the title to display in the {@link org.openstreetmap.josm.gui.PleaseWaitDialog}
070     * @throws IllegalArgumentException thrown if toUpdate is null
071     */
072    public PluginDownloadTask(ProgressMonitor monitor, Collection<PluginInformation> toUpdate, String title) {
073        super(title, monitor == null? NullProgressMonitor.INSTANCE: monitor, false /* don't ignore exceptions */);
074        CheckParameterUtil.ensureParameterNotNull(toUpdate, "toUpdate");
075        this.toUpdate.addAll(toUpdate);
076    }
077
078    /**
079     * Sets the collection of plugins to update.
080     *
081     * @param toUpdate the collection of plugins to update. Must not be null.
082     * @throws IllegalArgumentException thrown if toUpdate is null
083     */
084    public void setPluginsToDownload(Collection<PluginInformation> toUpdate) throws IllegalArgumentException{
085        CheckParameterUtil.ensureParameterNotNull(toUpdate, "toUpdate");
086        this.toUpdate.clear();
087        this.toUpdate.addAll(toUpdate);
088    }
089
090    @Override
091    protected void cancel() {
092        this.canceled = true;
093        synchronized(this) {
094            if (downloadConnection != null) {
095                downloadConnection.disconnect();
096            }
097        }
098    }
099
100    @Override
101    protected void finish() {}
102
103    protected void download(PluginInformation pi, File file) throws PluginDownloadException{
104        if (pi.mainversion > Version.getInstance().getVersion()) {
105            ExtendedDialog dialog = new ExtendedDialog(
106                    progressMonitor.getWindowParent(),
107                    tr("Skip download"),
108                    new String[] {
109                        tr("Download Plugin"),
110                        tr("Skip Download") }
111            );
112            dialog.setContent(tr("JOSM version {0} required for plugin {1}.", pi.mainversion, pi.name));
113            dialog.setButtonIcons(new String[] { "download.png", "cancel.png" });
114            dialog.showDialog();
115            int answer = dialog.getValue();
116            if (answer != 1)
117                throw new PluginDownloadException(tr("Download skipped"));
118        }
119        try {
120            if (pi.downloadlink == null) {
121                String msg = tr("Cannot download plugin ''{0}''. Its download link is not known. Skipping download.", pi.name);
122                Main.warn(msg);
123                throw new PluginDownloadException(msg);
124            }
125            URL url = new URL(pi.downloadlink);
126            synchronized(this) {
127                downloadConnection = CachedFile.connectFollowingRedirect(url, PLUGIN_MIME_TYPES, null);
128            }
129            try (
130                InputStream in = downloadConnection.getInputStream();
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.", pi.name, pi.downloadlink);
140            Main.warn(msg);
141            throw new PluginDownloadException(msg);
142        } catch (IOException e) {
143            if (canceled)
144                return;
145            throw new PluginDownloadException(e);
146        } finally {
147            synchronized(this) {
148                downloadConnection = null;
149            }
150        }
151    }
152
153    @Override
154    protected void realRun() throws SAXException, IOException {
155        File pluginDir = Main.pref.getPluginsDirectory();
156        if (!pluginDir.exists() && !pluginDir.mkdirs()) {
157            lastException = new PluginDownloadException(tr("Failed to create plugin directory ''{0}''", pluginDir.toString()));
158            failed.addAll(toUpdate);
159            return;
160        }
161        getProgressMonitor().setTicksCount(toUpdate.size());
162        for (PluginInformation d : toUpdate) {
163            if (canceled) return;
164            progressMonitor.subTask(tr("Downloading Plugin {0}...", d.name));
165            progressMonitor.worked(1);
166            File pluginFile = new File(pluginDir, d.name + ".jar.new");
167            try {
168                download(d, pluginFile);
169            } catch(PluginDownloadException e) {
170                Main.error(e);
171                failed.add(d);
172                continue;
173            }
174            downloaded.add(d);
175        }
176        PluginHandler.installDownloadedPlugins(false);
177    }
178
179    /**
180     * Replies true if the task was canceled by the user
181     *
182     * @return <code>true</code> if the task was stopped by the user
183     */
184    public boolean isCanceled() {
185        return canceled;
186    }
187
188    /**
189     * Replies the list of successfully downloaded plugins
190     *
191     * @return the list of successfully downloaded plugins
192     */
193    public Collection<PluginInformation> getFailedPlugins() {
194        return failed;
195    }
196
197    /**
198     * Replies the list of plugins whose download has failed
199     *
200     * @return the list of plugins whose download has failed
201     */
202    public Collection<PluginInformation> getDownloadedPlugins() {
203        return downloaded;
204    }
205}