Changeset 6091 in josm for trunk/src/org/openstreetmap/josm/io
- Timestamp:
- 2013-07-29T18:56:51+02:00 (11 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/io/remotecontrol
- Files:
-
- 10 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/remotecontrol/RequestProcessor.java
- Property svn:eol-style deleted
r6085 r6091 8 8 import java.io.OutputStream; 9 9 import java.io.OutputStreamWriter; 10 import java.io.PrintWriter; 11 import java.io.StringWriter; 10 12 import java.io.Writer; 11 13 import java.net.Socket; … … 17 19 import java.util.StringTokenizer; 18 20 import java.util.TreeMap; 21 import java.util.logging.Level; 22 import java.util.logging.Logger; 19 23 import java.util.regex.Matcher; 20 24 import java.util.regex.Pattern; … … 22 26 import org.openstreetmap.josm.io.remotecontrol.handler.AddNodeHandler; 23 27 import org.openstreetmap.josm.io.remotecontrol.handler.AddWayHandler; 28 import org.openstreetmap.josm.io.remotecontrol.handler.FeaturesHandler; 24 29 import org.openstreetmap.josm.io.remotecontrol.handler.ImageryHandler; 25 30 import org.openstreetmap.josm.io.remotecontrol.handler.ImportHandler; … … 127 132 addRequestHandlerClass(LoadObjectHandler.command, LoadObjectHandler.class, true); 128 133 addRequestHandlerClass(OpenFileHandler.command, OpenFileHandler.class, true); 134 addRequestHandlerClass(FeaturesHandler.command, FeaturesHandler.class, true); 129 135 } 130 136 … … 204 210 Class<? extends RequestHandler> handlerClass = handlers.get(command); 205 211 if (handlerClass == null) { 206 // no handler found 207 StringBuilder usage = new StringBuilder(1024); 208 for (Entry<String, Class<? extends RequestHandler>> handler : handlers.entrySet()) { 209 String[] mandatory = handler.getValue().newInstance().getMandatoryParams(); 210 usage.append("<li>"); 211 usage.append(handler.getKey()); 212 if (mandatory != null) { 213 usage.append("<br/>mandatory parameter: ").append(Utils.join(", ", Arrays.asList(mandatory))); 214 } 215 usage.append("</li>"); 216 } 212 String usage = getUsageAsHtml(); 217 213 String websiteDoc = "http://josm.openstreetmap.de/wiki/Help/Preferences/RemoteControl"; 218 214 String help = "No command specified! The following commands are available:<ul>" … … 365 361 out.write("\r\n"); 366 362 } 363 364 public static String getHandlersInfoAsJSON() { 365 StringBuilder r = new StringBuilder(); 366 boolean first = true; 367 r.append("["); 368 369 for (Entry<String, Class<? extends RequestHandler>> p : handlers.entrySet()) { 370 if (first) { 371 first = false; 372 } else { 373 r.append(", "); 374 } 375 r.append(getHanldlerInfoAsJSON(p.getKey())); 376 } 377 r.append("]"); 378 379 return r.toString(); 380 } 381 382 public static String getHanldlerInfoAsJSON(String cmd) { 383 StringWriter w = new StringWriter(); 384 PrintWriter r = new PrintWriter(w); 385 RequestHandler handler = null; 386 try { 387 Class c = handlers.get(cmd); 388 if (c==null) return null; 389 handler = handlers.get(cmd).newInstance(); 390 } catch (Exception ex) { 391 ex.printStackTrace(); 392 return null; 393 } 394 if (handler==null) return null; 395 396 r.printf("{ \"request\" : \"%s\"", cmd); 397 r.append(", \"parameters\" : ["); 398 399 String params[] = handler.getMandatoryParams(); 400 if (params != null) { 401 for (int i = 0; i < params.length; i++) { 402 if (i == 0) { 403 r.append('\"'); 404 } else { 405 r.append(", \""); 406 } 407 r.append(params[i]).append('\"'); 408 } 409 } 410 r.append("], \"optional\" : ["); 411 String optional[] = handler.getOptionalParams(); 412 if (optional != null) { 413 for (int i = 0; i < optional.length; i++) { 414 if (i == 0) { 415 r.append('\"'); 416 } else { 417 r.append(", \""); 418 } 419 r.append(optional[i]).append('\"'); 420 } 421 } 422 423 r.append("], \"examples\" : ["); 424 String examples[] = handler.getUsageExamples(); 425 if (examples != null) { 426 for (int i = 0; i < examples.length; i++) { 427 if (i == 0) { 428 r.append('\"'); 429 } else { 430 r.append(", \""); 431 } 432 r.append(examples[i]).append('\"'); 433 } 434 } 435 r.append("]}"); 436 try { 437 return w.toString(); 438 } finally { 439 try { 440 w.close(); 441 } catch (IOException ex) { 442 } 443 } 444 } 445 446 447 /** 448 * Reports HTML message with the description of all available commands 449 * @return 450 * @throws IllegalAccessException 451 * @throws InstantiationException 452 */ 453 public static String getUsageAsHtml() throws IllegalAccessException, InstantiationException { 454 // no handler found 455 StringBuilder usage = new StringBuilder(1024); 456 for (Entry<String, Class<? extends RequestHandler>> handler : handlers.entrySet()) { 457 RequestHandler sample = handler.getValue().newInstance(); 458 String[] mandatory = sample.getMandatoryParams(); 459 String[] optional = sample.getOptionalParams(); 460 String[] examples = sample.getUsageExamples(); 461 usage.append("<li>"); 462 usage.append(handler.getKey()); 463 if (mandatory != null) { 464 usage.append("<br/>mandatory parameters: ").append(Utils.join(", ", Arrays.asList(mandatory))); 465 } 466 if (optional != null) { 467 usage.append("<br/>optional parameters: ").append(Utils.join(", ", Arrays.asList(optional))); 468 } 469 if (examples != null) { 470 usage.append("<br/>examples: "); 471 for (String ex: examples) { 472 usage.append("<br/> <a href=\"http://localhost:8111"+ex+"\">"+ex+"</a>"); 473 } 474 } 475 usage.append("</li>"); 476 } 477 return usage.toString(); 478 } 367 479 } -
trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/AddNodeHandler.java
- Property svn:eol-style deleted
r6070 r6091 44 44 return new String[] { "lat", "lon" }; 45 45 } 46 47 @Override 48 public String[] getOptionalParams() 49 { 50 return new String[] { "addtags" }; 51 } 46 52 53 @Override 54 public String[] getUsageExamples() { 55 return new String[] { 56 "/add_node?lat=11&lon=22", 57 "/add_node?lon=13.3&lat=53.2&addtags=natural=tree|name=%20%20%20==Great%20Oak==" 58 }; 59 } 60 47 61 @Override 48 62 public String getPermissionMessage() { -
trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/AddWayHandler.java
r6070 r6091 46 46 return new String[]{"way"}; 47 47 } 48 49 @Override 50 public String[] getOptionalParams() { 51 return new String[] { "addtags" }; 52 } 48 53 54 @Override 55 public String[] getUsageExamples() { 56 return new String[] { 57 "/add_way?way=53.2,13.3;53.3,13.3;53.3,13.2", 58 "/add_way?&addtags=building=yes&way=45.437213,-2.810792;45.437988,-2.455983;45.224080,-2.455036;45.223302,-2.809845;45.437213,-2.810792" 59 }; 60 } 61 49 62 @Override 50 63 protected void handleRequest() throws RequestHandlerErrorException, RequestHandlerBadRequestException { -
trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/FeaturesHandler.java
- Property svn:eol-style deleted
r6090 r6091 2 2 package org.openstreetmap.josm.io.remotecontrol.handler; 3 3 4 import org.openstreetmap.josm.Main; 4 5 import static org.openstreetmap.josm.tools.I18n.tr; 5 6 … … 8 9 9 10 /** 10 * Handler for version request. 11 * Reports avalable commands, their parameters and examples 12 * @since 6091 11 13 */ 12 public class VersionHandler extends RequestHandler {14 public class FeaturesHandler extends RequestHandler { 13 15 14 16 /** 15 17 * The remote control command name used to reply version. 16 18 */ 17 public static final String command = " version";19 public static final String command = "features"; 18 20 19 21 @Override 20 22 protected void handleRequest() throws RequestHandlerErrorException, 21 23 RequestHandlerBadRequestException { 22 content = RequestProcessor.PROTOCOLVERSION; 24 StringBuilder buf = new StringBuilder(); 25 String q=args.get("q"); 26 if (q!=null) { 27 buf.append("["); 28 boolean first = true; 29 for (String s: q.split("[,\\s]+")) { 30 if (first) { 31 first = false; 32 } else { 33 buf.append(", "); 34 } 35 String info = RequestProcessor.getHanldlerInfoAsJSON("/"+s); 36 if (info!=null) { 37 buf.append(info); 38 } else { 39 Main.warn("Unknown handler {0} passed to /features request", s); 40 } 41 } 42 buf.append("]"); 43 } else { 44 buf.append(RequestProcessor.getHandlersInfoAsJSON()); 45 } 46 47 content = buf.toString(); 23 48 contentType = "application/json"; 24 49 if (args.containsKey("jsonp")) { … … 29 54 @Override 30 55 public String getPermissionMessage() { 31 return tr("Remote Control has been asked to report its protocol version. This enables web sites to detect a running JOSM.");56 return tr("Remote Control has been asked to report its supported features. This enables web sites to guess a running JOSM version"); 32 57 } 33 58 … … 41 66 return null; 42 67 } 68 69 @Override 70 public String[] getOptionalParams() { 71 return new String[]{"jsonp", "q"}; 72 } 43 73 44 74 @Override … … 46 76 // Nothing to do 47 77 } 78 79 @Override 80 public String[] getUsageExamples() { 81 return new String[] {"/features", "/features?q=import,add_node"}; 82 } 48 83 } -
trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/ImageryHandler.java
- Property svn:eol-style deleted
r5844 r6091 2 2 package org.openstreetmap.josm.io.remotecontrol.handler; 3 3 4 import java.util.Arrays; 4 5 import static org.openstreetmap.josm.tools.I18n.tr; 5 6 … … 11 12 import org.openstreetmap.josm.gui.util.GuiHelper; 12 13 import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault; 14 import org.openstreetmap.josm.tools.Utils; 13 15 14 16 /** … … 32 34 public String[] getMandatoryParams() { 33 35 return new String[]{"url"}; 36 } 37 38 @Override 39 public String[] getOptionalParams() { 40 return new String[] { "title", "type", "cookies", "min_zoom", "max_zoom"}; 34 41 } 35 42 … … 100 107 this.args = args; 101 108 } 102 109 103 110 @Override 104 111 protected void validateRequest() throws RequestHandlerBadRequestException { 105 112 // Nothing to do 106 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 } 107 127 } -
trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/ImportHandler.java
- Property svn:eol-style deleted
r6070 r6091 45 45 return new String[]{"url"}; 46 46 } 47 48 @Override 49 public String[] getOptionalParams() { 50 return new String[] {"new_layer"}; 51 } 47 52 53 @Override 54 public String[] getUsageExamples() { 55 return new String[] { "/import?urlhttp://josm.openstreetmap.de/browser/josm/trunk/data_nodist/direction-arrows.osm" }; 56 } 57 48 58 @Override 49 59 public String getPermissionMessage() { -
trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/LoadAndZoomHandler.java
- Property svn:eol-style deleted
r6084 r6091 73 73 74 74 @Override 75 public String[] getOptionalParams() 76 { 77 return new String[] {"new_layer", "addtags", "select", "zoom_mode"}; 78 } 79 80 @Override 81 public String[] getUsageExamples() { 82 if (command.equals(myCommand)) { 83 return new String[] { 84 "/load_and_zoom?addtags=wikipedia:de=Wei%C3%9Fe_Gasse|maxspeed=5&select=way23071688,way23076176,way23076177,&left=13.740&right=13.741&top=51.05&bottom=51.049", 85 "/load_and_zoom?left=8.19&right=8.20&top=48.605&bottom=48.590&select=node413602999&new_layer=true"}; 86 } else { 87 return new String[] { 88 "/zoom?left=8.19&right=8.20&top=48.605&bottom=48.590&select=node413602999"}; 89 } 90 } 91 92 @Override 75 93 protected void handleRequest() throws RequestHandlerErrorException 76 94 { -
trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/LoadObjectHandler.java
r6070 r6091 32 32 public String[] getMandatoryParams() { 33 33 return new String[]{"objects"}; 34 } 35 36 @Override 37 public String[] getOptionalParams() 38 { 39 return new String[] {"new_layer", "addtags"}; 40 } 41 42 @Override 43 public String[] getUsageExamples() { 44 return new String[] {"/load_object?new_layer=true&objects=w106159509", 45 "/load_object?new_layer=true&objects=r2263653&relation_members=true" 46 }; 34 47 } 35 48 -
trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/OpenFileHandler.java
r5927 r6091 25 25 return new String[]{"filename"}; 26 26 } 27 28 @Override 29 public String[] getOptionalParams() { 30 return null; 31 } 32 33 @Override 34 public String[] getUsageExamples() { 35 return new String[] {"/open_file?filename=/tmp/test.osm"}; 36 } 27 37 28 38 @Override -
trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/RequestHandler.java
- Property svn:eol-style deleted
r6070 r6091 7 7 import java.net.URLDecoder; 8 8 import java.text.MessageFormat; 9 import java.util.Collections; 9 10 import java.util.HashMap; 11 import java.util.HashSet; 10 12 import java.util.LinkedList; 11 13 import java.util.List; … … 106 108 107 109 abstract public String[] getMandatoryParams(); 110 111 public String[] getOptionalParams() { 112 return null; 113 } 114 115 public String[] getUsageExamples() { 116 return null; 117 } 108 118 109 119 /** … … 176 186 String query = req.substring(req.indexOf('?') + 1); 177 187 if (query.indexOf('#') != -1) { 178 query = query.substring(0, query.indexOf('#'));179 }188 query = query.substring(0, query.indexOf('#')); 189 } 180 190 String[] params = query.split("&", -1); 181 191 for (String param : params) { … … 194 204 void checkMandatoryParams() throws RequestHandlerBadRequestException { 195 205 String[] mandatory = getMandatoryParams(); 196 if(mandatory == null) return; 197 206 String[] optional = getOptionalParams(); 198 207 List<String> missingKeys = new LinkedList<String>(); 199 208 boolean error = false; 200 for (String key : mandatory) {209 if(mandatory != null) for (String key : mandatory) { 201 210 String value = args.get(key); 202 211 if ((value == null) || (value.length() == 0)) { … … 206 215 } 207 216 } 217 HashSet<String> knownParams = new HashSet<String>(); 218 if (mandatory != null) Collections.addAll(knownParams, mandatory); 219 if (optional != null) Collections.addAll(knownParams, optional); 220 for (String par: args.keySet()) { 221 if (!knownParams.contains(par)) { 222 Main.warn("Unknown remote control parameter {0}, skipping it", par); 223 } 224 } 208 225 if (error) { 209 226 throw new RequestHandlerBadRequestException( … … 211 228 + Utils.join(", ", missingKeys)); 212 229 } 230 213 231 } 214 232 -
trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/VersionHandler.java
- Property svn:eol-style deleted
r5680 r6091 43 43 44 44 @Override 45 public String[] getOptionalParams() { 46 return new String[]{"jsonp"}; 47 } 48 49 @Override 45 50 protected void validateRequest() throws RequestHandlerBadRequestException { 46 51 // Nothing to do 47 52 } 53 54 @Override 55 public String[] getUsageExamples() { 56 return new String[] { "/version", "/version?jsonp=test"}; 57 } 48 58 }
Note:
See TracChangeset
for help on using the changeset viewer.