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.util.Arrays;
|
---|
7 | import java.util.HashMap;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.Main;
|
---|
10 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
11 | import org.openstreetmap.josm.gui.layer.ImageryLayer;
|
---|
12 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
13 | import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
|
---|
14 | import org.openstreetmap.josm.tools.Utils;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Adds an imagery (WMS/TMS) layer. For instance, {@code /imagery?title=...&type=...&url=...}.
|
---|
18 | * @since 3715
|
---|
19 | */
|
---|
20 | public class ImageryHandler extends RequestHandler {
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * The remote control command name used to add an imagery layer.
|
---|
24 | */
|
---|
25 | public static final String command = "imagery";
|
---|
26 |
|
---|
27 | @Override
|
---|
28 | public String getPermissionMessage() {
|
---|
29 | return tr("Remote Control has been asked to load an imagery layer from the following URL:")
|
---|
30 | + "<br>" + args.get("url");
|
---|
31 | }
|
---|
32 |
|
---|
33 | @Override
|
---|
34 | public String[] getMandatoryParams() {
|
---|
35 | return new String[]{"url"};
|
---|
36 | }
|
---|
37 |
|
---|
38 | @Override
|
---|
39 | public String[] getOptionalParams() {
|
---|
40 | return new String[] { "title", "type", "cookies", "min_zoom", "max_zoom"};
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | public PermissionPrefWithDefault getPermissionPref() {
|
---|
45 | return PermissionPrefWithDefault.LOAD_IMAGERY;
|
---|
46 | }
|
---|
47 |
|
---|
48 | @Override
|
---|
49 | protected void handleRequest() throws RequestHandlerErrorException {
|
---|
50 | String url = args.get("url");
|
---|
51 | String title = args.get("title");
|
---|
52 | String type = args.get("type");
|
---|
53 | if ((title == null) || (title.isEmpty())) {
|
---|
54 | title = tr("Remote imagery");
|
---|
55 | }
|
---|
56 | String cookies = args.get("cookies");
|
---|
57 | final ImageryInfo imgInfo = new ImageryInfo(title, url, type, null, cookies);
|
---|
58 | String min_zoom = args.get("min_zoom");
|
---|
59 | if (min_zoom != null && !min_zoom.isEmpty()) {
|
---|
60 | try {
|
---|
61 | imgInfo.setDefaultMinZoom(Integer.parseInt(min_zoom));
|
---|
62 | } catch (NumberFormatException e) {
|
---|
63 | Main.error(e);
|
---|
64 | }
|
---|
65 | }
|
---|
66 | String max_zoom = args.get("max_zoom");
|
---|
67 | if (max_zoom != null && !max_zoom.isEmpty()) {
|
---|
68 | try {
|
---|
69 | imgInfo.setDefaultMaxZoom(Integer.parseInt(max_zoom));
|
---|
70 | } catch (NumberFormatException e) {
|
---|
71 | Main.error(e);
|
---|
72 | }
|
---|
73 | }
|
---|
74 | GuiHelper.runInEDT(new Runnable() {
|
---|
75 | @Override
|
---|
76 | public void run() {
|
---|
77 | try {
|
---|
78 | Main.main.addLayer(ImageryLayer.create(imgInfo));
|
---|
79 | } catch (IllegalArgumentException e) {
|
---|
80 | Main.error(e, false);
|
---|
81 | }
|
---|
82 | }
|
---|
83 | });
|
---|
84 | }
|
---|
85 |
|
---|
86 | @Override
|
---|
87 | protected void parseArgs() {
|
---|
88 | HashMap<String, String> args = new HashMap<>();
|
---|
89 | if (request.indexOf('?') != -1) {
|
---|
90 | String query = request.substring(request.indexOf('?') + 1);
|
---|
91 | if (query.indexOf("url=") == 0) {
|
---|
92 | args.put("url", decodeParam(query.substring(4)));
|
---|
93 | } else {
|
---|
94 | int urlIdx = query.indexOf("&url=");
|
---|
95 | if (urlIdx != -1) {
|
---|
96 | args.put("url", decodeParam(query.substring(urlIdx + 5)));
|
---|
97 | query = query.substring(0, urlIdx);
|
---|
98 | } else {
|
---|
99 | if (query.indexOf('#') != -1) {
|
---|
100 | query = query.substring(0, query.indexOf('#'));
|
---|
101 | }
|
---|
102 | }
|
---|
103 | String[] params = query.split("&", -1);
|
---|
104 | for (String param : params) {
|
---|
105 | int eq = param.indexOf('=');
|
---|
106 | if (eq != -1) {
|
---|
107 | args.put(param.substring(0, eq), decodeParam(param.substring(eq + 1)));
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|
112 | this.args = args;
|
---|
113 | }
|
---|
114 |
|
---|
115 | @Override
|
---|
116 | protected void validateRequest() throws RequestHandlerBadRequestException {
|
---|
117 | String url = args.get("url");
|
---|
118 | String type = args.get("type");
|
---|
119 | try {
|
---|
120 | ImageryLayer.create(new ImageryInfo(null, url, type, null, null));
|
---|
121 | } catch (IllegalArgumentException e) {
|
---|
122 | throw new RequestHandlerBadRequestException(e.getMessage(), e);
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | @Override
|
---|
127 | public String getUsage() {
|
---|
128 | return "adds an imagery layer (e.g. WMS, TMS))";
|
---|
129 | }
|
---|
130 |
|
---|
131 | @Override
|
---|
132 | public String[] getUsageExamples() {
|
---|
133 | final String types = Utils.join("|", Utils.transform(Arrays.asList(ImageryInfo.ImageryType.values()), new Utils.Function<ImageryInfo.ImageryType, String>() {
|
---|
134 | @Override
|
---|
135 | public String apply(ImageryInfo.ImageryType x) {
|
---|
136 | return x.getTypeString();
|
---|
137 | }
|
---|
138 | }));
|
---|
139 | return new String[] { "/imagery?title=osm&type=tms&url=https://a.tile.openstreetmap.org/%7Bzoom%7D/%7Bx%7D/%7By%7D.png",
|
---|
140 | "/imagery?title=landsat&type=wms&url=http://irs.gis-lab.info/?layers=landsat&SRS=%7Bproj%7D&WIDTH=%7Bwidth%7D&HEIGHT=%7Bheight%7D&BBOX=%7Bbbox%7D",
|
---|
141 | "/imagery?title=...&type={"+types+"}&url=....[&cookies=...][&min_zoom=...][&max_zoom=...]"};
|
---|
142 | }
|
---|
143 | }
|
---|