- All Superinterfaces:
MessageBodyFilters,MessageBodyWriters
Lifecycle
HTTP response is send to the client in two or more steps (chunks). First containsHTTP status code
and headers. First part can be send explicitly by calling ResponseHeaders.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 of ResponseHeaders will throw IllegalStateException.
Response content (body/payload) can be constructed using send(...) methods.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic enumDeclares common groups ofCache-Controlsettings. -
Method Summary
Modifier and TypeMethodDescriptiondefault ServerResponseaddHeader(Http.HeaderName name, String... values) Adds header values for a specified name.default ServerResponseaddHeader(Http.HeaderValue value) Adds header values for a specified name.default ServerResponseDeprecated.default ServerResponseDeprecated.default ServerResponsecachingStrategy(ServerResponse.CachingStrategy cachingStrategy) Sets theCache-Controlheader values according to the specified strategy.default voidheader(Http.HeaderValue headerValue) Set a header.headers()Returns response headers.registerFilter(MessageBodyFilter filter) Registers a message body filter.registerWriter(MessageBodyStreamWriter<?> writer) Register a stream writer.registerWriter(MessageBodyWriter<?> writer) Register a writer.longA unique correlation ID that is associated with this response and its associated request.send()Sends an empty response.Send aThrowableand close the response.send(Flow.Publisher<DataChunk> content) Send a message as is without any other marshalling, registered filters are applied.send(Flow.Publisher<DataChunk> content, boolean applyFilters) Send a message as is without any other marshalling.<T> Single<ServerResponse>send(Flow.Publisher<T> content, Class<T> clazz) Send a message with the given entity stream as content and close the response.send(Function<MessageBodyWriterContext, Flow.Publisher<DataChunk>> function) Send a message using the given marshalling function.<T> Single<ServerResponse>send(T content) Send a message and close the response.status()Returns actual response status code.default ServerResponsestatus(int statusCode) Sets new HTTP status code.status(Http.Status status) Sets new HTTP status.Returns actualWebServerinstance.whenSent()Completion stage is completed when response is completed.Get the writer context used to marshall data.
-
Method Details
-
webServer
WebServer webServer()Returns actualWebServerinstance.- Returns:
- an actual
WebServerinstance
-
status
Http.Status status()Returns actual response status code.Default value for handlers is
200and for failure handlers500. Value can be redefined usingstatus(int)method before headers are send.- Returns:
- an HTTP status code
-
status
Sets new HTTP status code. Can be done before headers are completed - seeResponseHeadersdocumentation.- Parameters:
statusCode- new status code- Returns:
- this instance of
ServerResponse - Throws:
AlreadyCompletedException- if headers were completed (sent to the client)
-
status
Sets new HTTP status. Can be done before headers are completed - seeResponseHeadersdocumentation.- 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
-
addHeader
Deprecated.Adds header values for a specified name.- Parameters:
name- header namevalues- header values- Returns:
- this instance of
ServerResponse - Throws:
NullPointerException- if the specified name is null.- See Also:
-
addHeader
Deprecated.Adds header values for a specified name.- Parameters:
name- header namevalues- header values- Returns:
- this instance of
ServerResponse - Throws:
NullPointerException- if the specified name is null.- See Also:
-
addHeader
Adds header values for a specified name.- Parameters:
name- header namevalues- header values- Returns:
- this instance of
ServerResponse - Throws:
NullPointerException- if the specified name is null.- See Also:
-
header
Set a header.- Parameters:
headerValue- header name and value to set (replaces existing)
-
addHeader
Adds header values for a specified name.- Parameters:
value- header value- Returns:
- this instance of
ServerResponse - Throws:
NullPointerException- if the specified name is null.- See Also:
-
cachingStrategy
Sets theCache-Controlheader values according to the specified strategy.- Parameters:
cachingStrategy-ServerResponse.CachingStrategyto apply to this response- Returns:
- updated response
-
writerContext
MessageBodyWriterContext writerContext()Get the writer context used to marshall data.- Returns:
- MessageBodyWriterContext
-
send
Send aThrowableand close the response. Invokes error handlers if defined.- Parameters:
content- theThrowableto send- Returns:
nullwhen invoked- Throws:
IllegalStateException- if anysend(...)method was already called- See Also:
-
send
Send a message and close the response.Marshalling
Data are marshaled using default orregisteredwriterto the format ofByteBufferPublisher. The last registered compatible writer is used.Default writers supports:
CharSequencebyte[]PathFile
Blocking
The method blocks only during marshalling. It means untilregistered writerproduce aPublisherand subscribe HTTP IO implementation on it. If the thread is used for publishing is up to HTTP IO and generated Publisher implementations. Use returnedSingleto monitor and react on finished sending process.- 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
Send a message with the given entity stream as content and close the response.- Type Parameters:
T- entity type- Parameters:
content- entity streamclazz- class representing the entity type- Returns:
- a completion stage of the response - completed when response is transferred
-
send
Send a message as is without any other marshalling, registered filters are applied. The response is completed when publisher sendFlow.Subscriber.onComplete()method to its subscriber.A single
Subscribersubscribes to the providedPublisherduring the method execution.Blocking
If the thread is used for publishing is up to HTTP IO and generated Publisher implementations. Use returnedSingleto monitor and react on finished sending process.- 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
Send a message as is without any other marshalling. The response is completed when publisher sendFlow.Subscriber.onComplete()method to its subscriber.A single
Subscribersubscribes to the providedPublisherduring the method execution.Blocking
If the thread is used for publishing is up to HTTP IO and generated Publisher implementations. Use returnedSingleto monitor and react on finished sending process.- Parameters:
content- a response content publisherapplyFilters- if true all registered filters are applied- Returns:
- a completion stage of the response - completed when response is transferred
- Throws:
IllegalStateException- if anysend(...)method was already called
-
send
Send a message using the given marshalling function.- Parameters:
function- marshalling function- Returns:
- a completion stage of the response - completed when response is transferred
-
send
Single<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
-
registerFilter
Description copied from interface:MessageBodyFiltersRegisters a message body filter.The registered filters are applied to form a chain from the first registered to the last registered. The first evaluation of the function transforms the original publisher to a new publisher. Any subsequent evaluation receives the publisher transformed by the last previously registered filter.
- Specified by:
registerFilterin interfaceMessageBodyFilters- Parameters:
filter- a function to map previously registered or originalPublisherto the new one. If returnsnullthen the result will be ignored.- Returns:
- this instance of
MessageBodyFilters - See Also:
-
registerWriter
Description copied from interface:MessageBodyWritersRegister a writer.- Specified by:
registerWriterin interfaceMessageBodyWriters- Parameters:
writer- writer to register- Returns:
- Writers
-
registerWriter
Description copied from interface:MessageBodyWritersRegister a stream writer.- Specified by:
registerWriterin interfaceMessageBodyWriters- Parameters:
writer- writer to register- Returns:
- Writers
-
whenSent
Single<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
-
addHeader(io.helidon.common.http.Http.HeaderName, String...)