1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions.downloadtasks;
|
---|
3 |
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.List;
|
---|
6 |
|
---|
7 | import org.openstreetmap.josm.io.XmlWriter;
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * Common abstract implementation of other download tasks
|
---|
11 | * @param <T> The downloaded data type
|
---|
12 | * @since 2322
|
---|
13 | */
|
---|
14 | public abstract class AbstractDownloadTask<T> implements DownloadTask {
|
---|
15 | private final List<Object> errorMessages;
|
---|
16 | private boolean canceled;
|
---|
17 | private boolean failed;
|
---|
18 | protected T downloadedData;
|
---|
19 |
|
---|
20 | public AbstractDownloadTask() {
|
---|
21 | errorMessages = new ArrayList<>();
|
---|
22 | }
|
---|
23 |
|
---|
24 | public boolean isCanceled() {
|
---|
25 | return canceled;
|
---|
26 | }
|
---|
27 |
|
---|
28 | public void setCanceled(boolean canceled) {
|
---|
29 | this.canceled = canceled;
|
---|
30 | }
|
---|
31 |
|
---|
32 | public boolean isFailed() {
|
---|
33 | return failed;
|
---|
34 | }
|
---|
35 |
|
---|
36 | public void setFailed(boolean failed) {
|
---|
37 | this.failed = failed;
|
---|
38 | }
|
---|
39 |
|
---|
40 | protected final void rememberErrorMessage(String message) {
|
---|
41 | errorMessages.add(message);
|
---|
42 | }
|
---|
43 |
|
---|
44 | protected final void rememberException(Exception exception) {
|
---|
45 | errorMessages.add(exception);
|
---|
46 | }
|
---|
47 |
|
---|
48 | protected final void rememberDownloadedData(T data) {
|
---|
49 | this.downloadedData = data;
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Replies the downloaded data.
|
---|
54 | * @return The downloaded data.
|
---|
55 | */
|
---|
56 | public final T getDownloadedData() {
|
---|
57 | return downloadedData;
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Override
|
---|
61 | public List<Object> getErrorObjects() {
|
---|
62 | return errorMessages;
|
---|
63 | }
|
---|
64 |
|
---|
65 | @Override
|
---|
66 | public String acceptsDocumentationSummary() {
|
---|
67 | StringBuilder buff = new StringBuilder("<tr><td>");
|
---|
68 | buff.append(getTitle())
|
---|
69 | .append(":</td><td>");
|
---|
70 | String[] patterns = getPatterns();
|
---|
71 | if (patterns.length > 0) {
|
---|
72 | buff.append("<ul>");
|
---|
73 | for (String pattern: patterns) {
|
---|
74 | buff.append("<li>")
|
---|
75 | .append(XmlWriter.encode(pattern))
|
---|
76 | .append("</li>");
|
---|
77 | }
|
---|
78 | buff.append("</ul>");
|
---|
79 | }
|
---|
80 | buff.append("</td></tr>");
|
---|
81 | return buff.toString();
|
---|
82 | }
|
---|
83 |
|
---|
84 | // Can be overridden for more complex checking logic
|
---|
85 | public boolean acceptsUrl(String url) {
|
---|
86 | if (url == null) return false;
|
---|
87 | for (String p: getPatterns()) {
|
---|
88 | if (url.matches(p)) {
|
---|
89 | return true;
|
---|
90 | }
|
---|
91 | }
|
---|
92 | return false;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Check / decide if the task is safe for remotecontrol.
|
---|
97 | *
|
---|
98 | * Keep in mind that a potential attacker has full control over the content
|
---|
99 | * of the file that will be downloaded.
|
---|
100 | * If it is possible to run arbitrary code or write to the local file
|
---|
101 | * system, then the task is (obviously) not save for remote execution.
|
---|
102 | *
|
---|
103 | * The default value is false = unsafe. Override in a subclass to
|
---|
104 | * allow running the task via remotecontol.
|
---|
105 | *
|
---|
106 | * @return true if it is safe to download and open any file of the
|
---|
107 | * corresponding format, false otherwise
|
---|
108 | */
|
---|
109 | public boolean isSafeForRemotecontrolRequests() {
|
---|
110 | return false;
|
---|
111 | }
|
---|
112 |
|
---|
113 | @Override
|
---|
114 | public boolean acceptsUrl(String url, boolean isRemotecontrol) {
|
---|
115 | if (isRemotecontrol && !isSafeForRemotecontrolRequests()) return false;
|
---|
116 | return acceptsUrl(url);
|
---|
117 | }
|
---|
118 |
|
---|
119 | // Default name to keep old plugins compatible
|
---|
120 | @Override
|
---|
121 | public String getTitle() {
|
---|
122 | return getClass().getName();
|
---|
123 | }
|
---|
124 |
|
---|
125 | @Override
|
---|
126 | public String toString() {
|
---|
127 | return this.getTitle();
|
---|
128 | }
|
---|
129 |
|
---|
130 | // Default pattern to keep old plugins compatible
|
---|
131 | @Override
|
---|
132 | public String[] getPatterns() {
|
---|
133 | return new String[]{};
|
---|
134 | }
|
---|
135 |
|
---|
136 | }
|
---|