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