public class HttpServerResponse extends Object implements WriteStream<Buffer>
An instance of this is created and associated to every instance of
HttpServerRequest
that.
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.
original
non RX-ified interface using Vert.x codegen.Constructor and Description |
---|
HttpServerResponse(HttpServerResponse delegate) |
Modifier and Type | Method and Description |
---|---|
HttpServerResponse |
bodyEndHandler(Handler<Void> handler)
Provide a handler that will be called just before the last part of the body is written to the wire
and the response is ended.
|
void |
close()
Close the underlying TCP connection corresponding to the request.
|
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
HttpServerResponse but writes some data to the response body before ending. |
void |
end(String chunk)
Same as
end(java.lang.String) but writes a String in UTF-8 encoding before ending the response. |
void |
end(String chunk,
String enc)
Same as
HttpServerResponse but writes a String with the specified encoding before ending the response. |
boolean |
ended() |
HttpServerResponse |
exceptionHandler(Handler<Throwable> handler)
Set an exception handler on the write stream.
|
Object |
getDelegate() |
int |
getStatusCode() |
String |
getStatusMessage() |
MultiMap |
headers() |
HttpServerResponse |
headersEndHandler(Handler<Future> handler)
Provide a handler that will be called just before the headers are written to the wire.
|
boolean |
headWritten() |
boolean |
isChunked() |
static HttpServerResponse |
newInstance(HttpServerResponse arg) |
HttpServerResponse |
putHeader(String name,
String value)
Put an HTTP header
|
HttpServerResponse |
putTrailer(String name,
String value)
Put an HTTP trailer
|
HttpServerResponse |
sendFile(String filename)
Ask the OS 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)
Like
sendFile(java.lang.String) but providing a handler which will be notified once the file has been completely
written to the wire. |
rx.Observable<Void> |
sendFileObservable(String filename)
Like
sendFile(java.lang.String) but providing a handler which will be notified once the file has been completely
written to the wire. |
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(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 . |
boolean |
writeQueueFull()
This will return
true if there are more bytes in the write queue than the value set using setWriteQueueMaxSize(int) |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
newInstance
newInstance
public HttpServerResponse(HttpServerResponse delegate)
public Object getDelegate()
getDelegate
in interface StreamBase
getDelegate
in interface WriteStream<Buffer>
public boolean writeQueueFull()
true
if there are more bytes in the write queue than the value set using setWriteQueueMaxSize(int)
writeQueueFull
in interface WriteStream<Buffer>
public HttpServerResponse exceptionHandler(Handler<Throwable> handler)
WriteStream
exceptionHandler
in interface StreamBase
exceptionHandler
in interface WriteStream<Buffer>
handler
- the exception handlerpublic HttpServerResponse write(Buffer data)
WriteStream
WriteStream.writeQueueFull()
method before writing. This is done automatically if using a Pump
.write
in interface WriteStream<Buffer>
data
- the data to writepublic HttpServerResponse setWriteQueueMaxSize(int maxSize)
WriteStream
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>
maxSize
- the max size of the write streampublic HttpServerResponse drainHandler(Handler<Void> handler)
WriteStream
Pump
for an example of this being used.drainHandler
in interface WriteStream<Buffer>
handler
- the handlerpublic int getStatusCode()
200
representing OK
.public HttpServerResponse setStatusCode(int statusCode)
statusCode
- public String getStatusMessage()
setStatusCode(int)
has been set to.public HttpServerResponse setStatusMessage(String statusMessage)
statusMessage
- public 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 the total size
of any data that is written in the respone body must be set in the Content-Length
header before any
data is written out.
An HTTP chunked response is typically used when you do not know the total size of the request body up front.
chunked
- public boolean isChunked()
public MultiMap headers()
public HttpServerResponse putHeader(String name, String value)
name
- the header namevalue
- the header value.public MultiMap trailers()
public HttpServerResponse putTrailer(String name, String value)
name
- the trailer namevalue
- the trailer valuepublic HttpServerResponse closeHandler(Handler<Void> handler)
handler
- the handlerpublic HttpServerResponse write(String chunk, String enc)
String
to the response body, encoded using the encoding enc
.chunk
- the string to writeenc
- the encoding to usepublic HttpServerResponse write(String chunk)
String
to the response body, encoded in UTF-8.chunk
- the string to writepublic void end(String chunk)
end(java.lang.String)
but writes a String in UTF-8 encoding before ending the response.chunk
- the string to write before ending the responsepublic void end(String chunk, String enc)
HttpServerResponse
but writes a String with the specified encoding before ending the response.chunk
- the string to write before ending the responseenc
- the encoding to usepublic void end(Buffer chunk)
HttpServerResponse
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 @code{Content-Length} header will be automatically set.chunk
- the buffer to write before ending the responsepublic void end()
Once the response has ended, it cannot be used any more.
public HttpServerResponse sendFile(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.The actual serve is asynchronous and may not complete until some time after this method has returned.
filename
- path to the file to servepublic HttpServerResponse sendFile(String filename, Handler<AsyncResult<Void>> resultHandler)
sendFile(java.lang.String)
but providing a handler which will be notified once the file has been completely
written to the wire.filename
- path to the file to serveresultHandler
- handler that will be called on completionpublic rx.Observable<Void> sendFileObservable(String filename)
sendFile(java.lang.String)
but providing a handler which will be notified once the file has been completely
written to the wire.filename
- path to the file to servepublic void close()
public boolean ended()
public boolean headWritten()
public HttpServerResponse headersEndHandler(Handler<Future> handler)
This provides a hook allowing you to add any more headers or do any more operations before this occurs. The handler will be passed a future, when you've completed the work you want to do you should complete (or fail) the future. This can be done after the handler has returned.
handler
- the handlerpublic HttpServerResponse bodyEndHandler(Handler<Void> handler)
This provides a hook allowing you to do any more operations before this occurs.
handler
- the handlerpublic static HttpServerResponse newInstance(HttpServerResponse arg)
Copyright © 2015. All Rights Reserved.