public class HTTPResponse
extends java.lang.Object
Constructor and Description |
---|
HTTPResponse()
Creates an empty
HTTPResponse . |
HTTPResponse(byte[] data)
Creates a new
HTTPResponse using the given byte array as response data. |
HTTPResponse(java.io.InputStream data)
Creates a new
HTTPResponse using the given InputStream as the response data. |
HTTPResponse(java.io.InputStream data,
int length)
Creates a new
HTTPResponse using the given InputStream as the response data. |
HTTPResponse(java.lang.String data)
Creates a new
HTTPResponse using the given String as response data. |
HTTPResponse(java.lang.String data,
java.lang.String encoding)
|
HTTPResponse(java.lang.String status,
java.lang.String mimeType,
java.io.InputStream body)
|
HTTPResponse(java.lang.String status,
java.lang.String mimeType,
java.lang.String body)
|
Modifier and Type | Method and Description |
---|---|
void |
addHeaderField(java.lang.String name,
java.lang.String value)
Adds a response header field.
|
static HTTPResponse |
createError(java.lang.String status,
java.lang.String msg)
|
static HTTPResponse |
createResponseFromStatus(java.lang.String status)
Creates a
HTTPResponse with given status and empty body. |
java.util.Map<java.lang.String,java.lang.String> |
getHeader()
Returns the response header.
|
java.lang.String |
getHeaderField(java.lang.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. |
java.lang.String |
getMimeType()
Returns the MIME-TYPE of the response.
|
java.lang.String |
getStatus()
Returns the response status.
|
void |
setMimeType(java.lang.String mimeType)
Set the response MIME-TYPE.
|
void |
setStatus(java.lang.String status)
Set the response status.
|
public HTTPResponse()
HTTPResponse
.public HTTPResponse(byte[] data)
HTTPResponse
using the given byte array as response data.data
- the data to send through response (as a raw byte array).public HTTPResponse(java.io.InputStream data)
HTTPResponse
using the given InputStream
as the response data.data
- the data to send through response (as a stream), the stream will be closed automatically when the
response is sent.public HTTPResponse(java.io.InputStream data, int length)
HTTPResponse
using the given InputStream
as the response data.data
- the data to send through response (as a stream), the stream will be closed automatically when the
response is sent.length
- the length of the response.public HTTPResponse(java.lang.String data)
HTTPResponse
using the given String
as response data. The data
is
transformed into bytes using the ISO-8859-1
encoding.data
- the data to send through response (as a raw string)public HTTPResponse(java.lang.String data, java.lang.String encoding) throws java.io.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
java.io.UnsupportedEncodingException
- when the specified encoding is not supported.public HTTPResponse(java.lang.String status, java.lang.String mimeType, java.io.InputStream body)
status
- the status of the response.mimeType
- the mime type of the response.body
- the InputStream
to be used as response data, the stream will be closed automatically when the
response is sent.public HTTPResponse(java.lang.String status, java.lang.String mimeType, java.lang.String body)
status
- the status of the response.mimeType
- the mime type of the response.body
- the String
to be used as response data.public static HTTPResponse createResponseFromStatus(java.lang.String status)
HTTPResponse
with given status and empty body.status
- the status of the response.public void addHeaderField(java.lang.String name, java.lang.String value)
name
- name of the header field to set.value
- value of the header filed.public java.util.Map<java.lang.String,java.lang.String> getHeader()
Map
of (String,String) representing the HTTP header fields (may be empty).public java.lang.String getHeaderField(java.lang.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.public java.lang.String getMimeType()
public java.lang.String getStatus()
public final void setMimeType(java.lang.String mimeType)
mimeType
- the response MIME-TYPE to set.public final void setStatus(java.lang.String status)
status
- the response status to set. Should be one of the HTTP_STATUS_*
constants defined in
HTTPConstants
public static HTTPResponse createError(java.lang.String status, java.lang.String msg)
status
- the error status. One of HTTP_STATUS_*
constant of the HTTPConstants
interface.msg
- an optional error message to add in response.HTTPResponse
that represent the error.HTTPConstants.HTTP_STATUS_BADREQUEST
,
HTTPConstants.HTTP_STATUS_FORBIDDEN
,
HTTPConstants.HTTP_STATUS_INTERNALERROR
,
HTTPConstants.HTTP_STATUS_MEDIA_TYPE
,
HTTPConstants.HTTP_STATUS_METHOD
,
HTTPConstants.HTTP_STATUS_NOTACCEPTABLE
,
HTTPConstants.HTTP_STATUS_NOTFOUND
,
HTTPConstants.HTTP_STATUS_NOTIMPLEMENTED
,
HTTPConstants.HTTP_STATUS_NOTMODIFIED
,
HTTPConstants.HTTP_STATUS_OK
,
HTTPConstants.HTTP_STATUS_REDIRECT