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

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

Rework console output:

  • new log level "error"
  • Replace nearly all calls to system.out and system.err to Main.(error|warn|info|debug)
  • Remove some unnecessary debug output
  • Some messages are modified (removal of "Info", "Warning", "Error" from the message itself -> notable i18n impact but limited to console error messages not seen by the majority of users, so that's ok)
File size: 4.7 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.Arrays;
7import java.util.HashMap;
8
9import org.openstreetmap.josm.Main;
10import org.openstreetmap.josm.data.imagery.ImageryInfo;
11import org.openstreetmap.josm.gui.layer.ImageryLayer;
12import org.openstreetmap.josm.gui.util.GuiHelper;
13import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
14import 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 */
20public 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 public void run() {
76 Main.main.addLayer(ImageryLayer.create(imgInfo));
77 }
78 });
79 }
80
81 @Override
82 protected void parseArgs() {
83 HashMap<String, String> args = new HashMap<String, String>();
84 if (request.indexOf('?') != -1) {
85 String query = request.substring(request.indexOf('?') + 1);
86 if (query.indexOf("url=") == 0) {
87 args.put("url", decodeParam(query.substring(4)));
88 } else {
89 int urlIdx = query.indexOf("&url=");
90 if (urlIdx != -1) {
91 args.put("url", decodeParam(query.substring(urlIdx + 5)));
92 query = query.substring(0, urlIdx);
93 } else {
94 if (query.indexOf('#') != -1) {
95 query = query.substring(0, query.indexOf('#'));
96 }
97 }
98 String[] params = query.split("&", -1);
99 for (String param : params) {
100 int eq = param.indexOf('=');
101 if (eq != -1) {
102 args.put(param.substring(0, eq), decodeParam(param.substring(eq + 1)));
103 }
104 }
105 }
106 }
107 this.args = args;
108 }
109
110 @Override
111 protected void validateRequest() throws RequestHandlerBadRequestException {
112 // Nothing to do
113 }
114
115 @Override
116 public String[] getUsageExamples() {
117 final String types = Utils.join("|", Utils.transform(Arrays.asList(ImageryInfo.ImageryType.values()), new Utils.Function<ImageryInfo.ImageryType, String>() {
118 @Override
119 public String apply(ImageryInfo.ImageryType x) {
120 return x.getUrlString();
121 }
122 }));
123 return new String[] { "/imagery?title=osm&type=tms&url=http://tile.openstreetmap.org/%7Bzoom%7D/%7Bx%7D/%7By%7D.png",
124 "/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",
125 "/imagery?title=...&type={"+types+"}&url=....[&cookies=...][&min_zoom=...][&max_zoom=...]"};
126 }
127}
Note: See TracBrowser for help on using the repository browser.