Class MappedInterceptor

java.lang.Object
com.codename1.backend.web.MappedInterceptor
All Implemented Interfaces:
HandlerInterceptor

public final class MappedInterceptor extends Object implements HandlerInterceptor

An interceptor limited to some paths, as Spring's registry.addInterceptor(x).addPathPatterns(...).excludePathPatterns(...).

Declare it as the bean in place of the interceptor it wraps:

@Bean
HandlerInterceptor audit() {
    return new MappedInterceptor(new AuditInterceptor())
            .addPathPatterns("/api/**")
            .excludePathPatterns("/api/health");
}

A pattern is a path in which * matches within one segment and a final /** matches the rest of the path, the prefix itself included. With no include pattern every path is included. Paths are matched as the routers match them, without the query and with unreserved escapes decoded.

  • Constructor Details

    • MappedInterceptor

      public MappedInterceptor(HandlerInterceptor delegate)
      Limits delegate to the patterns added next.
      Parameters:
      delegate - the interceptor to run on matching paths
  • Method Details

    • addPathPatterns

      public MappedInterceptor addPathPatterns(String... patterns)
      Runs the interceptor on these paths.
      Parameters:
      patterns - path patterns
      Returns:
      this interceptor
    • excludePathPatterns

      public MappedInterceptor excludePathPatterns(String... patterns)
      Never runs the interceptor on these paths, even when included.
      Parameters:
      patterns - path patterns
      Returns:
      this interceptor
    • matches

      public boolean matches(String path)
      Whether the interceptor runs for path.
      Parameters:
      path - a request path, without its query
      Returns:
      true when it is included and not excluded
    • preHandle

      public HttpServer.Response preHandle(HttpServer.Request request, HandlerMethod handler) throws Exception
      Description copied from interface: HandlerInterceptor
      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 HandlerInterceptor.afterCompletion(HttpServer.Request, HandlerMethod, HttpServer.Response, Throwable) runs only for the interceptors before this one.
      Specified by:
      preHandle in interface HandlerInterceptor
      Parameters:
      request - the request
      handler - 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 void postHandle(HttpServer.Request request, HandlerMethod handler, HttpServer.Response response) throws Exception
      Description copied from interface: HandlerInterceptor
      Called after the controller method returned normally; not after a refusal, and not after an exception, handled or not.
      Specified by:
      postHandle in interface HandlerInterceptor
      Parameters:
      request - the request
      handler - the route it matched
      response - the response about to be written
      Throws:
      Exception - fails the request
    • afterCompletion

      public void afterCompletion(HttpServer.Request request, HandlerMethod handler, HttpServer.Response response, Throwable failure)
      Description copied from interface: HandlerInterceptor
      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.
      Specified by:
      afterCompletion in interface HandlerInterceptor
      Parameters:
      request - the request
      handler - the route it matched
      response - the response, or null when the request failed unhandled
      failure - what failed and was not handled, or null