Class ApiServiceDispatcher
- Author:
- Niels Meijer
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Find allApiDispatchConfig
instances for a given URI for purpose of generating OpenAPI spec.findConfigForRequest
(ApiListener.HttpMethod method, String requestUri) Find theApiDispatchConfig
best matching a given request, consisting of the HTTP request method and request URI.Find anApiDispatchConfig
that has an exact match with the request URI, for purpose of generating the OpenAPI spec from it.jakarta.json.JsonObject
generateOpenApiJsonSchema
(String endpoint) jakarta.json.JsonObject
generateOpenApiJsonSchema
(ApiDispatchConfig client, String endpoint) static ApiServiceDispatcher
static Optional<Json2XmlValidator>
getJsonInputValidator
(PipeLine pipeLine) static Optional<Json2XmlValidator>
getJsonOutputValidator
(PipeLine pipeline, String exit) void
registerServiceClient
(ApiListener listener) static int
scoreRequestMethodMatch
(ApiListener.HttpMethod requestMethod, ApiDispatchConfig config) Calculate a numerical score for how well a given HTTP requestApiListener.HttpMethod
is matched by the givenApiDispatchConfig
.static int
scoreUriPattern
(String uriPattern) Calculate a numerical score for a URI pattern indicating how specific it is, based on the number of segments and wildcards.void
unregisterServiceClient
(ApiListener listener)
-
Constructor Details
-
ApiServiceDispatcher
public ApiServiceDispatcher()
-
-
Method Details
-
getInstance
-
findAllMatchingConfigsForUri
Find allApiDispatchConfig
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.- Parameters:
uri
- URI for which to find matchingApiDispatchConfig
instances.- Returns:
- List of
ApiDispatchConfig
instances matching the start of this request URI. (Nonnull, may be empty).
-
findExactMatchingConfigForUri
Find anApiDispatchConfig
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.- Parameters:
uri
- The full URI for which to generate an OpenAPI spec.- Returns:
- The
ApiDispatchConfig
from which to generate an OpenAPI spec.
-
findConfigForRequest
@Nullable public ApiDispatchConfig findConfigForRequest(@Nonnull ApiListener.HttpMethod method, @Nonnull String requestUri) Find theApiDispatchConfig
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 theOPTIONS
request will return correct results, and requests for unsupported methods will return HTTP status code405
instead of404
.So for instance if a configuration would have the following
ApiListener
s installed:- 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/dept456
would return theApiDispatchConfig
for /user/{userId}/department/{departmentId} containing ApiListener2- A request
OPTIONS /user/usr123/department/dept456
would return theApiDispatchConfig
for /user/{userId}/department/{departmentId} containing ApiListener2- A request
GET /user/usr123/avatar
would return theApiDispatchConfig
for /user/** containing ApiListener1- A request
POST /user/usr123/avatar
would return theApiDispatchConfig
for /user/{userId}/avatar containing ApiListener3- A request
PUT /user/usr123/avatar
(method PUT has not been configured!) would return theApiDispatchConfig
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 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.HttpMethod
is matched by the givenApiDispatchConfig
.- The score is positive 10 when the
HttpMethod
isApiListener.HttpMethod.OPTIONS
orApiDispatchConfig.hasMethod(ApiListener.HttpMethod)
returnstrue
.- In all other cases the score is negative 10.
- Parameters:
requestMethod
- TheApiListener.HttpMethod
of the requestconfig
- TheApiDispatchConfig
against 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()
-