public final class RestResponse extends javax.servlet.http.HttpServletResponseWrapper
Essentially an extended HttpServletResponse
with some special convenience methods that allow you to easily
output POJOs as responses.
Since this class extends HttpServletResponse
, developers are free to use these convenience methods, or
revert to using lower level methods like any other servlet response.
SC_ACCEPTED, SC_BAD_GATEWAY, SC_BAD_REQUEST, SC_CONFLICT, SC_CONTINUE, SC_CREATED, SC_EXPECTATION_FAILED, SC_FORBIDDEN, SC_FOUND, SC_GATEWAY_TIMEOUT, SC_GONE, SC_HTTP_VERSION_NOT_SUPPORTED, SC_INTERNAL_SERVER_ERROR, SC_LENGTH_REQUIRED, SC_METHOD_NOT_ALLOWED, SC_MOVED_PERMANENTLY, SC_MOVED_TEMPORARILY, SC_MULTIPLE_CHOICES, SC_NO_CONTENT, SC_NON_AUTHORITATIVE_INFORMATION, SC_NOT_ACCEPTABLE, SC_NOT_FOUND, SC_NOT_IMPLEMENTED, SC_NOT_MODIFIED, SC_OK, SC_PARTIAL_CONTENT, SC_PAYMENT_REQUIRED, SC_PRECONDITION_FAILED, SC_PROXY_AUTHENTICATION_REQUIRED, SC_REQUEST_ENTITY_TOO_LARGE, SC_REQUEST_TIMEOUT, SC_REQUEST_URI_TOO_LONG, SC_REQUESTED_RANGE_NOT_SATISFIABLE, SC_RESET_CONTENT, SC_SEE_OTHER, SC_SERVICE_UNAVAILABLE, SC_SWITCHING_PROTOCOLS, SC_TEMPORARY_REDIRECT, SC_UNAUTHORIZED, SC_UNSUPPORTED_MEDIA_TYPE, SC_USE_PROXY
Modifier and Type | Method and Description |
---|---|
void |
flushBuffer() |
PrintWriter |
getDirectWriter(String contentType)
Convenience method meant to be used when rendering directly to a browser with no buffering.
|
HtmlDocBuilder |
getHtmlDocBuilder()
Returns a programmatic interface for setting properties for the HTML doc view.
|
MediaType |
getMediaType()
Returns the
Content-Type header stripped of the charset attribute if present. |
FinishableServletOutputStream |
getNegotiatedOutputStream()
Equivalent to
ServletResponse.getOutputStream() , except wraps the output stream if an Encoder
was found that matched the Accept-Encoding header. |
FinishablePrintWriter |
getNegotiatedWriter()
Equivalent to
ServletResponse.getWriter() , except wraps the output stream if an Encoder was
found that matched the Accept-Encoding header and sets the Content-Encoding
header to the appropriate value. |
Object |
getOutput()
Returns the output that was set by calling
setOutput(Object) . |
javax.servlet.ServletOutputStream |
getOutputStream() |
boolean |
getOutputStreamCalled()
Returns
getOutputStream() has been called. |
HttpPartSerializer |
getPartSerializer()
Returns the HTTP-part serializer associated with this response.
|
RequestProperties |
getProperties()
Retrieve the properties active for this request.
|
SerializerGroup |
getSerializers()
Gets the serializer group for the response.
|
List<String> |
getSupportedEncodings()
Returns the codings that are valid for
Accept-Encoding and Content-Encoding headers on
the request. |
List<MediaType> |
getSupportedMediaTypes()
Returns the media types that are valid for
Accept headers on the request. |
PrintWriter |
getWriter()
Returns the writer to the response body.
|
boolean |
hasOutput()
Returns
|
RestResponse |
prop(String name,
Object value)
Shortcut for calling
getProperties().append(name, value); fluently. |
RestResponse |
sendPlainText(String text)
Sets the output to a plain-text message regardless of the content type.
|
void |
sendRedirect(String uri)
Redirects to the specified URI.
|
void |
setHeader(String name,
String value) |
RestResponse |
setOutput(Object output)
Sets the HTTP output on the response.
|
RestResponse |
setOutputs(Object... output)
Shortcut method that allows you to use var-args to simplify setting array output.
|
addCookie, addDateHeader, addHeader, addIntHeader, containsHeader, encodeRedirectUrl, encodeRedirectURL, encodeUrl, encodeURL, getHeader, getHeaderNames, getHeaders, getStatus, sendError, sendError, setDateHeader, setIntHeader, setStatus, setStatus
getBufferSize, getCharacterEncoding, getContentType, getLocale, getResponse, isCommitted, isWrapperFor, isWrapperFor, reset, resetBuffer, setBufferSize, setCharacterEncoding, setContentLength, setContentLengthLong, setContentType, setLocale, setResponse
public SerializerGroup getSerializers()
public List<MediaType> getSupportedMediaTypes()
Accept
headers on the request.public List<String> getSupportedEncodings() throws RestServletException
Accept-Encoding
and Content-Encoding
headers on
the request.RestServletException
public RestResponse setOutput(Object output)
The object type can be anything allowed by the registered response handlers.
Calling this method is functionally equivalent to returning the object in the REST Java method.
null
).
hasOutput()
method behavior.
output
- The output to serialize to the connection.public HtmlDocBuilder getHtmlDocBuilder()
This is the programmatic equivalent to the @RestMethod.htmldoc()
annotation.
public RequestProperties getProperties()
This contains all resource and method level properties from the following:
The returned object is modifiable and allows you to override session-level properties before
they get passed to the serializers.
However, properties are open-ended, and can be used for any purpose.
public RestResponse prop(String name, Object value)
getProperties().append(name, value);
fluently.name
- The property name.value
- The property value.public RestResponse setOutputs(Object... output)
output
- The output to serialize to the connection.public Object getOutput()
setOutput(Object)
.public boolean hasOutput()
setOutput(Object)
has been called, even if the value passed was public RestResponse sendPlainText(String text) throws IOException
text
- The output text to send.IOException
- If a problem occurred trying to write to the writer.public FinishableServletOutputStream getNegotiatedOutputStream() throws IOException
ServletResponse.getOutputStream()
, except wraps the output stream if an Encoder
was found that matched the Accept-Encoding
header.IOException
public javax.servlet.ServletOutputStream getOutputStream() throws IOException
getOutputStream
in interface javax.servlet.ServletResponse
getOutputStream
in class javax.servlet.ServletResponseWrapper
IOException
public boolean getOutputStreamCalled()
getOutputStream()
has been called.getOutputStream()
has been called.public PrintWriter getWriter() throws IOException
This methods bypasses any specified encoders and returns a regular unbuffered writer.
Use the getNegotiatedWriter()
method if you want to use the matched encoder (if any).
getWriter
in interface javax.servlet.ServletResponse
getWriter
in class javax.servlet.ServletResponseWrapper
IOException
public PrintWriter getDirectWriter(String contentType) throws IOException
Sets the header
This can be useful if you want to render a streaming 'console' on a web page.
contentType
- The value to set as the Content-Type
on the response.IOException
public FinishablePrintWriter getNegotiatedWriter() throws IOException
ServletResponse.getWriter()
, except wraps the output stream if an Encoder
was
found that matched the Accept-Encoding
header and sets the Content-Encoding
header to the appropriate value.IOException
public MediaType getMediaType()
Content-Type
header stripped of the charset attribute if present.media-type
portion of the Content-Type
header.public void sendRedirect(String uri) throws IOException
Relative URIs are always interpreted as relative to the context root. This is similar to how WAS handles redirect requests, and is different from how Tomcat handles redirect requests.
sendRedirect
in interface javax.servlet.http.HttpServletResponse
sendRedirect
in class javax.servlet.http.HttpServletResponseWrapper
IOException
public HttpPartSerializer getPartSerializer()
public void setHeader(String name, String value)
setHeader
in interface javax.servlet.http.HttpServletResponse
setHeader
in class javax.servlet.http.HttpServletResponseWrapper
public void flushBuffer() throws IOException
flushBuffer
in interface javax.servlet.ServletResponse
flushBuffer
in class javax.servlet.ServletResponseWrapper
IOException
Copyright © 2018 Apache. All rights reserved.