Changeset 16825 in josm


Ignore:
Timestamp:
2020-08-02T22:16:18+02:00 (4 years ago)
Author:
simon04
Message:

fix #19400 - Remote control: generate OpenAPI specification

Location:
trunk
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/RemoteControl.java

    r16589 r16825  
    3232     */
    3333    static final int protocolMajorVersion = 1;
    34     static final int protocolMinorVersion = 10;
     34    static final int protocolMinorVersion = 12;
    3535
    3636    /**
     
    9999        throw new UnknownHostException();
    100100    }
     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    }
    101109}
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/RequestProcessor.java

    r16767 r16825  
    1111import java.nio.charset.Charset;
    1212import java.nio.charset.StandardCharsets;
    13 import java.util.Arrays;
    1413import java.util.Collection;
    1514import java.util.Date;
     
    2423import java.util.regex.Matcher;
    2524import java.util.regex.Pattern;
    26 import java.util.stream.Collectors;
     25import java.util.stream.Stream;
    2726
    2827import javax.json.Json;
    29 import javax.json.JsonArray;
    30 import javax.json.JsonArrayBuilder;
    31 import javax.json.JsonObject;
    32 import javax.json.JsonObjectBuilder;
    3328
    3429import org.openstreetmap.josm.data.Version;
     
    4237import org.openstreetmap.josm.io.remotecontrol.handler.LoadDataHandler;
    4338import org.openstreetmap.josm.io.remotecontrol.handler.LoadObjectHandler;
     39import org.openstreetmap.josm.io.remotecontrol.handler.OpenApiHandler;
    4440import org.openstreetmap.josm.io.remotecontrol.handler.OpenFileHandler;
    4541import org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler;
     
    6258
    6359    /**
     60     * The string "JOSM RemoteControl"
     61     */
     62    public static final String JOSM_REMOTE_CONTROL = "JOSM RemoteControl";
     63
     64    /**
    6465     * RemoteControl protocol version. Change minor number for compatible
    6566     * interface extensions. Change major number in case of incompatible
     
    7071                    .add("major", RemoteControl.protocolMajorVersion)
    7172                    .add("minor", RemoteControl.protocolMinorVersion))
    72             .add("application", "JOSM RemoteControl")
     73            .add("application", JOSM_REMOTE_CONTROL)
    7374            .add("version", Version.getInstance().getVersion())
    7475            .build().toString();
     
    168169            addRequestHandlerClass(VersionHandler.command, VersionHandler.class, true);
    169170            addRequestHandlerClass(FeaturesHandler.command, FeaturesHandler.class, true);
     171            addRequestHandlerClass(OpenApiHandler.command, OpenApiHandler.class, true);
    170172        }
    171173    }
     
    393395        out.write("HTTP/1.1 " + status + "\r\n");
    394396        out.write("Date: " + new Date() + "\r\n");
    395         out.write("Server: JOSM RemoteControl\r\n");
     397        out.write("Server: " + JOSM_REMOTE_CONTROL + "\r\n");
    396398        out.write("Content-type: " + contentType + "; charset=" + RESPONSE_CHARSET.name().toLowerCase(Locale.ENGLISH) + "\r\n");
    397399        out.write("Access-Control-Allow-Origin: *\r\n");
     
    401403
    402404    /**
    403      * Returns the JSON information for the given (if null: all) handlers.
     405     * Returns the information for the given (if null: all) handlers.
    404406     * @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.
    420417     * @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) {
    425421        if (cmd == null) {
    426422            return null;
     
    432428            Class<?> c = handlers.get(cmd);
    433429            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;
    435433        } catch (ReflectiveOperationException ex) {
    436434            Logging.warn("Unknown handler " + cmd);
     
    438436            return null;
    439437        }
    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();
    459438    }
    460439
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/FeaturesHandler.java

    r16643 r16825  
    66import java.util.Arrays;
    77import java.util.Collection;
     8import java.util.stream.Collectors;
     9
     10import javax.json.Json;
     11import javax.json.JsonArray;
     12import javax.json.JsonArrayBuilder;
     13import javax.json.JsonObject;
     14import javax.json.JsonObjectBuilder;
    815
    916import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
     
    2532        String q = args.get("q");
    2633        Collection<String> handlers = q == null ? null : Arrays.asList(q.split("[,\\s]+", -1));
    27         content = RequestProcessor.getHandlersInfoAsJSON(handlers).toString();
     34        content = getHandlersInfoAsJSON(handlers).toString();
    2835        contentType = "application/json";
    2936        if (args.containsKey("jsonp")) {
    3037            content = args.get("jsonp") + " && " + args.get("jsonp") + '(' + content + ')';
    3138        }
     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();
    3265    }
    3366
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/RequestHandler.java

    r16643 r16825  
    310310        }
    311311        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;
    312320    }
    313321
  • trunk/test/unit/org/openstreetmap/josm/io/remotecontrol/RequestProcessorTest.java

    r16767 r16825  
    22package org.openstreetmap.josm.io.remotecontrol;
    33
    4 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
     4import static org.junit.jupiter.api.Assertions.assertTrue;
    55
    66import java.util.Arrays;
     7import java.util.Objects;
    78
    89import org.junit.jupiter.api.Test;
     
    1819    @Test
    1920    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));
    2123    }
    2224}
Note: See TracChangeset for help on using the changeset viewer.