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.net.MalformedURLException;
|
---|
7 | import java.net.URL;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.HashMap;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.Main;
|
---|
12 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
|
---|
13 | import org.openstreetmap.josm.actions.downloadtasks.DownloadTask;
|
---|
14 | import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Handler for import request
|
---|
18 | */
|
---|
19 | public class ImportHandler extends RequestHandler {
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * The remote control command name used to import data.
|
---|
23 | */
|
---|
24 | public static final String command = "import";
|
---|
25 |
|
---|
26 | private URL url;
|
---|
27 | private Collection<DownloadTask> suitableDownloadTasks;
|
---|
28 |
|
---|
29 | @Override
|
---|
30 | protected void handleRequest() throws RequestHandlerErrorException {
|
---|
31 | try {
|
---|
32 | if (suitableDownloadTasks != null && !suitableDownloadTasks.isEmpty()) {
|
---|
33 | // TODO: add new_layer parameter
|
---|
34 | // TODO: handle multiple suitable download tasks ?
|
---|
35 | suitableDownloadTasks.iterator().next().loadUrl(false, url.toExternalForm(), null);
|
---|
36 | }
|
---|
37 | } catch (Exception ex) {
|
---|
38 | System.out.println("RemoteControl: Error parsing import remote control request:");
|
---|
39 | ex.printStackTrace();
|
---|
40 | throw new RequestHandlerErrorException();
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | @Override
|
---|
45 | public String[] getMandatoryParams() {
|
---|
46 | return new String[]{"url"};
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override
|
---|
50 | public String getPermissionMessage() {
|
---|
51 | // URL can be any suitable URL giving back OSM data, including OSM API calls, even if calls to the main API
|
---|
52 | // should rather be passed to LoadAndZoomHandler or LoadObjectHandler.
|
---|
53 | // Other API instances will however use the import handler to force JOSM to make requests to this API instance.
|
---|
54 | // (Example with OSM-FR website that makes calls to the OSM-FR API)
|
---|
55 | // For user-friendliness, let's try to decode these OSM API calls to give a better confirmation message.
|
---|
56 | String taskMessage = null;
|
---|
57 | if (suitableDownloadTasks != null && !suitableDownloadTasks.isEmpty()) {
|
---|
58 | // TODO: handle multiple suitable download tasks ?
|
---|
59 | taskMessage = suitableDownloadTasks.iterator().next().getConfirmationMessage(url);
|
---|
60 | }
|
---|
61 | return tr("Remote Control has been asked to import data from the following URL:")
|
---|
62 | + "<br>" + (taskMessage == null ? url.toString() : taskMessage);
|
---|
63 | }
|
---|
64 |
|
---|
65 | @Override
|
---|
66 | public PermissionPrefWithDefault getPermissionPref() {
|
---|
67 | return PermissionPrefWithDefault.IMPORT_DATA;
|
---|
68 | }
|
---|
69 |
|
---|
70 | @Override
|
---|
71 | protected void parseArgs() {
|
---|
72 | HashMap<String, String> args = new HashMap<String, String>();
|
---|
73 | if (request.indexOf('?') != -1) {
|
---|
74 | String query = request.substring(request.indexOf('?') + 1);
|
---|
75 | if (query.indexOf("url=") == 0) {
|
---|
76 | args.put("url", decodeParam(query.substring(4)));
|
---|
77 | } else {
|
---|
78 | int urlIdx = query.indexOf("&url=");
|
---|
79 | if (urlIdx != -1) {
|
---|
80 | /*String url =*/ query.substring(urlIdx + 1);
|
---|
81 | args.put("url", decodeParam(query.substring(urlIdx + 5)));
|
---|
82 | query = query.substring(0, urlIdx);
|
---|
83 | } else {
|
---|
84 | if (query.indexOf('#') != -1) {
|
---|
85 | query = query.substring(0, query.indexOf('#'));
|
---|
86 | }
|
---|
87 | }
|
---|
88 | String[] params = query.split("&", -1);
|
---|
89 | for (String param : params) {
|
---|
90 | int eq = param.indexOf('=');
|
---|
91 | if (eq != -1) {
|
---|
92 | args.put(param.substring(0, eq), param.substring(eq + 1));
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|
97 | this.args = args;
|
---|
98 | }
|
---|
99 |
|
---|
100 | @Override
|
---|
101 | protected void validateRequest() throws RequestHandlerBadRequestException {
|
---|
102 | final String urlString = args.get("url");
|
---|
103 | try {
|
---|
104 | // Ensure the URL is valid
|
---|
105 | url = new URL(urlString);
|
---|
106 | } catch (MalformedURLException e) {
|
---|
107 | throw new RequestHandlerBadRequestException("MalformedURLException: "+e.getMessage());
|
---|
108 | }
|
---|
109 | // Find download tasks for the given URL
|
---|
110 | suitableDownloadTasks = Main.main.menu.openLocation.findDownloadTasks(urlString);
|
---|
111 | if (suitableDownloadTasks.isEmpty()) {
|
---|
112 | // It should maybe be better to reject the request in that case ?
|
---|
113 | // For compatibility reasons with older instances of JOSM, arbitrary choice of DownloadOsmTask
|
---|
114 | suitableDownloadTasks.add(new DownloadOsmTask());
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|