public interface HttpServerResponse extends WriteStream<HttpServerResponse>
Instances of this class are created and associated to every instance of
HttpServerRequest
that is created.
It allows the developer to control the HTTP response that is sent back to the client for a particular HTTP request. It contains methods that allow HTTP headers and trailers to be set, and for a body to be written out to the response.
It also allows files to be streamed by the kernel directly from disk to the outgoing HTTP connection, bypassing user space altogether (where supported by the underlying operating system). This is a very efficient way of serving files from the server since buffers do not have to be read one by one from the file and written to the outgoing socket.
It implements WriteStream
so it can be used with
Pump
to pump data with flow control.
Instances of this class are not thread-safe
Modifier and Type | Method and Description |
---|---|
void |
close()
Close the underlying TCP connection
|
HttpServerResponse |
closeHandler(Handler<java.lang.Void> handler)
Set a close handler for the response.
|
void |
end()
Ends the response.
|
void |
end(Buffer chunk)
Same as
end() but writes some data to the response body before ending. |
void |
end(java.lang.String chunk)
Same as
end(Buffer) but writes a String with the default encoding before ending the response. |
void |
end(java.lang.String chunk,
java.lang.String enc)
Same as
end(Buffer) but writes a String with the specified encoding before ending the response. |
int |
getStatusCode()
The HTTP status code of the response.
|
java.lang.String |
getStatusMessage()
The HTTP status message of the response.
|
MultiMap |
headers() |
boolean |
isChunked()
Is the response chunked?
|
HttpServerResponse |
putHeader(java.lang.CharSequence name,
java.lang.CharSequence value) |
HttpServerResponse |
putHeader(java.lang.CharSequence name,
java.lang.Iterable<java.lang.CharSequence> values) |
HttpServerResponse |
putHeader(java.lang.String name,
java.lang.Iterable<java.lang.String> values)
Put an HTTP header - fluent API
|
HttpServerResponse |
putHeader(java.lang.String name,
java.lang.String value)
Put an HTTP header - fluent API
|
HttpServerResponse |
putTrailer(java.lang.CharSequence name,
java.lang.CharSequence value) |
HttpServerResponse |
putTrailer(java.lang.CharSequence name,
java.lang.Iterable<java.lang.CharSequence> value) |
HttpServerResponse |
putTrailer(java.lang.String name,
java.lang.Iterable<java.lang.String> values)
Put an HTTP trailer - fluent API
|
HttpServerResponse |
putTrailer(java.lang.String name,
java.lang.String value)
Put an HTTP trailer - fluent API
|
HttpServerResponse |
sendFile(java.lang.String filename)
Tell the kernel to stream a file as specified by
filename directly
from disk to the outgoing connection, bypassing userspace altogether
(where supported by the underlying operating system. |
HttpServerResponse |
sendFile(java.lang.String filename,
Handler<AsyncResult<java.lang.Void>> resultHandler)
Same as
sendFile(String) but also takes a handler that will be called when the send has completed or
a failure has occurred |
HttpServerResponse |
sendFile(java.lang.String filename,
java.lang.String notFoundFile)
Same as
sendFile(String) but also takes the path to a resource to serve if the resource is not found |
HttpServerResponse |
sendFile(java.lang.String filename,
java.lang.String notFoundFile,
Handler<AsyncResult<java.lang.Void>> resultHandler)
Same as
sendFile(String, String) but also takes a handler that will be called when the send has completed or
a failure has occurred |
HttpServerResponse |
setChunked(boolean chunked)
If
chunked is true , this response will use HTTP chunked encoding, and each call to write to the body
will correspond to a new HTTP chunk sent on the wire. |
HttpServerResponse |
setStatusCode(int statusCode)
Set the status code
|
HttpServerResponse |
setStatusMessage(java.lang.String statusMessage)
Set the status message
|
MultiMap |
trailers() |
HttpServerResponse |
write(Buffer chunk)
Write a
Buffer to the response body. |
HttpServerResponse |
write(java.lang.String chunk)
Write a
String to the response body, encoded in UTF-8. |
HttpServerResponse |
write(java.lang.String chunk,
java.lang.String enc)
Write a
String to the response body, encoded using the encoding enc . |
exceptionHandler
drainHandler, setWriteQueueMaxSize, writeQueueFull
int getStatusCode()
200
representing OK
.HttpServerResponse setStatusCode(int statusCode)
java.lang.String getStatusMessage()
setStatusCode(int)
has been set to.HttpServerResponse setStatusMessage(java.lang.String statusMessage)
HttpServerResponse setChunked(boolean chunked)
chunked
is true
, this response will use HTTP chunked encoding, and each call to write to the body
will correspond to a new HTTP chunk sent on the wire.
If chunked encoding is used the HTTP header Transfer-Encoding
with a value of Chunked
will be
automatically inserted in the response.
If chunked
is false
, this response will not use HTTP chunked encoding, and therefore if any data is written the
body of the response, the total size of that data must be set in the Content-Length
header before any
data is written to the response body.
An HTTP chunked response is typically used when you do not know the total size of the request body up front.
boolean isChunked()
MultiMap headers()
HttpServerResponse putHeader(java.lang.String name, java.lang.String value)
name
- The header namevalue
- The header value.HttpServerResponse putHeader(java.lang.CharSequence name, java.lang.CharSequence value)
HttpServerResponse putHeader(java.lang.String name, java.lang.Iterable<java.lang.String> values)
name
- The header namevalues
- The header values.HttpServerResponse putHeader(java.lang.CharSequence name, java.lang.Iterable<java.lang.CharSequence> values)
MultiMap trailers()
HttpServerResponse putTrailer(java.lang.String name, java.lang.String value)
name
- The trailer namevalue
- The trailer valueHttpServerResponse putTrailer(java.lang.CharSequence name, java.lang.CharSequence value)
HttpServerResponse putTrailer(java.lang.String name, java.lang.Iterable<java.lang.String> values)
name
- The trailer namevalues
- The trailer valuesHttpServerResponse putTrailer(java.lang.CharSequence name, java.lang.Iterable<java.lang.CharSequence> value)
HttpServerResponse closeHandler(Handler<java.lang.Void> handler)
handler
- HttpServerResponse write(Buffer chunk)
Buffer
to the response body.write
in interface WriteStream<HttpServerResponse>
HttpServerResponse write(java.lang.String chunk, java.lang.String enc)
String
to the response body, encoded using the encoding enc
.HttpServerResponse write(java.lang.String chunk)
String
to the response body, encoded in UTF-8.void end(java.lang.String chunk)
end(Buffer)
but writes a String with the default encoding before ending the response.void end(java.lang.String chunk, java.lang.String enc)
end(Buffer)
but writes a String with the specified encoding before ending the response.void end(Buffer chunk)
end()
but writes some data to the response body before ending. If the response is not chunked and
no other data has been written then the Content-Length header will be automatically setvoid end()
Once the response has ended, it cannot be used any more.
HttpServerResponse sendFile(java.lang.String filename)
filename
directly
from disk to the outgoing connection, bypassing userspace altogether
(where supported by the underlying operating system.
This is a very efficient way to serve files.HttpServerResponse sendFile(java.lang.String filename, java.lang.String notFoundFile)
sendFile(String)
but also takes the path to a resource to serve if the resource is not foundHttpServerResponse sendFile(java.lang.String filename, Handler<AsyncResult<java.lang.Void>> resultHandler)
sendFile(String)
but also takes a handler that will be called when the send has completed or
a failure has occurredHttpServerResponse sendFile(java.lang.String filename, java.lang.String notFoundFile, Handler<AsyncResult<java.lang.Void>> resultHandler)
sendFile(String, String)
but also takes a handler that will be called when the send has completed or
a failure has occurredvoid close()