Class Handlers


  • public abstract class Handlers
    extends java.lang.Object
    Factory methods for handler decorations.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static Handler accepts​(java.lang.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.
      static Handler byContent​(Registry registry, Action<? super ByContentSpec> action)
      Builds a content negotiating handler.
      static Handler byMethod​(Registry registry, Action<? super ByMethodSpec> action)
      Builds a multi method handler.
      static Handler chain​(java.util.List<? extends Handler> handlers)
      Creates a handler chain from the given handlers.
      static Handler chain​(Handler... handlers)
      Creates a handler chain from the given handlers.
      static Handler chain​(Registry registry, Action<? super Chain> action)
      Builds a chain, backed by the given registry.
      static Handler chain​(ServerConfig serverConfig, Action<? super Chain> action)
      Builds a handler chain, with no backing registry.
      static Handler chain​(ServerConfig serverConfig, Registry registry, Action<? super Chain> action)
      Builds a chain, backed by the given registry.
      static Handler clientError​(int statusCode)
      A handler that simply calls Context.clientError(int) with the given status code.
      static Handler contentTypes​(java.lang.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.
      static Handler delete()
      A handler that delegates to the next handler if the request is DELETE, otherwise raises a 405 client error.
      static Handler files​(ServerConfig serverConfig, Action<? super FileHandlerSpec> config)
      Creates a handler that serves files from the file system.
      static Handler fileSystem​(ServerConfig serverConfig, java.lang.String path, Handler handler)
      A handlers that changes the FileSystemBinding for the given handlers.
      static Handler get()
      A handler that delegates to the next handler if the request is GET, otherwise raises a 405 client error.
      static Handler next()
      A handler that simply delegates to the next handler.
      static Handler notFound()
      Convenience for clientError(404).
      static Handler of​(Block block)
      Creates a handler from the given block
      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.
      static Handler options()
      A handler that delegates to the next handler if the request is OPTIONS, otherwise raises a 405 client error.
      static Handler patch()
      A handler that delegates to the next handler if the request is PATCH, otherwise raises a 405 client error.
      static Handler path​(java.lang.String path, Handler handler)
      Creates a handler that delegates to the given handlers if the request matches the given path exactly.
      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.
      static Handler post()
      A handler that delegates to the next handler if the request is POST, otherwise raises a 405 client error.
      static Handler prefix​(java.lang.String prefix, Handler handler)
      Creates a handler that delegates to the given handlers if the request path starts with the given prefix.
      static Handler put()
      A handler that delegates to the next handler if the request is PUT, otherwise raises a 405 client error.
      static Handler redirect​(int code, java.lang.String location)
      Creates a handler that always issues a redirect using Context.redirect(int, String) with exactly the given code and location.
      static Handler register​(Registry registry)
      A handler that simply calls Context.next(Registry) with the given registry.
      static Handler register​(Registry registry, Handler handler)
      A handler that simply calls Context.insert(Registry, Handler...) with the given registry and handler.
      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.
      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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • accepts

        public static Handler accepts​(java.lang.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 java.lang.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:
        java.lang.Exception - any thrown by config
      • chain

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

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

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

        public static Handler chain​(java.util.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​(java.lang.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,
                                         java.lang.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​(java.lang.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​(java.lang.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
      • redirect

        public static Handler redirect​(int code,
                                       java.lang.String location)
        Creates a handler that always issues a redirect using Context.redirect(int, String) with exactly the given code and location.

        This method will immediate throw an IllegalArgumentException if code is < 300 || > 399.

        Parameters:
        code - the 3XX HTTP status code
        location - the URL to set in the Location response header
        Returns:
        a handler
        See Also:
        Context.redirect(int, String)
      • 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 java.lang.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:
        java.lang.Exception - any thrown by action
        Since:
        1.5
      • byMethod

        public static Handler byMethod​(Registry registry,
                                       Action<? super ByMethodSpec> action)
                                throws java.lang.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:
        java.lang.Exception - any thrown by action
        Since:
        1.5