Class DispatchHandler

  • All Implemented Interfaces:
    Handler

    public class DispatchHandler
    extends Object
    implements Handler
    Dispatches to one of a list of handlers. When a request is handled, each handler's condition is evaluated. If a condition expression yields true, then the request is dispatched to the associated handler with no further processing.

    If no condition yields true then the handler will return a 404 not found response. Therefore, it's advisable to have a single "default" handler at the end of the list with no condition (unconditional) to handle otherwise un-dispatched requests.

    • Constructor Detail

      • DispatchHandler

        public DispatchHandler()
    • Method Detail

      • addBinding

        public DispatchHandler addBinding​(Expression<Boolean> condition,
                                          Handler handler,
                                          Expression<String> baseURI)
        Binds an expression to the current handler to dispatch to.
        Parameters:
        condition - Condition to evaluate to determine if associated handler should be dispatched to. If omitted, then dispatch is unconditional.
        handler - The name of the handler heap object to dispatch to if the associated condition yields true.
        baseURI - Overrides the existing request URI, making requests relative to a new base URI. Only scheme, host and port are used in the supplied URI. Default: leave URI untouched.
        Returns:
        The current dispatch handler.
      • addUnconditionalBinding

        public DispatchHandler addUnconditionalBinding​(Handler handler,
                                                       Expression<String> baseURI)
        Adds an unconditional bindings to the handler.
        Parameters:
        handler - The name of the handler heap object to dispatch to if the associated condition yields true.
        baseURI - Overrides the existing request URI, making requests relative to a new base URI. Only scheme, host and port are used in the supplied URI. Default: leave URI untouched.
        Returns:
        The current dispatch handler.
      • handle

        public Promise<Response,​NeverThrowsException> handle​(Context context,
                                                                   Request request)
        Description copied from interface: Handler
        Returns a Promise representing the asynchronous Response of the given request. If any (asynchronous) processing goes wrong, the promise still contains a Response (probably from the 4xx or 5xx status code family).

        A handler that doesn't hand-off the processing to another downstream handler is responsible for creating the response.

        The returned Promise contains the response returned from the server as-is. This is responsibility of the handler to produce the appropriate error response (404, 500, ...) in case of processing error.

        Note: As of Promise 2.0 implementation, it is not permitted to throw any runtime exception here. Doing so produce unexpected behaviour (most likely a server-side hang of the processing thread).

        Specified by:
        handle in interface Handler
        Parameters:
        context - The request context.
        request - The request.
        Returns:
        A Promise representing the response to be returned to the caller.