public interface MuResponse
A response sent to a client.
The status(int)
and headers()
methods are used to set the response code and response headers.
The addCookie(Cookie)
method can be used to add a cookie to the response.
There are several ways to send data back to the client:
write(String)
to send a text response without chunking.sendChunk(String)
to send a chunk of text (unlike write
it can be called multiple times)outputStream()
to send byteswriter()
to send text as an output stream.Note: only one of the above methods can be used per response, and aside from sendChunk
it is not allowed to call the same method more than once..
Modifier and Type | Method and Description |
---|---|
void |
addCookie(Cookie cookie)
Sends a cookie to the client.
|
void |
contentType(CharSequence contentType)
Sets the Content-Type response header.
|
boolean |
hasStartedSendingData()
Specifies whether or not any response data has already been sent to the client.
|
Headers |
headers()
Gets the response headers map which can be used to specify response headers.
|
OutputStream |
outputStream()
Gets an output stream that sends an HTTP chunk each time the
write
method is called. |
void |
redirect(String url)
Redirects to the given URL.
|
void |
redirect(URI uri)
Redirects to the given URI.
|
void |
sendChunk(String text)
Immediately sends the given text to the client as a chunk.
|
int |
status() |
void |
status(int value)
Sets the response code for this request.
|
void |
write(String text)
Writes the given text as the response body for this request.
|
Future<Void> |
writeAsync(String text)
Deprecated.
For async handling, call
MuRequest.handleAsync() |
PrintWriter |
writer()
A print writer that can be used to send text to the client.
|
int status()
void status(int value)
200
value
- The response code to send to the client.@Deprecated Future<Void> writeAsync(String text)
MuRequest.handleAsync()
text
- Text to sendvoid write(String text)
Writes the given text as the response body for this request. This can only be called once.
If you want to send multiple chunks of text, see sendChunk(String)
text
- The full response body to send to the client.IllegalStateException
- Thrown if this is called twice, or this is called after any other body-writing methods.void sendChunk(String text)
text
- Text to send to the client as an HTTP chunk.IllegalStateException
- Thrown if write(String)
or outputStream()
or writer()
was
already called.void redirect(String url)
url
- The full or relative URL to redirect to.void redirect(URI uri)
uri
- The full or relative URI to redirect to.Headers headers()
response.headers().set("access-control-allow-origin", "*");
void contentType(CharSequence contentType)
contentType
- The content type of the response, for example application/json
ContentTypes
void addCookie(Cookie cookie)
Sends a cookie to the client.
Example: response.addCookie(new Cookie("user", user));
If using HTTPS, it's recommended to use response.addCookie(Cookie.secureCookie("user", user));
cookie
- A cookie to store on the client.OutputStream outputStream()
Gets an output stream that sends an HTTP chunk each time the write
method is called.
You may consider wrapping it in a BufferedOutputStream
if you want to buffer the chunks before sending to the client.
If you are writing text, you may prefer the writer()
or sendChunk(String)
methods.
PrintWriter writer()
A print writer that can be used to send text to the client. It is a convenience method, wrapping outputStream()
in a PrintWriter.
You may prefer using sendChunk(String)
or write(String)
to send text.
boolean hasStartedSendingData()
status(int)
and headers()
can no longer be changed.true
if any data has been sent to the client; otherwise false
.Copyright © 2017–2019. All rights reserved.