source: josm/trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/ImportHandler.java@ 6248

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

Rework console output:

  • new log level "error"
  • Replace nearly all calls to system.out and system.err to Main.(error|warn|info|debug)
  • Remove some unnecessary debug output
  • Some messages are modified (removal of "Info", "Warning", "Error" from the message itself -> notable i18n impact but limited to console error messages not seen by the majority of users, so that's ok)
File size: 5.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io.remotecontrol.handler;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.net.MalformedURLException;
7import java.net.URL;
8import java.util.Collection;
9import java.util.HashMap;
10
11import org.openstreetmap.josm.Main;
12import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
13import org.openstreetmap.josm.actions.downloadtasks.DownloadTask;
14import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
15
16/**
17 * Handler for import request
18 */
19public 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: handle multiple suitable download tasks ?
34 suitableDownloadTasks.iterator().next().loadUrl(isLoadInNewLayer(), url.toExternalForm(), null);
35 }
36 } catch (Exception ex) {
37 Main.warn("RemoteControl: Error parsing import remote control request:");
38 ex.printStackTrace();
39 throw new RequestHandlerErrorException();
40 }
41 }
42
43 @Override
44 public String[] getMandatoryParams() {
45 return new String[]{"url"};
46 }
47
48 @Override
49 public String[] getOptionalParams() {
50 return new String[] {"new_layer"};
51 }
52
53 @Override
54 public String[] getUsageExamples() {
55 return new String[] { "/import?url="+Main.JOSM_WEBSITE+"/browser/josm/trunk/data_nodist/direction-arrows.osm" };
56 }
57
58 @Override
59 public String getPermissionMessage() {
60 // URL can be any suitable URL giving back OSM data, including OSM API calls, even if calls to the main API
61 // should rather be passed to LoadAndZoomHandler or LoadObjectHandler.
62 // Other API instances will however use the import handler to force JOSM to make requests to this API instance.
63 // (Example with OSM-FR website that makes calls to the OSM-FR API)
64 // For user-friendliness, let's try to decode these OSM API calls to give a better confirmation message.
65 String taskMessage = null;
66 if (suitableDownloadTasks != null && !suitableDownloadTasks.isEmpty()) {
67 // TODO: handle multiple suitable download tasks ?
68 taskMessage = suitableDownloadTasks.iterator().next().getConfirmationMessage(url);
69 }
70 return tr("Remote Control has been asked to import data from the following URL:")
71 + "<br>" + (taskMessage == null ? url.toString() : taskMessage);
72 }
73
74 @Override
75 public PermissionPrefWithDefault getPermissionPref() {
76 return PermissionPrefWithDefault.IMPORT_DATA;
77 }
78
79 @Override
80 protected void parseArgs() {
81 HashMap<String, String> args = new HashMap<String, String>();
82 if (request.indexOf('?') != -1) {
83 String query = request.substring(request.indexOf('?') + 1);
84 if (query.indexOf("url=") == 0) {
85 args.put("url", decodeParam(query.substring(4)));
86 } else {
87 int urlIdx = query.indexOf("&url=");
88 if (urlIdx != -1) {
89 args.put("url", decodeParam(query.substring(urlIdx + 5)));
90 query = query.substring(0, urlIdx);
91 } else {
92 if (query.indexOf('#') != -1) {
93 query = query.substring(0, query.indexOf('#'));
94 }
95 }
96 String[] params = query.split("&", -1);
97 for (String param : params) {
98 int eq = param.indexOf('=');
99 if (eq != -1) {
100 args.put(param.substring(0, eq), param.substring(eq + 1));
101 }
102 }
103 }
104 }
105 this.args = args;
106 }
107
108 @Override
109 protected void validateRequest() throws RequestHandlerBadRequestException {
110 final String urlString = args.get("url");
111 try {
112 // Ensure the URL is valid
113 url = new URL(urlString);
114 } catch (MalformedURLException e) {
115 throw new RequestHandlerBadRequestException("MalformedURLException: "+e.getMessage());
116 }
117 // Find download tasks for the given URL
118 suitableDownloadTasks = Main.main.menu.openLocation.findDownloadTasks(urlString);
119 if (suitableDownloadTasks.isEmpty()) {
120 // It should maybe be better to reject the request in that case ?
121 // For compatibility reasons with older instances of JOSM, arbitrary choice of DownloadOsmTask
122 suitableDownloadTasks.add(new DownloadOsmTask());
123 }
124 }
125}
Note: See TracBrowser for help on using the repository browser.