| 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.Main;
|
|---|
| 13 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
|
|---|
| 14 | import org.openstreetmap.josm.actions.downloadtasks.DownloadTask;
|
|---|
| 15 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 16 | import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
|
|---|
| 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(isLoadInNewLayer(), url.toExternalForm(), null);
|
|---|
| 42 | } else if (Main.pref.getBoolean("remotecontrol.import.interactive", true)) {
|
|---|
| 43 | // OpenLocationAction queries the user if more than one task is suitable
|
|---|
| 44 | MainApplication.getMenu().openLocation.openUrl(isLoadInNewLayer(), url.toExternalForm());
|
|---|
| 45 | } else {
|
|---|
| 46 | // Otherwise perform all tasks
|
|---|
| 47 | for (DownloadTask task : suitableDownloadTasks) {
|
|---|
| 48 | task.loadUrl(isLoadInNewLayer(), 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"};
|
|---|
| 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="+Main.getJOSMWebsite()+"/browser/josm/trunk/data_nodist/direction-arrows.osm"};
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | @Override
|
|---|
| 79 | public String getPermissionMessage() {
|
|---|
| 80 | // URL can be any suitable URL giving back OSM data, including OSM API calls, even if calls to the main API
|
|---|
| 81 | // should rather be passed to LoadAndZoomHandler or LoadObjectHandler.
|
|---|
| 82 | // Other API instances will however use the import handler to force JOSM to make requests to this API instance.
|
|---|
| 83 | // (Example with OSM-FR website that makes calls to the OSM-FR API)
|
|---|
| 84 | // For user-friendliness, let's try to decode these OSM API calls to give a better confirmation message.
|
|---|
| 85 | Set<String> taskMessages = new LinkedHashSet<>();
|
|---|
| 86 | if (suitableDownloadTasks != null && !suitableDownloadTasks.isEmpty()) {
|
|---|
| 87 | for (DownloadTask task : suitableDownloadTasks) {
|
|---|
| 88 | taskMessages.add(Utils.firstNonNull(task.getConfirmationMessage(url), url.toString()));
|
|---|
| 89 | }
|
|---|
| 90 | }
|
|---|
| 91 | return tr("Remote Control has been asked to import data from the following URL:")
|
|---|
| 92 | + Utils.joinAsHtmlUnorderedList(taskMessages);
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | @Override
|
|---|
| 96 | public PermissionPrefWithDefault getPermissionPref() {
|
|---|
| 97 | return PermissionPrefWithDefault.IMPORT_DATA;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | @Override
|
|---|
| 101 | protected void validateRequest() throws RequestHandlerBadRequestException {
|
|---|
| 102 | String urlString = args != null ? args.get("url") : null;
|
|---|
| 103 | if (Main.pref.getBoolean("remotecontrol.importhandler.fix_url_query", true)) {
|
|---|
| 104 | urlString = Utils.fixURLQuery(urlString);
|
|---|
| 105 | }
|
|---|
| 106 | try {
|
|---|
| 107 | // Ensure the URL is valid
|
|---|
| 108 | url = new URL(urlString);
|
|---|
| 109 | } catch (MalformedURLException e) {
|
|---|
| 110 | throw new RequestHandlerBadRequestException("MalformedURLException: "+e.getMessage(), e);
|
|---|
| 111 | }
|
|---|
| 112 | // Find download tasks for the given URL
|
|---|
| 113 | suitableDownloadTasks = MainApplication.getMenu().openLocation.findDownloadTasks(urlString, true);
|
|---|
| 114 | }
|
|---|
| 115 | }
|
|---|