public abstract class HttpServerResponse extends java.lang.Object implements WriteStream
An instance of this class is 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 partcularHTTP 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 | Field and Description |
---|---|
int |
statusCode
The HTTP status code of the response.
|
java.lang.String |
statusMessage
The HTTP status message of the response.
|
Modifier | Constructor and Description |
---|---|
protected |
HttpServerResponse() |
Modifier and Type | Method and Description |
---|---|
abstract void |
close()
Close the underlying TCP connection
|
abstract void |
closeHandler(Handler<java.lang.Void> handler)
Set a close handler for the response.
|
abstract void |
end()
Ends the response.
|
abstract void |
end(Buffer chunk)
Same as
end() but writes some data to the response body before ending. |
abstract void |
end(java.lang.String chunk)
Same as
end(Buffer) but writes a String with the default encoding before ending the response. |
abstract 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. |
abstract java.util.Map<java.lang.String,java.lang.Object> |
headers() |
abstract HttpServerResponse |
putHeader(java.lang.String name,
java.lang.Object value)
Put an HTTP header - fluent API
|
abstract HttpServerResponse |
putTrailer(java.lang.String name,
java.lang.Object value)
Put an HTTP trailer - fluent API
|
abstract 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. |
abstract 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. |
abstract java.util.Map<java.lang.String,java.lang.Object> |
trailers() |
abstract HttpServerResponse |
write(Buffer chunk)
Write a
Buffer to the response body. |
abstract HttpServerResponse |
write(Buffer chunk,
Handler<java.lang.Void> doneHandler)
Write a
Buffer to the response body. |
abstract HttpServerResponse |
write(java.lang.String chunk)
Write a
String to the response body, encoded in UTF-8. |
abstract HttpServerResponse |
write(java.lang.String chunk,
Handler<java.lang.Void> doneHandler)
Write a
String to the response body, encoded in UTF-8. |
abstract HttpServerResponse |
write(java.lang.String chunk,
java.lang.String enc)
Write a
String to the response body, encoded using the encoding enc . |
abstract HttpServerResponse |
write(java.lang.String chunk,
java.lang.String enc,
Handler<java.lang.Void> doneHandler)
Write a
String to the response body, encoded with encoding enc . |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
drainHandler, exceptionHandler, setWriteQueueMaxSize, writeBuffer, writeQueueFull
public int statusCode
200
representing OK
.public java.lang.String statusMessage
statusCode
has been set to.public abstract 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.
public abstract java.util.Map<java.lang.String,java.lang.Object> headers()
public abstract HttpServerResponse putHeader(java.lang.String name, java.lang.Object value)
name
- The header namevalue
- The header valuepublic abstract java.util.Map<java.lang.String,java.lang.Object> trailers()
public abstract HttpServerResponse putTrailer(java.lang.String name, java.lang.Object value)
name
- The trailer namevalue
- The trailer valuepublic abstract void closeHandler(Handler<java.lang.Void> handler)
handler
- public abstract HttpServerResponse write(Buffer chunk)
Buffer
to the response body.public abstract HttpServerResponse write(java.lang.String chunk, java.lang.String enc)
String
to the response body, encoded using the encoding enc
.public abstract HttpServerResponse write(java.lang.String chunk)
String
to the response body, encoded in UTF-8.public abstract HttpServerResponse write(Buffer chunk, Handler<java.lang.Void> doneHandler)
Buffer
to the response body. The doneHandler
is called after the buffer is actually written to the wire.public abstract HttpServerResponse write(java.lang.String chunk, java.lang.String enc, Handler<java.lang.Void> doneHandler)
String
to the response body, encoded with encoding enc
. The doneHandler
is called
after the buffer is actually written to the wire.public abstract HttpServerResponse write(java.lang.String chunk, Handler<java.lang.Void> doneHandler)
String
to the response body, encoded in UTF-8. The doneHandler
is called after the buffer
is actually written to the wire.public abstract void end(java.lang.String chunk)
end(Buffer)
but writes a String with the default encoding before ending the response.public abstract void end(java.lang.String chunk, java.lang.String enc)
end(Buffer)
but writes a String with the specified encoding before ending the response.public abstract 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 setpublic abstract void end()
Once the response has ended, it cannot be used any more.
public abstract 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.public abstract void close()