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

Last change on this file since 8249 was 8249, checked in by simon04, 9 years ago

see #11356 - Remote control: fix opening overpass query

The compatibility code calling DownloadOsmTask had not been executed.

  • Property svn:eol-style set to native
File size: 4.6 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.LinkedHashSet;
10import java.util.Set;
11
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.actions.OpenLocationAction;
14import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
15import org.openstreetmap.josm.actions.downloadtasks.DownloadTask;
16import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
17import org.openstreetmap.josm.tools.Utils;
18
19/**
20 * Handler for import request
21 */
22public class ImportHandler extends RequestHandler.RawURLParseRequestHandler {
23
24 /**
25 * The remote control command name used to import data.
26 */
27 public static final String command = "import";
28
29 private URL url;
30 private Collection<DownloadTask> suitableDownloadTasks;
31
32 @Override
33 protected void handleRequest() throws RequestHandlerErrorException {
34 try {
35 if (suitableDownloadTasks.isEmpty()) {
36 // It should maybe be better to reject the request in that case ?
37 // For compatibility reasons with older instances of JOSM, arbitrary choice of DownloadOsmTask
38 // As of 2015-04, Overpass Turbo requires this branch of code ...
39 Main.debug("Remote control, /import: defaulting to DownloadOsmTask");
40 new DownloadOsmTask().loadUrl(isLoadInNewLayer(), url.toExternalForm(), null);
41 } else if (Main.pref.getBoolean("remotecontrol.import.interactive", true)) {
42 // OpenLocationAction queries the user if more than one task is suitable
43 new OpenLocationAction().openUrl(isLoadInNewLayer(), url.toExternalForm());
44 } else {
45 // Otherwise perform all tasks
46 for (DownloadTask task : suitableDownloadTasks) {
47 task.loadUrl(isLoadInNewLayer(), url.toExternalForm(), null);
48 }
49 }
50 } catch (Exception ex) {
51 Main.warn("RemoteControl: Error parsing import remote control request:");
52 Main.error(ex);
53 throw new RequestHandlerErrorException(ex);
54 }
55 }
56
57 @Override
58 public String[] getMandatoryParams() {
59 return new String[]{"url"};
60 }
61
62 @Override
63 public String[] getOptionalParams() {
64 return new String[] {"new_layer"};
65 }
66
67 @Override
68 public String getUsage() {
69 return "downloads the specified OSM file and adds it to the current data set";
70 }
71
72 @Override
73 public String[] getUsageExamples() {
74 return new String[] { "/import?url="+Main.getJOSMWebsite()+"/browser/josm/trunk/data_nodist/direction-arrows.osm" };
75 }
76
77 @Override
78 public String getPermissionMessage() {
79 // URL can be any suitable URL giving back OSM data, including OSM API calls, even if calls to the main API
80 // should rather be passed to LoadAndZoomHandler or LoadObjectHandler.
81 // Other API instances will however use the import handler to force JOSM to make requests to this API instance.
82 // (Example with OSM-FR website that makes calls to the OSM-FR API)
83 // For user-friendliness, let's try to decode these OSM API calls to give a better confirmation message.
84 Set<String> taskMessages = new LinkedHashSet<>();
85 if (suitableDownloadTasks != null && !suitableDownloadTasks.isEmpty()) {
86 for (DownloadTask task : suitableDownloadTasks) {
87 taskMessages.add(Utils.firstNonNull(task.getConfirmationMessage(url), url.toString()));
88 }
89 }
90 return tr("Remote Control has been asked to import data from the following URL:")
91 + Utils.joinAsHtmlUnorderedList(taskMessages);
92 }
93
94 @Override
95 public PermissionPrefWithDefault getPermissionPref() {
96 return PermissionPrefWithDefault.IMPORT_DATA;
97 }
98
99 @Override
100 protected void validateRequest() throws RequestHandlerBadRequestException {
101 String urlString = args.get("url");
102 if (Main.pref.getBoolean("remotecontrol.importhandler.fix_url_query", true)) {
103 urlString = Utils.fixURLQuery(urlString);
104 }
105 try {
106 // Ensure the URL is valid
107 url = new URL(urlString);
108 } catch (MalformedURLException e) {
109 throw new RequestHandlerBadRequestException("MalformedURLException: "+e.getMessage(), e);
110 }
111 // Find download tasks for the given URL
112 suitableDownloadTasks = Main.main.menu.openLocation.findDownloadTasks(urlString, true);
113 }
114}
Note: See TracBrowser for help on using the repository browser.