public class HTTPResponse
extends java.lang.Object
Represents a HTTP Response.
Modifier and Type | Field and Description |
---|---|
static HTTPResponse |
RESPONSE_BAD_REQUEST
An empty HTTP response with status code 400.
|
static HTTPResponse |
RESPONSE_FORBIDDEN
An empty HTTP response with status code 403.
|
static HTTPResponse |
RESPONSE_INTERNAL_ERROR
An empty HTTP response with status code 500.
|
static HTTPResponse |
RESPONSE_METHOD_NOT_ALLOWED
An empty HTTP response with status code 405.
|
static HTTPResponse |
RESPONSE_MOVED_PERMANENTLY
An empty HTTP response with status code 301.
|
static HTTPResponse |
RESPONSE_NOT_ACCEPTABLE
An empty HTTP response with status code 406.
|
static HTTPResponse |
RESPONSE_NOT_FOUND
An empty HTTP response with status code 404.
|
static HTTPResponse |
RESPONSE_NOT_IMPLEMENTED
An empty HTTP response with status code 501.
|
static HTTPResponse |
RESPONSE_NOT_MODIFIED
An empty HTTP response with status code 304.
|
static HTTPResponse |
RESPONSE_OK
An empty HTTP response with status code 200.
|
static HTTPResponse |
RESPONSE_UNSUPPORTED_MEDIA_TYPE
An empty HTTP response with status code 415.
|
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.lang.String data)
Creates a new
HTTPResponse using the given String as response data. |
HTTPResponse(java.lang.String data,
java.lang.String encoding)
|
Modifier and Type | Method and Description |
---|---|
void |
addHeaderField(java.lang.String name,
java.lang.String value)
Adds a response header field.
|
protected void |
close()
Close the data stream if the field
data is non null and the value of dataStreamClosed is false. |
protected java.io.InputStream |
getData()
Returns the
InputStream from which response data can be read. |
java.util.Map<java.lang.String,java.lang.String> |
getHeader()
Returns the response header.
|
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.
|
protected byte[] |
getRawData()
Returns the byte array from which response data can be read.
|
java.lang.String |
getStatus()
Returns the response status.
|
protected void |
setDataStreamClosed()
Used in HTTPSession to indicate that response dataStream has been successfully read and closed.
|
void |
setMimeType(java.lang.String mimeType)
Set the response MIME-TYPE.
|
void |
setStatus(java.lang.String status)
Set the response status.
|
public static final HTTPResponse RESPONSE_OK
public static final HTTPResponse RESPONSE_MOVED_PERMANENTLY
public static final HTTPResponse RESPONSE_NOT_MODIFIED
public static final HTTPResponse RESPONSE_BAD_REQUEST
public static final HTTPResponse RESPONSE_FORBIDDEN
public static final HTTPResponse RESPONSE_NOT_FOUND
public static final HTTPResponse RESPONSE_METHOD_NOT_ALLOWED
public static final HTTPResponse RESPONSE_NOT_ACCEPTABLE
public static final HTTPResponse RESPONSE_UNSUPPORTED_MEDIA_TYPE
public static final HTTPResponse RESPONSE_INTERNAL_ERROR
public static final HTTPResponse RESPONSE_NOT_IMPLEMENTED
public HTTPResponse()
Creates an empty HTTPResponse
.
public HTTPResponse(byte[] data)
Creates a new 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)
Creates a new HTTPResponse
using the given InputStream
as the response data.
data
- the data to send through response (as a stream)public HTTPResponse(java.lang.String data)
Creates a new 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
Creates a new HTTPResponse
using the String
data
as response data and the
encoding
.
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 void addHeaderField(java.lang.String name, java.lang.String value)
Adds a response header field.
name
- name of the header field to set.value
- value of the header filed.protected void close()
Close the data stream if the field data
is non null and the value of dataStreamClosed
is false.
protected java.io.InputStream getData()
InputStream
from which response data can be read.InputStream
from which response data can be read. May be null
.public java.util.Map<java.lang.String,java.lang.String> getHeader()
Returns the response header.
Hashtable
of (String,String) representing the HTTP Header Fields (may be empty).protected long getLength()
-1
if the length is unknown.public java.lang.String getMimeType()
Returns the MIME-TYPE of the response.
protected byte[] getRawData()
null
.public java.lang.String getStatus()
Returns the response status.
protected void setDataStreamClosed()
public void setMimeType(java.lang.String mimeType)
Set the response MIME-TYPE.
mimeType
- the response MIME-TYPE to set.public void setStatus(java.lang.String status)
Set the response status.
status
- the response status to set. Should be one of the HTTP_STATUS_*
constants defined in
HTTPConstants