Package ej.hoka.http
Class HttpResponse
- java.lang.Object
-
- ej.hoka.http.HttpResponse
-
public class HttpResponse extends Object
Represents a HTTP Response.
-
-
Constructor Summary
Constructors Constructor Description HttpResponse()Creates an emptyHttpResponse.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddCookie(Cookie cookie)Adds cookie to the response.voidaddHeader(String name, String value)Adds a response header field.voidaddHeaders(Map<String,String> map)Adds a map of header fields.StringgetHeader(String key)Returns the header field value associated to the given header fieldkey.protected longgetLength()Returns the length (in bytes) of the response data or-1if the length is unknown.StringgetMimeType()Returns the MIME-TYPE of the response.StringgetStatus()Returns the response status.CookieremoveCookie(String name)Remove the cookie mapped by name.voidsetData(byte[] data)Set the data contained by this response.voidsetData(InputStream dataStream)Sets theInputStreamfrom which the response data can be read.voidsetData(InputStream dataStream, long length)Sets theInputStreamfrom which the response data can be read.voidsetData(String data)Set the data contained by this response.voidsetData(String data, String encoding)voidsetMimeType(String mimeType)Set the response MIME-TYPE.voidsetStatus(String status)Set the response status.
-
-
-
Constructor Detail
-
HttpResponse
public HttpResponse()
Creates an emptyHttpResponse.
-
-
Method Detail
-
addHeader
public void addHeader(String name, String value)
Adds a response header field. For Cookies @seeaddCookie(String, String, String, String, int, boolean, boolean)- Parameters:
name- name of the header field to set.value- value of the header filed.
-
addHeaders
public void addHeaders(Map<String,String> map)
Adds a map of header fields. For Cookies @seeaddCookie(String, String, String, String, int, boolean, boolean)- Parameters:
map- map of headers to add
-
getHeader
@Nullable public String getHeader(@Nullable String key)
Returns the header field value associated to the given header fieldkey.- Parameters:
key- a header field name (ifnull,nullis returned).- Returns:
- the replied header field value,
nullif the header field is not found.
-
getLength
protected long getLength()
Returns the length (in bytes) of the response data or-1if the length is unknown.- Returns:
- the length (in bytes) of the response data.
-
getMimeType
@Nullable public String getMimeType()
Returns the MIME-TYPE of the response.- Returns:
- the response MIME-TYPE.
-
getStatus
public String getStatus()
Returns the response status.- Returns:
- the response status.
-
setData
public void setData(@Nullable String data)
Set the data contained by this response.- Parameters:
data- the response data as a byte array (set to empty ifnullis given)
-
setData
public void setData(@Nullable String data, String encoding) throws UnsupportedEncodingException
- Parameters:
data- theStringto be used as response body.encoding- the encoding used to transform theStringdatato bytes. The following encodings can be used:ISO-8859-1ISO-8859-1 encoding, always supported by the platformUTF-8UTF-8 encoding, only supported if the "Embed UTF-8 encoding" option is enabled in the Run Configurations. If this option is not set, anUnsupportedEncodingExceptionis thrown.US-ASCIIUS-ASCII encoding
- Throws:
UnsupportedEncodingException- when the specified encoding is not supported.
-
setData
public void setData(@Nullable byte[] data)
Set the data contained by this response.- Parameters:
data- the response data as a byte array (set to empty ifnullis given)
-
setData
public void setData(InputStream dataStream)
Sets theInputStreamfrom 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.- Parameters:
dataStream- theInputStreamfrom which the response data can be read, the stream will be closed automatically when the response is sent.
-
setData
public void setData(InputStream dataStream, long length)
Sets theInputStreamfrom 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.
- Parameters:
dataStream- theInputStreamfrom 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 theInputStream.
-
setMimeType
public final void setMimeType(String mimeType)
Set the response MIME-TYPE.- Parameters:
mimeType- the response MIME-TYPE to set.
-
setStatus
public final void setStatus(String status)
Set the response status.- Parameters:
status- the response status to set. Should be one of theHTTP_STATUS_*constants defined inHttpConstants
-
addCookie
public void addCookie(Cookie cookie)
Adds cookie to the response. Can be invoked multiple times to insert more than one cookie.- Parameters:
cookie- the cookie to add.
-
-