Class Handler.Sequence

All Implemented Interfaces:
Handler, Handler.Collection, Handler.Container, Request.Handler, Container, Destroyable, Dumpable, Dumpable.DumpableContainer, LifeCycle, Invocable
Direct Known Subclasses:
ContextHandlerCollection
Enclosing interface:
Handler

@ManagedObject public static class Handler.Sequence extends Handler.AbstractContainer implements Handler.Collection

A Handler.Container that contains an ordered list of children Handlers whose Request.Handler.handle(Request, Response, Callback) method is invoked in sequence on each child until a child returns true.

  • Constructor Details

    • Sequence

      public Sequence(Handler... handlers)

      Creates a Sequence with the given Handlers.

      The created sequence is dynamic only if the Handler array is null or empty.

      Parameters:
      handlers - the Handlers of this Sequence
    • Sequence

      public Sequence(List<Handler> handlers)

      Creates a Sequence with the given Handlers.

      The created sequence is dynamic only if the Handler list is null or empty.

      Parameters:
      handlers - the Handlers of this Sequence
    • Sequence

      public Sequence(boolean dynamic, List<Handler> handlers)

      Creates a Sequence with the given dynamic parameter and the given Handlers.

      Parameters:
      dynamic - whether this Sequence is dynamic
      handlers - the Handlers of this Sequence
  • Method Details

    • handle

      public boolean handle(Request request, Response response, Callback callback) throws Exception
      Description copied from interface: Request.Handler

      Invoked to handle the passed HTTP request and response.

      The request is accepted by returning true, then handling must be concluded by completing the passed callback. The handling may be asynchronous, i.e. this method may return true and complete the given callback later, possibly from a different thread. If this method returns false, then the callback must not be invoked and any mutation on the response reversed.

      Exceptions thrown by this method may be subsequently handled by an error Request.Handler, if present, otherwise a default HTTP 500 error is generated and the callback completed while writing the error response.

      The simplest implementation is:

       public boolean handle(Request request, Response response, Callback callback)
       {
           callback.succeeded();
           return true;
       }
       

      A HelloWorld implementation is:

       public boolean handle(Request request, Response response, Callback callback)
       {
           response.write(true, ByteBuffer.wrap("Hello World\n".getBytes(StandardCharsets.UTF_8)), callback);
           return true;
       }
       
      Specified by:
      handle in interface Request.Handler
      Parameters:
      request - the HTTP request to handle
      response - the HTTP response to handle
      callback - the callback to complete when the handling is complete
      Returns:
      True if and only if the request will be handled, a response generated and the callback eventually called. This may occur within the scope of the call to this method, or asynchronously some time later. If false is returned, then this method must not generate a response, nor complete the callback.
      Throws:
      Exception - if there is a failure during the handling. Catchers cannot assume that the callback will be called and thus should attempt to complete the request as if a false had been returned.
      See Also:
    • getHandlers

      public List<Handler> getHandlers()
      Specified by:
      getHandlers in interface Handler.Container
      Returns:
      an immutable collection of Handlers directly contained by this Handler.
    • setHandlers

      public void setHandlers(List<Handler> handlers)
      Description copied from interface: Handler.Collection

      Sets the given Handlers as children of this collection of Handlers.

      The list is copied and any subsequent modification to the list does not have any effect on this Handler.

      Any existing children Handler is removed.

      Specified by:
      setHandlers in interface Handler.Collection
      Parameters:
      handlers - the Handler to set as children
    • getInvocationType

      public Invocable.InvocationType getInvocationType()
      Specified by:
      getInvocationType in interface Invocable
      Specified by:
      getInvocationType in interface Request.Handler
      Overrides:
      getInvocationType in class Handler.AbstractContainer
      Returns:
      The InvocationType of this object
    • newHandlers

      protected List<Handler> newHandlers(List<Handler> handlers)