Class ResponseDispatch
- java.lang.Object
-
- com.google.common.collect.ForwardingObject
-
- com.google.common.util.concurrent.ForwardingFuture<V>
-
- com.google.common.util.concurrent.ForwardingListenableFuture<java.lang.Boolean>
-
- com.yahoo.jdisc.handler.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 toRequestDispatch
, where you need to subclass and implement and override the appropriate methods. Because a Response is not aSharedResource
, 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 Summary
Constructors Constructor Description ResponseDispatch()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description boolean
cancel(boolean mayInterruptIfRunning)
ContentChannel
connect(ResponseHandler responseHandler)
This methods callsnewResponse()
to create a newResponse
, and then callsResponseHandler.handleResponse(Response)
with that.FastContentWriter
connectFastWriter(ResponseHandler responseHandler)
Convenience method for constructing aFastContentWriter
over theContentChannel
returned by callingconnect(ResponseHandler)
.protected com.google.common.util.concurrent.ListenableFuture<java.lang.Boolean>
delegate()
com.google.common.util.concurrent.ListenableFuture<java.lang.Boolean>
dispatch(ResponseHandler responseHandler)
This method callsconnect(ResponseHandler)
to establish aContentChannel
for theResponse
, and then iterates through all the ByteBuffers returned byresponseContent()
and writes them to that ContentChannel.boolean
isCancelled()
static ResponseDispatch
newInstance(int responseStatus, java.lang.Iterable<java.nio.ByteBuffer> content)
Factory method for creating a ResponseDispatch with aResponse
that has the given status code, and collection of ByteBuffer content.static ResponseDispatch
newInstance(int responseStatus, java.nio.ByteBuffer... content)
Factory method for creating a ResponseDispatch with aResponse
that has the given status code, and ByteBuffer content.static ResponseDispatch
newInstance(Response response, java.lang.Iterable<java.nio.ByteBuffer> content)
Factory method for creating a ResponseDispatch over a givenResponse
and ByteBuffer content.static ResponseDispatch
newInstance(Response response, java.nio.ByteBuffer... content)
Factory method for creating a ResponseDispatch over a givenResponse
and ByteBuffer content.protected abstract Response
newResponse()
Creates and returns theResponse
to dispatch.protected java.lang.Iterable<java.nio.ByteBuffer>
responseContent()
Returns an Iterable for the ByteBuffers that thedispatch(ResponseHandler)
method should write to theResponse
once it hasconnected
.-
Methods inherited from class com.google.common.util.concurrent.ForwardingListenableFuture
addListener
-
-
-
-
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 theResponse
once it hasconnected
. 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 newResponse
, and then callsResponseHandler.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 theContentChannel
returned by callingconnect(ResponseHandler)
.- Parameters:
responseHandler
- The ResponseHandler to connect to.- Returns:
- The FastContentWriter for the connected Response.
-
dispatch
public final com.google.common.util.concurrent.ListenableFuture<java.lang.Boolean> dispatch(ResponseHandler responseHandler)
This method calls
connect(ResponseHandler)
to establish aContentChannel
for theResponse
, and then iterates through all the ByteBuffers returned byresponseContent()
and writes them to that ContentChannel. This method uses afinally
block to make sure that the ContentChannel is alwaysclosed
.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 classcom.google.common.util.concurrent.ForwardingListenableFuture<java.lang.Boolean>
-
cancel
public final boolean cancel(boolean mayInterruptIfRunning)
- Specified by:
cancel
in interfacejava.util.concurrent.Future<java.lang.Boolean>
- Overrides:
cancel
in classcom.google.common.util.concurrent.ForwardingFuture<java.lang.Boolean>
-
isCancelled
public final boolean isCancelled()
- Specified by:
isCancelled
in interfacejava.util.concurrent.Future<java.lang.Boolean>
- Overrides:
isCancelled
in classcom.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.
-
-