1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.remotecontrol.handler;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.HashMap;
|
---|
7 | import java.util.StringTokenizer;
|
---|
8 |
|
---|
9 | import javax.swing.JOptionPane;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.Main;
|
---|
12 | import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * This is the parent of all classes that handle a specific remote control command
|
---|
16 | *
|
---|
17 | * @author Bodo Meissner
|
---|
18 | */
|
---|
19 | public abstract class RequestHandler {
|
---|
20 |
|
---|
21 | public static final String globalConfirmationKey = "remotecontrol.always-confirm";
|
---|
22 | public static final boolean globalConfirmationDefault = false;
|
---|
23 |
|
---|
24 | /** The GET request arguments */
|
---|
25 | protected HashMap<String,String> args;
|
---|
26 |
|
---|
27 | /** The request URL without "GET". */
|
---|
28 | protected String request;
|
---|
29 |
|
---|
30 | /** default response */
|
---|
31 | protected String content = "OK\r\n";
|
---|
32 | /** default content type */
|
---|
33 | protected String contentType = "text/plain";
|
---|
34 |
|
---|
35 | /** will be filled with the command assigned to the subclass */
|
---|
36 | protected String myCommand;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Check permission and parameters and handle request.
|
---|
40 | *
|
---|
41 | * @throws RequestHandlerForbiddenException
|
---|
42 | * @throws RequestHandlerBadRequestException
|
---|
43 | * @throws RequestHandlerErrorException
|
---|
44 | */
|
---|
45 | public final void handle() throws RequestHandlerForbiddenException, RequestHandlerBadRequestException, RequestHandlerErrorException
|
---|
46 | {
|
---|
47 | checkPermission();
|
---|
48 | checkMandatoryParams();
|
---|
49 | handleRequest();
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Handle a specific command sent as remote control.
|
---|
54 | *
|
---|
55 | * This method of the subclass will do the real work.
|
---|
56 | *
|
---|
57 | * @throws RequestHandlerErrorException
|
---|
58 | * @throws RequestHandlerBadRequestException
|
---|
59 | */
|
---|
60 | protected abstract void handleRequest() throws RequestHandlerErrorException, RequestHandlerBadRequestException;
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Get a specific message to ask the user for permission for the operation
|
---|
64 | * requested via remote control.
|
---|
65 | *
|
---|
66 | * This message will be displayed to the user if the preference
|
---|
67 | * remotecontrol.always-confirm is true.
|
---|
68 | *
|
---|
69 | * @return the message
|
---|
70 | */
|
---|
71 | abstract public String getPermissionMessage();
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Get a PermissionPref object containing the name of a special permission
|
---|
75 | * preference to individually allow the requested operation and an error
|
---|
76 | * message to be displayed when a disabled operation is requested.
|
---|
77 | *
|
---|
78 | * Default is not to check any special preference. Override this in a
|
---|
79 | * subclass to define permission preference and error message.
|
---|
80 | *
|
---|
81 | * @return the preference name and error message or null
|
---|
82 | */
|
---|
83 | public PermissionPrefWithDefault getPermissionPref()
|
---|
84 | {
|
---|
85 | /* Example:
|
---|
86 | return new PermissionPrefWithDefault("fooobar.remotecontrol",
|
---|
87 | true
|
---|
88 | "RemoteControl: foobar forbidden by preferences");
|
---|
89 | */
|
---|
90 | return null;
|
---|
91 | }
|
---|
92 |
|
---|
93 | protected String[] getMandatoryParams()
|
---|
94 | {
|
---|
95 | return null;
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Check permissions in preferences and display error message
|
---|
100 | * or ask for permission.
|
---|
101 | *
|
---|
102 | * @throws RequestHandlerForbiddenException
|
---|
103 | */
|
---|
104 | final public void checkPermission() throws RequestHandlerForbiddenException
|
---|
105 | {
|
---|
106 | /*
|
---|
107 | * If the subclass defines a specific preference and if this is set
|
---|
108 | * to false, abort with an error message.
|
---|
109 | *
|
---|
110 | * Note: we use the deprecated class here for compatibility with
|
---|
111 | * older versions of WMSPlugin.
|
---|
112 | */
|
---|
113 | PermissionPrefWithDefault permissionPref = getPermissionPref();
|
---|
114 | if((permissionPref != null) && (permissionPref.pref != null))
|
---|
115 | {
|
---|
116 | if (!Main.pref.getBoolean(permissionPref.pref, permissionPref.defaultVal)) {
|
---|
117 | System.out.println(permissionPref.message);
|
---|
118 | throw new RequestHandlerForbiddenException();
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | /* Does the user want to confirm everything?
|
---|
123 | * If yes, display specific confirmation message.
|
---|
124 | */
|
---|
125 | if (Main.pref.getBoolean(globalConfirmationKey, globalConfirmationDefault)) {
|
---|
126 | if (JOptionPane.showConfirmDialog(Main.parent,
|
---|
127 | "<html>" + getPermissionMessage() +
|
---|
128 | "<br>" + tr("Do you want to allow this?"),
|
---|
129 | tr("Confirm Remote Control action"),
|
---|
130 | JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) {
|
---|
131 | throw new RequestHandlerForbiddenException();
|
---|
132 | }
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Set request URL and parse args.
|
---|
138 | *
|
---|
139 | * @param url The request URL.
|
---|
140 | */
|
---|
141 | public void setUrl(String url) {
|
---|
142 | this.request = url;
|
---|
143 | parseArgs();
|
---|
144 | }
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Parse the request parameters as key=value pairs.
|
---|
148 | * The result will be stored in this.args.
|
---|
149 | *
|
---|
150 | * Can be overridden by subclass.
|
---|
151 | */
|
---|
152 | protected void parseArgs() {
|
---|
153 | StringTokenizer st = new StringTokenizer(this.request, "&?");
|
---|
154 | HashMap<String, String> args = new HashMap<String, String>();
|
---|
155 | // ignore first token which is the command
|
---|
156 | if(st.hasMoreTokens()) st.nextToken();
|
---|
157 | while (st.hasMoreTokens()) {
|
---|
158 | String param = st.nextToken();
|
---|
159 | int eq = param.indexOf("=");
|
---|
160 | if (eq > -1)
|
---|
161 | args.put(param.substring(0, eq),
|
---|
162 | param.substring(eq + 1));
|
---|
163 | }
|
---|
164 | this.args = args;
|
---|
165 | }
|
---|
166 |
|
---|
167 | void checkMandatoryParams() throws RequestHandlerBadRequestException {
|
---|
168 | String[] mandatory = getMandatoryParams();
|
---|
169 | if(mandatory == null) return;
|
---|
170 |
|
---|
171 | boolean error = false;
|
---|
172 | for (int i = 0; i < mandatory.length; ++i) {
|
---|
173 | String key = mandatory[i];
|
---|
174 | String value = args.get(key);
|
---|
175 | if ((value == null) || (value.length() == 0)) {
|
---|
176 | error = true;
|
---|
177 | System.out.println("'" + myCommand + "' remote control request must have '" + key + "' parameter");
|
---|
178 | }
|
---|
179 | }
|
---|
180 | if (error)
|
---|
181 | throw new RequestHandlerBadRequestException();
|
---|
182 | }
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * Save command associated with this handler.
|
---|
186 | *
|
---|
187 | * @param command The command.
|
---|
188 | */
|
---|
189 | public void setCommand(String command)
|
---|
190 | {
|
---|
191 | if (command.charAt(0) == '/') {
|
---|
192 | command = command.substring(1);
|
---|
193 | }
|
---|
194 | myCommand = command;
|
---|
195 | }
|
---|
196 |
|
---|
197 | public String getContent() {
|
---|
198 | return content;
|
---|
199 | }
|
---|
200 |
|
---|
201 | public String getContentType() {
|
---|
202 | return contentType;
|
---|
203 | }
|
---|
204 |
|
---|
205 | public static class RequestHandlerException extends Exception {
|
---|
206 | }
|
---|
207 |
|
---|
208 | public static class RequestHandlerErrorException extends RequestHandlerException {
|
---|
209 | }
|
---|
210 |
|
---|
211 | public static class RequestHandlerBadRequestException extends RequestHandlerException {
|
---|
212 | }
|
---|
213 |
|
---|
214 | public static class RequestHandlerForbiddenException extends RequestHandlerException {
|
---|
215 | private static final long serialVersionUID = 2263904699747115423L;
|
---|
216 | }
|
---|
217 | }
|
---|