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.public abstract class AbstractRouter<T extends AbstractRouter<T,R,H>,R,H> extends Object
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.
Modifier | Constructor and Description |
---|---|
protected |
AbstractRouter()
Creates a new abstract router with no routes defined.
|
protected |
AbstractRouter(AbstractRouter<T,R,H> 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.
|
T |
addRoute(RouteMatcher<R> matcher,
H handler)
Adds a new route to this router for the provided handler.
|
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 abstract T |
getThis()
Returns this
AbstractRouter instance, typed correctly. |
T |
removeAllRoutes()
Removes all of the routes from this router.
|
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 AbstractRouter()
protected AbstractRouter(AbstractRouter<T,R,H> router)
router
- The router to be copied.protected abstract T getThis()
AbstractRouter
instance, typed correctly.AbstractRouter
instance.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.Copyright 2011-2015 ForgeRock AS.