Interface Middleware
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
app.use((ctx, next) -> next.run().withHeader("X-Content-Type-Options", "nosniff"));
Middleware.Next.run() runs the rest of the chain (further middleware, then the handler) and
returns its response; exceptions from the rest of the chain are thrown from it. A middleware that
returns without calling it short-circuits: the handler does not run and the returned response is
used.
Error responses. When the handler or an inner middleware throws, the exception passes
through every middleware that is on the stack, so each can still observe or translate it, and is
mapped to a response only afterwards (problem response, error handler response, or 406). That
response does not come back through next.run(); instead each middleware that was entered
gets to decorate it through afterError(com.jsgalactic.axiom.context.Context, com.jsgalactic.axiom.http.Response), innermost first. A security-header middleware
therefore implements both methods:
Middleware headers = new Middleware() {
public Response handle(Context ctx, Next next) throws Exception { return secure(next.run()); }
public Response afterError(Context ctx, Response response) { return secure(response); }
};
Lifecycle and ownership. Middleware are registered before startup with use or
as route-level arguments, and the application holds the instance until it closes. Startup
composes the chain of every route once; no chain is built per request.
Thread safety. One instance serves every request, concurrently. Implementations must be
thread-safe and keep per-request state in local variables. A middleware runs on the request's
thread inside the admitted task, after admission and under the request deadline; a timeout or
cancellation interrupts it like a handler. The context and next follow the handler's
confinement rules: use them only on that thread and only until this method returns. Exceptions it
throws are handled exactly like handler exceptions.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceThe rest of a middleware chain for one request. -
Method Summary
Modifier and TypeMethodDescriptiondefault ResponseafterError(Context context, Response response) Decorates a response that was produced from an exception: a problem response for anAxiomException, the response of an error handler, the generic 500, or the 406 for an unacceptable response type.handle(Context context, Middleware.Next next) Handles one request, usually by callingnext.run().
-
Method Details
-
handle
Handles one request, usually by callingnext.run().- Parameters:
context- the request-scoped context shared with the handler; status settings made here apply to the handler's result mappingnext- the rest of the chain- Returns:
- the response to use; never null
- Throws:
Exception- to fail the request as a handler exception would
-
afterError
Decorates a response that was produced from an exception: a problem response for anAxiomException, the response of an error handler, the generic 500, or the 406 for an unacceptable response type. The default returns the response unchanged.It is called once per such response for each middleware that was entered when the failure happened (a middleware an outer one short-circuited before was never entered), innermost first, after error mapping and before the response leaves
app.handle. It is not called for responses that come back throughMiddleware.Next.run(), including responses a middleware or handler builds itself, nor for the router's 404, 405 and 501 answers (those flow through global middleware as ordinary responses), nor for failures before routing (413, CONNECT) and listener errors. An exception that nothing maps, and a request that was cancelled or timed out, never reach it.Like
handle(com.jsgalactic.axiom.context.Context, com.jsgalactic.axiom.context.Middleware.Next), it runs on the request's thread under the request deadline and must be thread-safe. It must not throw: an exception from it is not mapped again. AnAxiomExceptionanswers with its own undecorated problem response and anything else fails the request as an unexpected exception.- Parameters:
context- the failed request's contextresponse- the response produced from the exception, never null- Returns:
- the response to pass outwards; never null
-