public interface Response
The headers and status are configured, before committing the response with one of the send()
methods.
Modifier and Type | Method and Description |
---|---|
Response |
contentType(String contentType)
Sets the response
Content-Type header. |
Cookie |
cookie(String name,
String value)
Creates a new cookie with the given name and value.
|
Cookie |
expireCookie(String name)
Adds a cookie to the response with a 0 max-age, forcing the client to expire it.
|
Set<Cookie> |
getCookies()
The cookies that are to be part of the response.
|
MutableHeaders |
getHeaders()
The response headers.
|
MutableStatus |
getStatus()
The status that will be part of the response when sent.
|
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(InputStream inputStream)
Sends the response, using "
application/octet-stream " as the content type (if a content type hasn't
already been set) and the contents of the given input stream 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 |
send(String contentType,
byte[] bytes)
Sends the response, using the given content type and byte array as the response body.
|
void |
send(String contentType,
ByteBuf buffer)
Sends the response, using the given content type and bytes as the response body.
|
void |
send(String contentType,
InputStream inputStream)
Sends the response, using the given content type and the content of the given input stream as the response body.
|
void |
send(String contentType,
String body)
Sends the response, using the given content type and string as the response body.
|
void |
sendFile(Background background,
BasicFileAttributes attributes,
Path file)
Sends the response, using the given content type and the content of the given type as the response body.
|
void |
sendFile(Background background,
Path file)
Sends the response, using the given content type and the content of the given type as the response body.
|
Response |
status(int code)
Sets the status line of the response.
|
Response |
status(int code,
String message)
Sets the status line of the response.
|
MutableStatus getStatus()
By default, this will return a "200 OK"
response.
status(int)
Response status(int code)
The message used will be the standard for the code.
code
- The status code of the response to use when it is sent.Response status(int code, String message)
code
- The status code of the response to use when it is sent.message
- The status message of the response to use when it is sent.MutableHeaders getHeaders()
@NonBlocking void send()
@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(String 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(String contentType, byte[] bytes)
contentType
- The value of the Content-Type
headerbytes
- The response body@NonBlocking void send(InputStream inputStream) throws IOException
application/octet-stream
" as the content type (if a content type hasn't
already been set) and the contents of the given input stream as the response body.inputStream
- The response bodyIOException
- if the input stream cannot be consumed@NonBlocking void send(String contentType, InputStream inputStream) throws IOException
contentType
- The value of the Content-Type
headerinputStream
- response bodyIOException
- if the input stream cannot be consumed@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(String contentType, ByteBuf buffer)
contentType
- The value of the Content-Type
headerbuffer
- The response bodyResponse contentType(String contentType)
Content-Type
header.contentType
- The value of the Content-Type
headerSet<Cookie> getCookies()
The cookies are mutable.
Cookie cookie(String name, String value)
The cookie will have no expiry. Use the returned cookie object to fine tune the cookie.
name
- The name of the cookievalue
- The value of the cookieCookie expireCookie(String name)
name
- The name of the cookie to expire.@NonBlocking void sendFile(Background background, Path file)
Prefer sendFile(ratpack.handling.Background, java.nio.file.attribute.BasicFileAttributes, java.nio.file.Path)
where
the file attributes have already been retrieved to avoid another IO operation.
background
- the background operation manager to usefile
- The file whose contents are to be used as the response body@NonBlocking void sendFile(Background background, BasicFileAttributes attributes, Path file)
background
- the background operation manager to useattributes
- The attributes of the file, used for the headers.file
- The file whose contents are to be used as the response body