1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.remotecontrol.handler;
|
---|
3 |
|
---|
4 | import org.openstreetmap.josm.Main;
|
---|
5 | import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
|
---|
6 | import org.openstreetmap.josm.tools.Utils;
|
---|
7 |
|
---|
8 | import javax.swing.JLabel;
|
---|
9 | import javax.swing.JOptionPane;
|
---|
10 | import java.io.UnsupportedEncodingException;
|
---|
11 | import java.net.URI;
|
---|
12 | import java.net.URISyntaxException;
|
---|
13 | import java.net.URLDecoder;
|
---|
14 | import java.text.MessageFormat;
|
---|
15 | import java.util.Collections;
|
---|
16 | import java.util.HashMap;
|
---|
17 | import java.util.HashSet;
|
---|
18 | import java.util.LinkedList;
|
---|
19 | import java.util.List;
|
---|
20 | import java.util.Map;
|
---|
21 |
|
---|
22 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * This is the parent of all classes that handle a specific remote control command
|
---|
26 | *
|
---|
27 | * @author Bodo Meissner
|
---|
28 | */
|
---|
29 | public abstract class RequestHandler {
|
---|
30 |
|
---|
31 | public static final String globalConfirmationKey = "remotecontrol.always-confirm";
|
---|
32 | public static final boolean globalConfirmationDefault = false;
|
---|
33 | public static final String loadInNewLayerKey = "remotecontrol.new-layer";
|
---|
34 | public static final boolean loadInNewLayerDefault = false;
|
---|
35 |
|
---|
36 | /** The GET request arguments */
|
---|
37 | protected Map<String,String> args;
|
---|
38 |
|
---|
39 | /** The request URL without "GET". */
|
---|
40 | protected String request;
|
---|
41 |
|
---|
42 | /** default response */
|
---|
43 | protected String content = "OK\r\n";
|
---|
44 | /** default content type */
|
---|
45 | protected String contentType = "text/plain";
|
---|
46 |
|
---|
47 | /** will be filled with the command assigned to the subclass */
|
---|
48 | protected String myCommand;
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * who sent the request?
|
---|
52 | * the host from referer header or IP of request sender
|
---|
53 | */
|
---|
54 | protected String sender;
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Check permission and parameters and handle request.
|
---|
58 | *
|
---|
59 | * @throws RequestHandlerForbiddenException
|
---|
60 | * @throws RequestHandlerBadRequestException
|
---|
61 | * @throws RequestHandlerErrorException
|
---|
62 | */
|
---|
63 | public final void handle() throws RequestHandlerForbiddenException, RequestHandlerBadRequestException, RequestHandlerErrorException {
|
---|
64 | checkMandatoryParams();
|
---|
65 | validateRequest();
|
---|
66 | checkPermission();
|
---|
67 | handleRequest();
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Validates the request before attempting to perform it.
|
---|
72 | * @throws RequestHandlerBadRequestException
|
---|
73 | * @since 5678
|
---|
74 | */
|
---|
75 | protected abstract void validateRequest() throws RequestHandlerBadRequestException;
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Handle a specific command sent as remote control.
|
---|
79 | *
|
---|
80 | * This method of the subclass will do the real work.
|
---|
81 | *
|
---|
82 | * @throws RequestHandlerErrorException
|
---|
83 | * @throws RequestHandlerBadRequestException
|
---|
84 | */
|
---|
85 | protected abstract void handleRequest() throws RequestHandlerErrorException, RequestHandlerBadRequestException;
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Get a specific message to ask the user for permission for the operation
|
---|
89 | * requested via remote control.
|
---|
90 | *
|
---|
91 | * This message will be displayed to the user if the preference
|
---|
92 | * remotecontrol.always-confirm is true.
|
---|
93 | *
|
---|
94 | * @return the message
|
---|
95 | */
|
---|
96 | public abstract String getPermissionMessage();
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Get a PermissionPref object containing the name of a special permission
|
---|
100 | * preference to individually allow the requested operation and an error
|
---|
101 | * message to be displayed when a disabled operation is requested.
|
---|
102 | *
|
---|
103 | * Default is not to check any special preference. Override this in a
|
---|
104 | * subclass to define permission preference and error message.
|
---|
105 | *
|
---|
106 | * @return the preference name and error message or null
|
---|
107 | */
|
---|
108 | public abstract PermissionPrefWithDefault getPermissionPref();
|
---|
109 |
|
---|
110 | public abstract String[] getMandatoryParams();
|
---|
111 |
|
---|
112 | public String[] getOptionalParams() {
|
---|
113 | return null;
|
---|
114 | }
|
---|
115 |
|
---|
116 | public String getUsage() {
|
---|
117 | return null;
|
---|
118 | }
|
---|
119 |
|
---|
120 | public String[] getUsageExamples() {
|
---|
121 | return null;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Returns usage examples for the given command. To be overriden only my handlers that define several commands.
|
---|
126 | * @param cmd The command asked
|
---|
127 | * @return Usage examples for the given command
|
---|
128 | * @since 6332
|
---|
129 | */
|
---|
130 | public String[] getUsageExamples(String cmd) {
|
---|
131 | return getUsageExamples();
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Check permissions in preferences and display error message
|
---|
136 | * or ask for permission.
|
---|
137 | *
|
---|
138 | * @throws RequestHandlerForbiddenException
|
---|
139 | */
|
---|
140 | public final void checkPermission() throws RequestHandlerForbiddenException {
|
---|
141 | /*
|
---|
142 | * If the subclass defines a specific preference and if this is set
|
---|
143 | * to false, abort with an error message.
|
---|
144 | *
|
---|
145 | * Note: we use the deprecated class here for compatibility with
|
---|
146 | * older versions of WMSPlugin.
|
---|
147 | */
|
---|
148 | PermissionPrefWithDefault permissionPref = getPermissionPref();
|
---|
149 | if (permissionPref != null && permissionPref.pref != null) {
|
---|
150 | if (!Main.pref.getBoolean(permissionPref.pref, permissionPref.defaultVal)) {
|
---|
151 | String err = MessageFormat.format("RemoteControl: ''{0}'' forbidden by preferences", myCommand);
|
---|
152 | Main.info(err);
|
---|
153 | throw new RequestHandlerForbiddenException(err);
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | /* Does the user want to confirm everything?
|
---|
158 | * If yes, display specific confirmation message.
|
---|
159 | */
|
---|
160 | if (Main.pref.getBoolean(globalConfirmationKey, globalConfirmationDefault)) {
|
---|
161 | // Ensure dialog box does not exceed main window size
|
---|
162 | Integer maxWidth = (int) Math.max(200, Main.parent.getWidth()*0.6);
|
---|
163 | String message = "<html><div>" + getPermissionMessage() +
|
---|
164 | "<br/>" + tr("Do you want to allow this?") + "</div></html>";
|
---|
165 | JLabel label = new JLabel(message);
|
---|
166 | if (label.getPreferredSize().width > maxWidth) {
|
---|
167 | label.setText(message.replaceFirst("<div>", "<div style=\"width:" + maxWidth + "px;\">"));
|
---|
168 | }
|
---|
169 | if (JOptionPane.showConfirmDialog(Main.parent, label,
|
---|
170 | tr("Confirm Remote Control action"),
|
---|
171 | JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) {
|
---|
172 | String err = MessageFormat.format("RemoteControl: ''{0}'' forbidden by user''s choice", myCommand);
|
---|
173 | throw new RequestHandlerForbiddenException(err);
|
---|
174 | }
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Set request URL and parse args.
|
---|
180 | *
|
---|
181 | * @param url The request URL.
|
---|
182 | */
|
---|
183 | public void setUrl(String url) {
|
---|
184 | this.request = url;
|
---|
185 | parseArgs();
|
---|
186 | }
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Parse the request parameters as key=value pairs.
|
---|
190 | * The result will be stored in {@code this.args}.
|
---|
191 | *
|
---|
192 | * Can be overridden by subclass.
|
---|
193 | */
|
---|
194 | protected void parseArgs() {
|
---|
195 | try {
|
---|
196 | this.args = getRequestParameter(new URI(this.request));
|
---|
197 | } catch (URISyntaxException ex) {
|
---|
198 | throw new RuntimeException(ex);
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * @see <a href="http://blog.lunatech.com/2009/02/03/what-every-web-developer-must-know-about-url-encoding">
|
---|
204 | * What every web developer must know about URL encoding</a>
|
---|
205 | */
|
---|
206 | static Map<String, String> getRequestParameter(URI uri) {
|
---|
207 | Map<String, String> r = new HashMap<>();
|
---|
208 | if (uri.getRawQuery() == null) {
|
---|
209 | return r;
|
---|
210 | }
|
---|
211 | for (String kv : uri.getRawQuery().split("&")) {
|
---|
212 | try {
|
---|
213 | final String[] kvs = URLDecoder.decode(kv, "UTF-8").split("=", 2);
|
---|
214 | r.put(kvs[0], kvs.length > 1 ? kvs[1] : null);
|
---|
215 | } catch (UnsupportedEncodingException ex) {
|
---|
216 | throw new IllegalStateException(ex);
|
---|
217 | }
|
---|
218 | }
|
---|
219 | return r;
|
---|
220 | }
|
---|
221 |
|
---|
222 | void checkMandatoryParams() throws RequestHandlerBadRequestException {
|
---|
223 | String[] mandatory = getMandatoryParams();
|
---|
224 | String[] optional = getOptionalParams();
|
---|
225 | List<String> missingKeys = new LinkedList<>();
|
---|
226 | boolean error = false;
|
---|
227 | if(mandatory != null) for (String key : mandatory) {
|
---|
228 | String value = args.get(key);
|
---|
229 | if ((value == null) || (value.length() == 0)) {
|
---|
230 | error = true;
|
---|
231 | Main.warn("'" + myCommand + "' remote control request must have '" + key + "' parameter");
|
---|
232 | missingKeys.add(key);
|
---|
233 | }
|
---|
234 | }
|
---|
235 | HashSet<String> knownParams = new HashSet<>();
|
---|
236 | if (mandatory != null) Collections.addAll(knownParams, mandatory);
|
---|
237 | if (optional != null) Collections.addAll(knownParams, optional);
|
---|
238 | for (String par: args.keySet()) {
|
---|
239 | if (!knownParams.contains(par)) {
|
---|
240 | Main.warn("Unknown remote control parameter {0}, skipping it", par);
|
---|
241 | }
|
---|
242 | }
|
---|
243 | if (error) {
|
---|
244 | throw new RequestHandlerBadRequestException(
|
---|
245 | "The following keys are mandatory, but have not been provided: "
|
---|
246 | + Utils.join(", ", missingKeys));
|
---|
247 | }
|
---|
248 | }
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * Save command associated with this handler.
|
---|
252 | *
|
---|
253 | * @param command The command.
|
---|
254 | */
|
---|
255 | public void setCommand(String command)
|
---|
256 | {
|
---|
257 | if (command.charAt(0) == '/') {
|
---|
258 | command = command.substring(1);
|
---|
259 | }
|
---|
260 | myCommand = command;
|
---|
261 | }
|
---|
262 |
|
---|
263 | public String getContent() {
|
---|
264 | return content;
|
---|
265 | }
|
---|
266 |
|
---|
267 | public String getContentType() {
|
---|
268 | return contentType;
|
---|
269 | }
|
---|
270 |
|
---|
271 | protected boolean isLoadInNewLayer() {
|
---|
272 | return args.get("new_layer") != null && !args.get("new_layer").isEmpty()
|
---|
273 | ? Boolean.parseBoolean(args.get("new_layer"))
|
---|
274 | : Main.pref.getBoolean(loadInNewLayerKey, loadInNewLayerDefault);
|
---|
275 | }
|
---|
276 |
|
---|
277 | protected static String decodeParam(String param) {
|
---|
278 | try {
|
---|
279 | return URLDecoder.decode(param, "UTF-8");
|
---|
280 | } catch (UnsupportedEncodingException e) {
|
---|
281 | throw new RuntimeException(e);
|
---|
282 | }
|
---|
283 | }
|
---|
284 |
|
---|
285 | public void setSender(String sender) {
|
---|
286 | this.sender = sender;
|
---|
287 | }
|
---|
288 |
|
---|
289 | public static class RequestHandlerException extends Exception {
|
---|
290 |
|
---|
291 | public RequestHandlerException(String message) {
|
---|
292 | super(message);
|
---|
293 | }
|
---|
294 | public RequestHandlerException(String message, Throwable cause) {
|
---|
295 | super(message, cause);
|
---|
296 | }
|
---|
297 | public RequestHandlerException(Throwable cause) {
|
---|
298 | super(cause);
|
---|
299 | }
|
---|
300 | public RequestHandlerException() {
|
---|
301 | }
|
---|
302 | }
|
---|
303 |
|
---|
304 | public static class RequestHandlerErrorException extends RequestHandlerException {
|
---|
305 | public RequestHandlerErrorException(Throwable cause) {
|
---|
306 | super(cause);
|
---|
307 | }
|
---|
308 | }
|
---|
309 |
|
---|
310 | public static class RequestHandlerBadRequestException extends RequestHandlerException {
|
---|
311 |
|
---|
312 | public RequestHandlerBadRequestException(String message) {
|
---|
313 | super(message);
|
---|
314 | }
|
---|
315 | public RequestHandlerBadRequestException(String message, Throwable cause) {
|
---|
316 | super(message, cause);
|
---|
317 | }
|
---|
318 | }
|
---|
319 |
|
---|
320 | public static class RequestHandlerForbiddenException extends RequestHandlerException {
|
---|
321 | private static final long serialVersionUID = 2263904699747115423L;
|
---|
322 |
|
---|
323 | public RequestHandlerForbiddenException(String message) {
|
---|
324 | super(message);
|
---|
325 | }
|
---|
326 | }
|
---|
327 |
|
---|
328 | public static abstract class RawURLParseRequestHandler extends RequestHandler {
|
---|
329 | @Override
|
---|
330 | protected void parseArgs() {
|
---|
331 | HashMap<String, String> args = new HashMap<>();
|
---|
332 | if (request.indexOf('?') != -1) {
|
---|
333 | String query = request.substring(request.indexOf('?') + 1);
|
---|
334 | if (query.indexOf("url=") == 0) {
|
---|
335 | args.put("url", decodeParam(query.substring(4)));
|
---|
336 | } else {
|
---|
337 | int urlIdx = query.indexOf("&url=");
|
---|
338 | if (urlIdx != -1) {
|
---|
339 | args.put("url", decodeParam(query.substring(urlIdx + 5)));
|
---|
340 | query = query.substring(0, urlIdx);
|
---|
341 | } else if (query.indexOf('#') != -1) {
|
---|
342 | query = query.substring(0, query.indexOf('#'));
|
---|
343 | }
|
---|
344 | String[] params = query.split("&", -1);
|
---|
345 | for (String param : params) {
|
---|
346 | int eq = param.indexOf('=');
|
---|
347 | if (eq != -1) {
|
---|
348 | args.put(param.substring(0, eq), decodeParam(param.substring(eq + 1)));
|
---|
349 | }
|
---|
350 | }
|
---|
351 | }
|
---|
352 | }
|
---|
353 | this.args = args;
|
---|
354 | }
|
---|
355 | }
|
---|
356 | }
|
---|