-
- All Superinterfaces:
MessageBodyFilters
,MessageBodyWriters
public interface ServerResponse extends MessageBodyFilters, MessageBodyWriters
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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static class
ServerResponse.CachingStrategy
Declares common groups ofCache-Control
settings.
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Deprecated Methods Modifier and Type Method Description default ServerResponse
addHeader(String name, Iterable<String> values)
Adds header values for a specified name.default ServerResponse
addHeader(String name, String... values)
Adds header values for a specified name.default ServerResponse
addHeaders(Parameters parameters)
Copies all of the mappings from the specifiedparameters
to this response headers instance.default ServerResponse
cachingStrategy(ServerResponse.CachingStrategy cachingStrategy)
Sets theCache-Control
header values according to the specified strategy.ResponseHeaders
headers()
Returns response headers.ServerResponse
registerFilter(MessageBodyFilter filter)
Registers a message body filter.ServerResponse
registerFilter(Function<Flow.Publisher<DataChunk>,Flow.Publisher<DataChunk>> function)
Deprecated.since 2.0.0, useregisterFilter(io.helidon.media.common.MessageBodyFilter)
insteadServerResponse
registerWriter(MessageBodyStreamWriter<?> writer)
Register a stream writer.ServerResponse
registerWriter(MessageBodyWriter<?> writer)
Register a writer.<T> ServerResponse
registerWriter(Class<T> type, MediaType contentType, Function<? extends T,Flow.Publisher<DataChunk>> function)
Deprecated.since 2.0.0, useregisterWriter(io.helidon.media.common.MessageBodyWriter)
instead<T> ServerResponse
registerWriter(Class<T> type, Function<T,Flow.Publisher<DataChunk>> function)
Deprecated.Since 2.0.0, useregisterWriter(io.helidon.media.common.MessageBodyWriter)
instead<T> ServerResponse
registerWriter(Predicate<?> accept, MediaType contentType, Function<T,Flow.Publisher<DataChunk>> function)
Deprecated.since 2.0.0, useregisterWriter(io.helidon.media.common.MessageBodyWriter)
instead<T> ServerResponse
registerWriter(Predicate<?> accept, Function<T,Flow.Publisher<DataChunk>> function)
Deprecated.since 2.0.0, useregisterWriter(io.helidon.media.common.MessageBodyWriter)
insteadlong
requestId()
A unique correlation ID that is associated with this response and its associated request.Single<ServerResponse>
send()
Sends an empty response.Void
send(Throwable content)
Send aThrowable
and close the response.Single<ServerResponse>
send(Flow.Publisher<DataChunk> content)
Send a message as is without any other marshalling, registered filters are applied.Single<ServerResponse>
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.Single<ServerResponse>
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.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.Single<ServerResponse>
whenSent()
Completion stage is completed when response is completed.MessageBodyWriterContext
writerContext()
Get the writer context used to marshall data.
-
-
-
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
-
addHeader
default ServerResponse addHeader(String name, String... values)
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:
headers()
,Parameters.add(String, String...)
,header names constants
-
addHeader
default ServerResponse addHeader(String name, Iterable<String> values)
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:
headers()
,Parameters.add(String, Iterable)
,header names constants
-
addHeaders
default ServerResponse addHeaders(Parameters parameters)
Copies all of the mappings from the specifiedparameters
to this response headers instance.- Parameters:
parameters
- to copy.- Returns:
- this instance of
ServerResponse
- Throws:
NullPointerException
- if the specifiedparameters
are null.- See Also:
headers()
,Parameters.addAll(Parameters)
-
cachingStrategy
default ServerResponse cachingStrategy(ServerResponse.CachingStrategy cachingStrategy)
Sets theCache-Control
header values according to the specified strategy.- Parameters:
cachingStrategy
-ServerResponse.CachingStrategy
to apply to this response- Returns:
- updated response
-
writerContext
MessageBodyWriterContext writerContext()
Get the writer context used to marshall data.- Returns:
- MessageBodyWriterContext
-
send
Void send(Throwable content)
Send aThrowable
and close the response. Invokes error handlers if defined.- Parameters:
content
- theThrowable
to send- Returns:
null
when invoked- Throws:
IllegalStateException
- if anysend(...)
method was already called- See Also:
send(Object)
-
send
<T> Single<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 returnedSingle
to 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
<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.- 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
Single<ServerResponse> send(Flow.Publisher<DataChunk> content)
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
Subscriber
subscribes to the providedPublisher
during the method execution.Blocking
If the thread is used for publishing is up to HTTP IO and generated Publisher implementations. Use returnedSingle
to 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
Single<ServerResponse> send(Flow.Publisher<DataChunk> content, boolean applyFilters)
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
If the thread is used for publishing is up to HTTP IO and generated Publisher implementations. Use returnedSingle
to 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
Single<ServerResponse> send(Function<MessageBodyWriterContext,Flow.Publisher<DataChunk>> function)
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
-
registerWriter
@Deprecated(since="2.0.0") <T> ServerResponse registerWriter(Class<T> type, Function<T,Flow.Publisher<DataChunk>> function)
Deprecated.Since 2.0.0, useregisterWriter(io.helidon.media.common.MessageBodyWriter)
insteadRegisters 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
@Deprecated(since="2.0.0") <T> ServerResponse registerWriter(Class<T> type, MediaType contentType, Function<? extends T,Flow.Publisher<DataChunk>> function)
Deprecated.since 2.0.0, useregisterWriter(io.helidon.media.common.MessageBodyWriter)
insteadRegisters 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
@Deprecated <T> ServerResponse registerWriter(Predicate<?> accept, Function<T,Flow.Publisher<DataChunk>> function)
Deprecated.since 2.0.0, useregisterWriter(io.helidon.media.common.MessageBodyWriter)
insteadRegisters 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
@Deprecated <T> ServerResponse registerWriter(Predicate<?> accept, MediaType contentType, Function<T,Flow.Publisher<DataChunk>> function)
Deprecated.since 2.0.0, useregisterWriter(io.helidon.media.common.MessageBodyWriter)
insteadRegisters 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
@Deprecated ServerResponse registerFilter(Function<Flow.Publisher<DataChunk>,Flow.Publisher<DataChunk>> function)
Deprecated.since 2.0.0, useregisterFilter(io.helidon.media.common.MessageBodyFilter)
insteadRegisters 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
-
registerFilter
ServerResponse registerFilter(MessageBodyFilter filter)
Description copied from interface:MessageBodyFilters
Registers 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:
registerFilter
in interfaceMessageBodyFilters
- Parameters:
filter
- a function to map previously registered or originalPublisher
to the new one. If returnsnull
then the result will be ignored.- Returns:
- this instance of
MessageBodyFilters
- See Also:
MessageBodyContext.applyFilters(Flow.Publisher)
-
registerWriter
ServerResponse registerWriter(MessageBodyWriter<?> writer)
Description copied from interface:MessageBodyWriters
Register a writer.- Specified by:
registerWriter
in interfaceMessageBodyWriters
- Parameters:
writer
- writer to register- Returns:
- Writers
-
registerWriter
ServerResponse registerWriter(MessageBodyStreamWriter<?> writer)
Description copied from interface:MessageBodyWriters
Register a stream writer.- Specified by:
registerWriter
in 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
-
-