Class ResponseDispatch

java.lang.Object
com.yahoo.jdisc.handler.ResponseDispatch
All Implemented Interfaces:
Future<Boolean>
Direct Known Subclasses:
CallableResponseDispatch

public abstract class ResponseDispatch extends Object implements Future<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 Set.of(ByteBuffer.wrap(new byte[] { 6, 9 }));
         }
     }.dispatch(handler);
 }
 
Author:
Simon Thoresen Hult
  • Constructor Details

    • ResponseDispatch

      public ResponseDispatch()
  • Method Details

    • newResponse

      protected abstract Response newResponse()

      Creates and returns the Response to dispatch.

      Returns:
      The Response to dispatch.
    • responseContent

      protected Iterable<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.
    • connect

      public final ContentChannel connect(ResponseHandler responseHandler)

      This methods calls newResponse() to create a new Response, and then calls ResponseHandler.handleResponse(Response) with that.

      Parameters:
      responseHandler - The ResponseHandler to connect to.
      Returns:
      The ContentChannel to write the Response's content to.
    • connectFastWriter

      public final FastContentWriter connectFastWriter(ResponseHandler responseHandler)

      Convenience method for constructing a FastContentWriter over the ContentChannel returned by calling connect(ResponseHandler).

      Parameters:
      responseHandler - The ResponseHandler to connect to.
      Returns:
      The FastContentWriter for the connected Response.
    • dispatch

      public final CompletableFuture<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.
    • cancel

      public final boolean cancel(boolean mayInterruptIfRunning)
      Specified by:
      cancel in interface Future<Boolean>
    • isCancelled

      public final boolean isCancelled()
      Specified by:
      isCancelled in interface Future<Boolean>
    • isDone

      public boolean isDone()
      Specified by:
      isDone in interface Future<Boolean>
    • get

      Specified by:
      get in interface Future<Boolean>
      Throws:
      InterruptedException
      ExecutionException
    • get

      public Boolean get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
      Specified by:
      get in interface Future<Boolean>
      Throws:
      InterruptedException
      ExecutionException
      TimeoutException
    • newInstance

      public static ResponseDispatch newInstance(int responseStatus, 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, Iterable<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, 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, Iterable<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.