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.IOException;
009import java.io.InputStream;
010import java.net.MalformedURLException;
011import java.net.URL;
012import java.nio.file.Files;
013import java.nio.file.StandardCopyOption;
014import java.util.Collection;
015import java.util.LinkedList;
016
017import org.openstreetmap.josm.Main;
018import org.openstreetmap.josm.data.Version;
019import org.openstreetmap.josm.gui.ExtendedDialog;
020import org.openstreetmap.josm.gui.PleaseWaitRunnable;
021import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
022import org.openstreetmap.josm.gui.progress.ProgressMonitor;
023import org.openstreetmap.josm.tools.CheckParameterUtil;
024import org.openstreetmap.josm.tools.HttpClient;
025import org.openstreetmap.josm.tools.Logging;
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 * @since 2817
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 HttpClient 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 if toUpdate is null
057     */
058    public PluginDownloadTask(Component parent, Collection<PluginInformation> toUpdate, String title) {
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 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 if toUpdate is null
083     */
084    public void setPluginsToDownload(Collection<PluginInformation> toUpdate) {
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        // 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 = Main.pref.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}