Class ApiServiceDispatcher


public class ApiServiceDispatcher extends Object
This class registers dispatches requests to the proper registered ApiListeners. The dispatcher does not handle nor does it process messages!
Niels Meijer
  • Constructor Details

    • ApiServiceDispatcher

      public ApiServiceDispatcher()
  • Method Details

    • getInstance

      public static ApiServiceDispatcher getInstance()
    • findAllMatchingConfigsForUri

      @Nonnull public List<ApiDispatchConfig> findAllMatchingConfigsForUri(String uri)
      Find all ApiDispatchConfig instances for a given URI for purpose of generating OpenAPI spec.
      For this purpose, dispatch configurations for matching-patterns ending with /** are not included in the output of this method.
      URI patterns are matched up to have full or partial match for the length of the request URI, and returned URI patterns may have more segments than the request.
      uri - URI for which to find matching ApiDispatchConfig instances.
      List of ApiDispatchConfig instances matching the start of this request URI. (Nonnull, may be empty).
    • findExactMatchingConfigForUri

      @Nullable public ApiDispatchConfig findExactMatchingConfigForUri(@Nonnull String uri)
      Find an ApiDispatchConfig that has an exact match with the request URI, for purpose of generating the OpenAPI spec from it. Therefor no dispatch configurations for matching-patterns ending with /** are returned.
      uri - The full URI for which to generate an OpenAPI spec.
      The ApiDispatchConfig from which to generate an OpenAPI spec.
    • findConfigForRequest

      @Nullable public ApiDispatchConfig findConfigForRequest(@Nonnull ApiListener.HttpMethod method, @Nonnull String requestUri)
      Find the ApiDispatchConfig best matching a given request, consisting of the HTTP request method and request URI.

      This method will return the ApiDispatchConfig that has the most specific match with the request URI, and supports the requested HTTP method.

      ApiListener.HttpMethod.OPTIONS requests and requests for methods that are not supported by the configuration are always matched to the most specific URI pattern match. This is so that the OPTIONS request will return correct results, and requests for unsupported methods will return HTTP status code 405 instead of 404.

      So for instance if a configuration would have the following ApiListeners installed:

    • ApiListener1: GET on uri /user/**
    • ApiListener2: GET on uri /user/{userId}/department/{departmentId}
    • ApiListener3: POST on uri /user/{userId}/avatar
    • Then:
    • A request GET /user/usr123/department/dept456 would return the ApiDispatchConfig for /user/{userId}/department/{departmentId} containing ApiListener2
    • A request OPTIONS /user/usr123/department/dept456 would return the ApiDispatchConfig for /user/{userId}/department/{departmentId} containing ApiListener2
    • A request GET /user/usr123/avatar would return the ApiDispatchConfig for /user/** containing ApiListener1
    • A request POST /user/usr123/avatar would return the ApiDispatchConfig for /user/{userId}/avatar containing ApiListener3
    • A request PUT /user/usr123/avatar (method PUT has not been configured!) would return the ApiDispatchConfig for /user/{userId}/avatar containing ApiListener3. Error handling can then be specific about the URL hit not supporting this method.
    • Parameters:
      method - ApiListener.HttpMethod of the HTTP request received
      requestUri - URI of the HTTP request received
      The best matching ApiDispatchConfig, or null.
    • scoreRequestMethodMatch

      public static int scoreRequestMethodMatch(ApiListener.HttpMethod requestMethod, ApiDispatchConfig config)
      Calculate a numerical score for how well a given HTTP request ApiListener.HttpMethod is matched by the given ApiDispatchConfig.

    • The score is positive 10 when the HttpMethod is ApiListener.HttpMethod.OPTIONS or ApiDispatchConfig.hasMethod(ApiListener.HttpMethod) returns true.
    • In all other cases the score is negative 10.
    • Parameters:
      requestMethod - The ApiListener.HttpMethod of the request
      config - The ApiDispatchConfig against which to match the request method
      The calculated score of the match.
    • scoreUriPattern

      public static int scoreUriPattern(@Nonnull String uriPattern)
      Calculate a numerical score for a URI pattern indicating how specific it is, based on the number of segments and wildcards.

      The intent is to have a higher score the more specific a URI pattern is, thus the more segments the more specific the higher the score but the more wildcards, the less specific a patter is relative to another pattern of the same number of segments.

      Patterns ending with a /** "match all" wildcard are always scored as less specific than patterns which do not have the "match all" wildcard.

      Scoring rules:

    • The more slashes the longer the match the more specific
    • The more wildcards in the pattern the less specific
    • "Match-all" patterns ending with /** are penalized with a -10 starting score
    • Parameters:
      uriPattern - A pattern of a URI containing wildcards
      Numerical score calculated for the URI based on the rules above.
    • registerServiceClient

      public void registerServiceClient(ApiListener listener) throws ListenerException
    • unregisterServiceClient

      public void unregisterServiceClient(ApiListener listener)
    • getPatternClients

      public SortedMap<String,ApiDispatchConfig> getPatternClients()
    • generateOpenApiJsonSchema

      public jakarta.json.JsonObject generateOpenApiJsonSchema(String endpoint)
    • generateOpenApiJsonSchema

      public jakarta.json.JsonObject generateOpenApiJsonSchema(ApiDispatchConfig client, String endpoint)
    • getJsonValidator

      public static Json2XmlValidator getJsonValidator(PipeLine pipeline, boolean forOutputValidation)
    • clear

      public void clear()