Class ResponseDispatch

  • All Implemented Interfaces:
    com.google.common.util.concurrent.ListenableFuture<java.lang.Boolean>, java.util.concurrent.Future<java.lang.Boolean>
    Direct Known Subclasses:
    CallableResponseDispatch

    public abstract class ResponseDispatch
    extends com.google.common.util.concurrent.ForwardingListenableFuture<java.lang.Boolean>

    This class provides a convenient way of safely dispatching a Response. It is similar in use to RequestDispatch, where you need to subclass and implement and override the appropriate methods. Because a Response is not a SharedResource, its construction is less strenuous, and this class is able to provide a handful of convenient factory methods to dispatch the simplest of Responses.

    The following is a simple example on how to use this class without the factories:

     public void signalInternalError(ResponseHandler handler) {
         new ResponseDispatch() {
             @Override
             protected Response newResponse() {
                 return new Response(Response.Status.INTERNAL_SERVER_ERROR);
             }
             @Override
             protected Iterable<ByteBuffer> responseContent() {
                 return Collections.singleton(ByteBuffer.wrap(new byte[] { 6, 9 }));
             }
         }.dispatch(handler);
     }
     
    Author:
    Simon Thoresen Hult
    • Constructor Detail

      • ResponseDispatch

        public ResponseDispatch()
    • Method Detail

      • newResponse

        protected abstract Response newResponse()

        Creates and returns the Response to dispatch.

        Returns:
        The Response to dispatch.
      • responseContent

        protected java.lang.Iterable<java.nio.ByteBuffer> responseContent()

        Returns an Iterable for the ByteBuffers that the dispatch(ResponseHandler) method should write to the Response once it has connected. The default implementation returns an empty list. Because this method uses the Iterable interface, you can provide the ByteBuffers lazily, or as they become available.

        Returns:
        The ByteBuffers to write to the Response's ContentChannel.
      • dispatch

        public final com.google.common.util.concurrent.ListenableFuture<java.lang.Boolean> dispatch​(ResponseHandler responseHandler)

        This method calls connect(ResponseHandler) to establish a ContentChannel for the Response, and then iterates through all the ByteBuffers returned by responseContent() and writes them to that ContentChannel. This method uses a finally block to make sure that the ContentChannel is always closed.

        The returned Future will wait for all CompletionHandlers associated with the Response have been completed.

        Parameters:
        responseHandler - The ResponseHandler to dispatch to.
        Returns:
        A Future that can be waited for.
      • delegate

        protected final com.google.common.util.concurrent.ListenableFuture<java.lang.Boolean> delegate()
        Specified by:
        delegate in class com.google.common.util.concurrent.ForwardingListenableFuture<java.lang.Boolean>
      • cancel

        public final boolean cancel​(boolean mayInterruptIfRunning)
        Specified by:
        cancel in interface java.util.concurrent.Future<java.lang.Boolean>
        Overrides:
        cancel in class com.google.common.util.concurrent.ForwardingFuture<java.lang.Boolean>
      • isCancelled

        public final boolean isCancelled()
        Specified by:
        isCancelled in interface java.util.concurrent.Future<java.lang.Boolean>
        Overrides:
        isCancelled in class com.google.common.util.concurrent.ForwardingFuture<java.lang.Boolean>
      • newInstance

        public static ResponseDispatch newInstance​(int responseStatus,
                                                   java.nio.ByteBuffer... content)

        Factory method for creating a ResponseDispatch with a Response that has the given status code, and ByteBuffer content.

        Parameters:
        responseStatus - The status code of the Response to dispatch.
        content - The ByteBuffer content of the Response, may be empty.
        Returns:
        The created ResponseDispatch.
      • newInstance

        public static ResponseDispatch newInstance​(int responseStatus,
                                                   java.lang.Iterable<java.nio.ByteBuffer> content)

        Factory method for creating a ResponseDispatch with a Response that has the given status code, and collection of ByteBuffer content. Because this method uses the Iterable interface, you can create the ByteBuffers lazily, or provide them as they become available.

        Parameters:
        responseStatus - The status code of the Response to dispatch.
        content - The provider of the Response's ByteBuffer content.
        Returns:
        The created ResponseDispatch.
      • newInstance

        public static ResponseDispatch newInstance​(Response response,
                                                   java.nio.ByteBuffer... content)

        Factory method for creating a ResponseDispatch over a given Response and ByteBuffer content.

        Parameters:
        response - The Response to dispatch.
        content - The ByteBuffer content of the Response, may be empty.
        Returns:
        The created ResponseDispatch.
      • newInstance

        public static ResponseDispatch newInstance​(Response response,
                                                   java.lang.Iterable<java.nio.ByteBuffer> content)

        Factory method for creating a ResponseDispatch over a given Response and ByteBuffer content. Because this method uses the Iterable interface, you can create the ByteBuffers lazily, or provide them as they become available.

        Parameters:
        response - The Response to dispatch.
        content - The provider of the Response's ByteBuffer content.
        Returns:
        The created ResponseDispatch.