- Timestamp:
- 2020-06-07T13:09:55+02:00 (4 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/io/remotecontrol
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/remotecontrol/RequestProcessor.java
r16190 r16550 7 7 import java.io.InputStreamReader; 8 8 import java.io.OutputStreamWriter; 9 import java.io.PrintWriter;10 import java.io.StringWriter;11 9 import java.io.Writer; 12 10 import java.net.Socket; 13 11 import java.nio.charset.Charset; 14 12 import java.nio.charset.StandardCharsets; 13 import java.util.Arrays; 14 import java.util.Collection; 15 15 import java.util.Date; 16 16 import java.util.HashMap; … … 24 24 import java.util.regex.Matcher; 25 25 import java.util.regex.Pattern; 26 import java.util.stream.Collectors; 27 28 import javax.json.Json; 29 import javax.json.JsonArray; 30 import javax.json.JsonArrayBuilder; 31 import javax.json.JsonObject; 32 import javax.json.JsonObjectBuilder; 26 33 27 34 import org.openstreetmap.josm.gui.help.HelpUtil; … … 390 397 391 398 /** 392 * Returns the JSON information for all handlers. 393 * @return the JSON information for all handlers 394 */ 395 public static String getHandlersInfoAsJSON() { 396 StringBuilder r = new StringBuilder(); 397 boolean first = true; 398 r.append('['); 399 400 for (Entry<String, Class<? extends RequestHandler>> p : handlers.entrySet()) { 401 if (first) { 402 first = false; 403 } else { 404 r.append(", "); 405 } 406 r.append(getHandlerInfoAsJSON(p.getKey())); 407 } 408 r.append(']'); 409 410 return r.toString(); 399 * Returns the JSON information for the given (if null: all) handlers. 400 * @param handlers the handlers 401 * @return the JSON information for the given (if null: all) handlers 402 */ 403 public static JsonArray getHandlersInfoAsJSON(Collection<String> handlers) { 404 JsonArrayBuilder json = Json.createArrayBuilder(); 405 for (String s : Utils.firstNonNull(handlers, RequestProcessor.handlers.keySet())) { 406 json.add(getHandlerInfoAsJSON(s)); 407 } 408 return json.build(); 411 409 } 412 410 … … 416 414 * @return JSON information for the given handler 417 415 */ 418 public static String getHandlerInfoAsJSON(String cmd) { 419 try (StringWriter w = new StringWriter()) { 420 RequestHandler handler = null; 421 try { 422 Class<?> c = handlers.get(cmd); 423 if (c == null) return null; 424 handler = handlers.get(cmd).getConstructor().newInstance(); 425 } catch (ReflectiveOperationException ex) { 426 Logging.error(ex); 427 return null; 428 } 429 430 try (PrintWriter r = new PrintWriter(w)) { 431 printJsonInfo(cmd, r, handler); 432 return w.toString(); 433 } 434 } catch (IOException e) { 435 Logging.error(e); 416 public static JsonObject getHandlerInfoAsJSON(String cmd) { 417 RequestHandler handler; 418 try { 419 Class<?> c = handlers.get(cmd); 420 if (c == null) return null; 421 handler = handlers.get(cmd).getConstructor().newInstance(); 422 } catch (ReflectiveOperationException ex) { 423 Logging.warn("Unknown handler " + cmd); 424 Logging.error(ex); 436 425 return null; 437 426 } 438 } 439 440 private static void printJsonInfo(String cmd, PrintWriter r, RequestHandler handler) { 441 r.printf("{ \"request\" : \"%s\"", cmd); 427 return getHandlerInfoAsJSON(cmd, handler); 428 } 429 430 private static JsonObject getHandlerInfoAsJSON(String cmd, RequestHandler handler) { 431 JsonObjectBuilder json = Json.createObjectBuilder(); 432 json.add("request", cmd); 442 433 if (handler.getUsage() != null) { 443 r.printf(", \"usage\" : \"%s\"", handler.getUsage()); 444 } 445 r.append(", \"parameters\" : ["); 446 447 String[] params = handler.getMandatoryParams(); 448 if (params != null) { 449 for (int i = 0; i < params.length; i++) { 450 if (i == 0) { 451 r.append('\"'); 452 } else { 453 r.append(", \""); 454 } 455 r.append(params[i]).append('\"'); 456 } 457 } 458 r.append("], \"optional\" : ["); 459 String[] optional = handler.getOptionalParams(); 460 if (optional != null) { 461 for (int i = 0; i < optional.length; i++) { 462 if (i == 0) { 463 r.append('\"'); 464 } else { 465 r.append(", \""); 466 } 467 r.append(optional[i]).append('\"'); 468 } 469 } 470 471 r.append("], \"examples\" : ["); 472 String[] examples = handler.getUsageExamples(cmd.substring(1)); 473 if (examples != null) { 474 for (int i = 0; i < examples.length; i++) { 475 if (i == 0) { 476 r.append('\"'); 477 } else { 478 r.append(", \""); 479 } 480 r.append(examples[i]).append('\"'); 481 } 482 } 483 r.append("]}"); 434 json.add("usage", handler.getUsage()); 435 } 436 json.add("parameters", toJsonArray(handler.getMandatoryParams())); 437 json.add("optional", toJsonArray(handler.getOptionalParams())); 438 json.add("examples", toJsonArray(handler.getUsageExamples(cmd.substring(1)))); 439 return json.build(); 440 } 441 442 private static JsonArray toJsonArray(String[] strings) { 443 return Arrays.stream(strings) 444 .collect(Collectors.collectingAndThen(Collectors.toList(), Json::createArrayBuilder)) 445 .build(); 484 446 } 485 447 -
trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/FeaturesHandler.java
r12620 r16550 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.util.Arrays; 7 import java.util.Collection; 8 6 9 import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault; 7 10 import org.openstreetmap.josm.io.remotecontrol.RequestProcessor; 8 import org.openstreetmap.josm.tools.Logging;9 11 10 12 /** … … 20 22 21 23 @Override 22 protected void handleRequest() throws RequestHandlerErrorException, 23 RequestHandlerBadRequestException { 24 StringBuilder buf = new StringBuilder(); 24 protected void handleRequest() throws RequestHandlerErrorException, RequestHandlerBadRequestException { 25 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.getHandlerInfoAsJSON('/'+s); 36 if (info != null) { 37 buf.append(info); 38 } else { 39 Logging.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(); 26 Collection<String> handlers = q == null ? null : Arrays.asList(q.split("[,\\s]+")); 27 content = RequestProcessor.getHandlersInfoAsJSON(handlers).toString(); 48 28 contentType = "application/json"; 49 29 if (args.containsKey("jsonp")) {
Note:
See TracChangeset
for help on using the changeset viewer.