Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.

public interface HandlerInterceptor

Known subtypesMappedInterceptor

Runs around every controller method, for cross-cutting work that is not security: timing, auditing, tenant selection, request-scoped cleanup.

Declare one as a bean – a @Component class implementing this interface, or a @Bean method returning one – and the build hands it to every generated router. Several run in @Order order, and the ones without an order last in bean-name order.

Where it runs

  1. The security filter chain: authentication, CSRF, the URL rules. A request it refuses never reaches an interceptor.
  2. Route selection. Only a request that matched a controller route is intercepted; a 404 is not.
  3. preHandle, in order.
  4. Binding, validation and the controller method, with its method security.
  5. Exception handlers, when 3 or 4 threw.
  6. postHandle, in reverse order, only when the controller method returned normally.
  7. afterCompletion, in reverse order, for every interceptor whose preHandle let the request through – whatever happened after it.

Because the chain sits inside the security layer, an interceptor cannot admit a request security refused, and the failures security owns – AccessDeniedException, AuthenticationException and their subclasses, CSRF included – pass every interceptor and exception handler untouched on their way back to it.

Methods

public default HttpServer.Response preHandle(HttpServer.Request request, HandlerMethod handler) throws ExceptionCalled before binding.
public default void postHandle(HttpServer.Request request, HandlerMethod handler, HttpServer.Response response) throws ExceptionCalled after the controller method returned normally; not after a refusal, and not after an exception, handled or not.
public default void afterCompletion(HttpServer.Request request, HandlerMethod handler, HttpServer.Response response, Throwable failure)Called once the request is finished with, on success, on a refusal by a later interceptor, on a binding or validation failure and on a failure nothing handled.

Method details

preHandle

public default HttpServer.Response preHandle(HttpServer.Request request, HandlerMethod handler) throws Exception
Called before binding. Return null to let the request through, or a response to answer with instead – the controller method is then not called, and afterCompletion runs only for the interceptors before this one.

Parameters

request HttpServer.Request
the request
handler HandlerMethod
the route it matched

Returns

null to proceed, or the response to answer with

Throws

Exception
routed to the exception handlers like the controller’s own

postHandle

public default void postHandle(HttpServer.Request request, HandlerMethod handler, HttpServer.Response response) throws Exception
Called after the controller method returned normally; not after a refusal, and not after an exception, handled or not.

Parameters

request HttpServer.Request
the request
handler HandlerMethod
the route it matched
response HttpServer.Response
the response about to be written

Throws

Exception
fails the request

afterCompletion

public default void afterCompletion(HttpServer.Request request, HandlerMethod handler, HttpServer.Response response, Throwable failure)
Called once the request is finished with, on success, on a refusal by a later interceptor, on a binding or validation failure and on a failure nothing handled. This is the place for cleanup. A throw from here is logged and does not change the response.

Parameters

request HttpServer.Request
the request
handler HandlerMethod
the route it matched
response HttpServer.Response
the response, or null when the request failed unhandled
failure Throwable
what failed and was not handled, or null