T
- The type of the router.R
- The type of the request.H
- The type of the handler that will be used to handle routing requests.D
- The type of descriptor object that the APIs supported by this router can be described using.public abstract class AbstractRouter<T extends AbstractRouter<T,R,H,D>,R,H,D> extends Object implements Describable<D,R>, Describable.Listener
Generally speaking a router comprises of a series of routes, each of which is composed of a RouteMatcher
and a handler (H). When a request (R) is received the router invokes each RouteMatcher
to see if it
matches and then invokes the associated handler if it is the best match.
Concrete implementations of AbstractRouter
existing in both CHF
and CREST.
Describable.Listener
Modifier and Type | Field and Description |
---|---|
protected D |
api
Api of the current router.
|
protected RouteMatcher<R> |
thisRouterUriMatcher
Matches the current route.
|
Modifier | Constructor and Description |
---|---|
protected |
AbstractRouter()
Creates a new abstract router with no routes defined.
|
protected |
AbstractRouter(AbstractRouter<T,R,H,D> router)
Creates a new router containing the same routes and default route as the
provided router.
|
Modifier and Type | Method and Description |
---|---|
T |
addAllRoutes(T router)
Adds all of the routes defined in the provided router to this router.
|
void |
addDescriptorListener(Describable.Listener listener)
Add a listener for API Descriptor changes.
|
T |
addRoute(RouteMatcher<R> matcher,
H handler)
Adds a new route to this router for the provided handler.
|
D |
api(ApiProducer<D> producer)
Provide the API description for the component.
|
protected D |
buildApi(ApiProducer<D> producer)
Build an api with a given
ApiProducer . |
protected Pair<Context,H> |
getBestApiRoute(Context context,
R request)
Get the best route for an API request.
|
protected Pair<Context,H> |
getBestRoute(Context context,
R request)
Finds the best route that matches the given request based on the route
matchers of the registered routes.
|
protected Map<RouteMatcher<R>,H> |
getRoutes()
Gets all registered routes on this router.
|
protected abstract Pair<RouteMatcher<R>,H> |
getSelfApiHandler()
Return a
Describable handler that returns this AbstractRouter 's internal api description from the
Describable.handleApiRequest(Context, Object) method. |
protected abstract T |
getThis()
Returns this
AbstractRouter instance, typed correctly. |
D |
handleApiRequest(Context context,
R request)
Handle a request for the API Descriptor.
|
void |
notifyDescriptorChange()
Implement this method to handle changes to API Descriptors.
|
T |
removeAllRoutes()
Removes all of the routes from this router.
|
void |
removeDescriptorListener(Describable.Listener listener)
Remove a listener from API Descriptor changes.
|
boolean |
removeRoute(RouteMatcher<R>... routes)
Removes one or more routes from this router.
|
T |
setDefaultRoute(H handler)
Sets the handler to be used as the default route for requests which do
not match any of the other defined routes.
|
protected abstract RouteMatcher<R> |
uriMatcher(RoutingMode mode,
String pattern)
Create a URI matcher suitable for the request type
<R> . |
protected final RouteMatcher<R> thisRouterUriMatcher
protected D api
protected AbstractRouter()
protected AbstractRouter(AbstractRouter<T,R,H,D> router)
router
- The router to be copied.protected abstract T getThis()
AbstractRouter
instance, typed correctly.AbstractRouter
instance.protected final Map<RouteMatcher<R>,H> getRoutes()
public final T addAllRoutes(T router)
router
- The router whose routes are to be copied into this router.public final T addRoute(RouteMatcher<R> matcher, H handler)
The provided matcher can be used to remove this route later.
matcher
- The RouteMatcher
that will evaluate whether
the incoming request matches this route.handler
- The handler to which matching requests will be routed.public final T setDefaultRoute(H handler)
handler
- The handler to be used as the default route.public final T removeAllRoutes()
@SafeVarargs public final boolean removeRoute(RouteMatcher<R>... routes)
routes
- The RouteMatcher
s of the routes to be removed.true
if at least one of the routes was found and removed.protected Pair<Context,H> getBestRoute(Context context, R request) throws IncomparableRouteMatchException
context
- The request context.request
- The request to be matched against the registered routes.Pair
containing the decorated Context
and the
handler which is the best match for the given request or null
if
no route was found.IncomparableRouteMatchException
- If any of the registered
RouteMatcher
s could not be compared to one another.protected Pair<Context,H> getBestApiRoute(Context context, R request) throws IncomparableRouteMatchException
AbstractRouter.getBestRoute(Context, Object)
in that it
also checks to see whether this router itself is being addressed, and returns the merged descriptor for all the
routes if so.context
- The request context.request
- The request to be matched against the registered routes.Pair
containing the decorated Context
and the
handler which is the best match for the given request or null
if
no route was found.IncomparableRouteMatchException
- If any of the registered
RouteMatcher
s could not be compared to one another.protected abstract Pair<RouteMatcher<R>,H> getSelfApiHandler()
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.public D api(ApiProducer<D> producer)
Describable
api
in interface Describable<D,R>
producer
- The API producer that provides general information to be built into the descriptor.protected abstract RouteMatcher<R> uriMatcher(RoutingMode mode, String pattern)
<R>
.mode
- The routing mode.pattern
- The pattern.public D handleApiRequest(Context context, R request)
Describable
handleApiRequest
in interface Describable<D,R>
context
- The request context.request
- The request.public void addDescriptorListener(Describable.Listener listener)
Describable
addDescriptorListener
in interface Describable<D,R>
listener
- The listener.public void removeDescriptorListener(Describable.Listener listener)
Describable
removeDescriptorListener
in interface Describable<D,R>
listener
- The listener.public void notifyDescriptorChange()
Describable.Listener
notifyDescriptorChange
in interface Describable.Listener
protected D buildApi(ApiProducer<D> producer)
ApiProducer
.producer
- The given ApiProducer to use.Copyright © 2010-2018, ForgeRock All Rights Reserved.