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 boolean canceled;
047    private HttpURLConnection downloadConnection;
048
049    /**
050     * Creates the download task
051     *
052     * @param parent the parent component relative to which the {@link org.openstreetmap.josm.gui.PleaseWaitDialog} is displayed
053     * @param toUpdate a collection of plugin descriptions for plugins to update/download. Must not be null.
054     * @param title the title to display in the {@link org.openstreetmap.josm.gui.PleaseWaitDialog}
055     * @throws IllegalArgumentException if toUpdate is null
056     */
057    public PluginDownloadTask(Component parent, Collection<PluginInformation> toUpdate, String title) {
058        super(parent, title == null ? "" : title, false /* don't ignore exceptions */);
059        CheckParameterUtil.ensureParameterNotNull(toUpdate, "toUpdate");
060        this.toUpdate.addAll(toUpdate);
061    }
062
063    /**
064     * Creates the task
065     *
066     * @param monitor a progress monitor. Defaults to {@link NullProgressMonitor#INSTANCE} if null
067     * @param toUpdate a collection of plugin descriptions for plugins to update/download. Must not be null.
068     * @param title the title to display in the {@link org.openstreetmap.josm.gui.PleaseWaitDialog}
069     * @throws IllegalArgumentException if toUpdate is null
070     */
071    public PluginDownloadTask(ProgressMonitor monitor, Collection<PluginInformation> toUpdate, String title) {
072        super(title, monitor == null ? NullProgressMonitor.INSTANCE : monitor, false /* don't ignore exceptions */);
073        CheckParameterUtil.ensureParameterNotNull(toUpdate, "toUpdate");
074        this.toUpdate.addAll(toUpdate);
075    }
076
077    /**
078     * Sets the collection of plugins to update.
079     *
080     * @param toUpdate the collection of plugins to update. Must not be null.
081     * @throws IllegalArgumentException if toUpdate is null
082     */
083    public void setPluginsToDownload(Collection<PluginInformation> toUpdate) {
084        CheckParameterUtil.ensureParameterNotNull(toUpdate, "toUpdate");
085        this.toUpdate.clear();
086        this.toUpdate.addAll(toUpdate);
087    }
088
089    @Override
090    protected void cancel() {
091        this.canceled = true;
092        synchronized (this) {
093            if (downloadConnection != null) {
094                downloadConnection.disconnect();
095            }
096        }
097    }
098
099    @Override
100    protected void finish() {}
101
102    protected void download(PluginInformation pi, File file) throws PluginDownloadException {
103        if (pi.mainversion > Version.getInstance().getVersion()) {
104            ExtendedDialog dialog = new ExtendedDialog(
105                    progressMonitor.getWindowParent(),
106                    tr("Skip download"),
107                    new String[] {
108                        tr("Download Plugin"),
109                        tr("Skip Download") }
110            );
111            dialog.setContent(tr("JOSM version {0} required for plugin {1}.", pi.mainversion, pi.name));
112            dialog.setButtonIcons(new String[] {"download", "cancel"});
113            dialog.showDialog();
114            int answer = dialog.getValue();
115            if (answer != 1)
116                throw new PluginDownloadException(tr("Download skipped"));
117        }
118        try {
119            if (pi.downloadlink == null) {
120                String msg = tr("Cannot download plugin ''{0}''. Its download link is not known. Skipping download.", pi.name);
121                Main.warn(msg);
122                throw new PluginDownloadException(msg);
123            }
124            URL url = new URL(pi.downloadlink);
125            synchronized (this) {
126                downloadConnection = CachedFile.connectFollowingRedirect(url, PLUGIN_MIME_TYPES, null);
127            }
128            try (
129                InputStream in = downloadConnection.getInputStream();
130                OutputStream out = new FileOutputStream(file)
131            ) {
132                byte[] buffer = new byte[8192];
133                for (int read = in.read(buffer); read != -1; read = in.read(buffer)) {
134                    out.write(buffer, 0, read);
135                }
136            }
137        } catch (MalformedURLException e) {
138            String msg = tr("Cannot download plugin ''{0}''. Its download link ''{1}'' is not a valid URL. Skipping download.",
139                    pi.name, pi.downloadlink);
140            Main.warn(msg);
141            throw new PluginDownloadException(msg, e);
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            String message = tr("Downloading Plugin {0}...", d.name);
165            Main.info(message);
166            progressMonitor.subTask(message);
167            progressMonitor.worked(1);
168            File pluginFile = new File(pluginDir, d.name + ".jar.new");
169            try {
170                download(d, pluginFile);
171            } catch (PluginDownloadException e) {
172                Main.error(e);
173                failed.add(d);
174                continue;
175            }
176            downloaded.add(d);
177        }
178        PluginHandler.installDownloadedPlugins(false);
179    }
180
181    /**
182     * Replies true if the task was canceled by the user
183     *
184     * @return <code>true</code> if the task was stopped by the user
185     */
186    public boolean isCanceled() {
187        return canceled;
188    }
189
190    /**
191     * Replies the list of plugins whose download has failed.
192     *
193     * @return the list of plugins whose download has failed
194     */
195    public Collection<PluginInformation> getFailedPlugins() {
196        return failed;
197    }
198
199    /**
200     * Replies the list of successfully downloaded plugins.
201     *
202     * @return the list of successfully downloaded plugins
203     */
204    public Collection<PluginInformation> getDownloadedPlugins() {
205        return downloaded;
206    }
207}