Package com.jsgalactic.axiom.routing
Interface RouteGroup
- All Known Subinterfaces:
Application
public interface RouteGroup
Registers routes and middleware for a scope: the whole application, or a group of routes under a
path prefix created with
group(String, Consumer).
For a matched route the chain runs global middleware, then the middleware of each enclosing
group from the outermost to the innermost, then the route's own middleware, then the handler.
use(Middleware) applies to every route of its scope wherever it is called; middleware of
one scope run in use order. Startup composes each route's chain once.
Lifecycle. Registration is allowed only before the application starts and fails with
IllegalStateException afterwards. A group passed to a group callback is valid
only until that callback returns. Registration is thread-safe; middleware and handlers are shared
across concurrent requests and must be thread-safe.
-
Method Summary
Modifier and TypeMethodDescriptiondefault Routedelete(String path, Handler handler, Middleware... middleware) Registers a DELETE route; seeroute(java.lang.String, java.lang.String, com.jsgalactic.axiom.context.Handler, com.jsgalactic.axiom.context.Middleware...).<E extends Exception>
RouteGrouperror(Class<E> type, ErrorHandler<? super E> handler) Maps exceptions of a class and its subclasses to responses for the routes of this scope and its nested groups, with the same rules asApplication.error(java.lang.Class<E>, com.jsgalactic.axiom.context.ErrorHandler<? super E>).default Routeget(String path, Handler handler, Middleware... middleware) group(String prefix, Consumer<RouteGroup> configure) Creates a nested group and configures it immediately on the calling thread.default Routehead(String path, Handler handler, Middleware... middleware) default Routeoptions(String path, Handler handler, Middleware... middleware) Registers an OPTIONS route; seeroute(java.lang.String, java.lang.String, com.jsgalactic.axiom.context.Handler, com.jsgalactic.axiom.context.Middleware...).default Routepatch(String path, Handler handler, Middleware... middleware) default Routepost(String path, Handler handler, Middleware... middleware) default Routeput(String path, Handler handler, Middleware... middleware) route(String method, String path, Handler handler, Middleware... middleware) Registers a route in this scope.use(Middleware middleware) Adds middleware for every route of this scope, including routes registered before this call and routes of nested groups.
-
Method Details
-
route
Registers a route in this scope. In a group the template is the group's prefix followed bypath, which is empty (the prefix itself) or starts with/; the composed template is validated by the ordinary routing rules.- Parameters:
method- case-sensitive HTTP method tokenpath- path template, relative to the group's prefixhandler- callback invoked for matching requestsmiddleware- route-level middleware, outermost first, run after group middleware- Returns:
- the registered route identity, with the composed template
- Throws:
IllegalArgumentException- for an invalid method or template, or a conflicting routeIllegalStateException- if configuration has ended
-
get
Registers a GET route; seeroute(java.lang.String, java.lang.String, com.jsgalactic.axiom.context.Handler, com.jsgalactic.axiom.context.Middleware...).- Parameters:
path- path templatehandler- callbackmiddleware- route-level middleware- Returns:
- registered route
-
post
Registers a POST route; seeroute(java.lang.String, java.lang.String, com.jsgalactic.axiom.context.Handler, com.jsgalactic.axiom.context.Middleware...).- Parameters:
path- path templatehandler- callbackmiddleware- route-level middleware- Returns:
- registered route
-
put
Registers a PUT route; seeroute(java.lang.String, java.lang.String, com.jsgalactic.axiom.context.Handler, com.jsgalactic.axiom.context.Middleware...).- Parameters:
path- path templatehandler- callbackmiddleware- route-level middleware- Returns:
- registered route
-
patch
Registers a PATCH route; seeroute(java.lang.String, java.lang.String, com.jsgalactic.axiom.context.Handler, com.jsgalactic.axiom.context.Middleware...).- Parameters:
path- path templatehandler- callbackmiddleware- route-level middleware- Returns:
- registered route
-
delete
Registers a DELETE route; seeroute(java.lang.String, java.lang.String, com.jsgalactic.axiom.context.Handler, com.jsgalactic.axiom.context.Middleware...).- Parameters:
path- path templatehandler- callbackmiddleware- route-level middleware- Returns:
- registered route
-
head
Registers a HEAD route; seeroute(java.lang.String, java.lang.String, com.jsgalactic.axiom.context.Handler, com.jsgalactic.axiom.context.Middleware...). Without one, HEAD uses the GET route of the same template, including its middleware.- Parameters:
path- path templatehandler- callbackmiddleware- route-level middleware- Returns:
- registered route
-
options
Registers an OPTIONS route; seeroute(java.lang.String, java.lang.String, com.jsgalactic.axiom.context.Handler, com.jsgalactic.axiom.context.Middleware...). Without one, OPTIONS for a routed path is answered automatically with 204 and an Allow header; an explicit route replaces that answer for the paths it matches.- Parameters:
path- path templatehandler- callbackmiddleware- route-level middleware- Returns:
- registered route
-
use
Adds middleware for every route of this scope, including routes registered before this call and routes of nested groups. On the application it is global: it also wraps the answers the router produces itself (404, 405, automatic OPTIONS, 501 for an unrecognized method), for whichctx.matchedRoute()is empty andctx.route()throwsIllegalStateException.- Parameters:
middleware- shared, thread-safe middleware- Returns:
- this scope
- Throws:
IllegalStateException- if configuration has ended
-
group
Creates a nested group and configures it immediately on the calling thread. Its prefix is appended to this scope's prefix. Registration is atomic: ifconfigurethrows, everything it registered (routes and their admission policies, nested groups, middleware) is removed before the exception propagates, so a partly configured group never serves routes without middleware a later statement would have added. While the callback runs,start()fails withIllegalStateException.- Parameters:
prefix- appended to this scope's prefix: empty for a group that only scopes middleware (its routes keep this scope's prefix), or a template prefix starting with/that does not end with/and has no wildcard; it may declare parameters such as/users/:id, whose names must not repeat those of enclosing prefixesconfigure- registers the group's routes, middleware and nested groups- Returns:
- this scope
- Throws:
NullPointerException- ifprefixorconfigureis nullIllegalArgumentException- for an invalid prefixIllegalStateException- if configuration has ended
-
error
Maps exceptions of a class and its subclasses to responses for the routes of this scope and its nested groups, with the same rules asApplication.error(java.lang.Class<E>, com.jsgalactic.axiom.context.ErrorHandler<? super E>). For a request, the handlers of the group that owns the matched route are consulted first, then those of each enclosing group, then the application's: the first scope with a handler for the exception's class or a superclass wins, even if an outer scope has a nearer class. Everything that runs for the request uses the route's scopes, including global middleware. The built-in mapping ofAxiomExceptioncounts as a handler at the application level, so a group's handler forExceptiondoes not replace it. Requests no route serves (404, 405, OPTIONS, 501) have no group; only the application's handlers apply to exceptions of their custom handlers and global middleware. A handler registered in a group that fails to configure is removed with the group, like its routes.- Type Parameters:
E- exception type- Parameters:
type- exception classhandler- shared, thread-safe mapping- Returns:
- this scope
- Throws:
IllegalArgumentException- if this scope already has a handler for the classIllegalStateException- if configuration has ended
-