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,- Describable<org.forgerock.api.models.ApiDescription,Request>,- Describable.Listener
 
 public class Router extends 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 matcherand 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 returnedRouteMatcherto 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 SummaryNested Classes Modifier and Type Class Description static classRouter.UriTemplateRepresents a URI template string that will be used to match and route incoming requests.- 
Nested classes/interfaces inherited from interface org.forgerock.services.descriptor.DescribableDescribable.Listener
 
- 
 - 
Field Summary- 
Fields inherited from class org.forgerock.services.routing.AbstractRouterapi, thisRouterUriMatcher
 
- 
 - 
Constructor SummaryConstructors Constructor Description Router()Creates a new router with no routes defined.Router(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 SummaryAll Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description 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.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.protected Pair<RouteMatcher<Request>,RequestHandler>getSelfApiHandler()Return aDescribablehandler that returns thisAbstractRouter's internal api description from theDescribable.handleApiRequest(Context, Object)method.protected RoutergetThis()Returns thisAbstractRouterinstance, typed correctly.Promise<ActionResponse,ResourceException>handleAction(Context context, ActionRequest request)Handles performing an action on a resource, and optionally returns an associated result.org.forgerock.api.models.ApiDescriptionhandleApiRequest(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 aPromisethat will be completed when the resource has been added.Promise<ResourceResponse,ResourceException>handleDelete(Context context, DeleteRequest request)Deletes a JSON resource, returning aPromisethat 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 aPromisethat 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 aPromisethat will be completed when the search has completed.Promise<ResourceResponse,ResourceException>handleRead(Context context, ReadRequest request)Reads a JSON resource, returning aPromisethat 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 aPromisethat 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.UriTemplateuriTemplate(String template)Creates aRouter.UriTemplatefrom a URI template string that will be used to match and route incoming requests.- 
Methods inherited from class org.forgerock.services.routing.AbstractRouteraddAllRoutes, addDescriptorListener, addRoute, api, buildApi, getBestApiRoute, getBestRoute, getRoutes, notifyDescriptorChange, removeAllRoutes, removeDescriptorListener, removeRoute, setDefaultRoute
 
- 
 
- 
- 
- 
Constructor Detail- 
Routerpublic Router() Creates a new router with no routes defined.
 - 
Routerpublic Router(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- 
getThisprotected Router getThis() Description copied from class:AbstractRouterReturns thisAbstractRouterinstance, typed correctly.- Specified by:
- getThisin class- AbstractRouter<Router,Request,RequestHandler,org.forgerock.api.models.ApiDescription>
- Returns:
- This AbstractRouterinstance.
 
 - 
uriMatcherprotected RouteMatcher<Request> uriMatcher(RoutingMode mode, String pattern) Description copied from class:AbstractRouterCreate a URI matcher suitable for the request type<R>.- Specified by:
- uriMatcherin class- AbstractRouter<Router,Request,RequestHandler,org.forgerock.api.models.ApiDescription>
- Parameters:
- mode- The routing mode.
- pattern- The pattern.
- Returns:
- The matcher.
 
 - 
addRoutepublic 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 RouteMatcherfor the route that can be used to remove the route at a later point.
 
 - 
addRoutepublic 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 RouteMatcherfor the route that can be used to remove the route at a later point.
 
 - 
addRoutepublic RouteMatcher<Request> addRoute(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 RouteMatcherfor the route that can be used to remove the route at a later point.
 
 - 
uriTemplatepublic static Router.UriTemplate uriTemplate(String template) Creates aRouter.UriTemplatefrom a URI template string that will be used to match and route incoming requests.- Parameters:
- template- The URI template.
- Returns:
- A UriTemplateinstance.
 
 - 
addRoutepublic RouteMatcher<Request> addRoute(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 RouteMatcherfor the route that can be used to remove the route at a later point.
 
 - 
addRoutepublic RouteMatcher<Request> addRoute(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 RouteMatcherfor the route that can be used to remove the route at a later point.
 
 - 
addRoutepublic RouteMatcher<Request> addRoute(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 RouteMatcherfor the route that can be used to remove the route at a later point.
 
 - 
handleActionpublic Promise<ActionResponse,ResourceException> handleAction(Context context, ActionRequest request) Description copied from interface:RequestHandlerHandles 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 returnedPromisemust be completed with the exception.Action expects failure exceptions as follows: ForbiddenExceptionif access to the resource is forbidden.NotSupportedExceptionif the requested functionality is not implemented/supportedBadRequestExceptionif the passed identifier, parameters or filter is invalidNotFoundExceptionif the specified resource could not be found.- Specified by:
- handleActionin interface- RequestHandler
- Parameters:
- context- The request server context, such as associated principal.
- request- The action request.
- Returns:
- A Promisecontaining the result of the operation.
 
 - 
handleCreatepublic Promise<ResourceResponse,ResourceException> handleCreate(Context context, CreateRequest request) Description copied from interface:RequestHandlerAdds a new JSON resource, returning aPromisethat will be completed when the resource has been added.Create expects failure exceptions as follows: - CreateNotSupportedExceptionif create is not implemented or supported by the RequestHandler.
- ForbiddenExceptionif access to the resource is forbidden.
- NotSupportedExceptionif the requested functionality is not implemented/supported
- PreconditionFailedExceptionif a resource with the same ID already exists
- BadRequestExceptionif the passed identifier or value is invalid
- NotFoundExceptionif the specified id could not be resolved, for example when an intermediate resource in the hierarchy does not exist.
 - Specified by:
- handleCreatein interface- RequestHandler
- Parameters:
- context- The request server context, such as associated principal.
- request- The create request.
- Returns:
- A Promisecontaining the result of the operation.
 
 - 
handleDeletepublic Promise<ResourceResponse,ResourceException> handleDelete(Context context, DeleteRequest request) Description copied from interface:RequestHandlerDeletes a JSON resource, returning aPromisethat will be completed when the resource has been deleted.Delete expects failure exceptions as follows: - ForbiddenExceptionif access to the resource is forbidden
- NotSupportedExceptionif the requested functionality is not implemented/supported
- BadRequestExceptionif the passed identifier is invalid
- NotFoundExceptionif the specified resource could not be found
- PreconditionRequiredExceptionif version is required, but is- null
- PreconditionFailedExceptionif version did not match the existing resource.
 - Specified by:
- handleDeletein interface- RequestHandler
- Parameters:
- context- The request server context, such as associated principal.
- request- The delete request.
- Returns:
- A Promisecontaining the result of the operation.
 
 - 
handlePatchpublic Promise<ResourceResponse,ResourceException> handlePatch(Context context, PatchRequest request) Description copied from interface:RequestHandlerUpdates a JSON resource by applying a set of changes to its existing content, returning aPromisethat will be completed when the resource has been updated.Patch expects failure exceptions as follows: - ForbiddenExceptionif access to the resource is forbidden
- NotSupportedExceptionif the requested functionality is not implemented/supported
- PreconditionRequiredExceptionif version is required, but is- null
- PreconditionFailedExceptionif version did not match the existing resource
- BadRequestExceptionif the passed identifier or filter is invalid
- NotFoundExceptionif the specified resource could not be found
- ConflictExceptionif patch could not be applied for the given resource state.
 - Specified by:
- handlePatchin interface- RequestHandler
- Parameters:
- context- The request server context, such as associated principal.
- request- The patch request.
- Returns:
- A Promisecontaining the result of the operation.
 
 - 
handleQuerypublic Promise<QueryResponse,ResourceException> handleQuery(Context context, QueryRequest request, QueryResourceHandler handler) Description copied from interface:RequestHandlerSearches for all JSON resources matching a user specified set of criteria, returning aPromisethat 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 aQueryResponseif the query has completed successfully, orResourceExceptionif the query did not complete successfully (even if some matching resources were returned).Query expects failure exceptions as follows: - ForbiddenExceptionif access to the resource is forbidden
- NotSupportedExceptionif the requested functionality is not implemented/supported
- BadRequestExceptionif the passed identifier, parameters or filter is invalid
- NotFoundExceptionif the specified resource could not be found
 - Specified by:
- handleQueryin interface- RequestHandler
- 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 Promisecontaining the result of the operation.
 
 - 
handleReadpublic Promise<ResourceResponse,ResourceException> handleRead(Context context, ReadRequest request) Description copied from interface:RequestHandlerReads a JSON resource, returning aPromisethat will be completed when the resource has been read.Read expects failure exceptions as follows: - ForbiddenExceptionif access to the resource is forbidden.
- NotSupportedExceptionif the requested functionality is not implemented/supported
- BadRequestExceptionif the passed identifier or filter is invalid
- NotFoundExceptionif the specified resource could not be found.
 - Specified by:
- handleReadin interface- RequestHandler
- Parameters:
- context- The request server context, such as associated principal.
- request- The read request.
- Returns:
- A Promisecontaining the result of the operation.
 
 - 
handleUpdatepublic Promise<ResourceResponse,ResourceException> handleUpdate(Context context, UpdateRequest request) Description copied from interface:RequestHandlerUpdates a JSON resource by replacing its existing content with new content, returning aPromisethat will be completed when the resource has been updated.Update expects failure the following failure exceptions: - ForbiddenExceptionif access to the resource is forbidden
- NotSupportedExceptionif the requested functionality is not implemented/supported
- PreconditionRequiredExceptionif version is required, but is- null
- PreconditionFailedExceptionif version did not match the existing resource
- BadRequestExceptionif the passed identifier or filter is invalid
- NotFoundExceptionif the specified resource could not be found.
 - Specified by:
- handleUpdatein interface- RequestHandler
- Parameters:
- context- The request server context, such as associated principal.
- request- The update request.
- Returns:
- A Promisecontaining the result of the operation.
 
 - 
handleApiRequestpublic org.forgerock.api.models.ApiDescription handleApiRequest(Context context, Request request) Description copied from interface:DescribableHandle a request for the API Descriptor. This method should not do any computation, but should return the already computed descriptor.- Specified by:
- handleApiRequestin interface- Describable<org.forgerock.api.models.ApiDescription,Request>
- Overrides:
- handleApiRequestin class- AbstractRouter<Router,Request,RequestHandler,org.forgerock.api.models.ApiDescription>
- Parameters:
- context- The request context.
- request- The request.
- Returns:
- The descriptor.
 
 - 
getSelfApiHandlerprotected Pair<RouteMatcher<Request>,RequestHandler> getSelfApiHandler() Description copied from class:AbstractRouterReturn aDescribablehandler that returns thisAbstractRouter's internal api description from theDescribable.handleApiRequest(Context, Object)method. All other methods should throw anUnsupportedOperationException, as they should never be used.- Specified by:
- getSelfApiHandlerin class- AbstractRouter<Router,Request,RequestHandler,org.forgerock.api.models.ApiDescription>
- Returns:
- The describable handler.
 
 
- 
 
-