Class ResponseDispatch
- java.lang.Object
-
- com.yahoo.jdisc.handler.ResponseDispatch
-
- 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 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)
.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.Boolean
get()
Boolean
get(long timeout, TimeUnit unit)
boolean
isCancelled()
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
newResponse()
Creates and returns theResponse
to dispatch.protected Iterable<ByteBuffer>
responseContent()
Returns an Iterable for the ByteBuffers that thedispatch(ResponseHandler)
method should write to theResponse
once it hasconnected
.
-
-
-
Method Detail
-
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 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 CompletableFuture<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.
-
cancel
public final boolean cancel(boolean mayInterruptIfRunning)
-
isCancelled
public final boolean isCancelled()
- Specified by:
isCancelled
in interfaceFuture<Boolean>
-
get
public Boolean get() throws InterruptedException, ExecutionException
- 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
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.
-
-