Class Handlers


  • public abstract class Handlers
    extends Object
    Factory methods for handler decorations.
    • Method Detail

      • accepts

        public static Handler accepts​(String... contentTypes)
        A handler that delegates to the next handler if the request claims that it can accept one of the given types, otherwise raises a 406 client error.
        Parameters:
        contentTypes - The content types to verify that the request can support for the response
        Returns:
        A handler
      • files

        public static Handler files​(ServerConfig serverConfig,
                                    Action<? super FileHandlerSpec> config)
                             throws Exception
        Creates a handler that serves files from the file system.

        This method is a standalone version of Chain.files(Action).

        Parameters:
        serverConfig - the server config
        config - the configuration of the file handler
        Returns:
        a file serving handler
        Throws:
        Exception - any thrown by config
      • chain

        public static Handler chain​(ServerConfig serverConfig,
                                    Action<? super Chain> action)
                             throws Exception
        Builds a handler chain, with no backing registry.
        Parameters:
        serverConfig - The server config
        action - The chain definition
        Returns:
        A handler
        Throws:
        Exception - any thrown by action
      • chain

        public static Handler chain​(Registry registry,
                                    Action<? super Chain> action)
                             throws Exception
        Builds a chain, backed by the given registry.
        Parameters:
        registry - The registry.
        action - The chain building action.
        Returns:
        A handler
        Throws:
        Exception - any thrown by action
      • chain

        public static Handler chain​(List<? extends Handler> handlers)
        Creates a handler chain from the given handlers.
        Parameters:
        handlers - The handlers to connect into a chain
        Returns:
        A new handler that is the given handlers connected into a chain
      • chain

        public static Handler chain​(Handler... handlers)
        Creates a handler chain from the given handlers.
        Parameters:
        handlers - The handlers to connect into a chain
        Returns:
        A new handler that is the given handlers connected into a chain
      • clientError

        public static Handler clientError​(int statusCode)
        A handler that simply calls Context.clientError(int) with the given status code.
        Parameters:
        statusCode - The 4xx client error status code
        Returns:
        A handler
      • contentTypes

        public static Handler contentTypes​(String... contentTypes)
        A handler that delegates to the next handler if the content type of the request is one of the given types, otherwise raises a 415 client error.
        Parameters:
        contentTypes - The request content types to require
        Returns:
        A handler
      • delete

        public static Handler delete()
        A handler that delegates to the next handler if the request is DELETE, otherwise raises a 405 client error.
        Returns:
        A handler
      • fileSystem

        public static Handler fileSystem​(ServerConfig serverConfig,
                                         String path,
                                         Handler handler)
        A handlers that changes the FileSystemBinding for the given handlers.

        The new file system binding will be created by the FileSystemBinding.binding(String) method of the contextual binding.

        Parameters:
        serverConfig - The application server config
        path - The relative path to the new file system binding point
        handler - The handler to execute with the new file system binding
        Returns:
        A handler
      • get

        public static Handler get()
        A handler that delegates to the next handler if the request is GET, otherwise raises a 405 client error.
        Returns:
        A handler
      • next

        public static Handler next()
        A handler that simply delegates to the next handler.

        Effectively a noop.

        Returns:
        A handler
      • patch

        public static Handler patch()
        A handler that delegates to the next handler if the request is PATCH, otherwise raises a 405 client error.
        Returns:
        A handler
      • options

        public static Handler options()
        A handler that delegates to the next handler if the request is OPTIONS, otherwise raises a 405 client error.
        Returns:
        A handler
        Since:
        1.1
      • path

        public static Handler path​(String path,
                                   Handler handler)
        Creates a handler that delegates to the given handlers if the request matches the given path exactly.

        The path is relative to the contextual PathBinding of the exchange.

        A new contextual PathBinding will be established for the given handlers, using the given path as the bind point.

        Parameters:
        path - The exact path to match to
        handler - The handlers to delegate to if the path matches
        Returns:
        A handler
      • path

        public static Handler path​(PathBinder pathBinder,
                                   Handler handler)
        Creates a handler that delegates to the given handlers if the request can be bound by the given path binder.
        Parameters:
        pathBinder - The path binder that may bind to the request path
        handler - The handlers to delegate to if path binder does bind to the path
        Returns:
        A handler
      • post

        public static Handler post()
        A handler that delegates to the next handler if the request is POST, otherwise raises a 405 client error.
        Returns:
        A handler
      • prefix

        public static Handler prefix​(String prefix,
                                     Handler handler)
        Creates a handler that delegates to the given handlers if the request path starts with the given prefix.

        The prefix is relative to the contextual PathBinding of the exchange.

        A new contextual PathBinding will be established for the given handlers, using the given prefix as the bind point.

        Parameters:
        prefix - The path prefix to match
        handler - The handler to delegate to
        Returns:
        A handler
      • put

        public static Handler put()
        A handler that delegates to the next handler if the request is PUT, otherwise raises a 405 client error.
        Returns:
        A handler
      • when

        public static Handler when​(Predicate<? super Context> test,
                                   Handler handler)
        Creates a handler that inserts and delegates the given handler if the predicate applies to the context.

        If the predicate does not apply, calls Context.next().

        Parameters:
        test - the test whether to when to the given handler
        handler - the handler to insert if the predicate applies
        Returns:
        a handler
      • whenOrElse

        public static Handler whenOrElse​(Predicate<? super Context> test,
                                         Handler ifHandler,
                                         Handler elseHandler)
        Creates a handler that inserts and delegates to the appropriate handler depending if the predicate applies to the context.

        Parameters:
        test - the test whether to delegate to the appropriate handler
        ifHandler - the handler to insert if the predicate applies
        elseHandler - the handler to insert if the predicate doesn't apply
        Returns:
        a handler
        Since:
        1.5
      • onlyIf

        public static Handler onlyIf​(Predicate<? super Context> test,
                                     Handler handler)
        Creates a handler that delegates to the given handler if the predicate applies to the context.

        If the predicate does not apply, calls Context.next().

        This method does not insert the handler as when(Predicate, Handler) does; it calls its Handler.handle(Context) method directly

        Parameters:
        test - the test whether to when to the given handler
        handler - the handler to call if the predicate applies
        Returns:
        a handler
      • of

        public static Handler of​(Block block)
        Creates a handler from the given block

        The created handler simply invokes the block.

        Parameters:
        block - the block to invoke
        Returns:
        a handler
        Since:
        1.5
      • byContent

        public static Handler byContent​(Registry registry,
                                        Action<? super ByContentSpec> action)
                                 throws Exception
        Builds a content negotiating handler.
        Parameters:
        registry - the registry to obtain handlers from for by-class lookups
        action - the spec action
        Returns:
        a content negotiating handler
        Throws:
        Exception - any thrown by action
        Since:
        1.5
      • byMethod

        public static Handler byMethod​(Registry registry,
                                       Action<? super ByMethodSpec> action)
                                throws Exception
        Builds a multi method handler.
        Parameters:
        registry - the registry to obtain handlers from for by-class lookups
        action - the spec action
        Returns:
        a multi method handler
        Throws:
        Exception - any thrown by action
        Since:
        1.5