Package com.codingchili.core.protocol
Class Protocol<RequestType>
java.lang.Object
com.codingchili.core.protocol.Protocol<RequestType>
Maps packet data to authorizer and manages authentication status for authorizer.
Route documentation and listing may be retrieved using #getSchema()
or by calling the protocol with the #CoreStrings.PROTOCOL_DOCUMENTATION
route. The documentation route is enabled whenever a handler class or route
is documented using either #document(String)
, #setDescription(String)
or by adding the #Description
annotation to the class or handler method.
Aug, 2020 - removed use of reflectasm as method reflection in J11 is 10% faster. If setAccessible(true) is called then J11 reflection is 50% faster.
-
Constructor Summary
ConstructorDescriptionProtocol()
Protocol(Receiver<RequestType> handler)
Creates a protocol by mapping annotated methods. -
Method Summary
Modifier and TypeMethodDescriptionannotated(Receiver<RequestType> handler)
Processes annotations on the given handler.authenticator(Function<Request,io.vertx.core.Future<RoleType>> authenticator)
Set the authenticator used toprocess(Request)
requests.Adds a documentation string to the last added route.Returns the route handler for the given target route and its access level.Returns the route handler for the given target route and its access level.Sets the model of the last added route.void
Processes a request with some additional error handling.routeMapper(Function<Request,String> mapper)
Set the route mapper used to process requests.setAuthorizationHandler(AuthorizationHandler<RequestType> authorizer)
Replaces the authorization handler used to map roles to routes.setDataModel(Class<?> model)
Sets the data model used for requests for documentation purposes.setDescription(String description)
Sets the documentation string for this protocol suite.Sets the default role of routes added _after_ this call.static RuntimeException
static <T extends Exception>
voidthrowAsUnchecked(Throwable throwable)
use(String route, RequestHandler<RequestType> handler)
Registers a handler for the given route.use(String route, RequestHandler<RequestType> handler, RoleType... role)
Registers a handler for the given route with an access level.
-
Constructor Details
-
Protocol
public Protocol() -
Protocol
Creates a protocol by mapping annotated methods.- Parameters:
handler
- contains methods to be mapped.
-
-
Method Details
-
annotated
Processes annotations on the given handler.- Parameters:
handler
- the handler that is annotated- Returns:
- fluent
-
endpoint
- Parameters:
endpoint
- sets the endpoint of the api, this is only used for documentation purposes.- Returns:
- fluent
-
setAuthorizationHandler
Replaces the authorization handler used to map roles to routes.- Parameters:
authorizer
- the new authorizer to use- Returns:
- fluent
-
setDataModel
Sets the data model used for requests for documentation purposes. Set automatically when using #annotated(Receiver)
- Parameters:
model
- the data transfer object.- Returns:
- fluent
-
setRole
Sets the default role of routes added _after_ this call.Annotated alternative #
Roles
on handler class- Parameters:
role
- the role to set for requests added after calling this method.- Returns:
- fluent
-
throwAny
-
throwAsUnchecked
- Throws:
T extends Exception
-
use
Registers a handler for the given route.Annotated alternative #
Api
- Parameters:
route
- the route to register a handler for.handler
- the handler to be registered for the given route.- Returns:
- the updated protocol specification for fluent use.
-
use
public Protocol<RequestType> use(String route, RequestHandler<RequestType> handler, RoleType... role)Registers a handler for the given route with an access level.Annotated alternative #
Api
- Parameters:
route
- the route to register a handler for.handler
- the handler to be registered for the given route with the access level.role
- specifies the authorization level required to access the route.- Returns:
- the updated protocol specification for fluent use.
-
get
public RequestHandler<RequestType> get(String route) throws AuthorizationRequiredException, HandlerMissingExceptionReturns the route handler for the given target route and its access level.- Parameters:
route
- the handler route to find.- Returns:
- the handler that is mapped to the route.
- Throws:
AuthorizationRequiredException
- when authorization level is not fulfilled for the given route.HandlerMissingException
- when the requested route handler is not registered.
-
get
public RequestHandler<RequestType> get(String route, RoleType role) throws AuthorizationRequiredException, HandlerMissingExceptionReturns the route handler for the given target route and its access level.- Parameters:
route
- the handler route to findrole
- list of roles that are allowed to map to a route- Returns:
- the handler that is mapped to the route and access level.
- Throws:
AuthorizationRequiredException
- when authorization level is not fulfilled for given route.HandlerMissingException
- when the requested route handler is not registered.
-
process
Processes a request with some additional error handling. The authenticatorauthenticator(Function)
is invoked with a future to support asynchronous cryptography operations, the request size is checked against the configured maximum value. The route to invoke may be configured by specifying a customrouteMapper(Function)
. Any exceptions thrown by the invoked route, or if the route is missing or authorization insufficient, an error will be written as a response to the request.- Parameters:
request
- the request to be processed.
-
routeMapper
Set the route mapper used to process requests. A route mapper determines which protocol route that is to be invoked for the given request. The default mapper invokesRequest.route()
.- Parameters:
mapper
- the mapper to use for this protocol instance.- Returns:
- fluent.
-
authenticator
public Protocol<RequestType> authenticator(Function<Request,io.vertx.core.Future<RoleType>> authenticator)Set the authenticator used toprocess(Request)
requests. The authenticator consumes a Request and produces an optionally asynchronous result that indicates which role is valid for the current request. The default authenticator responds withRole.PUBLIC
.- Parameters:
authenticator
- the authenticator to use for this protocol instance.- Returns:
- fluent.
-
document
Adds a documentation string to the last added route.Annotated alternative #
Description
on requesthandler class- Parameters:
routeDescription
- route protocol description text- Returns:
- fluent
-
model
Sets the model of the last added route.- Parameters:
model
- the model to set for the route.- Returns:
- fluent
-
getSchema
- Returns:
- returns a list of all registered routes on the protoocol.
-
setDescription
Sets the documentation string for this protocol suite.Annotated alternative #
Description
on Handler class- Parameters:
description
- description text.- Returns:
- fluent
-
available
- Returns:
- a list of all registered routes in the protocol.
-