org.jboss.seam.mock
Class EnhancedMockHttpServletRequest

java.lang.Object
  extended by org.jboss.seam.mock.EnhancedMockHttpServletRequest
All Implemented Interfaces:
javax.servlet.http.HttpServletRequest, javax.servlet.ServletRequest

public class EnhancedMockHttpServletRequest
extends Object
implements javax.servlet.http.HttpServletRequest

Mock implementation of the HttpServletRequest interface. Supports the Servlet 2.4 API level.

Used for testing the web framework; also useful for testing application controllers.

Since:
1.0.2
Author:
Juergen Hoeller, Rod Johnson, Rick Evans, Mark Fisher

Field Summary
static String DEFAULT_PROTOCOL
          The default protocol: 'http'.
static String DEFAULT_REMOTE_ADDR
          The default remote address: '127.0.0.1'.
static String DEFAULT_REMOTE_HOST
          The default remote host: 'localhost'.
static String DEFAULT_SERVER_ADDR
          The default server address: '127.0.0.1'.
static String DEFAULT_SERVER_NAME
          The default server name: 'localhost'.
static int DEFAULT_SERVER_PORT
          The default server port: '80'.
 
Fields inherited from interface javax.servlet.http.HttpServletRequest
BASIC_AUTH, CLIENT_CERT_AUTH, DIGEST_AUTH, FORM_AUTH
 
Constructor Summary
EnhancedMockHttpServletRequest()
          Create a new MockHttpServletRequest with a default MockServletContext.
EnhancedMockHttpServletRequest(javax.servlet.http.HttpSession session)
           
EnhancedMockHttpServletRequest(javax.servlet.http.HttpSession session, String principalName, Set<String> principalRoles)
           
EnhancedMockHttpServletRequest(javax.servlet.http.HttpSession session, String principalName, Set<String> principalRoles, javax.servlet.http.Cookie[] cookies, String method)
           
EnhancedMockHttpServletRequest(javax.servlet.ServletContext servletContext)
          Create a new MockHttpServletRequest.
EnhancedMockHttpServletRequest(javax.servlet.ServletContext servletContext, String method, String requestURI)
          Create a new MockHttpServletRequest.
EnhancedMockHttpServletRequest(String method, String requestURI)
          Create a new MockHttpServletRequest with a default MockServletContext.
 
Method Summary
 void addCookie(javax.servlet.http.Cookie cookie)
           
 void addHeader(String name, Object value)
          Add a header entry for the given name.
 void addParameter(String name, String value)
          Add a single value for the specified HTTP parameter.
 void addParameter(String name, String[] values)
          Add an array of values for the specified HTTP parameter.
 void addParameters(Map params)
          Adds all provided parameters without replacing any existing values.
 void addPreferredLocale(Locale locale)
          Add a new preferred locale, before any existing locales.
 void addQueryParameter(String name, String value)
          Add a query parameter that will be appended to the URI query string.
 void addRole(String role)
          Deprecated. in favor of addUserRole
 void addUserRole(String role)
           
 void clearAttributes()
          Clear all of this request's attributes.
 void close()
          Mark this request as completed, keeping its state.
 Object getAttribute(String name)
           
 Enumeration getAttributeNames()
           
 String getAuthType()
           
 int getContentLength()
           
 String getContentType()
           
 String getContextPath()
           
 javax.servlet.http.Cookie[] getCookies()
           
 long getDateHeader(String name)
           
 String getHeader(String name)
           
 Enumeration getHeaderNames()
           
 Map<String,String[]> getHeaders()
           
 Enumeration getHeaders(String name)
           
 String getCharacterEncoding()
           
 javax.servlet.ServletInputStream getInputStream()
           
 int getIntHeader(String name)
           
 String getLocalAddr()
           
 Locale getLocale()
           
 Enumeration getLocales()
           
 String getLocalName()
           
 int getLocalPort()
           
 String getMethod()
           
 String getParameter(String name)
           
 Map getParameterMap()
           
 Enumeration getParameterNames()
           
 Map<String,String[]> getParameters()
           
 String[] getParameterValues(String name)
           
 String getPathInfo()
           
 String getPathTranslated()
           
 String getProtocol()
           
 Map<String,String> getQueryParameters()
           
 String getQueryString()
           
 BufferedReader getReader()
           
 String getRealPath(String path)
           
 String getRemoteAddr()
           
 String getRemoteHost()
           
 int getRemotePort()
           
 String getRemoteUser()
           
 javax.servlet.RequestDispatcher getRequestDispatcher(String path)
           
 String getRequestedSessionId()
           
 String getRequestURI()
           
 StringBuffer getRequestURL()
           
 String getServerName()
           
 int getServerPort()
           
 javax.servlet.ServletContext getServletContext()
          Return the ServletContext that this request is associated with.
 String getServletPath()
           
 javax.servlet.http.HttpSession getSession()
           
 javax.servlet.http.HttpSession getSession(boolean create)
           
 String getScheme()
           
 Principal getUserPrincipal()
           
protected  void checkActive()
          Check whether this request is still active (that is, not completed yet), throwing an IllegalStateException if not active anymore.
 void invalidate()
          Invalidate this request, clearing its state.
 boolean isActive()
          Return whether this request is still active (that is, not completed yet).
 boolean isAllParametersInQueryString()
           
 boolean isRequestedSessionIdFromCookie()
           
 boolean isRequestedSessionIdFromUrl()
           
 boolean isRequestedSessionIdFromURL()
           
 boolean isRequestedSessionIdValid()
           
 boolean isSecure()
           
 boolean isUserInRole(String role)
           
 void removeAllParameters()
          Removes all existing parameters.
 void removeAttribute(String name)
           
 void removeParameter(String name)
          Remove already registered values for the specified HTTP parameter, if any.
 void removeQueryParameter(String name)
           
 void setAttribute(String name, Object value)
           
 void setAuthType(String authType)
           
 void setContent(byte[] content)
           
 void setContentType(String contentType)
           
 void setContextPath(String contextPath)
           
 void setCookies(javax.servlet.http.Cookie[] cookies)
           
 void setCharacterEncoding(String characterEncoding)
           
 void setLocalAddr(String localAddr)
           
 void setLocalName(String localName)
           
 void setLocalPort(int localPort)
           
 void setMethod(String method)
           
 void setParameter(String name, String value)
          Set a single value for the specified HTTP parameter.
 void setParameter(String name, String[] values)
          Set an array of values for the specified HTTP parameter.
 void setParameters(Map params)
          Sets all provided parameters replacing any existing values for the provided parameter names.
 void setPathInfo(String pathInfo)
           
 void setProtocol(String protocol)
           
 void setQueryString(String queryString)
           
 void setRemoteAddr(String remoteAddr)
           
 void setRemoteHost(String remoteHost)
           
 void setRemotePort(int remotePort)
           
 void setRemoteUser(String remoteUser)
           
 void setRequestedSessionIdFromCookie(boolean requestedSessionIdFromCookie)
           
 void setRequestedSessionIdFromURL(boolean requestedSessionIdFromURL)
           
 void setRequestedSessionIdValid(boolean requestedSessionIdValid)
           
 void setRequestURI(String requestURI)
           
 void setSecure(boolean secure)
           
 void setServerName(String serverName)
           
 void setServerPort(int serverPort)
           
 void setServletPath(String servletPath)
           
 void setSession(javax.servlet.http.HttpSession session)
           
 void setScheme(String scheme)
           
 void setUserPrincipal(Principal userPrincipal)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_PROTOCOL

public static final String DEFAULT_PROTOCOL
The default protocol: 'http'.

See Also:
Constant Field Values

DEFAULT_SERVER_ADDR

public static final String DEFAULT_SERVER_ADDR
The default server address: '127.0.0.1'.

See Also:
Constant Field Values

DEFAULT_SERVER_NAME

public static final String DEFAULT_SERVER_NAME
The default server name: 'localhost'.

See Also:
Constant Field Values

DEFAULT_SERVER_PORT

public static final int DEFAULT_SERVER_PORT
The default server port: '80'.

See Also:
Constant Field Values

DEFAULT_REMOTE_ADDR

public static final String DEFAULT_REMOTE_ADDR
The default remote address: '127.0.0.1'.

See Also:
Constant Field Values

DEFAULT_REMOTE_HOST

public static final String DEFAULT_REMOTE_HOST
The default remote host: 'localhost'.

See Also:
Constant Field Values
Constructor Detail

EnhancedMockHttpServletRequest

public EnhancedMockHttpServletRequest()
Create a new MockHttpServletRequest with a default MockServletContext.

See Also:
MockServletContext

EnhancedMockHttpServletRequest

public EnhancedMockHttpServletRequest(String method,
                                      String requestURI)
Create a new MockHttpServletRequest with a default MockServletContext.

Parameters:
method - the request method (may be null)
requestURI - the request URI (may be null)
See Also:
setMethod(java.lang.String), setRequestURI(java.lang.String), MockServletContext

EnhancedMockHttpServletRequest

public EnhancedMockHttpServletRequest(javax.servlet.ServletContext servletContext)
Create a new MockHttpServletRequest.

Parameters:
servletContext - the ServletContext that the request runs in (may be null to use a default MockServletContext)
See Also:
MockServletContext

EnhancedMockHttpServletRequest

public EnhancedMockHttpServletRequest(javax.servlet.ServletContext servletContext,
                                      String method,
                                      String requestURI)
Create a new MockHttpServletRequest.

Parameters:
servletContext - the ServletContext that the request runs in (may be null to use a default MockServletContext)
method - the request method (may be null)
requestURI - the request URI (may be null)
See Also:
setMethod(java.lang.String), setRequestURI(java.lang.String), MockServletContext

EnhancedMockHttpServletRequest

public EnhancedMockHttpServletRequest(javax.servlet.http.HttpSession session)

EnhancedMockHttpServletRequest

public EnhancedMockHttpServletRequest(javax.servlet.http.HttpSession session,
                                      String principalName,
                                      Set<String> principalRoles)

EnhancedMockHttpServletRequest

public EnhancedMockHttpServletRequest(javax.servlet.http.HttpSession session,
                                      String principalName,
                                      Set<String> principalRoles,
                                      javax.servlet.http.Cookie[] cookies,
                                      String method)
Method Detail

getServletContext

public javax.servlet.ServletContext getServletContext()
Return the ServletContext that this request is associated with. (Not available in the standard HttpServletRequest interface for some reason.)


isActive

public boolean isActive()
Return whether this request is still active (that is, not completed yet).


close

public void close()
Mark this request as completed, keeping its state.


invalidate

public void invalidate()
Invalidate this request, clearing its state.


checkActive

protected void checkActive()
                    throws IllegalStateException
Check whether this request is still active (that is, not completed yet), throwing an IllegalStateException if not active anymore.

Throws:
IllegalStateException

getAttribute

public Object getAttribute(String name)
Specified by:
getAttribute in interface javax.servlet.ServletRequest

getAttributeNames

public Enumeration getAttributeNames()
Specified by:
getAttributeNames in interface javax.servlet.ServletRequest

getCharacterEncoding

public String getCharacterEncoding()
Specified by:
getCharacterEncoding in interface javax.servlet.ServletRequest

setCharacterEncoding

public void setCharacterEncoding(String characterEncoding)
Specified by:
setCharacterEncoding in interface javax.servlet.ServletRequest

setContent

public void setContent(byte[] content)

getContentLength

public int getContentLength()
Specified by:
getContentLength in interface javax.servlet.ServletRequest

setContentType

public void setContentType(String contentType)

getContentType

public String getContentType()
Specified by:
getContentType in interface javax.servlet.ServletRequest

getInputStream

public javax.servlet.ServletInputStream getInputStream()
                                                throws IOException
Specified by:
getInputStream in interface javax.servlet.ServletRequest
Throws:
IOException

setParameter

public void setParameter(String name,
                         String value)
Set a single value for the specified HTTP parameter.

If there are already one or more values registered for the given parameter name, they will be replaced.


setParameter

public void setParameter(String name,
                         String[] values)
Set an array of values for the specified HTTP parameter.

If there are already one or more values registered for the given parameter name, they will be replaced.


setParameters

public void setParameters(Map params)
Sets all provided parameters replacing any existing values for the provided parameter names. To add without replacing existing values, use addParameters(java.util.Map).


addParameter

public void addParameter(String name,
                         String value)
Add a single value for the specified HTTP parameter.

If there are already one or more values registered for the given parameter name, the given value will be added to the end of the list.


addParameter

public void addParameter(String name,
                         String[] values)
Add an array of values for the specified HTTP parameter.

If there are already one or more values registered for the given parameter name, the given values will be added to the end of the list.


addParameters

public void addParameters(Map params)
Adds all provided parameters without replacing any existing values. To replace existing values, use setParameters(java.util.Map).


removeParameter

public void removeParameter(String name)
Remove already registered values for the specified HTTP parameter, if any.


removeAllParameters

public void removeAllParameters()
Removes all existing parameters.


getParameter

public String getParameter(String name)
Specified by:
getParameter in interface javax.servlet.ServletRequest

getParameterNames

public Enumeration getParameterNames()
Specified by:
getParameterNames in interface javax.servlet.ServletRequest

getParameterValues

public String[] getParameterValues(String name)
Specified by:
getParameterValues in interface javax.servlet.ServletRequest

getParameterMap

public Map getParameterMap()
Specified by:
getParameterMap in interface javax.servlet.ServletRequest

getParameters

public Map<String,String[]> getParameters()

addQueryParameter

public void addQueryParameter(String name,
                              String value)
Add a query parameter that will be appended to the URI query string.


removeQueryParameter

public void removeQueryParameter(String name)

getQueryParameters

public Map<String,String> getQueryParameters()

setProtocol

public void setProtocol(String protocol)

getProtocol

public String getProtocol()
Specified by:
getProtocol in interface javax.servlet.ServletRequest

setScheme

public void setScheme(String scheme)

getScheme

public String getScheme()
Specified by:
getScheme in interface javax.servlet.ServletRequest

setServerName

public void setServerName(String serverName)

getServerName

public String getServerName()
Specified by:
getServerName in interface javax.servlet.ServletRequest

setServerPort

public void setServerPort(int serverPort)

getServerPort

public int getServerPort()
Specified by:
getServerPort in interface javax.servlet.ServletRequest

getReader

public BufferedReader getReader()
                         throws UnsupportedEncodingException
Specified by:
getReader in interface javax.servlet.ServletRequest
Throws:
UnsupportedEncodingException

setRemoteAddr

public void setRemoteAddr(String remoteAddr)

getRemoteAddr

public String getRemoteAddr()
Specified by:
getRemoteAddr in interface javax.servlet.ServletRequest

setRemoteHost

public void setRemoteHost(String remoteHost)

getRemoteHost

public String getRemoteHost()
Specified by:
getRemoteHost in interface javax.servlet.ServletRequest

setAttribute

public void setAttribute(String name,
                         Object value)
Specified by:
setAttribute in interface javax.servlet.ServletRequest

removeAttribute

public void removeAttribute(String name)
Specified by:
removeAttribute in interface javax.servlet.ServletRequest

clearAttributes

public void clearAttributes()
Clear all of this request's attributes.


addPreferredLocale

public void addPreferredLocale(Locale locale)
Add a new preferred locale, before any existing locales.


getLocale

public Locale getLocale()
Specified by:
getLocale in interface javax.servlet.ServletRequest

getLocales

public Enumeration getLocales()
Specified by:
getLocales in interface javax.servlet.ServletRequest

setSecure

public void setSecure(boolean secure)

isSecure

public boolean isSecure()
Specified by:
isSecure in interface javax.servlet.ServletRequest

getRequestDispatcher

public javax.servlet.RequestDispatcher getRequestDispatcher(String path)
Specified by:
getRequestDispatcher in interface javax.servlet.ServletRequest

getRealPath

public String getRealPath(String path)
Specified by:
getRealPath in interface javax.servlet.ServletRequest

setRemotePort

public void setRemotePort(int remotePort)

getRemotePort

public int getRemotePort()
Specified by:
getRemotePort in interface javax.servlet.ServletRequest

setLocalName

public void setLocalName(String localName)

getLocalName

public String getLocalName()
Specified by:
getLocalName in interface javax.servlet.ServletRequest

setLocalAddr

public void setLocalAddr(String localAddr)

getLocalAddr

public String getLocalAddr()
Specified by:
getLocalAddr in interface javax.servlet.ServletRequest

setLocalPort

public void setLocalPort(int localPort)

getLocalPort

public int getLocalPort()
Specified by:
getLocalPort in interface javax.servlet.ServletRequest

setAuthType

public void setAuthType(String authType)

getAuthType

public String getAuthType()
Specified by:
getAuthType in interface javax.servlet.http.HttpServletRequest

setCookies

public void setCookies(javax.servlet.http.Cookie[] cookies)

getCookies

public javax.servlet.http.Cookie[] getCookies()
Specified by:
getCookies in interface javax.servlet.http.HttpServletRequest

addCookie

public void addCookie(javax.servlet.http.Cookie cookie)

addHeader

public void addHeader(String name,
                      Object value)
Add a header entry for the given name.

If there was no entry for that header name before, the value will be used as-is. In case of an existing entry, a String array will be created, adding the given value (more specifically, its toString representation) as further element.

Multiple values can only be stored as list of Strings, following the Servlet spec (see getHeaders accessor). As alternative to repeated addHeader calls for individual elements, you can use a single call with an entire array or Collection of values as parameter.

See Also:
getHeaderNames(), getHeader(java.lang.String), getHeaders(), getDateHeader(java.lang.String), getIntHeader(java.lang.String)

getDateHeader

public long getDateHeader(String name)
Specified by:
getDateHeader in interface javax.servlet.http.HttpServletRequest

getHeader

public String getHeader(String name)
Specified by:
getHeader in interface javax.servlet.http.HttpServletRequest

getHeaders

public Map<String,String[]> getHeaders()

getHeaders

public Enumeration getHeaders(String name)
Specified by:
getHeaders in interface javax.servlet.http.HttpServletRequest

getHeaderNames

public Enumeration getHeaderNames()
Specified by:
getHeaderNames in interface javax.servlet.http.HttpServletRequest

getIntHeader

public int getIntHeader(String name)
Specified by:
getIntHeader in interface javax.servlet.http.HttpServletRequest

setMethod

public void setMethod(String method)

getMethod

public String getMethod()
Specified by:
getMethod in interface javax.servlet.http.HttpServletRequest

setPathInfo

public void setPathInfo(String pathInfo)

getPathInfo

public String getPathInfo()
Specified by:
getPathInfo in interface javax.servlet.http.HttpServletRequest

getPathTranslated

public String getPathTranslated()
Specified by:
getPathTranslated in interface javax.servlet.http.HttpServletRequest

setContextPath

public void setContextPath(String contextPath)

getContextPath

public String getContextPath()
Specified by:
getContextPath in interface javax.servlet.http.HttpServletRequest

setQueryString

public void setQueryString(String queryString)

getQueryString

public String getQueryString()
Specified by:
getQueryString in interface javax.servlet.http.HttpServletRequest

setRemoteUser

public void setRemoteUser(String remoteUser)

getRemoteUser

public String getRemoteUser()
Specified by:
getRemoteUser in interface javax.servlet.http.HttpServletRequest

addRole

public void addRole(String role)
Deprecated. in favor of addUserRole

See Also:
addUserRole(java.lang.String)

addUserRole

public void addUserRole(String role)

isUserInRole

public boolean isUserInRole(String role)
Specified by:
isUserInRole in interface javax.servlet.http.HttpServletRequest

setUserPrincipal

public void setUserPrincipal(Principal userPrincipal)

getUserPrincipal

public Principal getUserPrincipal()
Specified by:
getUserPrincipal in interface javax.servlet.http.HttpServletRequest

getRequestedSessionId

public String getRequestedSessionId()
Specified by:
getRequestedSessionId in interface javax.servlet.http.HttpServletRequest

setRequestURI

public void setRequestURI(String requestURI)

getRequestURI

public String getRequestURI()
Specified by:
getRequestURI in interface javax.servlet.http.HttpServletRequest

getRequestURL

public StringBuffer getRequestURL()
Specified by:
getRequestURL in interface javax.servlet.http.HttpServletRequest

setServletPath

public void setServletPath(String servletPath)

getServletPath

public String getServletPath()
Specified by:
getServletPath in interface javax.servlet.http.HttpServletRequest

setSession

public void setSession(javax.servlet.http.HttpSession session)

getSession

public javax.servlet.http.HttpSession getSession(boolean create)
Specified by:
getSession in interface javax.servlet.http.HttpServletRequest

getSession

public javax.servlet.http.HttpSession getSession()
Specified by:
getSession in interface javax.servlet.http.HttpServletRequest

setRequestedSessionIdValid

public void setRequestedSessionIdValid(boolean requestedSessionIdValid)

isRequestedSessionIdValid

public boolean isRequestedSessionIdValid()
Specified by:
isRequestedSessionIdValid in interface javax.servlet.http.HttpServletRequest

setRequestedSessionIdFromCookie

public void setRequestedSessionIdFromCookie(boolean requestedSessionIdFromCookie)

isRequestedSessionIdFromCookie

public boolean isRequestedSessionIdFromCookie()
Specified by:
isRequestedSessionIdFromCookie in interface javax.servlet.http.HttpServletRequest

setRequestedSessionIdFromURL

public void setRequestedSessionIdFromURL(boolean requestedSessionIdFromURL)

isRequestedSessionIdFromURL

public boolean isRequestedSessionIdFromURL()
Specified by:
isRequestedSessionIdFromURL in interface javax.servlet.http.HttpServletRequest

isRequestedSessionIdFromUrl

public boolean isRequestedSessionIdFromUrl()
Specified by:
isRequestedSessionIdFromUrl in interface javax.servlet.http.HttpServletRequest

isAllParametersInQueryString

public boolean isAllParametersInQueryString()