public class ServletHttpResponse extends Object implements HttpResponse
HttpResponse
wrapping a HttpServletResponse
.Constructor and Description |
---|
ServletHttpResponse(javax.servlet.http.HttpServletResponse servletResponse)
Initiate.
|
Modifier and Type | Method and Description |
---|---|
HttpHeader |
addHeader(String name,
String value)
Adds a
HttpHeader for the response. |
Charset |
getContentCharset()
Obtains the
Charset for the content. |
String |
getContentType()
Obtains the
Content-Type . |
ServletServerOutputStream |
getEntity()
Obtains the
ServerOutputStream to write the entity of the
response. |
ServerWriter |
getEntityWriter()
Obtains the
ServerWriter to write the entity of the response. |
HttpHeader |
getHeader(String name)
Obtains the first
HttpHeader by the name. |
HttpHeader[] |
getHeaders()
Obtains all the
HttpHeader instances for the response. |
int |
getStatus()
Obtains the status.
|
String |
getStatusMessage()
Obtains the current status message.
|
String |
getVersion()
Obtains the HTTP version.
|
void |
removeHeader(HttpHeader header)
Removes the particular
HttpHeader from the response. |
void |
removeHeaders(String name)
Removes all
HttpHeader instances by the name. |
void |
reset()
Resets the
HttpResponse by clearing HttpHeader instances
and the entity. |
void |
send()
Sends this
HttpResponse . |
void |
setContentType(String contentType,
Charset charset)
Specifies the
Content-Type and optionally the
Charset . |
void |
setStatus(int status)
Specifies the status of the response with the default status message.
|
void |
setStatus(int status,
String statusMessage)
Specifies the status of the response including specifying the status
human readable message.
|
void |
setVersion(String version)
Allows specifying the HTTP version on the response.
|
public ServletHttpResponse(javax.servlet.http.HttpServletResponse servletResponse)
servletResponse
- HttpServletResponse
.public HttpHeader addHeader(String name, String value)
HttpResponse
Adds a HttpHeader
for the response.
HttpHeader
instances are provided on the response in the order
they are added.
addHeader
in interface HttpResponse
name
- Name of HttpHeader
.value
- Value of HttpHeader
.HttpHeader
instance added.public HttpHeader getHeader(String name)
HttpResponse
HttpHeader
by the name.getHeader
in interface HttpResponse
name
- Name of the HttpHeader
.HttpHeader
by the name.public HttpHeader[] getHeaders()
HttpResponse
HttpHeader
instances for the response.getHeaders
in interface HttpResponse
HttpHeader
instances for the response.public void removeHeader(HttpHeader header)
HttpResponse
HttpHeader
from the response.removeHeader
in interface HttpResponse
header
- HttpHeader
to be removed from the response.public void removeHeaders(String name)
HttpResponse
Removes all HttpHeader
instances by the name.
This method compliments HttpResponse.addHeader(String, String)
to allow
adding a new single HttpHeader
instance by name.
removeHeaders
in interface HttpResponse
name
- Name of the HttpHeader
instances to remove.public void setStatus(int status)
HttpResponse
Specifies the status of the response with the default status message.
This defaults to 200 assuming the request was processed successfully.
setStatus
in interface HttpResponse
status
- Status of the response.public void setStatus(int status, String statusMessage)
HttpResponse
setStatus
in interface HttpResponse
status
- Status of the response.statusMessage
- Human readable status message.HttpResponse.setStatus(int)
public void setVersion(String version)
HttpResponse
Allows specifying the HTTP version on the response. Values should be either HTTP/1.0 or HTTP/1.1.
This defaults to value on HttpRequest
.
setVersion
in interface HttpResponse
version
- HTTP version.public String getVersion()
HttpResponse
getVersion
in interface HttpResponse
public int getStatus()
HttpResponse
Obtains the status.
This is the current status. The status may changed based on particular HTTP rules (e.g. 200 becoming 204 due to no entity) or there being a failure in processing the message.
getStatus
in interface HttpResponse
public String getStatusMessage()
HttpResponse
getStatusMessage
in interface HttpResponse
HttpResponse.getStatus()
public String getContentType()
HttpResponse
Content-Type
.getContentType
in interface HttpResponse
Content-Type
. May be null
if no
Content-Type
has been specified.public Charset getContentCharset()
HttpResponse
Obtains the Charset
for the content.
If no Charset
has been specified, the default Charset
will be returned.
getContentCharset
in interface HttpResponse
Charset
.public ServletServerOutputStream getEntity() throws IOException
HttpResponse
Obtains the ServerOutputStream
to write the entity of the
response.
Only one of HttpResponse.getEntity()
or HttpResponse.getEntityWriter()
may be
called.
Closing the returned ServerOutputStream
is similar to calling
HttpResponse.send()
.
getEntity
in interface HttpResponse
ServerOutputStream
to write the entity of the response.IOException
- Should HttpResponse.getEntityWriter()
already be provided.HttpResponse.getEntityWriter()
,
HttpResponse.send()
public void setContentType(String contentType, Charset charset)
HttpResponse
Specifies the Content-Type
and optionally the
Charset
. The charset
parameter will automatically be
added to the Content-Type
on using the
HttpResponse.getEntityWriter()
.
This must be specified before calling HttpResponse.getEntityWriter()
.
setContentType
in interface HttpResponse
contentType
- Content-Type
. May be null
to unset
the Content-Type
.charset
- Charset
for the Content-Type
. This may be
null
to use the default Charset
. Also use
null
for Content-Type
s that do not
require character encoding.public ServerWriter getEntityWriter() throws IOException
HttpResponse
Obtains the ServerWriter
to write the entity of the response.
Only one of HttpResponse.getEntity()
or HttpResponse.getEntityWriter()
may be
called.
Closing the returned ServerOutputStream
is similar to calling
HttpResponse.send()
.
getEntityWriter
in interface HttpResponse
ServerWriter
to write the entity of the response.IOException
- Should HttpResponse.getEntity()
already be provided.HttpResponse.getEntity()
,
HttpResponse.send()
public void send() throws IOException
HttpResponse
send
in interface HttpResponse
IOException
- If fails to send this HttpResponse
.public void reset() throws IOException
HttpResponse
HttpResponse
by clearing HttpHeader
instances
and the entity.reset
in interface HttpResponse
IOException
- If committed to send the HttpResponse
.Copyright © 2005–2016. All rights reserved.