public interface HttpServerResponse extends WriteStream<Buffer>
Represents a server-side HTTP response.<p>
Instances of this class are created and associated to every instance of
HttpServerRequest
that is created.<p>
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.<p>
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.<p>
It implements WriteStream
so it can be used with
Pump
to pump data with flow control.<p>
Instances of this class are not thread-safe<p>
Modifier and Type | Method and Description |
---|---|
HttpServerResponse |
bodyEndHandler(Handler<Void> handler) |
void |
close()
Close the underlying TCP connection
|
HttpServerResponse |
closeHandler(Handler<Void> handler)
Set a close handler for the response.
|
HttpServerResponse |
drainHandler(Handler<Void> handler)
Set a drain handler on the stream.
|
void |
end()
Ends the response.
|
void |
end(Buffer chunk)
Same as
end() but writes some data to the response body before ending. |
void |
end(String chunk)
Same as
end(Buffer) but writes a String with the default encoding before ending the response. |
void |
end(String chunk,
String enc)
Same as
end(Buffer) but writes a String with the specified encoding before ending the response. |
boolean |
ended() |
HttpServerResponse |
exceptionHandler(Handler<Throwable> handler)
Set an exception handler.
|
int |
getStatusCode()
The HTTP status code of the response.
|
String |
getStatusMessage()
The HTTP status message of the response.
|
MultiMap |
headers() |
HttpServerResponse |
headersEndHandler(Handler<Void> handler) |
boolean |
headWritten() |
boolean |
isChunked()
Is the response chunked?
|
HttpServerResponse |
putHeader(CharSequence name,
CharSequence value) |
HttpServerResponse |
putHeader(CharSequence name,
Iterable<CharSequence> values) |
HttpServerResponse |
putHeader(String name,
Iterable<String> values)
Put an HTTP header - fluent API
|
HttpServerResponse |
putHeader(String name,
String value)
Put an HTTP header - fluent API
|
HttpServerResponse |
putTrailer(CharSequence name,
CharSequence value) |
HttpServerResponse |
putTrailer(CharSequence name,
Iterable<CharSequence> value) |
HttpServerResponse |
putTrailer(String name,
Iterable<String> values)
Put an HTTP trailer - fluent API
|
HttpServerResponse |
putTrailer(String name,
String value)
Put an HTTP trailer - fluent API
|
HttpServerResponse |
sendFile(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(String filename,
Handler<AsyncResult<Void>> resultHandler) |
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.<p>
If chunked encoding is used the HTTP header Transfer-Encoding with a value of Chunked will be
automatically inserted in the response.<p>
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 <b>before</b> any
data is written to the response body.<p>
An HTTP chunked response is typically used when you do not know the total size of the request body up front. |
HttpServerResponse |
setStatusCode(int statusCode)
Set the status code
|
HttpServerResponse |
setStatusMessage(String statusMessage)
Set the status message
|
HttpServerResponse |
setWriteQueueMaxSize(int maxSize)
Set the maximum size of the write queue to
maxSize . |
MultiMap |
trailers() |
HttpServerResponse |
write(Buffer data)
Write some data to the stream.
|
HttpServerResponse |
write(String chunk)
Write a
String to the response body, encoded in UTF-8. |
HttpServerResponse |
write(String chunk,
String enc)
Write a
String to the response body, encoded using the encoding enc . |
writeQueueFull
HttpServerResponse exceptionHandler(Handler<Throwable> handler)
StreamBase
Set an exception handler.
exceptionHandler
in interface StreamBase
exceptionHandler
in interface WriteStream<Buffer>
HttpServerResponse write(Buffer data)
WriteStream
Write some data to the stream. The data is put on an internal write queue, and the write actually happens
asynchronously. To avoid running out of memory by putting too much on the write queue,
check the WriteStream.writeQueueFull()
method before writing. This is done automatically if using a Pump
.
write
in interface WriteStream<Buffer>
HttpServerResponse setWriteQueueMaxSize(int maxSize)
WriteStream
Set the maximum size of the write queue to maxSize
. You will still be able to write to the stream even
if there is more than maxSize
bytes in the write queue. This is used as an indicator by classes such as
Pump
to provide flow control.
setWriteQueueMaxSize
in interface WriteStream<Buffer>
HttpServerResponse drainHandler(Handler<Void> handler)
WriteStream
Set a drain handler on the stream. If the write queue is full, then the handler will be called when the write
queue has been reduced to maxSize / 2. See Pump
for an example of this being used.
drainHandler
in interface WriteStream<Buffer>
int getStatusCode()
The HTTP status code of the response. The default is 200
representing OK
.
HttpServerResponse setStatusCode(int statusCode)
Set the status code
String getStatusMessage()
The HTTP status message of the response. If this is not specified a default value will be used depending on what
setStatusCode(int)
has been set to.
HttpServerResponse setStatusMessage(String statusMessage)
Set the status message
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.<p>
If chunked encoding is used the HTTP header Transfer-Encoding
with a value of Chunked
will be
automatically inserted in the response.<p>
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 <b>before</b> any
data is written to the response body.<p>
An HTTP chunked response is typically used when you do not know the total size of the request body up front.
boolean isChunked()
Is the response chunked?
MultiMap headers()
HttpServerResponse putHeader(String name, String value)
Put an HTTP header - fluent API
name
- The header namevalue
- The header value.HttpServerResponse putHeader(CharSequence name, CharSequence value)
HttpServerResponse putHeader(String name, Iterable<String> values)
Put an HTTP header - fluent API
name
- The header namevalues
- The header values.HttpServerResponse putHeader(CharSequence name, Iterable<CharSequence> values)
MultiMap trailers()
HttpServerResponse putTrailer(String name, String value)
Put an HTTP trailer - fluent API
name
- The trailer namevalue
- The trailer valueHttpServerResponse putTrailer(CharSequence name, CharSequence value)
HttpServerResponse putTrailer(String name, Iterable<String> values)
Put an HTTP trailer - fluent API
name
- The trailer namevalues
- The trailer valuesHttpServerResponse putTrailer(CharSequence name, Iterable<CharSequence> value)
HttpServerResponse closeHandler(Handler<Void> handler)
Set a close handler for the response. This will be called if the underlying connection closes before the response is complete.
handler
- HttpServerResponse write(String chunk, String enc)
Write a String
to the response body, encoded using the encoding enc
.
HttpServerResponse write(String chunk)
Write a String
to the response body, encoded in UTF-8.
void end(String chunk)
Same as end(Buffer)
but writes a String with the default encoding before ending the response.
void end(String chunk, String enc)
Same as end(Buffer)
but writes a String with the specified encoding before ending the response.
void end(Buffer chunk)
Same as 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 set
void end()
Ends the response. If no data has been written to the response body, the actual response won’t get written until this method gets called.<p> Once the response has ended, it cannot be used any more.
HttpServerResponse sendFile(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.
This is a very efficient way to serve files.<p>
HttpServerResponse sendFile(String filename, Handler<AsyncResult<Void>> resultHandler)
void close()
Close the underlying TCP connection
boolean ended()
boolean headWritten()
HttpServerResponse headersEndHandler(Handler<Void> handler)
HttpServerResponse bodyEndHandler(Handler<Void> handler)
Copyright © 2014. All Rights Reserved.