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