public class ServerResponse
extends java.lang.Object
Constructor and Description |
---|
ServerResponse(int responseCode) |
ServerResponse(int responseCode,
java.lang.String responseMessage) |
Modifier and Type | Method and Description |
---|---|
T |
addAllHeaders(java.util.Map<java.lang.String,java.util.List<java.lang.String>> headerMap)
Add all name/values from the given map.
|
T |
addHeader(HttpHeaderName name,
java.lang.String value)
Add a header value to the header with the given name.
|
T |
addHeader(java.lang.String name,
java.lang.String value)
Add a header value to the header with the given name.
|
T |
addHeaders(java.util.Map<HttpHeaderName,java.util.List<java.lang.String>> headerMap)
Add all name/values from the given map.
|
T |
appendContent(byte[] data)
Appends the given bytes to the content.
|
T |
appendContent(java.io.InputStream inStream)
Appends the bytes from the given stream to the content.
|
void |
appendContent(java.lang.String text)
Appends the given text to the content using the currently defined
character encoding.
|
protected java.nio.charset.Charset |
asCharset(java.lang.String contentType) |
protected MediaType |
asMediaType(java.lang.String contentType) |
static ServerResponse |
create(int responseCode) |
static ServerResponse |
create(int responseCode,
java.lang.String responseMessage) |
java.nio.charset.Charset |
getCharset()
Returns the character encoding defined in the Content-Type header
or null if it is not set.
|
protected java.nio.charset.Charset |
getCharsetEncoding() |
protected java.io.ByteArrayOutputStream |
getContentBuffer() |
byte[] |
getContentData()
Returns the content data or null if no content is available.
|
protected java.io.ByteArrayOutputStream |
getContentDataStream() |
int |
getContentLength() |
MediaType |
getContentMediaType()
Returns the media type specified in the "Content-Type" header
of null if not set.
|
java.io.Reader |
getContentReader()
Returns the content data as reader (with correct char encoding) or null
if no content is available.
|
java.lang.String |
getContentText()
Returns the content data as text (with correct char encoding) or null
if no content is available.
|
protected java.lang.String |
getContentTypeHeader()
Returns the "Content-Type" header unparsed.
|
protected java.nio.charset.Charset |
getDefaultCharset() |
java.lang.String |
getHeader(HttpHeaderName name)
Returns a single value (the first) of the header with the given name.
|
java.lang.String |
getHeader(java.lang.String name)
Returns a single value (the first) of the header with the given name.
|
protected Headers |
getHeaders() |
java.lang.String |
getLocationHeader()
Returns the value of the "Location" header or null if it is not present.
|
protected Logger |
getLogger() |
protected int |
getReadBufferSize() |
int |
getResponseCode() |
java.lang.String |
getResponseMessage() |
boolean |
hasContentData() |
boolean |
isCodeIn(int... codes)
Returns true if the response has a status code that equals
one of the given codes.
|
boolean |
isCodeInRange(int fromCode,
int toCode)
Returns true if the response has a status code that is in the specified
code range (fromCode and toCode both included).
|
boolean |
isOk()
Returns true if the response has returned with the OK status code (i.e.
|
boolean |
isRedirect()
Returns true if the response code is one of the redirect codes 301, 302, 303, 307, 308.
|
T |
removeHeader(HttpHeaderName name)
Returns the header with the given name and all its associated values.
|
T |
removeHeader(java.lang.String name)
Returns the header with the given name and all its associated values.
|
T |
self() |
ServerResponse |
setCharEncoding(java.lang.String charEncoding)
Sets the character encoding to be used on the received content when it gets
converted to text.
|
T |
setCharset(java.nio.charset.Charset charset)
Sets the character encoding in the Content-Type header
or removes it if the given parameter is null.
|
protected T |
setContentDataStream(java.io.ByteArrayOutputStream bodyData) |
T |
setContentMediaType(MediaType mediaType)
Sets the media type in the Content-Type header
or removes the entire header if the given parameter is null.
|
T |
setContentType(MediaType mediaType,
java.nio.charset.Charset charset)
Sets the "Content-Type" header.
|
protected T |
setContentTypeHeader(MediaType mediaType,
java.nio.charset.Charset charset)
Sets the "Content-Type" header.
|
T |
setHeader(HttpHeaderName name,
java.lang.String value)
A a header with a single value.
|
T |
setHeader(java.lang.String name,
java.lang.String value)
A a header with a single value.
|
ServerResponse |
setResponseMessage(java.lang.String responseMessage) |
java.lang.String |
toString() |
public ServerResponse(int responseCode)
public ServerResponse(int responseCode, java.lang.String responseMessage)
public static ServerResponse create(int responseCode)
public static ServerResponse create(int responseCode, java.lang.String responseMessage)
public int getResponseCode()
public java.lang.String getResponseMessage()
public ServerResponse setResponseMessage(java.lang.String responseMessage)
public ServerResponse setCharEncoding(java.lang.String charEncoding)
charEncoding
- The encoding name (e.g. "UTF-8"). If null the default HTTP encoding will be set.public boolean isOk()
public boolean isRedirect()
public boolean isCodeIn(int... codes)
public boolean isCodeInRange(int fromCode, int toCode)
public java.lang.String getLocationHeader()
public java.lang.String toString()
toString
in class java.lang.Object
public T setContentType(MediaType mediaType, java.nio.charset.Charset charset)
mediaType
- The media type to set. If null the header will be removed.charset
- The charset to be set. If null it will be ignored.public java.nio.charset.Charset getCharset()
public T setCharset(java.nio.charset.Charset charset)
java.lang.IllegalStateException
- If no media type is specified yet.public MediaType getContentMediaType()
public T setContentMediaType(MediaType mediaType)
public boolean hasContentData()
public int getContentLength()
public T appendContent(byte[] data)
public T appendContent(java.io.InputStream inStream) throws java.io.IOException
inStream
- The stream from which to read the bytes (must not be null).java.io.IOException
- If any problem with the input stream occurs.public void appendContent(java.lang.String text)
text
- The text to be appended (null will be ignored).java.lang.IllegalStateException
- If no encoding is set.public byte[] getContentData()
public java.io.Reader getContentReader()
public java.lang.String getContentText()
public T addAllHeaders(java.util.Map<java.lang.String,java.util.List<java.lang.String>> headerMap)
headerMap
- The header names and values to be a added (if null, nothing will be done).public T addHeaders(java.util.Map<HttpHeaderName,java.util.List<java.lang.String>> headerMap)
headerMap
- The header names and values to be a added (if null, nothing will be done).public T addHeader(java.lang.String name, java.lang.String value)
public T addHeader(HttpHeaderName name, java.lang.String value)
public T setHeader(java.lang.String name, java.lang.String value)
public T setHeader(HttpHeaderName name, java.lang.String value)
public java.lang.String getHeader(java.lang.String name)
public java.lang.String getHeader(HttpHeaderName name)
public T removeHeader(java.lang.String name)
public T removeHeader(HttpHeaderName name)
public T self()
self
in interface IExtensibleFluentApi<T extends org.pfsw.net.http.CommunicationData>
protected MediaType asMediaType(java.lang.String contentType)
protected java.nio.charset.Charset asCharset(java.lang.String contentType)
protected java.lang.String getContentTypeHeader()
protected T setContentTypeHeader(MediaType mediaType, java.nio.charset.Charset charset)
protected java.nio.charset.Charset getCharsetEncoding()
protected java.nio.charset.Charset getDefaultCharset()
protected java.io.ByteArrayOutputStream getContentBuffer()
protected java.io.ByteArrayOutputStream getContentDataStream()
protected T setContentDataStream(java.io.ByteArrayOutputStream bodyData)
protected Headers getHeaders()
protected int getReadBufferSize()
protected Logger getLogger()