Interface Response
- All Superinterfaces:
Content.Sink
- All Known Subinterfaces:
AuthenticationState.Deferred.DeferredResponse
,ServerUpgradeResponse
,ServerUpgradeResponse
- All Known Implementing Classes:
ContextResponse
,GzipResponseAndCallback
,HttpChannelState.ChannelResponse
,Response.Wrapper
,ServerUpgradeResponseDelegate
,ServerUpgradeResponseImpl
,ServletContextResponse
,ServletCoreResponse
,StatisticsHandler.MinimumDataRateHandler.MinimumDataRateResponse
The representation of an HTTP response, for any protocol version (HTTP/1.1, HTTP/2, HTTP/3).
-
Nested Class Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
addCookie
(Response response, HttpCookie cookie) Adds an HTTPHttpHeader.SET_COOKIE
header to the response.static <T> T
Unwraps the given response, recursively, until the wrapped instance is an instance of the given type, otherwise returnsnull
.static OutputStream
asBufferedOutputStream
(Request request, Response response) Wraps aResponse
as aOutputStream
that performs buffering.static Content.Sink
asBufferedSink
(Request request, Response response) Wraps aResponse
as aContent.Sink
that performs buffering.static long
getContentBytesWritten
(Response response) static Response
getOriginalResponse
(Response response) Unwraps the given response until the innermost wrapped response instance.int
boolean
Returns whether the last write has been initiated on the response.boolean
Returns whether this response has already been committed.boolean
Returns whether the response completed successfully.static Content.Chunk.Processor
newTrailersChunkProcessor
(Response response) Returns a chunk processor suitable to be passed to theContent.copy(Content.Source, Content.Sink, Content.Chunk.Processor, Callback)
method, that will handleTrailers
chunks by adding their fields to theHttpFields
supplied bygetTrailersSupplier()
.static void
putCookie
(Response response, HttpCookie cookie) Put a HTTPHttpHeader.SET_COOKIE
header to the response.static void
replaceCookie
(Response response, HttpCookie cookie) Deprecated.void
reset()
Resets this response, clearing the HTTP status code, HTTP headers and HTTP trailers.static void
sendRedirect
(Request request, Response response, Callback callback, int code, String location, boolean consumeAvailable) Sends a302
HTTP redirect status code to the given location.static void
sendRedirect
(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 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.void
setStatus
(int code) Set the response HTTP status code.void
setTrailersSupplier
(Supplier<HttpFields> trailers) Sets the supplier for the HTTP trailers.static String
toRedirectURI
(Request request, String location) Common point to generate a proper "Location" header for redirects.void
write
(boolean last, ByteBuffer byteBuffer, Callback callback) Writes the givenByteBuffer
, notifying theCallback
when the write is complete.static void
writeError
(Request request, Response response, Callback callback, int status) Writes an error response with the given HTTP status code.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.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.static void
writeError
(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
Request getRequest()- Returns:
- the
Request
associated with thisResponse
-
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:
true
iflast==true
has 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
CompletableFuture
is notified of the result of this write, whether it succeeded or failed.- Parameters:
status
- the interim HTTP status codeheaders
- the HTTP headers- Returns:
- a
CompletableFuture
with the result of the write
-
write
Writes the given
ByteBuffer
, notifying theCallback
when 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
Callback
and a call to this method.The invocation of the passed
Callback
is 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
Callback
should not block waiting for a callback of a future call to this method.- Specified by:
write
in 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 handleTrailers
chunks by adding their fields to theHttpFields
supplied 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:
-
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_303
code is used, unless the request is HTTP/1.0, in which caseHttpStatus.MOVED_TEMPORARILY_302
is used, unless the request is not aGET
and the protocol isHTTP/1.1
or later, in which case aHttpStatus.SEE_OTHER_303
is 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_303
code is used, unless the request is HTTP/1.0, in which caseHttpStatus.MOVED_TEMPORARILY_302
is used, unless the request is not aGET
and the protocol isHTTP/1.1
or later, in which case aHttpStatus.SEE_OTHER_303
is 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
302
HTTP 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 isnull
IllegalStateException
- 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_COOKIE
header to the response.- Parameters:
response
- the HTTP responsecookie
- the HTTP cookie to add- See Also:
-
putCookie
Put a HTTP
HttpHeader.SET_COOKIE
header to the response.If a matching
HttpHeader.SET_COOKIE
already 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.Handler
returned 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.Handler
returned 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.Handler
returned 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.Handler
returned 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
Unwraps the given response until the innermost wrapped response instance.
- Parameters:
response
- the response to unwrap- Returns:
- the innermost wrapped response instance
- See Also:
-
getContentBytesWritten
- Parameters:
response
- the HTTP response- Returns:
- the number of response content bytes written to the network so far,
or
-1
if the number is unknown
-
asBufferedOutputStream
Wraps a
Response
as aOutputStream
that performs buffering. The necessaryByteBufferPool
is 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 aResponse
as aContent.Sink
that performs buffering. The necessaryByteBufferPool
is 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)