public class HttpResponse extends HttpMessage
An HTTP response message sent as a response to an HttpRequest. When this response is sent to an HttpRequest’s sender, the connection stream will be closed if openChannel has not been called on the request, and will be flushed but not closed if openChannel has been called on the request.
| Modifier and Type | Class and Description |
|---|---|
static class |
HttpResponse.Builder |
| Constructor and Description |
|---|
HttpResponse(ActorRef<? super WebMessage> from,
HttpResponse.Builder builder) |
HttpResponse(ActorRef<? super WebMessage> from,
HttpResponse httpResponse)
Use when forwarding
|
| Modifier and Type | Method and Description |
|---|---|
protected String |
contentString() |
static HttpResponse.Builder |
error(HttpRequest request,
int status,
String body)
Creates an
HttpResponse indicating an error, with a given status code and a text body. |
static HttpResponse.Builder |
error(HttpRequest request,
int status,
Throwable cause)
Creates an
HttpResponse indicating an error, with a given status code and an attached exception that may be reported back to the client. |
ByteBuffer |
getByteBufferBody()
The message’s binary body, if it has one;
null otherwise. |
Charset |
getCharacterEncoding()
The
Charset representing character encoding used in the body of this message. |
int |
getContentLength()
The length, in bytes, of the message body and made available by the input stream, or
-1 if the length is not known or is greater than Integer.MAX_VALUE. |
String |
getContentType()
The MIME type of the body of the request, or
null if the type is not known For example, text/html; charset=UTF-8. |
Collection<Cookie> |
getCookies()
A collection all of the
Cookie objects the client sent with this message. |
Throwable |
getError()
An exception optionally associated with an error status code.
|
ActorRef<WebMessage> |
getFrom()
The actor that sent this message.
|
Multimap<String,String> |
getHeaders()
A multimap of the headers contained in this message and (all) their values.
|
String |
getRedirectPath()
The redirect URL target if this is a
redirect response. |
HttpRequest |
getRequest()
The
HttpRequest this is a response to. |
int |
getStatus()
The response’s HTTP status code.
|
String |
getStringBody()
The message’s text body, if it has one;
null otherwise. |
static HttpResponse.Builder |
ok(HttpRequest request,
ByteBuffer body)
Creates an
HttpResponse with a binary body and response code 200. |
static HttpResponse.Builder |
ok(HttpRequest request,
String body)
Creates an
HttpResponse with a text body and response code 200. |
static HttpResponse.Builder |
redirect(HttpRequest request,
String redirectPath)
Sends a temporary redirect response to the client using the specified redirect location URL and clears the buffer.
|
boolean |
shouldStartActor() |
getHeader, getHeaderValuestoStringpublic HttpResponse(ActorRef<? super WebMessage> from, HttpResponse httpResponse)
Use when forwarding
from - httpResponse - public HttpResponse(ActorRef<? super WebMessage> from, HttpResponse.Builder builder)
public static HttpResponse.Builder ok(HttpRequest request, String body)
Creates an HttpResponse with a text body and response code 200.
request - the HttpRequest this is a response to.body - the response bodyHttpResponse.Builder that can be used to add headers and other metadata to the response.public static HttpResponse.Builder ok(HttpRequest request, ByteBuffer body)
Creates an HttpResponse with a binary body and response code 200.
request - the HttpRequest this is a response to.body - the response bodyHttpResponse.Builder that can be used to add headers and other metadata to the response.public static HttpResponse.Builder error(HttpRequest request, int status, Throwable cause)
Creates an HttpResponse indicating an error, with a given status code and an attached exception that may be reported back to the client.
request - the HttpRequest this is a response to.status - the response status codecause - the exception that caused the errorHttpResponse.Builder that can be used to add headers and other metadata to the response.public static HttpResponse.Builder error(HttpRequest request, int status, String body)
Creates an HttpResponse indicating an error, with a given status code and a text body.
request - the HttpRequest this is a response to.status - the response status codebody - the response bodyHttpResponse.Builder that can be used to add headers and other metadata to the response.public static HttpResponse.Builder redirect(HttpRequest request, String redirectPath)
Sends a temporary redirect response to the client using the specified redirect location URL and clears the buffer. The status code is set to SC_FOUND 302 (Found). This method can accept relative URLs; the container must convert the relative URL to an absolute URL before sending the response to the client. If the location is relative without a leading ‘/’ the container interprets it as relative to the current request URI. If the location is relative with a leading ‘/’ the container interprets it as relative to the container root. If the location is relative with two leading ‘/’ the container interprets it as a network-path reference (see RFC 3986: Uniform Resource Identifier (URI): Generic Syntax, section 4.2 "Relative Reference").
redirectPath - the redirect location URLpublic ActorRef<WebMessage> getFrom()
WebMessageThe actor that sent this message. This can be a virtual actor representing the web client.
getFrom in interface FromMessagegetFrom in class WebMessagepublic String getContentType()
HttpMessageThe MIME type of the body of the request, or null if the type is not known For example, text/html; charset=UTF-8.
getContentType in class HttpMessagepublic Charset getCharacterEncoding()
HttpMessageThe Charset representing character encoding used in the body of this message. This method returns null if the message does not specify a character encoding.
getCharacterEncoding in class HttpMessagepublic int getContentLength()
HttpMessageThe length, in bytes, of the message body and made available by the input stream, or -1 if the length is not known or is greater than Integer.MAX_VALUE.
getContentLength in class HttpMessagepublic String getStringBody()
WebMessageThe message’s text body, if it has one; null otherwise.
getStringBody in class WebMessagepublic ByteBuffer getByteBufferBody()
WebMessageThe message’s binary body, if it has one; null otherwise.
getByteBufferBody in class WebMessagepublic Collection<Cookie> getCookies()
HttpMessageA collection all of the Cookie objects the client sent with this message. This method returns an empty collection if no cookies were sent.
getCookies in class HttpMessagepublic Multimap<String,String> getHeaders()
HttpMessageA multimap of the headers contained in this message and (all) their values. If the request has no headers, returns an empty multimap.
getHeaders in class HttpMessagepublic HttpRequest getRequest()
The HttpRequest this is a response to.
public int getStatus()
The response’s HTTP status code.
public Throwable getError()
An exception optionally associated with an error status code.
public String getRedirectPath()
The redirect URL target if this is a redirect response.
public boolean shouldStartActor()
protected String contentString()
contentString in class WebMessage