Interface Response
- All Superinterfaces:
 Content.Sink
- All Known Subinterfaces:
 AuthenticationState.Deferred.DeferredResponse, ServerUpgradeResponse, ServerUpgradeResponse
- All Known Implementing Classes:
 ContextResponse, GzipResponseAndCallback, Response.Wrapper, ServletContextResponse, ServletContextResponse, ServletCoreResponse, ServletCoreResponse, ServletCoreResponse, ServletCoreResponse, StatisticsHandler.MinimumDataRateHandler.MinimumDataRateResponse
The representation of an HTTP response, for any protocol version (HTTP/1.1, HTTP/2, HTTP/3).
- 
Nested Class Summary
Nested Classes - 
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddCookie(Response response, HttpCookie cookie) Adds an HTTPHttpHeader.SET_COOKIEheader to the response.static <T> TUnwraps the given response, recursively, until the wrapped instance is an instance of the given type, otherwise returnsnull.static OutputStreamasBufferedOutputStream(Request request, Response response) Wraps aResponseas aOutputStreamthat performs buffering.static Content.SinkasBufferedSink(Request request, Response response) Wraps aResponseas aContent.Sinkthat performs buffering.static <T extends Response>
TasInContext(Response response, Class<T> type) Unwrap a Response back to the given type, ensuring that we do not cross a context boundary (as might be the case during cross-context dispatch).static longgetContentBytesWritten(Response response) static ResponsegetOriginalResponse(Response response) Unwraps the given response until the innermost wrapped response instance.intbooleanReturns whether the last write has been initiated on the response.booleanReturns whether this response has already been committed.booleanReturns whether the response completed successfully.static Content.Chunk.ProcessornewTrailersChunkProcessor(Response response) Returns a chunk processor suitable to be passed to theContent.copy(Content.Source, Content.Sink, Content.Chunk.Processor, Callback)method, that will handleTrailerschunks by adding their fields to theHttpFieldssupplied bygetTrailersSupplier().static voidputCookie(Response response, HttpCookie cookie) Put a HTTPHttpHeader.SET_COOKIEheader to the response.static voidreplaceCookie(Response response, HttpCookie cookie) Deprecated.voidreset()Resets this response, clearing the HTTP status code, HTTP headers and HTTP trailers.static voidsendRedirect(Request request, Response response, Callback callback, int code, String location, boolean consumeAvailable) Sends a302HTTP redirect status code to the given location.static voidsendRedirect(Request request, Response response, Callback callback, int code, String location, boolean consumeAvailable, ByteBuffer content) Sends a302HTTP redirect status code to the given location.static voidsendRedirect(Request request, Response response, Callback callback, String location) Sends a HTTP redirect status code to the given location, without consuming the available request content.static voidsendRedirect(Request request, Response response, Callback callback, String location, boolean consumeAvailable) Sends HTTP redirect status code to the given location, without consuming the available request content.voidsetStatus(int code) Set the response HTTP status code.voidsetTrailersSupplier(Supplier<HttpFields> trailers) Sets the supplier for the HTTP trailers.static StringtoRedirectURI(Request request, String location) Common point to generate a proper "Location" header for redirects.voidwrite(boolean last, ByteBuffer byteBuffer, Callback callback) Writes the givenByteBuffer, notifying theCallbackwhen the write is complete.static voidwriteError(Request request, Response response, Callback callback, int status) Writes an error response with the given HTTP status code.static voidwriteError(Request request, Response response, Callback callback, int status, String message) Writes an error response with the given HTTP status code, and the given message in the response content.static voidwriteError(Request request, Response response, Callback callback, int status, String message, Throwable cause) Writes an error response with the given HTTP status code, and the given message in the response content.static voidwriteError(Request request, Response response, Callback callback, Throwable cause) Writes an error response with HTTP status code500.writeInterim(int status, HttpFields headers) Writes anHTTP interim response, with the given HTTP status code and HTTP headers. 
- 
Method Details
- 
getRequest
 - 
getStatus
int getStatus()- Returns:
 - the response HTTP status code
 
 - 
setStatus
void setStatus(int code) Set the response HTTP status code.- Parameters:
 code- the response HTTP status code
 - 
getHeaders
HttpFields.Mutable getHeaders()- Returns:
 - the response HTTP headers
 
 - 
getTrailersSupplier
Supplier<HttpFields> getTrailersSupplier()- Returns:
 - a supplier for the HTTP trailers
 
 - 
setTrailersSupplier
Sets the supplier for the HTTP trailers.
The method
Supplier.get()may be called by the implementation multiple times, so it is important that the same value is returned in every invocation.Example:
// Correct usage. HttpFields.Mutable trailers = HttpFields.build(); response.setTrailersSupplier(() -> trailers); // WRONG usage, as the value changes for // every invocation of supplier.get(). response.setTrailersSupplier(() -> HttpFields.build());- Parameters:
 trailers- a supplier for the HTTP trailers
 - 
isCommitted
boolean isCommitted()Returns whether this response has already been committed.
Committing a response means that the HTTP status code and HTTP headers cannot be modified anymore, typically because they have already been serialized and sent over the network.
- Returns:
 - whether this response has already been committed
 
 - 
hasLastWrite
boolean hasLastWrite()Returns whether the last write has been initiated on the response.
- Returns:
 trueiflast==truehas been passed towrite(boolean, ByteBuffer, Callback).
 - 
isCompletedSuccessfully
boolean isCompletedSuccessfully()Returns whether the response completed successfully.
The response HTTP status code, HTTP headers and content have been successfully serialized and sent over the network without errors.
- Returns:
 - whether the response completed successfully
 
 - 
reset
void reset()Resets this response, clearing the HTTP status code, HTTP headers and HTTP trailers.
- Throws:
 IllegalStateException- if the response is alreadycommitted
 - 
writeInterim
Writes an
HTTP interim response, with the given HTTP status code and HTTP headers.It is possible to write more than one interim response, for example in case of
HttpStatus.EARLY_HINTS_103.The returned
CompletableFutureis notified of the result of this write, whether it succeeded or failed.- Parameters:
 status- the interim HTTP status codeheaders- the HTTP headers- Returns:
 - a 
CompletableFuturewith the result of the write 
 - 
write
Writes the given
ByteBuffer, notifying theCallbackwhen the write is complete.Implementations guarantee that calls to this method are safely reentrant so that stack overflows are avoided in the case of mutual recursion between the execution of the
Callbackand a call to this method.The invocation of the passed
Callbackis serialized with previous calls of this method, so that it is not invoked until any invocation of the callback of a previous call to this method has returned.Thus a
Callbackshould not block waiting for a callback of a future call to this method.- Specified by:
 writein interfaceContent.Sink- Parameters:
 last- whether the ByteBuffer is the last to writebyteBuffer- the ByteBuffer to writecallback- the callback to notify when the write operation is complete
 - 
newTrailersChunkProcessor
Returns a chunk processor suitable to be passed to the
Content.copy(Content.Source, Content.Sink, Content.Chunk.Processor, Callback)method, that will handleTrailerschunks by adding their fields to theHttpFieldssupplied bygetTrailersSupplier().This is specifically useful for writing trailers that have been received via the
Content.Source.read()API, for example when echoing a request to a response:Content.copy(request, response, Response.asTrailerChunkHandler(response), callback);
- Parameters:
 response- The response for which to process a trailers chunk. If thesetTrailersSupplier(Supplier)method has not been called prior to this method, then a noop processor is returned.- Returns:
 - A chunk processor that will add trailer chunks to the response's trailer supplied fields.
 - See Also:
 
 - 
asInContext
Unwrap a Response back to the given type, ensuring that we do not cross a context boundary (as might be the case during cross-context dispatch).- Parameters:
 response- the possibly wrapped response to unwraptype- the type to unwrap to- Returns:
 - the response unwrapped back to the given type, or null if it cannot be unwrapped to that type or a context boundary is crossed.
 
 - 
as
Unwraps the given response, recursively, until the wrapped instance is an instance of the given type, otherwise returns
null.- Type Parameters:
 T- the response type- Parameters:
 response- the response to unwraptype- the response type to find- Returns:
 - the response as the given type, or 
null - See Also:
 
 - 
sendRedirect
Sends a HTTP redirect status code to the given location, without consuming the available request content. The
HttpStatus.SEE_OTHER_303code is used, unless the request is HTTP/1.0, in which caseHttpStatus.MOVED_TEMPORARILY_302is used, unless the request is not aGETand the protocol isHTTP/1.1or later, in which case aHttpStatus.SEE_OTHER_303is used to make the client consistently redirect with aGET.- Parameters:
 request- the HTTP requestresponse- the HTTP responsecallback- the callback to completelocation- the redirect location as an absolute URI or encoded relative URI path.- See Also:
 
 - 
sendRedirect
static void sendRedirect(Request request, Response response, Callback callback, String location, boolean consumeAvailable) Sends HTTP redirect status code to the given location, without consuming the available request content. The
HttpStatus.SEE_OTHER_303code is used, unless the request is HTTP/1.0, in which caseHttpStatus.MOVED_TEMPORARILY_302is used, unless the request is not aGETand the protocol isHTTP/1.1or later, in which case aHttpStatus.SEE_OTHER_303is used to make the client consistently redirect with aGET.- Parameters:
 request- the HTTP requestresponse- the HTTP responsecallback- the callback to completelocation- the redirect location as an absolute URI or encoded relative URI path.consumeAvailable- whether to consumer the available request content- See Also:
 
 - 
sendRedirect
static void sendRedirect(Request request, Response response, Callback callback, int code, String location, boolean consumeAvailable) Sends a
302HTTP redirect status code to the given location.- Parameters:
 request- the HTTP requestresponse- the HTTP responsecallback- the callback to completecode- the redirect HTTP status codelocation- the redirect location as an absolute URI or encoded relative URI path.consumeAvailable- whether to consumer the available request content- Throws:
 IllegalArgumentException- if the status code is not a redirect, or the location isnullIllegalStateException- if the response is alreadycommitted- See Also:
 
 - 
sendRedirect
static void sendRedirect(Request request, Response response, Callback callback, int code, String location, boolean consumeAvailable, ByteBuffer content) Sends a
302HTTP redirect status code to the given location.- Parameters:
 request- the HTTP requestresponse- the HTTP responsecallback- the callback to completecode- the redirect HTTP status code, or 0 for a defaultlocation- the redirect location as an absolute URI or encoded relative URI path.consumeAvailable- whether to consumer the available request contentcontent- the content of the response, or null for a generated HTML message ifHttpConfiguration.isGenerateRedirectBody()istrue.- Throws:
 IllegalArgumentException- if the status code is not a redirect, or the location isnullIllegalStateException- if the response is alreadycommitted- See Also:
 
 - 
toRedirectURI
Common point to generate a proper "Location" header for redirects.- Parameters:
 request- the request the redirect should be based on (needed when relative locations are provided, so that server name, scheme, port can be built out properly)location- the redirect location as an absolute URI or encoded relative URI path. If a relative path starts with '/', then it is relative to the root, otherwise it is relative to the request.- Returns:
 - the full redirect "Location" URL (including scheme, host, port, path, etc...)
 
 - 
addCookie
Adds an HTTP
HttpHeader.SET_COOKIEheader to the response.- Parameters:
 response- the HTTP responsecookie- the HTTP cookie to add- See Also:
 
 - 
putCookie
Put a HTTP
HttpHeader.SET_COOKIEheader to the response.If a matching
HttpHeader.SET_COOKIEalready exists for matching name, path, domain etc. then it will be replaced.- Parameters:
 response- the HTTP responsecookie- the HTTP cookie to add- See Also:
 
 - 
replaceCookie
Deprecated.Replace a cookie- Parameters:
 response- the HTTP responsecookie- the HTTP cookie to add
 - 
writeError
Writes an error response with HTTP status code
500.The error
Request.Handlerreturned byContext.getErrorHandler(), if any, is invoked.- Parameters:
 request- the HTTP requestresponse- the HTTP responsecallback- the callback to completecause- the cause of the error
 - 
writeError
Writes an error response with the given HTTP status code.
The error
Request.Handlerreturned byContext.getErrorHandler(), if any, is invoked.- Parameters:
 request- the HTTP requestresponse- the HTTP responsecallback- the callback to completestatus- the error HTTP status code
 - 
writeError
static void writeError(Request request, Response response, Callback callback, int status, String message) Writes an error response with the given HTTP status code, and the given message in the response content.
The error
Request.Handlerreturned byContext.getErrorHandler(), if any, is invoked.- Parameters:
 request- the HTTP requestresponse- the HTTP responsecallback- the callback to completestatus- the error HTTP status codemessage- the error message to write in the response content
 - 
writeError
static void writeError(Request request, Response response, Callback callback, int status, String message, Throwable cause) Writes an error response with the given HTTP status code, and the given message in the response content.
The error
Request.Handlerreturned byContext.getErrorHandler(), if any, is invoked.- Parameters:
 request- the HTTP requestresponse- the HTTP responsecallback- the callback to completestatus- the error HTTP status codemessage- the error message to write in the response contentcause- the cause of the error
 - 
getOriginalResponse
 - 
getContentBytesWritten
- Parameters:
 response- the HTTP response- Returns:
 - the number of response content bytes written to the network so far,
or 
-1if the number is unknown 
 - 
asBufferedOutputStream
Wraps a
Responseas aOutputStreamthat performs buffering. The necessaryByteBufferPoolis taken from the request's connector while the size and direction of the buffer is read from the request'sHttpConfiguration.This is equivalent to:
Content.Sink.asOutputStream(Response.asBufferedSink(request, response))- Parameters:
 request- the request from which to get the buffering sink's settingsresponse- the response to wrap- Returns:
 - a buffering 
OutputStream 
 - 
asBufferedSink
Wraps aResponseas aContent.Sinkthat performs buffering. The necessaryByteBufferPoolis taken from the request's connector while the size, direction of the buffer and commit size are read from the request'sHttpConfiguration.- Parameters:
 request- the request from which to get the buffering sink's settingsresponse- the response to wrap- Returns:
 - a buffering 
Content.Sink 
 
 - 
 
putCookie(Response, HttpCookie)