Class Router
- java.lang.Object
-
- org.forgerock.services.routing.AbstractRouter<Router,Request,RequestHandler,org.forgerock.api.models.ApiDescription>
-
- org.forgerock.json.resource.Router
-
- All Implemented Interfaces:
RequestHandler
,org.forgerock.services.descriptor.Describable<org.forgerock.api.models.ApiDescription,Request>
,org.forgerock.services.descriptor.Describable.Listener
public class Router extends org.forgerock.services.routing.AbstractRouter<Router,Request,RequestHandler,org.forgerock.api.models.ApiDescription> implements RequestHandler
A router which routes requests based on route predicates. Each route is comprised of aroute matcher
and a corresponding handler, when routing a request the router will callRouteMatcher.evaluate(org.forgerock.services.context.Context, R)
for each registered route and use the returnedRouteMatcher
to determine which route best matches the request.Routes may be added and removed from a router as follows:
Handler users = ...; Router router = new Router(); RouteMatcher routeOne = RouteMatchers.requestUriMatcher(EQUALS, "users"); RouteMatcher routeTwo = RouteMatchers.requestUriMatcher(EQUALS, "users/{userId}"); router.addRoute(routeOne, users); router.addRoute(routeTwo, users); // Deregister a route. router.removeRoute(routeOne, routeTwo);
- See Also:
AbstractRouter
,RouteMatchers
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Router.UriTemplate
Represents a URI template string that will be used to match and route incoming requests.
-
Constructor Summary
Constructors Constructor Description Router()
Creates a new router with no routes defined.Router(org.forgerock.services.routing.AbstractRouter<Router,Request,RequestHandler,org.forgerock.api.models.ApiDescription> router)
Creates a new router containing the same routes and default route as the provided router.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description org.forgerock.services.routing.RouteMatcher<Request>
addRoute(org.forgerock.http.routing.RoutingMode mode, Router.UriTemplate uriTemplate, RequestHandler handler)
Adds a new route to this router for the provided request handler.org.forgerock.services.routing.RouteMatcher<Request>
addRoute(org.forgerock.http.routing.Version version, CollectionResourceProvider provider)
Adds a new route to this router for the provided collection resource provider.org.forgerock.services.routing.RouteMatcher<Request>
addRoute(org.forgerock.http.routing.Version version, RequestHandler handler)
Adds a new route to this router for the provided request handler.org.forgerock.services.routing.RouteMatcher<Request>
addRoute(org.forgerock.http.routing.Version version, SingletonResourceProvider provider)
Adds a new route to this router for the provided singleton resource provider.org.forgerock.services.routing.RouteMatcher<Request>
addRoute(Router.UriTemplate uriTemplate, CollectionResourceProvider provider)
Adds a new route to this router for the provided collection resource provider.org.forgerock.services.routing.RouteMatcher<Request>
addRoute(Router.UriTemplate uriTemplate, SingletonResourceProvider provider)
Adds a new route to this router for the provided singleton resource provider.protected Pair<org.forgerock.services.routing.RouteMatcher<Request>,RequestHandler>
getSelfApiHandler()
protected Router
getThis()
Promise<ActionResponse,ResourceException>
handleAction(org.forgerock.services.context.Context context, ActionRequest request)
Handles performing an action on a resource, and optionally returns an associated result.org.forgerock.api.models.ApiDescription
handleApiRequest(org.forgerock.services.context.Context context, Request request)
Promise<ResourceResponse,ResourceException>
handleCreate(org.forgerock.services.context.Context context, CreateRequest request)
Adds a new JSON resource, returning aPromise
that will be completed when the resource has been added.Promise<ResourceResponse,ResourceException>
handleDelete(org.forgerock.services.context.Context context, DeleteRequest request)
Deletes a JSON resource, returning aPromise
that will be completed when the resource has been deleted.Promise<ResourceResponse,ResourceException>
handlePatch(org.forgerock.services.context.Context context, PatchRequest request)
Updates a JSON resource by applying a set of changes to its existing content, returning aPromise
that will be completed when the resource has been updated.Promise<QueryResponse,ResourceException>
handleQuery(org.forgerock.services.context.Context context, QueryRequest request, QueryResourceHandler handler)
Searches for all JSON resources matching a user specified set of criteria, returning aPromise
that will be completed when the search has completed.Promise<ResourceResponse,ResourceException>
handleRead(org.forgerock.services.context.Context context, ReadRequest request)
Reads a JSON resource, returning aPromise
that will be completed when the resource has been read.Promise<ResourceResponse,ResourceException>
handleUpdate(org.forgerock.services.context.Context context, UpdateRequest request)
Updates a JSON resource by replacing its existing content with new content, returning aPromise
that will be completed when the resource has been updated.protected org.forgerock.services.routing.RouteMatcher<Request>
uriMatcher(org.forgerock.http.routing.RoutingMode mode, String pattern)
static Router.UriTemplate
uriTemplate(String template)
Creates aRouter.UriTemplate
from a URI template string that will be used to match and route incoming requests.
-
-
-
Constructor Detail
-
Router
public Router()
Creates a new router with no routes defined.
-
Router
public Router(org.forgerock.services.routing.AbstractRouter<Router,Request,RequestHandler,org.forgerock.api.models.ApiDescription> router)
Creates a new router containing the same routes and default route as the provided router. Changes to the returned router's routing table will not impact the provided router.- Parameters:
router
- The router to be copied.
-
-
Method Detail
-
getThis
protected Router getThis()
- Specified by:
getThis
in classorg.forgerock.services.routing.AbstractRouter<Router,Request,RequestHandler,org.forgerock.api.models.ApiDescription>
-
uriMatcher
protected org.forgerock.services.routing.RouteMatcher<Request> uriMatcher(org.forgerock.http.routing.RoutingMode mode, String pattern)
- Specified by:
uriMatcher
in classorg.forgerock.services.routing.AbstractRouter<Router,Request,RequestHandler,org.forgerock.api.models.ApiDescription>
-
addRoute
public org.forgerock.services.routing.RouteMatcher<Request> addRoute(Router.UriTemplate uriTemplate, CollectionResourceProvider provider)
Adds a new route to this router for the provided collection resource provider. New routes may be added while this router is processing requests.The provided URI template must match the resource collection itself, not resource instances. For example:
CollectionResourceProvider users = ...; Router router = new Router(); // This is valid usage: the template matches the resource collection. router.addRoute(Router.uriTemplate("users"), users); // This is invalid usage: the template matches resource instances. router.addRoute(Router.uriTemplate("users/{userId}"), users);
- Parameters:
uriTemplate
- The URI template which request resource names must match.provider
- The collection resource provider to which matching requests will be routed.- Returns:
- The
RouteMatcher
for the route that can be used to remove the route at a later point.
-
addRoute
public org.forgerock.services.routing.RouteMatcher<Request> addRoute(Router.UriTemplate uriTemplate, SingletonResourceProvider provider)
Adds a new route to this router for the provided singleton resource provider. New routes may be added while this router is processing requests.- Parameters:
uriTemplate
- The URI template which request resource names must match.provider
- The singleton resource provider to which matching requests will be routed.- Returns:
- The
RouteMatcher
for the route that can be used to remove the route at a later point.
-
addRoute
public org.forgerock.services.routing.RouteMatcher<Request> addRoute(org.forgerock.http.routing.RoutingMode mode, Router.UriTemplate uriTemplate, RequestHandler handler)
Adds a new route to this router for the provided request handler. New routes may be added while this router is processing requests.- Parameters:
mode
- Indicates how the URI template should be matched against resource names.uriTemplate
- The URI template which request resource names must match.handler
- The request handler to which matching requests will be routed.- Returns:
- The
RouteMatcher
for the route that can be used to remove the route at a later point.
-
uriTemplate
public static Router.UriTemplate uriTemplate(String template)
Creates aRouter.UriTemplate
from a URI template string that will be used to match and route incoming requests.- Parameters:
template
- The URI template.- Returns:
- A
UriTemplate
instance.
-
addRoute
public org.forgerock.services.routing.RouteMatcher<Request> addRoute(org.forgerock.http.routing.Version version, CollectionResourceProvider provider)
Adds a new route to this router for the provided collection resource provider. New routes may be added while this router is processing requests.- Parameters:
version
- The resource API version the the request must match.provider
- The collection resource provider to which matching requests will be routed.- Returns:
- The
RouteMatcher
for the route that can be used to remove the route at a later point.
-
addRoute
public org.forgerock.services.routing.RouteMatcher<Request> addRoute(org.forgerock.http.routing.Version version, SingletonResourceProvider provider)
Adds a new route to this router for the provided singleton resource provider. New routes may be added while this router is processing requests.- Parameters:
version
- The resource API version the the request must match.provider
- The singleton resource provider to which matching requests will be routed.- Returns:
- The
RouteMatcher
for the route that can be used to remove the route at a later point.
-
addRoute
public org.forgerock.services.routing.RouteMatcher<Request> addRoute(org.forgerock.http.routing.Version version, RequestHandler handler)
Adds a new route to this router for the provided request handler. New routes may be added while this router is processing requests.- Parameters:
version
- The resource API version the the request must match.handler
- The request handler to which matching requests will be routed.- Returns:
- The
RouteMatcher
for the route that can be used to remove the route at a later point.
-
handleAction
public Promise<ActionResponse,ResourceException> handleAction(org.forgerock.services.context.Context context, ActionRequest request)
Description copied from interface:RequestHandler
Handles performing an action on a resource, and optionally returns an associated result. The execution of an action is allowed to incur side effects.Actions are parametric; a set of named parameters is provided as input to the action. The action result is a JSON object structure composed of basic Java types; its overall structure is defined by a specific implementation.
On completion, the action result (or null) must be used to complete the returned
Promise
. On failure, the returnedPromise
must be completed with the exception.Action expects failure exceptions as follows:
ForbiddenException
if access to the resource is forbidden.NotSupportedException
if the requested functionality is not implemented/supportedBadRequestException
if the passed identifier, parameters or filter is invalidNotFoundException
if the specified resource could not be found.- Specified by:
handleAction
in interfaceRequestHandler
- Parameters:
context
- The request server context, such as associated principal.request
- The action request.- Returns:
- A
Promise
containing the result of the operation.
-
handleCreate
public Promise<ResourceResponse,ResourceException> handleCreate(org.forgerock.services.context.Context context, CreateRequest request)
Description copied from interface:RequestHandler
Adds a new JSON resource, returning aPromise
that will be completed when the resource has been added.Create expects failure exceptions as follows:
CreateNotSupportedException
if create is not implemented or supported by the RequestHandler.ForbiddenException
if access to the resource is forbidden.NotSupportedException
if the requested functionality is not implemented/supportedPreconditionFailedException
if a resource with the same ID already existsBadRequestException
if the passed identifier or value is invalidNotFoundException
if the specified id could not be resolved, for example when an intermediate resource in the hierarchy does not exist.
- Specified by:
handleCreate
in interfaceRequestHandler
- Parameters:
context
- The request server context, such as associated principal.request
- The create request.- Returns:
- A
Promise
containing the result of the operation.
-
handleDelete
public Promise<ResourceResponse,ResourceException> handleDelete(org.forgerock.services.context.Context context, DeleteRequest request)
Description copied from interface:RequestHandler
Deletes a JSON resource, returning aPromise
that will be completed when the resource has been deleted.Delete expects failure exceptions as follows:
ForbiddenException
if access to the resource is forbiddenNotSupportedException
if the requested functionality is not implemented/supportedBadRequestException
if the passed identifier is invalidNotFoundException
if the specified resource could not be foundPreconditionRequiredException
if version is required, but isnull
PreconditionFailedException
if version did not match the existing resource.
- Specified by:
handleDelete
in interfaceRequestHandler
- Parameters:
context
- The request server context, such as associated principal.request
- The delete request.- Returns:
- A
Promise
containing the result of the operation.
-
handlePatch
public Promise<ResourceResponse,ResourceException> handlePatch(org.forgerock.services.context.Context context, PatchRequest request)
Description copied from interface:RequestHandler
Updates a JSON resource by applying a set of changes to its existing content, returning aPromise
that will be completed when the resource has been updated.Patch expects failure exceptions as follows:
ForbiddenException
if access to the resource is forbiddenNotSupportedException
if the requested functionality is not implemented/supportedPreconditionRequiredException
if version is required, but isnull
PreconditionFailedException
if version did not match the existing resourceBadRequestException
if the passed identifier or filter is invalidNotFoundException
if the specified resource could not be foundConflictException
if patch could not be applied for the given resource state.
- Specified by:
handlePatch
in interfaceRequestHandler
- Parameters:
context
- The request server context, such as associated principal.request
- The patch request.- Returns:
- A
Promise
containing the result of the operation.
-
handleQuery
public Promise<QueryResponse,ResourceException> handleQuery(org.forgerock.services.context.Context context, QueryRequest request, QueryResourceHandler handler)
Description copied from interface:RequestHandler
Searches for all JSON resources matching a user specified set of criteria, returning aPromise
that will be completed when the search has completed.Implementations must invoke
QueryResourceHandler.handleResource(ResourceResponse)
for each resource which matches the query criteria. Once all matching resources have been returned implementations are required to return either aQueryResponse
if the query has completed successfully, orResourceException
if the query did not complete successfully (even if some matching resources were returned).Query expects failure exceptions as follows:
ForbiddenException
if access to the resource is forbiddenNotSupportedException
if the requested functionality is not implemented/supportedBadRequestException
if the passed identifier, parameters or filter is invalidNotFoundException
if the specified resource could not be found
- Specified by:
handleQuery
in interfaceRequestHandler
- Parameters:
context
- The request server context, such as associated principal.request
- The query request.handler
- The query resource handler to be notified for each matching resource.- Returns:
- A
Promise
containing the result of the operation.
-
handleRead
public Promise<ResourceResponse,ResourceException> handleRead(org.forgerock.services.context.Context context, ReadRequest request)
Description copied from interface:RequestHandler
Reads a JSON resource, returning aPromise
that will be completed when the resource has been read.Read expects failure exceptions as follows:
ForbiddenException
if access to the resource is forbidden.NotSupportedException
if the requested functionality is not implemented/supportedBadRequestException
if the passed identifier or filter is invalidNotFoundException
if the specified resource could not be found.
- Specified by:
handleRead
in interfaceRequestHandler
- Parameters:
context
- The request server context, such as associated principal.request
- The read request.- Returns:
- A
Promise
containing the result of the operation.
-
handleUpdate
public Promise<ResourceResponse,ResourceException> handleUpdate(org.forgerock.services.context.Context context, UpdateRequest request)
Description copied from interface:RequestHandler
Updates a JSON resource by replacing its existing content with new content, returning aPromise
that will be completed when the resource has been updated.Update expects failure the following failure exceptions:
ForbiddenException
if access to the resource is forbiddenNotSupportedException
if the requested functionality is not implemented/supportedPreconditionRequiredException
if version is required, but isnull
PreconditionFailedException
if version did not match the existing resourceBadRequestException
if the passed identifier or filter is invalidNotFoundException
if the specified resource could not be found.
- Specified by:
handleUpdate
in interfaceRequestHandler
- Parameters:
context
- The request server context, such as associated principal.request
- The update request.- Returns:
- A
Promise
containing the result of the operation.
-
handleApiRequest
public org.forgerock.api.models.ApiDescription handleApiRequest(org.forgerock.services.context.Context context, Request request)
- Specified by:
handleApiRequest
in interfaceorg.forgerock.services.descriptor.Describable<org.forgerock.api.models.ApiDescription,Request>
- Overrides:
handleApiRequest
in classorg.forgerock.services.routing.AbstractRouter<Router,Request,RequestHandler,org.forgerock.api.models.ApiDescription>
-
getSelfApiHandler
protected Pair<org.forgerock.services.routing.RouteMatcher<Request>,RequestHandler> getSelfApiHandler()
- Specified by:
getSelfApiHandler
in classorg.forgerock.services.routing.AbstractRouter<Router,Request,RequestHandler,org.forgerock.api.models.ApiDescription>
-
-