Changeset 16825 in josm
- Timestamp:
- 2020-08-02T22:16:18+02:00 (4 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/remotecontrol/RemoteControl.java
r16589 r16825 32 32 */ 33 33 static final int protocolMajorVersion = 1; 34 static final int protocolMinorVersion = 1 0;34 static final int protocolMinorVersion = 12; 35 35 36 36 /** … … 99 99 throw new UnknownHostException(); 100 100 } 101 102 /** 103 * Returns the RemoteControl HTTP protocol version 104 * @return the RemoteControl HTTP protocol version 105 */ 106 public static String getVersion() { 107 return protocolMajorVersion + "." + protocolMinorVersion; 108 } 101 109 } -
trunk/src/org/openstreetmap/josm/io/remotecontrol/RequestProcessor.java
r16767 r16825 11 11 import java.nio.charset.Charset; 12 12 import java.nio.charset.StandardCharsets; 13 import java.util.Arrays;14 13 import java.util.Collection; 15 14 import java.util.Date; … … 24 23 import java.util.regex.Matcher; 25 24 import java.util.regex.Pattern; 26 import java.util.stream. Collectors;25 import java.util.stream.Stream; 27 26 28 27 import javax.json.Json; 29 import javax.json.JsonArray;30 import javax.json.JsonArrayBuilder;31 import javax.json.JsonObject;32 import javax.json.JsonObjectBuilder;33 28 34 29 import org.openstreetmap.josm.data.Version; … … 42 37 import org.openstreetmap.josm.io.remotecontrol.handler.LoadDataHandler; 43 38 import org.openstreetmap.josm.io.remotecontrol.handler.LoadObjectHandler; 39 import org.openstreetmap.josm.io.remotecontrol.handler.OpenApiHandler; 44 40 import org.openstreetmap.josm.io.remotecontrol.handler.OpenFileHandler; 45 41 import org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler; … … 62 58 63 59 /** 60 * The string "JOSM RemoteControl" 61 */ 62 public static final String JOSM_REMOTE_CONTROL = "JOSM RemoteControl"; 63 64 /** 64 65 * RemoteControl protocol version. Change minor number for compatible 65 66 * interface extensions. Change major number in case of incompatible … … 70 71 .add("major", RemoteControl.protocolMajorVersion) 71 72 .add("minor", RemoteControl.protocolMinorVersion)) 72 .add("application", "JOSM RemoteControl")73 .add("application", JOSM_REMOTE_CONTROL) 73 74 .add("version", Version.getInstance().getVersion()) 74 75 .build().toString(); … … 168 169 addRequestHandlerClass(VersionHandler.command, VersionHandler.class, true); 169 170 addRequestHandlerClass(FeaturesHandler.command, FeaturesHandler.class, true); 171 addRequestHandlerClass(OpenApiHandler.command, OpenApiHandler.class, true); 170 172 } 171 173 } … … 393 395 out.write("HTTP/1.1 " + status + "\r\n"); 394 396 out.write("Date: " + new Date() + "\r\n"); 395 out.write("Server: JOSM RemoteControl\r\n");397 out.write("Server: " + JOSM_REMOTE_CONTROL + "\r\n"); 396 398 out.write("Content-type: " + contentType + "; charset=" + RESPONSE_CHARSET.name().toLowerCase(Locale.ENGLISH) + "\r\n"); 397 399 out.write("Access-Control-Allow-Origin: *\r\n"); … … 401 403 402 404 /** 403 * Returns the JSONinformation for the given (if null: all) handlers.405 * Returns the information for the given (if null: all) handlers. 404 406 * @param handlers the handlers 405 * @return the JSON information for the given (if null: all) handlers 406 */ 407 public static JsonArray getHandlersInfoAsJSON(Collection<String> handlers) { 408 JsonArrayBuilder json = Json.createArrayBuilder(); 409 for (String s : Utils.firstNonNull(handlers, RequestProcessor.handlers.keySet())) { 410 JsonObject infoAsJson = getHandlerInfoAsJSON(s); 411 if (infoAsJson != null) { 412 json.add(infoAsJson); 413 } 414 } 415 return json.build(); 416 } 417 418 /** 419 * Returns the JSON information for a given handler. 407 * @return the information for the given (if null: all) handlers 408 */ 409 public static Stream<RequestHandler> getHandlersInfo(Collection<String> handlers) { 410 return Utils.firstNonNull(handlers, RequestProcessor.handlers.keySet()).stream() 411 .map(RequestProcessor::getHandlerInfo) 412 .filter(Objects::nonNull); 413 } 414 415 /** 416 * Returns the information for a given handler. 420 417 * @param cmd handler key 421 * @return JSON information for the given handler 422 */ 423 public static JsonObject getHandlerInfoAsJSON(String cmd) { 424 RequestHandler handler; 418 * @return the information for the given handler 419 */ 420 public static RequestHandler getHandlerInfo(String cmd) { 425 421 if (cmd == null) { 426 422 return null; … … 432 428 Class<?> c = handlers.get(cmd); 433 429 if (c == null) return null; 434 handler = handlers.get(cmd).getConstructor().newInstance(); 430 RequestHandler handler = handlers.get(cmd).getConstructor().newInstance(); 431 handler.setCommand(cmd); 432 return handler; 435 433 } catch (ReflectiveOperationException ex) { 436 434 Logging.warn("Unknown handler " + cmd); … … 438 436 return null; 439 437 } 440 return getHandlerInfoAsJSON(cmd, handler);441 }442 443 private static JsonObject getHandlerInfoAsJSON(String cmd, RequestHandler handler) {444 JsonObjectBuilder json = Json.createObjectBuilder();445 json.add("request", cmd);446 if (handler.getUsage() != null) {447 json.add("usage", handler.getUsage());448 }449 json.add("parameters", toJsonArray(handler.getMandatoryParams()));450 json.add("optional", toJsonArray(handler.getOptionalParams()));451 json.add("examples", toJsonArray(handler.getUsageExamples(cmd.substring(1))));452 return json.build();453 }454 455 private static JsonArray toJsonArray(String[] strings) {456 return Arrays.stream(strings)457 .collect(Collectors.collectingAndThen(Collectors.toList(), Json::createArrayBuilder))458 .build();459 438 } 460 439 -
trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/FeaturesHandler.java
r16643 r16825 6 6 import java.util.Arrays; 7 7 import java.util.Collection; 8 import java.util.stream.Collectors; 9 10 import javax.json.Json; 11 import javax.json.JsonArray; 12 import javax.json.JsonArrayBuilder; 13 import javax.json.JsonObject; 14 import javax.json.JsonObjectBuilder; 8 15 9 16 import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault; … … 25 32 String q = args.get("q"); 26 33 Collection<String> handlers = q == null ? null : Arrays.asList(q.split("[,\\s]+", -1)); 27 content = RequestProcessor.getHandlersInfoAsJSON(handlers).toString();34 content = getHandlersInfoAsJSON(handlers).toString(); 28 35 contentType = "application/json"; 29 36 if (args.containsKey("jsonp")) { 30 37 content = args.get("jsonp") + " && " + args.get("jsonp") + '(' + content + ')'; 31 38 } 39 } 40 41 private static JsonArray getHandlersInfoAsJSON(Collection<String> handlers) { 42 JsonArrayBuilder json = Json.createArrayBuilder(); 43 RequestProcessor.getHandlersInfo(handlers) 44 .map(FeaturesHandler::getHandlerInfoAsJSON) 45 .forEach(json::add); 46 return json.build(); 47 } 48 49 private static JsonObject getHandlerInfoAsJSON(RequestHandler handler) { 50 JsonObjectBuilder json = Json.createObjectBuilder(); 51 json.add("request", handler.getCommand()); 52 if (handler.getUsage() != null) { 53 json.add("usage", handler.getUsage()); 54 } 55 json.add("parameters", toJsonArray(handler.getMandatoryParams())); 56 json.add("optional", toJsonArray(handler.getOptionalParams())); 57 json.add("examples", toJsonArray(handler.getUsageExamples(handler.getCommand()))); 58 return json.build(); 59 } 60 61 private static JsonArray toJsonArray(String[] strings) { 62 return Arrays.stream(strings) 63 .collect(Collectors.collectingAndThen(Collectors.toList(), Json::createArrayBuilder)) 64 .build(); 32 65 } 33 66 -
trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/RequestHandler.java
r16643 r16825 310 310 } 311 311 myCommand = command; 312 } 313 314 /** 315 * Returns the command associated with this handler. 316 * @return the command associated with this handler. 317 */ 318 public String getCommand() { 319 return myCommand; 312 320 } 313 321 -
trunk/test/unit/org/openstreetmap/josm/io/remotecontrol/RequestProcessorTest.java
r16767 r16825 2 2 package org.openstreetmap.josm.io.remotecontrol; 3 3 4 import static org.junit.jupiter.api.Assertions.assert DoesNotThrow;4 import static org.junit.jupiter.api.Assertions.assertTrue; 5 5 6 6 import java.util.Arrays; 7 import java.util.Objects; 7 8 8 9 import org.junit.jupiter.api.Test; … … 18 19 @Test 19 20 public void testFeaturesDoesNotThrowNPE() { 20 assertDoesNotThrow(() -> RequestProcessor.getHandlersInfoAsJSON(Arrays.asList("add_node", "/add_node", "", null))); 21 assertTrue(RequestProcessor.getHandlersInfo(Arrays.asList("add_node", "/add_node", "", null)) 22 .noneMatch(Objects::isNull)); 21 23 } 22 24 }
Note:
See TracChangeset
for help on using the changeset viewer.