public abstract class ResponseDispatch extends com.google.common.util.concurrent.ForwardingListenableFuture<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); }
Modifier and Type | Field and Description |
---|---|
private FutureConjunction |
completions |
Constructor and Description |
---|
ResponseDispatch() |
Modifier and Type | Method and Description |
---|---|
boolean |
cancel(boolean mayInterruptIfRunning) |
ContentChannel |
connect(ResponseHandler responseHandler)
This methods calls
newResponse() to create a new Response , and then calls ResponseHandler.handleResponse(Response) with that. |
FastContentWriter |
connectFastWriter(ResponseHandler responseHandler)
Convenience method for constructing a
FastContentWriter over the ContentChannel returned by
calling connect(ResponseHandler) . |
protected com.google.common.util.concurrent.ListenableFuture<Boolean> |
delegate() |
com.google.common.util.concurrent.ListenableFuture<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. |
boolean |
isCancelled() |
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. |
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. |
static ResponseDispatch |
newInstance(Response response,
ByteBuffer... content)
Factory method for creating a ResponseDispatch over a given
Response and ByteBuffer content. |
static ResponseDispatch |
newInstance(Response response,
Iterable<ByteBuffer> content)
Factory method for creating a ResponseDispatch over a given
Response and ByteBuffer content. |
protected abstract Response |
newResponse()
Creates and returns the
Response to dispatch. |
protected Iterable<ByteBuffer> |
responseContent()
Returns an Iterable for the ByteBuffers that the
dispatch(ResponseHandler) method should write to the
Response once it has connected . |
addListener
private final FutureConjunction completions
protected abstract Response newResponse()
Creates and returns the Response
to dispatch.
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.
public final ContentChannel connect(ResponseHandler responseHandler)
This methods calls newResponse()
to create a new Response
, and then calls ResponseHandler.handleResponse(Response)
with that.
responseHandler
- The ResponseHandler to connect to.public final FastContentWriter connectFastWriter(ResponseHandler responseHandler)
Convenience method for constructing a FastContentWriter
over the ContentChannel
returned by
calling connect(ResponseHandler)
.
responseHandler
- The ResponseHandler to connect to.public final com.google.common.util.concurrent.ListenableFuture<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.
responseHandler
- The ResponseHandler to dispatch to.protected final com.google.common.util.concurrent.ListenableFuture<Boolean> delegate()
delegate
in class com.google.common.util.concurrent.ForwardingListenableFuture<Boolean>
public final boolean cancel(boolean mayInterruptIfRunning)
public final boolean isCancelled()
isCancelled
in interface Future<Boolean>
isCancelled
in class com.google.common.util.concurrent.ForwardingFuture<Boolean>
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.
responseStatus
- The status code of the Response to dispatch.content
- The ByteBuffer content of the Response, may be empty.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.
responseStatus
- The status code of the Response to dispatch.content
- The provider of the Response's ByteBuffer content.public static ResponseDispatch newInstance(Response response, ByteBuffer... content)
Factory method for creating a ResponseDispatch over a given Response
and ByteBuffer content.
response
- The Response to dispatch.content
- The ByteBuffer content of the Response, may be empty.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.
response
- The Response to dispatch.content
- The provider of the Response's ByteBuffer content.Copyright © 2018. All rights reserved.