source: josm/trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/ImageryHandler.java@ 5837

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

fix #8586 - Remote Control: allow imagery handler to work without any mapFrame + add wiki link in HTTP 400 error message

  • Property svn:eol-style set to native
File size: 3.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.util.HashMap;
7
8import org.openstreetmap.josm.Main;
9import org.openstreetmap.josm.data.imagery.ImageryInfo;
10import org.openstreetmap.josm.gui.layer.ImageryLayer;
11import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
12
13/**
14 * Adds an imagery (WMS/TMS) layer. For instance, {@code /imagery?title=...&type=...&url=...}.
15 * @since 3715
16 */
17public class ImageryHandler extends RequestHandler {
18
19 /**
20 * The remote control command name used to add an imagery layer.
21 */
22 public static final String command = "imagery";
23
24 @Override
25 public String getPermissionMessage() {
26 return tr("Remote Control has been asked to load an imagery layer from the following URL:")
27 + "<br>" + args.get("url");
28 }
29
30 @Override
31 public String[] getMandatoryParams() {
32 return new String[]{"url"};
33 }
34
35 @Override
36 public PermissionPrefWithDefault getPermissionPref() {
37 return PermissionPrefWithDefault.LOAD_IMAGERY;
38 }
39
40 @Override
41 protected void handleRequest() throws RequestHandlerErrorException {
42 String url = args.get("url");
43 String title = args.get("title");
44 String type = args.get("type");
45 if ((title == null) || (title.isEmpty())) {
46 title = tr("Remote imagery");
47 }
48 String cookies = args.get("cookies");
49 ImageryInfo imgInfo = new ImageryInfo(title, url, type, null, cookies);
50 String min_zoom = args.get("min_zoom");
51 if (min_zoom != null && !min_zoom.isEmpty()) {
52 try {
53 imgInfo.setDefaultMinZoom(Integer.parseInt(min_zoom));
54 } catch (NumberFormatException e) {
55 System.err.println("NumberFormatException ("+e.getMessage()+")");
56 }
57 }
58 String max_zoom = args.get("max_zoom");
59 if (max_zoom != null && !max_zoom.isEmpty()) {
60 try {
61 imgInfo.setDefaultMaxZoom(Integer.parseInt(max_zoom));
62 } catch (NumberFormatException e) {
63 System.err.println("NumberFormatException ("+e.getMessage()+")");
64 }
65 }
66 Main.main.addLayer(ImageryLayer.create(imgInfo));
67 }
68
69 @Override
70 protected void parseArgs() {
71 HashMap<String, String> args = new HashMap<String, String>();
72 if (request.indexOf('?') != -1) {
73 String query = request.substring(request.indexOf('?') + 1);
74 if (query.indexOf("url=") == 0) {
75 args.put("url", decodeParam(query.substring(4)));
76 } else {
77 int urlIdx = query.indexOf("&url=");
78 if (urlIdx != -1) {
79 args.put("url", decodeParam(query.substring(urlIdx + 5)));
80 query = query.substring(0, urlIdx);
81 } else {
82 if (query.indexOf('#') != -1) {
83 query = query.substring(0, query.indexOf('#'));
84 }
85 }
86 String[] params = query.split("&", -1);
87 for (String param : params) {
88 int eq = param.indexOf('=');
89 if (eq != -1) {
90 args.put(param.substring(0, eq), decodeParam(param.substring(eq + 1)));
91 }
92 }
93 }
94 }
95 this.args = args;
96 }
97
98 @Override
99 protected void validateRequest() throws RequestHandlerBadRequestException {
100 // Nothing to do
101 }
102}
Note: See TracBrowser for help on using the repository browser.