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