source: josm/trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadTask.java@ 17330

Last change on this file since 17330 was 17330, checked in by Don-vip, 3 years ago

fix #20131 - remote control: report errors in case of OSM API error (load_and_zoom) or no valid identifier (load_object)

  • Property svn:eol-style set to native
File size: 5.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions.downloadtasks;
3
4import java.net.URL;
5import java.util.List;
6import java.util.Objects;
7import java.util.concurrent.Future;
8import java.util.stream.Collectors;
9
10import org.openstreetmap.josm.data.Bounds;
11import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
12import org.openstreetmap.josm.gui.progress.ProgressMonitor;
13import org.openstreetmap.josm.tools.ExceptionUtil;
14
15/**
16 * Interface defining a general download task used to download geographic data (OSM data, GPX tracks, etc.) for a given URL or geographic area.
17 */
18public interface DownloadTask {
19
20 /**
21 * Asynchronously launches the download task for a given bounding box.
22 *
23 * Set <code>progressMonitor</code> to null, if the task should create, open, and close a progress monitor.
24 * Set progressMonitor to {@link NullProgressMonitor#INSTANCE} if progress information is to
25 * be discarded.
26 *
27 * You can wait for the asynchronous download task to finish by synchronizing on the returned
28 * {@link Future}, but make sure not to freeze up JOSM. Example:
29 * <pre>
30 * Future&lt;?&gt; future = task.download(...);
31 * // DON'T run this on the Swing EDT or JOSM will freeze
32 * future.get(); // waits for the dowload task to complete
33 * </pre>
34 *
35 * The following example uses a pattern which is better suited if a task is launched from
36 * the Swing EDT:
37 * <pre>
38 * final Future&lt;?&gt; future = task.download(...);
39 * Runnable runAfterTask = new Runnable() {
40 * public void run() {
41 * // this is not strictly necessary because of the type of executor service
42 * // Main.worker is initialized with, but it doesn't harm either
43 * //
44 * future.get(); // wait for the download task to complete
45 * doSomethingAfterTheTaskCompleted();
46 * }
47 * }
48 * MainApplication.worker.submit(runAfterTask);
49 * </pre>
50 *
51 * @param settings download settings
52 *
53 * @param downloadArea the area to download
54 * @param progressMonitor the progressMonitor
55 * @return the future representing the asynchronous task
56 * @since 13927
57 */
58 Future<?> download(DownloadParams settings, Bounds downloadArea, ProgressMonitor progressMonitor);
59
60 /**
61 * Asynchronously launches the download task for a given bounding URL.
62 *
63 * Set progressMonitor to null, if the task should create, open, and close a progress monitor.
64 * Set progressMonitor to {@link NullProgressMonitor#INSTANCE} if progress information is to
65 * be discarded.
66
67 * @param settings download settings
68 * @param url the url to download from
69 * @param progressMonitor the progressMonitor
70 * @return the future representing the asynchronous task
71 *
72 * @see #download(DownloadParams, Bounds, ProgressMonitor)
73 * @since 13927
74 */
75 Future<?> loadUrl(DownloadParams settings, String url, ProgressMonitor progressMonitor);
76
77 /**
78 * Returns true if the task is able to open the given URL, false otherwise.
79 * @param url the url to download from
80 * @param isRemotecontrol True if download request comes from remotecontrol.
81 * @return True if the task is able to open the given URL, false otherwise.
82 * Return false, if the request comes from remotecontrol, but the task is not
83 * safe for remotecontrol.
84 * A task is not safe for remotecontrol if it is possible to prepare a file
85 * for download which does something unintended, e.g. gain access to the
86 * local file system.
87 */
88 boolean acceptsUrl(String url, boolean isRemotecontrol);
89
90 /**
91 * Returns a short HTML documentation string, describing acceptable URLs.
92 * @return The HTML documentation
93 * @since 6031
94 */
95 String acceptsDocumentationSummary();
96
97 /**
98 * Returns human-readable description of the task
99 * @return The task description
100 * @since 6031
101 */
102 String getTitle();
103
104 /**
105 * Returns regular expressions that match the URLs
106 * @return The array of accepted URL patterns
107 * @since 6031
108 */
109 String[] getPatterns();
110
111 /**
112 * Replies the error objects of the task. Empty list, if no error messages are available.
113 *
114 * Error objects are either {@link String}s with error messages or {@link Exception}s.
115 *
116 * @return the list of error objects
117 */
118 List<Object> getErrorObjects();
119
120 /**
121 * Replies the error messages of the task. Empty list, if no error messages are available.
122 *
123 * @return the list of error messages
124 * @since 17330
125 */
126 default List<String> getErrorMessages() {
127 return getErrorObjects().stream().map(o -> {
128 if (o instanceof String) {
129 return (String) o;
130 } else if (o instanceof Exception) {
131 return ExceptionUtil.explainException((Exception) o).replace("<html>", "").replace("</html>", "");
132 } else {
133 return (String) null;
134 }
135 }).filter(Objects::nonNull).collect(Collectors.toList());
136 }
137
138 /**
139 * Cancels the asynchronous download task.
140 *
141 */
142 void cancel();
143
144 /**
145 * Replies the HTML-formatted confirmation message to be shown to user when the given URL needs to be confirmed before loading.
146 * @param url The URL to be confirmed
147 * @return The HTML-formatted confirmation message to be shown to user
148 * @since 5691
149 */
150 String getConfirmationMessage(URL url);
151
152 /**
153 * Sets whether the map view will zoom to download area after download
154 * @param zoomAfterDownload if true, the map view will zoom to download area after download
155 * @since 13261
156 */
157 void setZoomAfterDownload(boolean zoomAfterDownload);
158}
Note: See TracBrowser for help on using the repository browser.