Interface ServerResponse
-
public interface ServerResponse
Represents HTTP Response.Lifecycle
HTTP response is send to the client in two or more steps (chunks). First containsHTTP status code
andheaders
. First part can be send explicitly by callingResponseHeaders.send()
method or implicitly by sending a first part of the the response content. As soon as first part is send it become immutable -status(int)
method and all muting operations ofResponseHeaders
will throwIllegalStateException
.Response content (body/payload) can be constructed using
send(...)
methods.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description ResponseHeaders
headers()
Returns response headers.ServerResponse
registerFilter(Function<Flow.Publisher<DataChunk>,Flow.Publisher<DataChunk>> function)
Registers a provider of the new response content publisher - typically a filter.<T> ServerResponse
registerWriter(Class<T> type, MediaType contentType, Function<? extends T,Flow.Publisher<DataChunk>> function)
Registers a content writer for a given type and media type.<T> ServerResponse
registerWriter(Class<T> type, Function<T,Flow.Publisher<DataChunk>> function)
Registers a content writer for a given type.<T> ServerResponse
registerWriter(Predicate<?> accept, MediaType contentType, Function<T,Flow.Publisher<DataChunk>> function)
Registers a content writer for all accepted contents.<T> ServerResponse
registerWriter(Predicate<?> accept, Function<T,Flow.Publisher<DataChunk>> function)
Registers a content writer for all accepted contents.long
requestId()
A unique correlation ID that is associated with this response and its associated request.CompletionStage<ServerResponse>
send()
Sends an empty response.CompletionStage<ServerResponse>
send(Flow.Publisher<DataChunk> content)
Send a message as is without any other marshalling.<T> CompletionStage<ServerResponse>
send(T content)
Send a message and close the response.Http.ResponseStatus
status()
Returns actual response status code.default ServerResponse
status(int statusCode)
Sets new HTTP status code.ServerResponse
status(Http.ResponseStatus status)
Sets new HTTP status.WebServer
webServer()
Returns actualWebServer
instance.CompletionStage<ServerResponse>
whenSent()
Completion stage is completed when response is completed.
-
-
-
Method Detail
-
webServer
WebServer webServer()
Returns actualWebServer
instance.- Returns:
- an actual
WebServer
instance
-
status
Http.ResponseStatus status()
Returns actual response status code.Default value for handlers is
200
and for failure handlers500
. Value can be redefined usingstatus(int)
method before headers are send.- Returns:
- an HTTP status code
-
status
default ServerResponse status(int statusCode) throws AlreadyCompletedException
Sets new HTTP status code. Can be done before headers are completed - seeResponseHeaders
documentation.- Parameters:
statusCode
- new status code- Returns:
- this instance of
ServerResponse
- Throws:
AlreadyCompletedException
- if headers were completed (sent to the client)
-
status
ServerResponse status(Http.ResponseStatus status) throws AlreadyCompletedException, NullPointerException
Sets new HTTP status. Can be done before headers are completed - seeResponseHeaders
documentation.- Parameters:
status
- new status- Returns:
- this instance of
ServerResponse
- Throws:
AlreadyCompletedException
- if headers were completed (sent to the client)NullPointerException
- if status parameter isnull
-
headers
ResponseHeaders headers()
Returns response headers. It can be modified before headers are sent to the client.- Returns:
- a response headers
-
send
<T> CompletionStage<ServerResponse> send(T content)
Send a message and close the response.Marshalling
Data are marshaled using default orregistered
writer
to the format ofByteBuffer
Publisher
. The last registered compatible writer is used.Default writers supports:
CharSequence
byte[]
Path
File
Blocking
The method blocks only during marshalling. It means untilregistered writer
produce aPublisher
and subscribe HTTP IO implementation on it. If the thread is used for publishing is up to HTTP IO and generated Publisher implementations. Use returnedCompletionStage
to monitor and react on finished sending tryProcess.- Type Parameters:
T
- a type of the content- Parameters:
content
- a response content to send- Returns:
- a completion stage of the response - completed when response is transferred
- Throws:
IllegalArgumentException
- if there is no registered writer for a given typeIllegalStateException
- if anysend(...)
method was already called
-
send
CompletionStage<ServerResponse> send(Flow.Publisher<DataChunk> content)
Send a message as is without any other marshalling. The response is completed when publisher sendFlow.Subscriber.onComplete()
method to its subscriber.A single
Subscriber
subscribes to the providedPublisher
during the method execution.Blocking
The method blocks only during marshalling. It means untilregistered writer
produce aPublisher
and subscribe HTTP IO implementation on it. If the thread is used for publishing is up to HTTP IO and generated Publisher implementations. Use returnedCompletionStage
to monitor and react on finished sending tryProcess.- Parameters:
content
- a response content publisher- Returns:
- a completion stage of the response - completed when response is transferred
- Throws:
IllegalStateException
- if anysend(...)
method was already called
-
send
CompletionStage<ServerResponse> send()
Sends an empty response. Do nothing if response was already send.- Returns:
- a completion stage of the response - completed when response is transferred
-
registerWriter
<T> ServerResponse registerWriter(Class<T> type, Function<T,Flow.Publisher<DataChunk>> function)
Registers a content writer for a given type.Registered writer is used to marshal response content of given type to the
Publisher
ofresponse chunks
.- Type Parameters:
T
- a type of the content- Parameters:
type
- a type of the content. Ifnull
then accepts any type.function
- a writer function- Returns:
- this instance of
ServerResponse
- Throws:
NullPointerException
- iffunction
parameter isnull
-
registerWriter
<T> ServerResponse registerWriter(Class<T> type, MediaType contentType, Function<? extends T,Flow.Publisher<DataChunk>> function)
Registers a content writer for a given type and media type.Registered writer is used to marshal response content of given type to the
Publisher
ofresponse chunks
. It is used only ifContent-Type
header is compatible with a given content type or if it isnull
. IfContent-Type
isnull
and it is still possible to modify headers (headers were not send yet), the provided content type will be set.- Type Parameters:
T
- a type of the content- Parameters:
type
- a type of the content. Ifnull
then accepts any type.contentType
- aContent-Type
of the entityfunction
- a writer function- Returns:
- this instance of
ServerResponse
- Throws:
NullPointerException
- iffunction
parameter isnull
-
registerWriter
<T> ServerResponse registerWriter(Predicate<?> accept, Function<T,Flow.Publisher<DataChunk>> function)
Registers a content writer for all accepted contents.Registered writer is used to marshal response content of given type to the
Publisher
ofresponse chunks
.- Type Parameters:
T
- a type of the content- Parameters:
accept
- a predicate to test if content is marshallable by the writer. Ifnull
then accepts any type.function
- a writer function- Returns:
- this instance of
ServerResponse
- Throws:
NullPointerException
- iffunction
parameter isnull
-
registerWriter
<T> ServerResponse registerWriter(Predicate<?> accept, MediaType contentType, Function<T,Flow.Publisher<DataChunk>> function)
Registers a content writer for all accepted contents.Registered writer is used to marshal response content of given type to the
Publisher
ofresponse chunks
. It is used only ifContent-Type
header is compatible with a given content type or if it isnull
. IfContent-Type
isnull
and it is still possible to modify headers (headers were not send yet), the provided content type will be set.- Type Parameters:
T
- a type of the content- Parameters:
accept
- a predicate to test if content is marshallable by the writer. Ifnull
then accepts any type.contentType
- aContent-Type
of the entityfunction
- a writer function- Returns:
- this instance of
ServerResponse
- Throws:
NullPointerException
- iffunction
parameter isnull
-
registerFilter
ServerResponse registerFilter(Function<Flow.Publisher<DataChunk>,Flow.Publisher<DataChunk>> function)
Registers a provider of the new response content publisher - typically a filter.All response content is always represented by a single
Publisher
ofresponse chunks
. This method can be used to filter or completely replace original publisher by a new one with different contract. For example data coding, logging, filtering, caching, etc.New publisher is created at the moment of content write by any
send(...)
method including the empty one.All registered filters are used as a chain from original content
Publisher
, first registered to the last registered.- Parameters:
function
- a function to map previously registered or originalPublisher
to the new one. If returnsnull
then the result will be ignored.- Returns:
- this instance of
ServerResponse
- Throws:
NullPointerException
- if parameterfunction
isnull
-
whenSent
CompletionStage<ServerResponse> whenSent()
Completion stage is completed when response is completed.It can be used to react on the response completion without invocation of a closing event.
- Returns:
- a completion stage of the response
-
requestId
long requestId()
A unique correlation ID that is associated with this response and its associated request.- Returns:
- a unique correlation ID associated with this response and its request
-
-