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.length() == 0)) {
|
---|
52 | title = tr("Remote imagery");
|
---|
53 | }
|
---|
54 | String cookies = args.get("cookies");
|
---|
55 | ImageryLayer imgLayer = ImageryLayer.create(new ImageryInfo(title, url, type, null, cookies));
|
---|
56 | Main.main.addLayer(imgLayer);
|
---|
57 | }
|
---|
58 |
|
---|
59 | @Override
|
---|
60 | protected void parseArgs() {
|
---|
61 | HashMap<String, String> args = new HashMap<String, String>();
|
---|
62 | if (request.indexOf('?') != -1) {
|
---|
63 | String query = request.substring(request.indexOf('?') + 1);
|
---|
64 | if (query.indexOf("url=") == 0) {
|
---|
65 | args.put("url", decodeParam(query.substring(4)));
|
---|
66 | } else {
|
---|
67 | int urlIdx = query.indexOf("&url=");
|
---|
68 | if (urlIdx != -1) {
|
---|
69 | args.put("url", decodeParam(query.substring(urlIdx + 5)));
|
---|
70 | query = query.substring(0, urlIdx);
|
---|
71 | } else {
|
---|
72 | if (query.indexOf('#') != -1) {
|
---|
73 | query = query.substring(0, query.indexOf('#'));
|
---|
74 | }
|
---|
75 | }
|
---|
76 | String[] params = query.split("&", -1);
|
---|
77 | for (String param : params) {
|
---|
78 | int eq = param.indexOf('=');
|
---|
79 | if (eq != -1) {
|
---|
80 | args.put(param.substring(0, eq), decodeParam(param.substring(eq + 1)));
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|
85 | this.args = args;
|
---|
86 | }
|
---|
87 |
|
---|
88 | private String decodeParam(String param) {
|
---|
89 | try {
|
---|
90 | return URLDecoder.decode(param, "UTF-8");
|
---|
91 | } catch (UnsupportedEncodingException e) {
|
---|
92 | throw new RuntimeException();
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|