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> |
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 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 RouteMatchers 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
RouteMatchers could not be compared to one another.public D api(ApiProducer<D> producer)
Describableapi in interface Describable<D,R>producer - The API producer that provides general information to be built into the descriptor.protected D buildApi(ApiProducer<D> producer)
ApiProducer.producer - The given ApiProducer to use.protected abstract RouteMatcher<R> uriMatcher(RoutingMode mode, String pattern)
<R>.mode - The routing mode.pattern - The pattern.public D handleApiRequest(Context context, R request)
DescribablehandleApiRequest in interface Describable<D,R>context - The request context.request - The request.public void addDescriptorListener(Describable.Listener listener)
DescribableaddDescriptorListener in interface Describable<D,R>listener - The listener.public void removeDescriptorListener(Describable.Listener listener)
DescribableremoveDescriptorListener in interface Describable<D,R>listener - The listener.public void notifyDescriptorChange()
Describable.ListenernotifyDescriptorChange in interface Describable.ListenerCopyright © 2025 Open Identity Platform Community. All rights reserved.