Class EntityHttpServletResponse
- java.lang.Object
-
- org.sakaiproject.entitybroker.util.http.EntityHttpServletResponse
-
- All Implemented Interfaces:
HttpServletResponse,ServletResponse
public class EntityHttpServletResponse extends Object implements HttpServletResponse
This is here to allow us to receive response data back which will not mess up an existing response object and to allow for mocking of responses, based on and built from the example in spring framework- Author:
- Aaron Zeckoski (azeckoski @ gmail.com)
-
-
Field Summary
Fields Modifier and Type Field Description Vector<Cookie>cookiesstatic intDEFAULT_SERVER_PORTConcurrentHashMap<String,Vector<String>>headers-
Fields inherited from interface javax.servlet.http.HttpServletResponse
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
-
-
Constructor Summary
Constructors Constructor Description EntityHttpServletResponse()Create a default response that is valid for testingEntityHttpServletResponse(HttpServletResponse response)Create a servlet response using the various values and codes stored in the given one, makes copies mostly
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddCookie(Cookie cookie)voidaddDateHeader(String name, long value)voidaddHeader(String name, String value)voidaddIntHeader(String name, int value)voidclearHeaders()booleancontainsHeader(String name)StringencodeRedirectUrl(String url)StringencodeRedirectURL(String url)The default implementation delegates toencodeURL(java.lang.String), returning the given URL String as-is.StringencodeUrl(String url)StringencodeURL(String url)The default implementation returns the given URL String as-is.voidflushBuffer()Map<String,Vector<String>>getActualHeaders()Get all headers in this responseintgetBufferSize()StringgetCharacterEncoding()byte[]getContentAsByteArray()StringgetContentAsString()intgetContentLength()StringgetContentType()CookiegetCookie(String name)Cookie[]getCookies()StringgetErrorMessage()StringgetForwardedUrl()StringgetHeader(String name)Return the primary value for the given header, if any, Will return the first value in case of multiple valuesSetgetHeaderNames()Return the names of all specified headers as a Set of Strings.Map<String,String[]>getHeaders()Get all headers in this response as a map of string (name) -> String[] (values)List<String>getHeaders(String name)Return the primary value for the given header, if any, Will return the first value in case of multiple valuesStringgetIncludedUrl()LocalegetLocale()ServletOutputStreamgetOutputStream()StringgetRedirectedUrl()intgetStatus()PrintWritergetWriter()booleanisCommitted()booleanisOutputStreamAccessAllowed()Return whethergetOutputStream()access is allowed.booleanisRedirected()booleanisWriterAccessAllowed()Return whethergetOutputStream()access is allowed.voidremoveHeader(String name)Delete a header and all values by namevoidreset()voidresetBuffer()voidsendError(int status)voidsendError(int status, String errorMessage)voidsendRedirect(String url)voidsetBufferSize(int bufferSize)voidsetCharacterEncoding(String characterEncoding)voidsetCommitted(boolean committed)voidsetContentLength(int contentLength)voidsetContentType(String contentType)voidsetDateHeader(String name, long value)voidsetForwardedUrl(String forwardedUrl)voidsetHeader(String name, String value)voidsetIncludedUrl(String includedUrl)voidsetIntHeader(String name, int value)voidsetLocale(Locale locale)voidsetOutputStreamAccessAllowed(boolean outputStreamAccessAllowed)Set whethergetOutputStream()access is allowed.voidsetStatus(int status)voidsetStatus(int status, String errorMessage)voidsetWriterAccessAllowed(boolean writerAccessAllowed)Set whethergetWriter()access is allowed.
-
-
-
Field Detail
-
DEFAULT_SERVER_PORT
public static final int DEFAULT_SERVER_PORT
- See Also:
- Constant Field Values
-
headers
public ConcurrentHashMap<String,Vector<String>> headers
-
-
Constructor Detail
-
EntityHttpServletResponse
public EntityHttpServletResponse()
Create a default response that is valid for testing
-
EntityHttpServletResponse
public EntityHttpServletResponse(HttpServletResponse response)
Create a servlet response using the various values and codes stored in the given one, makes copies mostly- Parameters:
response- any valid response, cannot be null
-
-
Method Detail
-
setForwardedUrl
public void setForwardedUrl(String forwardedUrl)
-
getForwardedUrl
public String getForwardedUrl()
-
setIncludedUrl
public void setIncludedUrl(String includedUrl)
-
getIncludedUrl
public String getIncludedUrl()
-
setOutputStreamAccessAllowed
public void setOutputStreamAccessAllowed(boolean outputStreamAccessAllowed)
Set whethergetOutputStream()access is allowed.Default is
true.
-
isOutputStreamAccessAllowed
public boolean isOutputStreamAccessAllowed()
Return whethergetOutputStream()access is allowed.
-
setWriterAccessAllowed
public void setWriterAccessAllowed(boolean writerAccessAllowed)
Set whethergetWriter()access is allowed.Default is
true.
-
isWriterAccessAllowed
public boolean isWriterAccessAllowed()
Return whethergetOutputStream()access is allowed.
-
setCharacterEncoding
public void setCharacterEncoding(String characterEncoding)
- Specified by:
setCharacterEncodingin interfaceServletResponse
-
getCharacterEncoding
public String getCharacterEncoding()
- Specified by:
getCharacterEncodingin interfaceServletResponse
-
getOutputStream
public ServletOutputStream getOutputStream()
- Specified by:
getOutputStreamin interfaceServletResponse
-
getWriter
public PrintWriter getWriter() throws UnsupportedEncodingException
- Specified by:
getWriterin interfaceServletResponse- Throws:
UnsupportedEncodingException
-
getContentAsByteArray
public byte[] getContentAsByteArray()
- Returns:
- the content as a byte array OR empty array if there is no content
-
getContentAsString
public String getContentAsString()
- Returns:
- a string representing the content of this response OR "" if there is no content
- Throws:
RuntimeException- if the encoding fails and the content cannot be retrieved
-
setContentLength
public void setContentLength(int contentLength)
- Specified by:
setContentLengthin interfaceServletResponse
-
getContentLength
public int getContentLength()
-
setContentType
public void setContentType(String contentType)
- Specified by:
setContentTypein interfaceServletResponse
-
getContentType
public String getContentType()
- Specified by:
getContentTypein interfaceServletResponse
-
setBufferSize
public void setBufferSize(int bufferSize)
- Specified by:
setBufferSizein interfaceServletResponse
-
getBufferSize
public int getBufferSize()
- Specified by:
getBufferSizein interfaceServletResponse
-
flushBuffer
public void flushBuffer()
- Specified by:
flushBufferin interfaceServletResponse
-
resetBuffer
public void resetBuffer()
- Specified by:
resetBufferin interfaceServletResponse
-
setCommitted
public void setCommitted(boolean committed)
-
isCommitted
public boolean isCommitted()
- Specified by:
isCommittedin interfaceServletResponse
-
reset
public void reset()
- Specified by:
resetin interfaceServletResponse
-
setLocale
public void setLocale(Locale locale)
- Specified by:
setLocalein interfaceServletResponse
-
getLocale
public Locale getLocale()
- Specified by:
getLocalein interfaceServletResponse
-
addCookie
public void addCookie(Cookie cookie)
- Specified by:
addCookiein interfaceHttpServletResponse
-
getCookies
public Cookie[] getCookies()
-
encodeURL
public String encodeURL(String url)
The default implementation returns the given URL String as-is.Can be overridden in subclasses, appending a session id or the like.
- Specified by:
encodeURLin interfaceHttpServletResponse
-
encodeRedirectURL
public String encodeRedirectURL(String url)
The default implementation delegates toencodeURL(java.lang.String), returning the given URL String as-is.Can be overridden in subclasses, appending a session id or the like in a redirect-specific fashion. For general URL encoding rules, override the common
encodeURL(java.lang.String)method instead, appyling to redirect URLs as well as to general URLs.- Specified by:
encodeRedirectURLin interfaceHttpServletResponse
-
encodeUrl
public String encodeUrl(String url)
- Specified by:
encodeUrlin interfaceHttpServletResponse
-
encodeRedirectUrl
public String encodeRedirectUrl(String url)
- Specified by:
encodeRedirectUrlin interfaceHttpServletResponse
-
sendError
public void sendError(int status, String errorMessage) throws IOException- Specified by:
sendErrorin interfaceHttpServletResponse- Throws:
IOException
-
sendError
public void sendError(int status) throws IOException- Specified by:
sendErrorin interfaceHttpServletResponse- Throws:
IOException
-
sendRedirect
public void sendRedirect(String url) throws IOException
- Specified by:
sendRedirectin interfaceHttpServletResponse- Throws:
IOException
-
isRedirected
public boolean isRedirected()
- Returns:
- true if this response was redirected
-
getRedirectedUrl
public String getRedirectedUrl()
- Returns:
- the URL this response was forwarded or redirected to OR null if not redirected
-
setDateHeader
public void setDateHeader(String name, long value)
- Specified by:
setDateHeaderin interfaceHttpServletResponse
-
addDateHeader
public void addDateHeader(String name, long value)
- Specified by:
addDateHeaderin interfaceHttpServletResponse
-
setHeader
public void setHeader(String name, String value)
- Specified by:
setHeaderin interfaceHttpServletResponse
-
addHeader
public void addHeader(String name, String value)
- Specified by:
addHeaderin interfaceHttpServletResponse
-
setIntHeader
public void setIntHeader(String name, int value)
- Specified by:
setIntHeaderin interfaceHttpServletResponse
-
addIntHeader
public void addIntHeader(String name, int value)
- Specified by:
addIntHeaderin interfaceHttpServletResponse
-
containsHeader
public boolean containsHeader(String name)
- Specified by:
containsHeaderin interfaceHttpServletResponse
-
getHeader
public String getHeader(String name)
Return the primary value for the given header, if any, Will return the first value in case of multiple values- Parameters:
name- the name of the header- Returns:
- the first value in this header OR null if there is no header by this name
-
getHeaders
public List<String> getHeaders(String name)
Return the primary value for the given header, if any, Will return the first value in case of multiple values- Parameters:
name- the name of the header- Returns:
- the list of all values in this header OR null if there are none
-
getHeaderNames
public Set getHeaderNames()
Return the names of all specified headers as a Set of Strings.- Returns:
- the
Setof header nameStrings, or an emptySetif none
-
getActualHeaders
public Map<String,Vector<String>> getActualHeaders()
Get all headers in this response- Returns:
- all headers as a map of string (header name) -> List(String) (header values)
-
getHeaders
public Map<String,String[]> getHeaders()
Get all headers in this response as a map of string (name) -> String[] (values)- Returns:
- all headers in this response as a map of string (name) -> String[] (values)
-
removeHeader
public void removeHeader(String name)
Delete a header and all values by name- Parameters:
name- the name key of the header
-
clearHeaders
public void clearHeaders()
-
setStatus
public void setStatus(int status)
- Specified by:
setStatusin interfaceHttpServletResponse
-
setStatus
public void setStatus(int status, String errorMessage)- Specified by:
setStatusin interfaceHttpServletResponse
-
getStatus
public int getStatus()
-
getErrorMessage
public String getErrorMessage()
-
-