public class HttpResponse extends Object
Constructor and Description |
---|
HttpResponse()
Creates an empty
HttpResponse . |
Modifier and Type | Method and Description |
---|---|
void |
addCookie(Cookie cookie)
Adds cookie to the response.
|
void |
addHeader(String name,
String value)
Adds a response header field.
|
void |
addHeaders(Map<String,String> map)
Adds a map of header fields.
|
String |
getHeader(String key)
Returns the header field value associated to the given header field
key . |
protected long |
getLength()
Returns the length (in bytes) of the response data or
-1 if the length is unknown. |
String |
getMimeType()
Returns the MIME-TYPE of the response.
|
String |
getStatus()
Returns the response status.
|
Cookie |
removeCookie(String name)
Remove the cookie mapped by name.
|
void |
setData(byte[] data)
Set the data contained by this response.
|
void |
setData(InputStream dataStream)
Sets the
InputStream from which the response data can be read. |
void |
setData(InputStream dataStream,
long length)
Sets the
InputStream from which the response data can be read. |
void |
setData(String data)
Set the data contained by this response.
|
void |
setData(String data,
String encoding)
|
void |
setMimeType(String mimeType)
Set the response MIME-TYPE.
|
void |
setStatus(String status)
Set the response status.
|
public HttpResponse()
HttpResponse
.public void addCookie(Cookie cookie)
cookie
- the cookie to add.public void addHeader(String name, String value)
addCookie(String, String, String, String, int, boolean, boolean)
name
- name of the header field to set.value
- value of the header filed.public void addHeaders(Map<String,String> map)
addCookie(String, String, String, String, int, boolean, boolean)
map
- map of headers to add@Nullable public String getHeader(@Nullable String key)
key
.key
- a header field name (if null
, null
is returned).null
if the header field is not found.protected long getLength()
-1
if the length is unknown.@Nullable public String getMimeType()
public String getStatus()
@Nullable public Cookie removeCookie(String name)
name
- of the cookiepublic void setData(@Nullable byte[] data)
data
- the response data as a byte array (set to empty if null
is given)public void setData(InputStream dataStream)
InputStream
from which the response data can be read.
This method should be used only if response data length is not known in advance. If the length is known by
advance the setData(InputStream, long)
should be used instead of this one. When response data is
specified with this method, the response must be sent using the chunked transfer-coding which increase the
response message size.
dataStream
- the InputStream
from which the response data can be read, the stream will be closed
automatically when the response is sent.public void setData(InputStream dataStream, long length)
InputStream
from which the response data can be read.
This method should be used when response data length is known in advance. It allows to transfer response body without using the chunked transfer-coding. This reduces response message size.
dataStream
- the InputStream
from which the response data can be read, the stream will be closed
automatically when the response is sent.length
- the number of byte to be read from the InputStream
.public void setData(@Nullable String data)
data
- the response data as a byte array (set to empty if null
is given)public void setData(@Nullable String data, String encoding) throws UnsupportedEncodingException
data
- the String
to be used as response body.encoding
- the encoding used to transform the String
data
to bytes. The following encodings
can be used:
ISO-8859-1
ISO-8859-1 encoding, always supported by the platform
UTF-8
UTF-8 encoding, only supported if the "Embed UTF-8 encoding" option is enabled
in the Run Configurations. If this option is not set, an UnsupportedEncodingException
is
thrown.
US-ASCII
US-ASCII encoding
UnsupportedEncodingException
- when the specified encoding is not supported.public final void setMimeType(String mimeType)
mimeType
- the response MIME-TYPE to set.public final void setStatus(String status)
status
- the response status to set. Should be one of the HTTP_STATUS_*
constants defined in
HttpConstants