public interface Response extends ResponseMetaData
The headers and status are configured, before committing the response with one of the send()
methods.
Modifier and Type | Method and Description |
---|---|
Response |
beforeSend(Action<? super ResponseMetaData> responseFinalizer)
Register an action to execute upon the response immediately before sending it to the client.
|
Response |
contentType(CharSequence contentType)
Sets the response
Content-Type header. |
default Response |
contentTypeIfNotSet(CharSequence contentType)
Sets the response
Content-Type header, if it has not already been set. |
Response |
contentTypeIfNotSet(Supplier<CharSequence> contentType) |
Response |
noCompress()
Prevents the response from being compressed.
|
void |
send()
Sends the response back to the client, with no body.
|
void |
send(byte[] bytes)
Sends the response, using "
application/octet-stream " as the content type (if a content type hasn't
already been set) and the given byte array as the response body. |
void |
send(ByteBuf buffer)
Sends the response, using "
application/octet-stream " as the content type (if a content type hasn't
already been set) and the given bytes as the response body. |
void |
send(CharSequence contentType,
byte[] bytes)
Sends the response, using the given content type and byte array as the response body.
|
void |
send(CharSequence contentType,
ByteBuf buffer)
Sends the response, using the given content type and bytes as the response body.
|
void |
send(CharSequence contentType,
String body)
Sends the response, using the given content type and string as the response body.
|
void |
send(String text)
Sends the response, using "
text/plain " as the content type and the given string as the response body. |
void |
sendFile(Path file)
Sends the response, using the file as the response body.
|
void |
sendStream(Publisher<? extends ByteBuf> stream)
Sends the response, streaming the bytes emitted by the given publisher.
|
default Response |
status(int code)
Sets the status line of the response.
|
Response |
status(Status status)
Sets the status line of the response.
|
cookie, expireCookie, getCookies, getHeaders, getStatus
default Response status(int code)
ResponseMetaData
The message used will be the standard for the code.
status
in interface ResponseMetaData
code
- The status code of the response to use when it is sent.Response status(Status status)
ResponseMetaData
status
in interface ResponseMetaData
status
- The status of the response to use when it is sent.@NonBlocking void send()
Response contentTypeIfNotSet(Supplier<CharSequence> contentType)
@NonBlocking void send(String text)
text/plain
" as the content type and the given string as the response body.
Equivalent to calling "send\("text/plain", text)
.
text
- The text to render as a plain text response.@NonBlocking void send(CharSequence contentType, String body)
The string will be sent in "utf8" encoding, and the given content type will have this appended.
That is, given a contentType
of "application/json
" the actual value for the Content-Type
header will be "application/json;charset=utf8
".
The value given for content type will override any previously set value for this header.
contentType
- The value of the content type headerbody
- The string to render as the body of the response@NonBlocking void send(byte[] bytes)
application/octet-stream
" as the content type (if a content type hasn't
already been set) and the given byte array as the response body.bytes
- The response body@NonBlocking void send(CharSequence contentType, byte[] bytes)
contentType
- The value of the Content-Type
headerbytes
- The response body@NonBlocking void send(ByteBuf buffer)
application/octet-stream
" as the content type (if a content type hasn't
already been set) and the given bytes as the response body.buffer
- The response body@NonBlocking void send(CharSequence contentType, ByteBuf buffer)
contentType
- The value of the Content-Type
headerbuffer
- The response bodyResponse contentType(CharSequence contentType)
ResponseMetaData
Content-Type
header.contentType
in interface ResponseMetaData
contentType
- The value of the Content-Type
headerdefault Response contentTypeIfNotSet(CharSequence contentType)
ResponseMetaData
Content-Type
header, if it has not already been set.contentTypeIfNotSet
in interface ResponseMetaData
contentType
- The value of the Content-Type
header@NonBlocking void sendFile(Path file)
This method does not set the content length, content type or anything else.
It is generally preferable to use the Context.render(Object)
method with a file/path object,
or an Chain.files(Action)
.
file
- the response body@NonBlocking void sendStream(Publisher<? extends ByteBuf> stream)
This method does not perform chunked transfer encoding.
It merely sends the raw bytes emitted by the publisher.
As such, it is generally preferable to render chunks
than use this method directly.
The response headers will be sent as is, without the implicit addition of a Content-Length
header like the other send methods.
Back pressure is applied to the given publisher based on the flow control of the network connection.
That is, items are requested from the publisher as they are able to be sent by the underlying Netty layer.
As such, the given publisher MUST respect back pressure.
If this is not feasible, consider using Streams.buffer(org.reactivestreams.Publisher)
.
The back pressure applied will be irregular, based on factors including:
Data requested of the publisher is not always written immediately to the client. Netty maintains its own buffer that is fed by the given publisher. This means that data is more likely ready to send as soon as the client receives it.
If your data source produces a small amount of data that is expensive to produce (i.e. there is a significant latency between a data request and the production of data) you may want to consider an intermediate buffer to maximize throughput to the client. However, this is rarely necessary.
stream
- a stream of byte bufs to be written to the responseResponse beforeSend(Action<? super ResponseMetaData> responseFinalizer)
responseFinalizer
- The action to execute on this response.Response noCompress()
noCompress
in interface ResponseMetaData
this