Class ApiServiceDispatcher
- Author:
- Niels Meijer
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Find allApiDispatchConfiginstances for a given URI for purpose of generating OpenAPI spec.findConfigForRequest(ApiListener.HttpMethod method, String requestUri) Find theApiDispatchConfigbest matching a given request, consisting of the HTTP request method and request URI.Find anApiDispatchConfigthat has an exact match with the request URI, for purpose of generating the OpenAPI spec from it.jakarta.json.JsonObjectgenerateOpenApiJsonSchema(String endpoint) jakarta.json.JsonObjectgenerateOpenApiJsonSchema(ApiDispatchConfig client, String endpoint) static ApiServiceDispatcherstatic Optional<Json2XmlValidator> getJsonInputValidator(PipeLine pipeLine) static Optional<Json2XmlValidator> getJsonOutputValidator(PipeLine pipeline, String exit) voidregisterServiceClient(ApiListener listener) static intscoreRequestMethodMatch(ApiListener.HttpMethod requestMethod, ApiDispatchConfig config) Calculate a numerical score for how well a given HTTP requestApiListener.HttpMethodis matched by the givenApiDispatchConfig.static intscoreUriPattern(String uriPattern) Calculate a numerical score for a URI pattern indicating how specific it is, based on the number of segments and wildcards.voidunregisterServiceClient(ApiListener listener)
-
Constructor Details
-
ApiServiceDispatcher
public ApiServiceDispatcher()
-
-
Method Details
-
getInstance
-
findAllMatchingConfigsForUri
Find allApiDispatchConfiginstances 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.- Parameters:
uri- URI for which to find matchingApiDispatchConfiginstances.- Returns:
- List of
ApiDispatchConfiginstances matching the start of this request URI. (Nonnull, may be empty).
-
findExactMatchingConfigForUri
Find anApiDispatchConfigthat 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.- Parameters:
uri- The full URI for which to generate an OpenAPI spec.- Returns:
- The
ApiDispatchConfigfrom which to generate an OpenAPI spec.
-
findConfigForRequest
@Nullable public ApiDispatchConfig findConfigForRequest(@Nonnull ApiListener.HttpMethod method, @Nonnull String requestUri) Find theApiDispatchConfigbest matching a given request, consisting of the HTTP request method and request URI.This method will return the
ApiDispatchConfigthat has the most specific match with the request URI, and supports the requested HTTP method.ApiListener.HttpMethod.OPTIONSrequests 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 theOPTIONSrequest will return correct results, and requests for unsupported methods will return HTTP status code405instead of404.So for instance if a configuration would have the following
ApiListeners installed: Then:- ApiListener1: GET on uri /user/**
- ApiListener2: GET on uri /user/{userId}/department/{departmentId}
- ApiListener3: POST on uri /user/{userId}/avatar
- A request
GET /user/usr123/department/dept456would return theApiDispatchConfigfor /user/{userId}/department/{departmentId} containing ApiListener2- A request
OPTIONS /user/usr123/department/dept456would return theApiDispatchConfigfor /user/{userId}/department/{departmentId} containing ApiListener2- A request
GET /user/usr123/avatarwould return theApiDispatchConfigfor /user/** containing ApiListener1- A request
POST /user/usr123/avatarwould return theApiDispatchConfigfor /user/{userId}/avatar containing ApiListener3- A request
PUT /user/usr123/avatar(method PUT has not been configured!) would return theApiDispatchConfigfor /user/{userId}/avatar containing ApiListener3. Error handling can then be specific about the URL hit not supporting this method.- Parameters:
method-ApiListener.HttpMethodof the HTTP request receivedrequestUri- URI of the HTTP request received- Returns:
- The best matching
ApiDispatchConfig, ornull.
-
scoreRequestMethodMatch
public static int scoreRequestMethodMatch(ApiListener.HttpMethod requestMethod, ApiDispatchConfig config) Calculate a numerical score for how well a given HTTP requestApiListener.HttpMethodis matched by the givenApiDispatchConfig.- The score is positive 10 when the
HttpMethodisApiListener.HttpMethod.OPTIONSorApiDispatchConfig.hasMethod(ApiListener.HttpMethod)returnstrue.- In all other cases the score is negative 10.
- Parameters:
requestMethod- TheApiListener.HttpMethodof the requestconfig- TheApiDispatchConfigagainst which to match the request method- Returns:
- The calculated score of the match.
- The score is positive 10 when the
-
scoreUriPattern
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- Returns:
- Numerical score calculated for the URI based on the rules above.
-
registerServiceClient
-
unregisterServiceClient
-
getPatternClients
-
generateOpenApiJsonSchema
-
generateOpenApiJsonSchema
-
getJsonInputValidator
-
getJsonOutputValidator
-
clear
public void clear()
-