org.springframework.mock.web
Class MockHttpServletResponse

java.lang.Object
  extended by org.springframework.mock.web.MockHttpServletResponse

public class MockHttpServletResponse
extends java.lang.Object

Mock implementation of the javax.servlet.http.HttpServletResponse interface.

Compatible with Servlet 2.5 as well as Servlet 3.0.

Since:
1.0.2
Author:
Juergen Hoeller, Rod Johnson

Nested Class Summary
private  class MockHttpServletResponse.ResponsePrintWriter
          Inner class that adapts the PrintWriter to mark the response as committed once the buffer size is exceeded.
private  class MockHttpServletResponse.ResponseServletOutputStream
          Inner class that adapts the ServletOutputStream to mark the response as committed once the buffer size is exceeded.
 
Field Summary
private  int bufferSize
           
private  java.lang.String characterEncoding
           
private  boolean charset
           
private static java.lang.String CHARSET_PREFIX
           
private  boolean committed
           
private  java.io.ByteArrayOutputStream content
           
private static java.lang.String CONTENT_LENGTH_HEADER
           
private static java.lang.String CONTENT_TYPE_HEADER
           
private  int contentLength
           
private  java.lang.String contentType
           
private  java.util.List<Cookie> cookies
           
private  java.lang.String errorMessage
           
private  java.lang.String forwardedUrl
           
private  java.util.Map<java.lang.String,HeaderValueHolder> headers
           
private  java.util.List<java.lang.String> includedUrls
           
private  java.util.Locale locale
           
private  ServletOutputStream outputStream
           
private  boolean outputStreamAccessAllowed
           
private  java.lang.String redirectedUrl
           
private  int status
           
private  java.io.PrintWriter writer
           
private  boolean writerAccessAllowed
           
 
Constructor Summary
MockHttpServletResponse()
           
 
Method Summary
 void addCookie(Cookie cookie)
           
 void addDateHeader(java.lang.String name, long value)
           
 void addHeader(java.lang.String name, java.lang.String value)
           
private  void addHeaderValue(java.lang.String name, java.lang.Object value)
           
 void addIncludedUrl(java.lang.String includedUrl)
           
 void addIntHeader(java.lang.String name, int value)
           
 boolean containsHeader(java.lang.String name)
           
private  void doAddHeaderValue(java.lang.String name, java.lang.Object value, boolean replace)
           
 java.lang.String encodeRedirectUrl(java.lang.String url)
           
 java.lang.String encodeRedirectURL(java.lang.String url)
          The default implementation delegates to encodeURL(java.lang.String), returning the given URL String as-is.
 java.lang.String encodeUrl(java.lang.String url)
           
 java.lang.String encodeURL(java.lang.String url)
          The default implementation returns the given URL String as-is.
 void flushBuffer()
           
 int getBufferSize()
           
 java.lang.String getCharacterEncoding()
           
 byte[] getContentAsByteArray()
           
 java.lang.String getContentAsString()
           
 int getContentLength()
           
 java.lang.String getContentType()
           
 Cookie getCookie(java.lang.String name)
           
 Cookie[] getCookies()
           
 java.lang.String getErrorMessage()
           
 java.lang.String getForwardedUrl()
           
 java.lang.String getHeader(java.lang.String name)
          Return the primary value for the given header as a String, if any.
 java.util.Set<java.lang.String> getHeaderNames()
          Return the names of all specified headers as a Set of Strings.
 java.util.List<java.lang.String> getHeaders(java.lang.String name)
          Return all values for the given header as a List of Strings.
 java.lang.Object getHeaderValue(java.lang.String name)
          Return the primary value for the given header, if any.
 java.util.List<java.lang.Object> getHeaderValues(java.lang.String name)
          Return all values for the given header as a List of value objects.
 java.lang.String getIncludedUrl()
           
 java.util.List<java.lang.String> getIncludedUrls()
           
 java.util.Locale getLocale()
           
 ServletOutputStream getOutputStream()
           
 java.lang.String getRedirectedUrl()
           
 int getStatus()
           
 java.io.PrintWriter getWriter()
           
 boolean isCommitted()
           
 boolean isOutputStreamAccessAllowed()
          Return whether getOutputStream() access is allowed.
 boolean isWriterAccessAllowed()
          Return whether getOutputStream() access is allowed.
 void reset()
           
 void resetBuffer()
           
 void sendError(int status)
           
 void sendError(int status, java.lang.String errorMessage)
           
 void sendRedirect(java.lang.String url)
           
 void setBufferSize(int bufferSize)
           
 void setCharacterEncoding(java.lang.String characterEncoding)
           
 void setCommitted(boolean committed)
           
private  void setCommittedIfBufferSizeExceeded()
           
 void setContentLength(int contentLength)
           
 void setContentType(java.lang.String contentType)
           
 void setDateHeader(java.lang.String name, long value)
           
 void setForwardedUrl(java.lang.String forwardedUrl)
           
 void setHeader(java.lang.String name, java.lang.String value)
           
private  void setHeaderValue(java.lang.String name, java.lang.Object value)
           
 void setIncludedUrl(java.lang.String includedUrl)
           
 void setIntHeader(java.lang.String name, int value)
           
 void setLocale(java.util.Locale locale)
           
 void setOutputStreamAccessAllowed(boolean outputStreamAccessAllowed)
          Set whether getOutputStream() access is allowed.
private  boolean setSpecialHeader(java.lang.String name, java.lang.Object value)
           
 void setStatus(int status)
           
 void setStatus(int status, java.lang.String errorMessage)
           
 void setWriterAccessAllowed(boolean writerAccessAllowed)
          Set whether getWriter() access is allowed.
private  void updateContentTypeHeader()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CHARSET_PREFIX

private static final java.lang.String CHARSET_PREFIX
See Also:
Constant Field Values

CONTENT_TYPE_HEADER

private static final java.lang.String CONTENT_TYPE_HEADER
See Also:
Constant Field Values

CONTENT_LENGTH_HEADER

private static final java.lang.String CONTENT_LENGTH_HEADER
See Also:
Constant Field Values

outputStreamAccessAllowed

private boolean outputStreamAccessAllowed

writerAccessAllowed

private boolean writerAccessAllowed

characterEncoding

private java.lang.String characterEncoding

charset

private boolean charset

content

private final java.io.ByteArrayOutputStream content

outputStream

private final ServletOutputStream outputStream

writer

private java.io.PrintWriter writer

contentLength

private int contentLength

contentType

private java.lang.String contentType

bufferSize

private int bufferSize

committed

private boolean committed

locale

private java.util.Locale locale

cookies

private final java.util.List<Cookie> cookies

headers

private final java.util.Map<java.lang.String,HeaderValueHolder> headers

status

private int status

errorMessage

private java.lang.String errorMessage

redirectedUrl

private java.lang.String redirectedUrl

forwardedUrl

private java.lang.String forwardedUrl

includedUrls

private final java.util.List<java.lang.String> includedUrls
Constructor Detail

MockHttpServletResponse

public MockHttpServletResponse()
Method Detail

setOutputStreamAccessAllowed

public void setOutputStreamAccessAllowed(boolean outputStreamAccessAllowed)
Set whether getOutputStream() access is allowed.

Default is true.


isOutputStreamAccessAllowed

public boolean isOutputStreamAccessAllowed()
Return whether getOutputStream() access is allowed.


setWriterAccessAllowed

public void setWriterAccessAllowed(boolean writerAccessAllowed)
Set whether getWriter() access is allowed.

Default is true.


isWriterAccessAllowed

public boolean isWriterAccessAllowed()
Return whether getOutputStream() access is allowed.


setCharacterEncoding

public void setCharacterEncoding(java.lang.String characterEncoding)

updateContentTypeHeader

private void updateContentTypeHeader()

getCharacterEncoding

public java.lang.String getCharacterEncoding()

getOutputStream

public ServletOutputStream getOutputStream()

getWriter

public java.io.PrintWriter getWriter()
                              throws java.io.UnsupportedEncodingException
Throws:
java.io.UnsupportedEncodingException

getContentAsByteArray

public byte[] getContentAsByteArray()

getContentAsString

public java.lang.String getContentAsString()
                                    throws java.io.UnsupportedEncodingException
Throws:
java.io.UnsupportedEncodingException

setContentLength

public void setContentLength(int contentLength)

getContentLength

public int getContentLength()

setContentType

public void setContentType(java.lang.String contentType)

getContentType

public java.lang.String getContentType()

setBufferSize

public void setBufferSize(int bufferSize)

getBufferSize

public int getBufferSize()

flushBuffer

public void flushBuffer()

resetBuffer

public void resetBuffer()

setCommittedIfBufferSizeExceeded

private void setCommittedIfBufferSizeExceeded()

setCommitted

public void setCommitted(boolean committed)

isCommitted

public boolean isCommitted()

reset

public void reset()

setLocale

public void setLocale(java.util.Locale locale)

getLocale

public java.util.Locale getLocale()

addCookie

public void addCookie(Cookie cookie)

getCookies

public Cookie[] getCookies()

getCookie

public Cookie getCookie(java.lang.String name)

containsHeader

public boolean containsHeader(java.lang.String name)

getHeaderNames

public java.util.Set<java.lang.String> getHeaderNames()
Return the names of all specified headers as a Set of Strings.

As of Servlet 3.0, this method is also defined HttpServletResponse.

Returns:
the Set of header name Strings, or an empty Set if none

getHeader

public java.lang.String getHeader(java.lang.String name)
Return the primary value for the given header as a String, if any. Will return the first value in case of multiple values.

As of Servlet 3.0, this method is also defined HttpServletResponse. As of Spring 3.1, it returns a stringified value for Servlet 3.0 compatibility. Consider using getHeaderValue(String) for raw Object access.

Parameters:
name - the name of the header
Returns:
the associated header value, or null if none

getHeaders

public java.util.List<java.lang.String> getHeaders(java.lang.String name)
Return all values for the given header as a List of Strings.

As of Servlet 3.0, this method is also defined HttpServletResponse. As of Spring 3.1, it returns a List of stringified values for Servlet 3.0 compatibility. Consider using getHeaderValues(String) for raw Object access.

Parameters:
name - the name of the header
Returns:
the associated header values, or an empty List if none

getHeaderValue

public java.lang.Object getHeaderValue(java.lang.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 associated header value, or null if none

getHeaderValues

public java.util.List<java.lang.Object> getHeaderValues(java.lang.String name)
Return all values for the given header as a List of value objects.

Parameters:
name - the name of the header
Returns:
the associated header values, or an empty List if none

encodeURL

public java.lang.String encodeURL(java.lang.String url)
The default implementation returns the given URL String as-is.

Can be overridden in subclasses, appending a session id or the like.


encodeRedirectURL

public java.lang.String encodeRedirectURL(java.lang.String url)
The default implementation delegates to encodeURL(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.


encodeUrl

public java.lang.String encodeUrl(java.lang.String url)

encodeRedirectUrl

public java.lang.String encodeRedirectUrl(java.lang.String url)

sendError

public void sendError(int status,
                      java.lang.String errorMessage)
               throws java.io.IOException
Throws:
java.io.IOException

sendError

public void sendError(int status)
               throws java.io.IOException
Throws:
java.io.IOException

sendRedirect

public void sendRedirect(java.lang.String url)
                  throws java.io.IOException
Throws:
java.io.IOException

getRedirectedUrl

public java.lang.String getRedirectedUrl()

setDateHeader

public void setDateHeader(java.lang.String name,
                          long value)

addDateHeader

public void addDateHeader(java.lang.String name,
                          long value)

setHeader

public void setHeader(java.lang.String name,
                      java.lang.String value)

addHeader

public void addHeader(java.lang.String name,
                      java.lang.String value)

setIntHeader

public void setIntHeader(java.lang.String name,
                         int value)

addIntHeader

public void addIntHeader(java.lang.String name,
                         int value)

setHeaderValue

private void setHeaderValue(java.lang.String name,
                            java.lang.Object value)

addHeaderValue

private void addHeaderValue(java.lang.String name,
                            java.lang.Object value)

setSpecialHeader

private boolean setSpecialHeader(java.lang.String name,
                                 java.lang.Object value)

doAddHeaderValue

private void doAddHeaderValue(java.lang.String name,
                              java.lang.Object value,
                              boolean replace)

setStatus

public void setStatus(int status)

setStatus

public void setStatus(int status,
                      java.lang.String errorMessage)

getStatus

public int getStatus()

getErrorMessage

public java.lang.String getErrorMessage()

setForwardedUrl

public void setForwardedUrl(java.lang.String forwardedUrl)

getForwardedUrl

public java.lang.String getForwardedUrl()

setIncludedUrl

public void setIncludedUrl(java.lang.String includedUrl)

getIncludedUrl

public java.lang.String getIncludedUrl()

addIncludedUrl

public void addIncludedUrl(java.lang.String includedUrl)

getIncludedUrls

public java.util.List<java.lang.String> getIncludedUrls()