Class ResponseDispatch
- Direct Known Subclasses:
CallableResponseDispatch
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 Summary
-
Method Summary
Modifier and TypeMethodDescriptionfinal boolean
cancel
(boolean mayInterruptIfRunning) final ContentChannel
connect
(ResponseHandler responseHandler) This methods callsnewResponse()
to create a newResponse
, and then callsResponseHandler.handleResponse(Response)
with that.final FastContentWriter
connectFastWriter
(ResponseHandler responseHandler) Convenience method for constructing aFastContentWriter
over theContentChannel
returned by callingconnect(ResponseHandler)
.final CompletableFuture<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.get()
final boolean
boolean
isDone()
static ResponseDispatch
newInstance
(int responseStatus, Iterable<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, ByteBuffer... content) Factory method for creating a ResponseDispatch with aResponse
that has the given status code, and ByteBuffer content.static ResponseDispatch
newInstance
(Response response, Iterable<ByteBuffer> content) Factory method for creating a ResponseDispatch over a givenResponse
and ByteBuffer content.static ResponseDispatch
newInstance
(Response response, ByteBuffer... content) Factory method for creating a ResponseDispatch over a givenResponse
and ByteBuffer content.protected abstract Response
Creates and returns theResponse
to dispatch.protected Iterable<ByteBuffer>
Returns an Iterable for the ByteBuffers that thedispatch(ResponseHandler)
method should write to theResponse
once it hasconnected
.
-
Constructor Details
-
ResponseDispatch
public ResponseDispatch()
-
-
Method Details
-
newResponse
Creates and returns the
Response
to dispatch.- Returns:
- The Response to dispatch.
-
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
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
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
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.
-
cancel
public final boolean cancel(boolean mayInterruptIfRunning) -
isCancelled
public final boolean isCancelled()- Specified by:
isCancelled
in interfaceFuture<Boolean>
-
isDone
public boolean isDone() -
get
- Specified by:
get
in interfaceFuture<Boolean>
- Throws:
InterruptedException
ExecutionException
-
get
public Boolean get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException - Specified by:
get
in interfaceFuture<Boolean>
- Throws:
InterruptedException
ExecutionException
TimeoutException
-
newInstance
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
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
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
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.
-