001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.actions.downloadtasks;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.io.IOException;
007import java.net.URISyntaxException;
008import java.net.URL;
009import java.util.concurrent.Future;
010
011import org.openstreetmap.josm.actions.SessionLoadAction.Loader;
012import org.openstreetmap.josm.data.Bounds;
013import org.openstreetmap.josm.gui.MainApplication;
014import org.openstreetmap.josm.gui.progress.ProgressMonitor;
015import org.openstreetmap.josm.spi.preferences.Config;
016import org.openstreetmap.josm.tools.HttpClient;
017import org.openstreetmap.josm.tools.Logging;
018
019/**
020 * Task allowing to download JOSM session (*.jos, *.joz file).
021 * @since 6215
022 */
023public class DownloadSessionTask extends AbstractDownloadTask<Object> {
024
025    private static final String PATTERN_SESSION = "https?://.*/.*\\.jo(s|z)";
026
027    private Loader loader;
028
029    @Override
030    public String getTitle() {
031        return tr("Download session");
032    }
033
034    @Override
035    public String[] getPatterns() {
036        return new String[]{PATTERN_SESSION};
037    }
038
039    @Override
040    public Future<?> download(DownloadParams settings, Bounds downloadArea, ProgressMonitor progressMonitor) {
041        return null;
042    }
043
044    @Override
045    public Future<?> loadUrl(DownloadParams settings, String url, ProgressMonitor progressMonitor) {
046        if (url != null && (url.matches(PATTERN_SESSION))) {
047            try {
048                URL u = new URL(url);
049                loader = new Loader(HttpClient.create(u).connect().getContent(), u.toURI(), url.endsWith(".joz"));
050                return MainApplication.worker.submit(loader);
051            } catch (URISyntaxException | IOException e) {
052                Logging.error(e);
053            }
054        }
055        return null;
056    }
057
058    @Override
059    public void cancel() {
060        if (loader != null) {
061            loader.cancel();
062        }
063    }
064
065    @Override
066    public String getConfirmationMessage(URL url) {
067        // TODO
068        return null;
069    }
070
071    /**
072     * Do not allow to load a session file via remotecontrol.
073     *
074     * Session importers can be added by plugins and there is currently
075     * no way to ensure that these are safe for remotecontol.
076     * @return <code>true</code> if session import is allowed
077     */
078    @Override
079    public boolean isSafeForRemotecontrolRequests() {
080        return Config.getPref().getBoolean("remotecontrol.import.allow_session", false);
081    }
082}