Class ControllerHooks

java.lang.Object
com.codename1.backend.web.ControllerHooks
All Implemented Interfaces:
HttpServer.Handler

public final class ControllerHooks extends Object implements HttpServer.Handler

The interceptors and global exception resolvers every generated router runs a matched request through.

The generated wiring builds one from the application's HandlerInterceptor beans and @ControllerAdvice beans and hands it to each router. A router whose controller declares no exception handler and whose route validates nothing skips it entirely while it isEmpty(), so an application with none of these pays one field read per request.

Exception handling precedence
  1. A failure security owns -- AccessDeniedException or AuthenticationException, CSRF failures included -- and any Error are never offered to a handler. They go straight back to the server and the security chain, so a broad @ExceptionHandler(Exception.class) cannot turn a refusal into a success.
  2. The controller's own @ExceptionHandler methods; among those that apply, the one declaring the closest superclass of what was thrown.
  3. Each @ControllerAdvice, in @Order order (unordered ones last, by bean name), each again choosing its closest match. The first that answers wins.
  4. A ValidationException nothing handled answers 400 with its field errors as JSON.
  5. Anything else is rethrown, and the server answers it as before: the status of a ResponseStatusException, otherwise a 500 that reveals nothing about the failure.
  • Constructor Details

    • ControllerHooks

      public ControllerHooks(HandlerInterceptor[] interceptors, ExceptionResolver[] resolvers)
      Null entries are dropped: a conditional bean that was not created is null in the wiring, and leaving the hole in would fail every request.
      Parameters:
      interceptors - in the order they run
      resolvers - the global resolvers, in the order they are asked
  • Method Details

    • none

      public static ControllerHooks none()
      No interceptors and no global resolvers.
      Returns:
      the shared empty instance
    • isEmpty

      public boolean isEmpty()
      Whether there is nothing to run.
      Returns:
      true with no interceptors and no global resolvers
    • handle

      public HttpServer.Response handle(HttpServer.Request request)
      Never a router: always null. The wiring hands these hooks to the server among its handlers, and the server takes them out of the chain and asks notFound(HttpServer.Request) only once every router and static file has passed.
      Specified by:
      handle in interface HttpServer.Handler
      Parameters:
      request - the request
      Returns:
      null
    • hasResolvers

      public boolean hasResolvers()
      Whether any global resolver could answer an unmatched path.
      Returns:
      true when a @ControllerAdvice or resolver bean exists
    • notFound

      public HttpServer.Response notFound(HttpServer.Request request) throws Exception
      The answer for a request nothing served: the global resolvers are offered a NoHandlerFoundException, with a null handler. Null when none answers, which leaves the server's own 404. Security failures and Errors are not asked here, as nothing threw them.
      Parameters:
      request - the request
      Returns:
      the response, or null
      Throws:
      Exception - what a resolver threw
    • invoke

      public HttpServer.Response invoke(HttpServer.Request request, HandlerMethod handler, ControllerHooks.Invocation call, int route, String[] bound, ExceptionResolver local) throws Exception
      Runs one matched route through the interceptors and exception handlers.
      Parameters:
      request - the request
      handler - the route it matched
      call - the router
      route - the route's index in the router
      bound - the path variables, or null
      local - the controller's own exception handlers, or null
      Returns:
      the response
      Throws:
      Exception - a failure nothing handled