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