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

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

remove extra whitespaces

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