public class Router extends AbstractRouter<Router,Request,RequestHandler,ApiDescription> implements RequestHandler
route matcher
and a corresponding
handler, when routing a request the router will call
RouteMatcher.evaluate(org.forgerock.services.context.Context, R)
for each
registered route and use the returned RouteMatcher
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);
AbstractRouter
,
RouteMatchers
Modifier and Type | Class and Description |
---|---|
static class |
Router.UriTemplate
Represents a URI template string that will be used to match and route
incoming requests.
|
Describable.Listener
api, thisRouterUriMatcher
Constructor and Description |
---|
Router()
Creates a new router with no routes defined.
|
Router(AbstractRouter<Router,Request,RequestHandler,ApiDescription> router)
Creates a new router containing the same routes and default route as the
provided router.
|
Modifier and Type | Method and Description |
---|---|
RouteMatcher<Request> |
addRoute(Router.UriTemplate uriTemplate,
CollectionResourceProvider provider)
Adds a new route to this router for the provided collection resource
provider.
|
RouteMatcher<Request> |
addRoute(Router.UriTemplate uriTemplate,
SingletonResourceProvider provider)
Adds a new route to this router for the provided singleton resource
provider.
|
RouteMatcher<Request> |
addRoute(RoutingMode mode,
Router.UriTemplate uriTemplate,
RequestHandler handler)
Adds a new route to this router for the provided request handler.
|
RouteMatcher<Request> |
addRoute(Version version,
CollectionResourceProvider provider)
Adds a new route to this router for the provided collection resource
provider.
|
RouteMatcher<Request> |
addRoute(Version version,
RequestHandler handler)
Adds a new route to this router for the provided request handler.
|
RouteMatcher<Request> |
addRoute(Version version,
SingletonResourceProvider provider)
Adds a new route to this router for the provided singleton resource
provider.
|
protected Pair<RouteMatcher<Request>,RequestHandler> |
getSelfApiHandler()
Return a
Describable handler that returns this AbstractRouter 's internal api description from the
Describable.handleApiRequest(Context, Object) method. |
protected Router |
getThis()
Returns this
AbstractRouter instance, typed correctly. |
Promise<ActionResponse,ResourceException> |
handleAction(Context context,
ActionRequest request)
Handles performing an action on a resource, and optionally returns an
associated result.
|
ApiDescription |
handleApiRequest(Context context,
Request request)
Handle a request for the API Descriptor.
|
Promise<ResourceResponse,ResourceException> |
handleCreate(Context context,
CreateRequest request)
Adds a new JSON resource, returning a
Promise that will be
completed when the resource has been added. |
Promise<ResourceResponse,ResourceException> |
handleDelete(Context context,
DeleteRequest request)
Deletes a JSON resource, returning a
Promise that will be
completed when the resource has been deleted. |
Promise<ResourceResponse,ResourceException> |
handlePatch(Context context,
PatchRequest request)
Updates a JSON resource by applying a set of changes to its existing
content, returning a
Promise that will be completed when the
resource has been updated. |
Promise<QueryResponse,ResourceException> |
handleQuery(Context context,
QueryRequest request,
QueryResourceHandler handler)
Searches for all JSON resources matching a user specified set of
criteria, returning a
Promise that will be completed when the
search has completed. |
Promise<ResourceResponse,ResourceException> |
handleRead(Context context,
ReadRequest request)
Reads a JSON resource, returning a
Promise that will be
completed when the resource has been read. |
Promise<ResourceResponse,ResourceException> |
handleUpdate(Context context,
UpdateRequest request)
Updates a JSON resource by replacing its existing content with new
content, returning a
Promise that will be completed when the
resource has been updated. |
protected RouteMatcher<Request> |
uriMatcher(RoutingMode mode,
String pattern)
Create a URI matcher suitable for the request type
<R> . |
static Router.UriTemplate |
uriTemplate(String template)
Creates a
Router.UriTemplate from a URI template string that will be
used to match and route incoming requests. |
addAllRoutes, addDescriptorListener, addRoute, api, buildApi, getBestApiRoute, getBestRoute, getRoutes, notifyDescriptorChange, removeAllRoutes, removeDescriptorListener, removeRoute, setDefaultRoute
public Router()
public Router(AbstractRouter<Router,Request,RequestHandler,ApiDescription> router)
router
- The router to be copied.protected Router getThis()
AbstractRouter
AbstractRouter
instance, typed correctly.getThis
in class AbstractRouter<Router,Request,RequestHandler,ApiDescription>
AbstractRouter
instance.protected RouteMatcher<Request> uriMatcher(RoutingMode mode, String pattern)
AbstractRouter
<R>
.uriMatcher
in class AbstractRouter<Router,Request,RequestHandler,ApiDescription>
mode
- The routing mode.pattern
- The pattern.public RouteMatcher<Request> addRoute(Router.UriTemplate uriTemplate, CollectionResourceProvider provider)
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);
uriTemplate
- The URI template which request resource names must match.provider
- The collection resource provider to which matching requests
will be routed.RouteMatcher
for the route that can be used to
remove the route at a later point.public RouteMatcher<Request> addRoute(Router.UriTemplate uriTemplate, SingletonResourceProvider provider)
uriTemplate
- The URI template which request resource names must match.provider
- The singleton resource provider to which matching requests
will be routed.RouteMatcher
for the route that can be used to
remove the route at a later point.public RouteMatcher<Request> addRoute(RoutingMode mode, Router.UriTemplate uriTemplate, RequestHandler handler)
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.RouteMatcher
for the route that can be used to
remove the route at a later point.public static Router.UriTemplate uriTemplate(String template)
Router.UriTemplate
from a URI template string that will be
used to match and route incoming requests.template
- The URI template.UriTemplate
instance.public RouteMatcher<Request> addRoute(Version version, CollectionResourceProvider provider)
version
- The resource API version the the request must match.provider
- The collection resource provider to which matching
requests will be routed.RouteMatcher
for the route that can be used to
remove the route at a later point.public RouteMatcher<Request> addRoute(Version version, SingletonResourceProvider provider)
version
- The resource API version the the request must match.provider
- The singleton resource provider to which matching
requests will be routed.RouteMatcher
for the route that can be used to
remove the route at a later point.public RouteMatcher<Request> addRoute(Version version, RequestHandler handler)
version
- The resource API version the the request must match.handler
- The request handler to which matching requests will be routed.RouteMatcher
for the route that can be used to
remove the route at a later point.public Promise<ActionResponse,ResourceException> handleAction(Context context, ActionRequest request)
RequestHandler
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 returned Promise
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/supported
BadRequestException
if the passed identifier, parameters or
filter is invalid NotFoundException
if the specified resource
could not be found.
handleAction
in interface RequestHandler
context
- The request server context, such as associated principal.request
- The action request.Promise
containing the result of the operation.public Promise<ResourceResponse,ResourceException> handleCreate(Context context, CreateRequest request)
RequestHandler
Promise
that will be
completed when the resource has been added.
Create expects failure exceptions as follows:
ForbiddenException
if access to the resource is forbidden.
NotSupportedException
if the requested functionality is not
implemented/supported
PreconditionFailedException
if a resource with the same ID
already exists
BadRequestException
if the passed identifier or value is
invalid
NotFoundException
if the specified id could not be resolved,
for example when an intermediate resource in the hierarchy does not
exist.
handleCreate
in interface RequestHandler
context
- The request server context, such as associated principal.request
- The create request.Promise
containing the result of the operation.public Promise<ResourceResponse,ResourceException> handleDelete(Context context, DeleteRequest request)
RequestHandler
Promise
that will be
completed when the resource has been deleted.
Delete expects failure exceptions as follows:
ForbiddenException
if access to the resource is forbidden
NotSupportedException
if the requested functionality is not
implemented/supported
BadRequestException
if the passed identifier is invalid
NotFoundException
if the specified resource could not be
found
PreconditionRequiredException
if version is required, but is
null
PreconditionFailedException
if version did not match the
existing resource.
handleDelete
in interface RequestHandler
context
- The request server context, such as associated principal.request
- The delete request.Promise
containing the result of the operation.public Promise<ResourceResponse,ResourceException> handlePatch(Context context, PatchRequest request)
RequestHandler
Promise
that will be completed when the
resource has been updated.
Patch expects failure exceptions as follows:
ForbiddenException
if access to the resource is forbidden
NotSupportedException
if the requested functionality is not
implemented/supported
PreconditionRequiredException
if version is required, but is
null
PreconditionFailedException
if version did not match the
existing resource
BadRequestException
if the passed identifier or filter is
invalid
NotFoundException
if the specified resource could not be
found
ConflictException
if patch could not be applied for the given
resource state.
handlePatch
in interface RequestHandler
context
- The request server context, such as associated principal.request
- The patch request.Promise
containing the result of the operation.public Promise<QueryResponse,ResourceException> handleQuery(Context context, QueryRequest request, QueryResourceHandler handler)
RequestHandler
Promise
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 a
QueryResponse
if the query has completed successfully, or
ResourceException
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 forbidden
NotSupportedException
if the requested functionality is not
implemented/supported
BadRequestException
if the passed identifier, parameters or
filter is invalid
NotFoundException
if the specified resource could not be
found
handleQuery
in interface RequestHandler
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.Promise
containing the result of the operation.public Promise<ResourceResponse,ResourceException> handleRead(Context context, ReadRequest request)
RequestHandler
Promise
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/supported
BadRequestException
if the passed identifier or filter is
invalid
NotFoundException
if the specified resource could not be
found.
handleRead
in interface RequestHandler
context
- The request server context, such as associated principal.request
- The read request.Promise
containing the result of the operation.public Promise<ResourceResponse,ResourceException> handleUpdate(Context context, UpdateRequest request)
RequestHandler
Promise
that will be completed when the
resource has been updated.
Update expects failure the following failure exceptions:
ForbiddenException
if access to the resource is forbidden
NotSupportedException
if the requested functionality is not
implemented/supported
PreconditionRequiredException
if version is required, but is
null
PreconditionFailedException
if version did not match the
existing resource
BadRequestException
if the passed identifier or filter is
invalid
NotFoundException
if the specified resource could not be
found.
handleUpdate
in interface RequestHandler
context
- The request server context, such as associated principal.request
- The update request.Promise
containing the result of the operation.public ApiDescription handleApiRequest(Context context, Request request)
Describable
handleApiRequest
in interface Describable<ApiDescription,Request>
handleApiRequest
in class AbstractRouter<Router,Request,RequestHandler,ApiDescription>
context
- The request context.request
- The request.protected Pair<RouteMatcher<Request>,RequestHandler> getSelfApiHandler()
AbstractRouter
Describable
handler that returns this AbstractRouter
's internal api description from the
Describable.handleApiRequest(Context, Object)
method. All other methods should throw an
UnsupportedOperationException
, as they should never be used.getSelfApiHandler
in class AbstractRouter<Router,Request,RequestHandler,ApiDescription>
Copyright © 2010-2018, ForgeRock All Rights Reserved.