public final class RestResponse extends 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 |
---|---|
RestResponse |
attr(String name,
Object value)
Shortcut for calling
|
void |
flushBuffer() |
RequestAttributes |
getAttributes()
Shortcut for calling
|
Charset |
getCharset()
Wrapper around
ServletResponseWrapper.getCharacterEncoding() that converts the value to a Charset . |
PrintWriter |
getDirectWriter(String contentType)
Convenience method meant to be used when rendering directly to a browser with no buffering.
|
HtmlDocBuilder |
getHtmlDocBuilder()
Deprecated.
|
protected HttpServletResponse |
getInner()
Returns the wrapped servlet request.
|
MediaType |
getMediaType()
Returns the
|
FinishableServletOutputStream |
getNegotiatedOutputStream()
Equivalent to
ServletResponse.getOutputStream() , except wraps the output stream if an Encoder
was found that matched the |
FinishablePrintWriter |
getNegotiatedWriter()
Equivalent to
ServletResponse.getWriter() , except wraps the output stream if an Encoder was
found that matched the |
Object |
getOutput()
Returns the output that was set by calling
setOutput(Object) . |
<T> T |
getOutput(Class<T> c)
Returns this value cast to the specified class.
|
ServletOutputStream |
getOutputStream() |
boolean |
getOutputStreamCalled()
Returns
getOutputStream() has been called. |
RequestProperties |
getProperties()
Deprecated.
|
ResponseBeanMeta |
getResponseMeta()
Returns the metadata about this response.
|
SerializerGroup |
getSerializers()
Gets the serializer group for the response.
|
List<String> |
getSupportedEncodings()
Returns the codings that are valid for
|
List<MediaType> |
getSupportedMediaTypes()
Returns the media types that are valid for
|
PrintWriter |
getWriter()
Returns the writer to the response body.
|
boolean |
hasOutput()
Returns
|
RestResponse |
header(HttpPartSchema schema,
String name,
Object value)
Sets a header on the request.
|
RestResponse |
header(HttpPartSerializerSession serializer,
HttpPartSchema schema,
String name,
Object value)
Sets a header on the request.
|
RestResponse |
header(NameValuePair pair)
Sets a header from a
NameValuePair . |
RestResponse |
header(String name,
Object value)
Sets a header on the request.
|
boolean |
isOutputType(Class<?> c)
Returns
|
RestResponse |
prop(String name,
Object value)
Deprecated.
|
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.
|
RestResponse |
setDebug()
Shortcut for calling
|
RestResponse |
setDebug(Boolean b)
Sets the
|
RestResponse |
setException(Throwable t)
Sets the
|
void |
setHeader(HttpPart h)
Same as
setHeader(String, String) but header is defined as a response part |
void |
setHeader(String name,
String value) |
void |
setHeaderSafe(String name,
String value)
Same as
setHeader(String, String) but strips invalid characters from the value if present. |
void |
setHeaderSafe(String name,
String value,
int maxLength)
Same as
setHeader(String, String) but strips invalid characters from the value if present. |
RestResponse |
setNoTrace()
Shortcut for calling
|
RestResponse |
setNoTrace(Boolean b)
Sets the
|
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.
|
RestResponse |
setResponseMeta(ResponseBeanMeta rbm)
Sets metadata about this response.
|
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
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getBufferSize, getCharacterEncoding, getContentType, getLocale, isCommitted, reset, resetBuffer, setBufferSize, setCharacterEncoding, setContentLength, setContentLengthLong, setContentType, setLocale
public SerializerGroup getSerializers()
public List<MediaType> getSupportedMediaTypes()
public List<String> getSupportedEncodings()
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.
hasOutput()
method behavior.
output
- The output to serialize to the connection.@Deprecated public HtmlDocBuilder getHtmlDocBuilder()
HtmlDocConfig
This is the programmatic equivalent to the @RestMethod(htmldoc)
annotation.
@Deprecated public RequestProperties getProperties()
getAttributes()
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.
@Deprecated public RestResponse prop(String name, Object value)
attr(String,Object)
name
- The property name.value
- The property value.public RequestAttributes getAttributes()
public RestResponse attr(String name, Object value)
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 NotAcceptable, IOException
ServletResponse.getOutputStream()
, except wraps the output stream if an Encoder
was found that matched the NotAcceptable
- If unsupported Accept-Encoding value specified.IOException
- Thrown by underlying stream.public ServletOutputStream getOutputStream() throws IOException
getOutputStream
in interface ServletResponse
getOutputStream
in class 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 ServletResponse
getWriter
in class 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 IOException
- Thrown by underlying stream.public FinishablePrintWriter getNegotiatedWriter() throws NotAcceptable, IOException
ServletResponse.getWriter()
, except wraps the output stream if an Encoder
was
found that matched the NotAcceptable
- If unsupported charset in request header Accept-Charset.IOException
- Thrown by underlying stream.public MediaType getMediaType()
public Charset getCharset()
ServletResponseWrapper.getCharacterEncoding()
that converts the value to a Charset
.Charset
.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 HttpServletResponse
sendRedirect
in class HttpServletResponseWrapper
IOException
public void setHeader(String name, String value)
setHeader
in interface HttpServletResponse
setHeader
in class HttpServletResponseWrapper
public void setHeaderSafe(String name, String value)
setHeader(String, String)
but strips invalid characters from the value if present.
These include CTRL characters, newlines, and non-ISO8859-1 characters.
Also limits the string length to 1024 characters.name
- Header name.value
- Header value.public void setHeaderSafe(String name, String value, int maxLength)
setHeader(String, String)
but strips invalid characters from the value if present.
These include CTRL characters, newlines, and non-ISO8859-1 characters.name
- Header name.value
- Header value.maxLength
- The maximum length of the header value.
Will be truncated with public RestResponse header(String name, Object value) throws SchemaValidationException, SerializeException
name
- The header name.value
- The header value.
SchemaValidationException
- Header failed schema validation.SerializeException
- Header could not be serialized.public RestResponse header(NameValuePair pair)
NameValuePair
.
Note that this bypasses the part serializer and set the header value directly.
pair
- The header to set. Nulls are ignored.public RestResponse header(HttpPartSchema schema, String name, Object value) throws SchemaValidationException, SerializeException
schema
- The schema to use to serialize the header, or name
- The header name.value
- The header value.
SchemaValidationException
- Header failed schema validation.SerializeException
- Header could not be serialized.public RestResponse header(HttpPartSerializerSession serializer, HttpPartSchema schema, String name, Object value) throws SchemaValidationException, SerializeException
serializer
- The serializer to use to serialize the header, or schema
- The schema to use to serialize the header, or name
- The header name.value
- The header value.
SchemaValidationException
- Header failed schema validation.SerializeException
- Header could not be serialized.public void setHeader(HttpPart h) throws SchemaValidationException, SerializeException
setHeader(String, String)
but header is defined as a response parth
- Header to set.SchemaValidationException
- Header part did not pass validation.SerializeException
- Header part could not be serialized.public RestResponse setException(Throwable t)
This exception is used by BasicRestCallLogger
for logging purposes.
t
- The attribute value.public RestResponse setNoTrace(Boolean b)
This flag is used by BasicRestCallLogger
and tells it not to log the current request.
b
- The attribute value.public RestResponse setNoTrace()
public RestResponse setDebug(Boolean b) throws IOException
This flag is used by BasicRestCallLogger
to help determine how a request should be logged.
b
- The attribute value.IOException
- If bodies could not be cached.public RestResponse setDebug() throws IOException
IOException
- If bodies could not be cached.public ResponseBeanMeta getResponseMeta()
public RestResponse setResponseMeta(ResponseBeanMeta rbm)
rbm
- The metadata about this response.public boolean isOutputType(Class<?> c)
c
- The type to check against.public <T> T getOutput(Class<T> c)
c
- The class to cast to.protected HttpServletResponse getInner()
public void flushBuffer() throws IOException
flushBuffer
in interface ServletResponse
flushBuffer
in class ServletResponseWrapper
IOException
Copyright © 2016–2020 The Apache Software Foundation. All rights reserved.