net.sf.click.servlet
Class MockRequest

java.lang.Object
  extended bynet.sf.click.servlet.MockRequest
All Implemented Interfaces:
HttpServletRequest, ServletRequest

public class MockRequest
extends Object
implements HttpServletRequest

Mock implementation of HttpServletRequest.

Implements all of the methods from the standard HttpServletRequest class plus helper methods to aid setting up a request.

This class was adapted from Apache Wicket.

Author:
Chris Turner, Bob Schellink

Field Summary
 
Fields inherited from interface javax.servlet.http.HttpServletRequest
BASIC_AUTH, CLIENT_CERT_AUTH, DIGEST_AUTH, FORM_AUTH
 
Constructor Summary
MockRequest()
          Create new MockRequest.
MockRequest(Locale locale)
          Create new MockRequest for the specified local.
MockRequest(Locale locale, ServletContext servletContext)
          Create a new MockRequest for the specified locale and servletContext.
MockRequest(Locale locale, ServletContext servletContext, HttpSession session)
          Create a new MockRequest for the specified arguments.
MockRequest(Locale locale, String contextPath, String servletPath, ServletContext servletContext, HttpSession session)
          Create a new MockRequest for the specified arguments.
MockRequest(ServletContext servletContext)
          Create a new MockRequest for the specified context.
 
Method Summary
 void addCookie(Cookie cookie)
          Add a new cookie.
 void addFile(String fieldName, File file, String contentType)
          Add an uploaded file to the request.
 void addHeader(String name, String value)
          Add a header to the request.
 Object getAttribute(String name)
          Get an attribute.
 Enumeration getAttributeNames()
          Get the names of all of the values.
 String getAuthType()
          Get the auth type.
 String getCharacterEncoding()
          Get the current character encoding.
 int getContentLength()
          Return the length of the content.
 String getContentType()
          If useMultiPartContentType set as true return the correct content-type.
 String getContextPath()
          Returns the portion of the request URI that indicates the context of the request.
 Cookie[] getCookies()
          Get all of the cookies for this request.
 long getDateHeader(String name)
          Get the given header as a date.
 String getForward()
          Returns the url that was forwarded to, otherwise return null.
 String getHeader(String name)
          Get the given header value.
 Enumeration getHeaderNames()
          Get the names of all of the headers.
 Enumeration getHeaders(String name)
          Get enumeration of all header values with the given name.
 List getIncludes()
          Returns the list of server side included url's.
 ServletInputStream getInputStream()
          Returns an input stream if there has been added some uploaded files.
 int getIntHeader(String name)
          Get the given header as an int.
 String getLocalAddr()
          Return the local address, "127.0.0.1".
 Locale getLocale()
          Get the locale of the request.
 Enumeration getLocales()
          Return all the accepted locales.
 String getLocalName()
          Return the local name, "127.0.0.1".
 int getLocalPort()
          Return the local port, 80.
 String getMethod()
          Get the method.
 String getParameter(String name)
          Get the request parameter with the given name.
 Map getParameterMap()
          Get the map of all of the parameters.
 Enumeration getParameterNames()
          Get the names of all of the parameters.
 String[] getParameterValues(String name)
          Get the values for the given parameter.
 String getPathInfo()
          Get the path info.
 String getPathTranslated()
          Always returns null.
 String getProtocol()
          Get the protocol.
 String getQueryString()
          Get the query string part of the request.
 BufferedReader getReader()
          This feature is not implemented at this time as we are not supporting binary servlet input.
 String getRealPath(String name)
          Deprecated. Use ServletContext.getRealPath(String) instead.
 String getRemoteAddr()
          Get the remote address of the client.
 String getRemoteHost()
          Get the remote host.
 int getRemotePort()
          Return the remote port, 80.
 String getRemoteUser()
          Get the name of the remote user from the REMOTE_USER header.
 RequestDispatcher getRequestDispatcher(String path)
          Returns a RequestDispatcher for the specified path.
 String getRequestedSessionId()
          Get the requested session id.
 String getRequestURI()
          Returns context path and servlet path concatenated, typically /applicationClassName/applicationClassName.
 StringBuffer getRequestURL()
          Returns (an attempt at) a reconstructed URL based on it's constituent parts.
 String getScheme()
          Get the scheme http, https, or ftp.
 String getServerName()
          Get the host server name to which the request was sent.
 int getServerPort()
          Returns the port number to which the request was sent.
 String getServletPath()
          Return a String containing the name or path of the servlet being called.
 HttpSession getSession()
          Returns the current HttpSession associated with this request.
 HttpSession getSession(boolean create)
          Returns the current HttpSession associated with this request.
 Principal getUserPrincipal()
          Get the user principal.
 boolean hasUploadedFiles()
           
 void initialize()
          Reset the request back to a default state.
 boolean isPost()
          Return whether the request is a post or not.
 boolean isRequestedSessionIdFromCookie()
          Check whether session id is from a cookie.
 boolean isRequestedSessionIdFromUrl()
          Check whether session id is from a url rewrite.
 boolean isRequestedSessionIdFromURL()
          Check whether session id is from a url rewrite.
 boolean isRequestedSessionIdValid()
          Check whether the session id is valid.
 boolean isSecure()
          Always returns false.
 boolean isUserInRole(String name)
          NOT IMPLEMENTED.
 void removeAttribute(String name)
          Remove the given attribute.
 void removeParameter(String name)
          Remove the specified parameter.
 void reset()
          Delegate to initialize method.
 void setAttribute(String name, Object o)
          Set the given attribute.
 void setAuthType(String authType)
          Set the auth type.
 void setCharacterEncoding(String encoding)
          Set the character encoding.
 void setContextPath(String contextPath)
          Set the portion of the request URI that indicates the context of the request.
 void setCookies(Cookie[] theCookies)
          Set the cookies.
 void setHttpSession(HttpSession session)
          Set the request's session instance.
 void setMethod(String method)
          Set the method.
 void setParameter(String name, String value)
          Set a parameter.
 void setParameter(String name, String[] values)
          Set the specified parameter name to the array of strings.
 void setParameters(Map parameters)
          Sets a map of parameters.
 void setPathInfo(String path)
          Set the path that this request is supposed to be serving.
 void setScheme(String scheme)
          Set the request's scheme, for example http, https, or ftp.
 void setServerName(String serverName)
          Sets the host server name to which the request was sent.
 void setServerPort(int serverPort)
          Set the port number to which the request was sent.
 void setServletContext(ServletContext servletContext)
          Set the request's servletContext instance.
 void setServletPath(String servletPath)
          Set the string containing the name or path of the servlet being called.
 void setSession(HttpSession session)
          Set the current HttpSession associated with this request.
 void setUseMultiPartContentType(boolean useMultiPartContentType)
          True will force Request to generate multiPart ContentType and ContentLength.
 String toString()
          Returns the String representation of the mock request.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MockRequest

public MockRequest()
Create new MockRequest.


MockRequest

public MockRequest(Locale locale)
Create new MockRequest for the specified local.

Parameters:
locale - locale for this request

MockRequest

public MockRequest(ServletContext servletContext)
Create a new MockRequest for the specified context.

Parameters:
servletContext - the servletContext for this request

MockRequest

public MockRequest(Locale locale,
                   ServletContext servletContext)
Create a new MockRequest for the specified locale and servletContext.

Parameters:
locale - locale for this request
servletContext - the servletContext for this request

MockRequest

public MockRequest(Locale locale,
                   ServletContext servletContext,
                   HttpSession session)
Create a new MockRequest for the specified arguments.

Parameters:
locale - The request locale, or null to use the default locale
session - The session object
servletContext - The current servlet context

MockRequest

public MockRequest(Locale locale,
                   String contextPath,
                   String servletPath,
                   ServletContext servletContext,
                   HttpSession session)
Create a new MockRequest for the specified arguments.

Parameters:
locale - The request locale, or null to use the default locale
contextPath - the request context path
servletPath - the request servlet path
servletContext - The current servlet context
session - the request session
Method Detail

setServletContext

public void setServletContext(ServletContext servletContext)
Set the request's servletContext instance.

Parameters:
servletContext - the new ServletContext instance

setHttpSession

public void setHttpSession(HttpSession session)
Set the request's session instance.

Parameters:
session - the new HttpSession instance

addCookie

public void addCookie(Cookie cookie)
Add a new cookie.

Parameters:
cookie - The cookie

addFile

public void addFile(String fieldName,
                    File file,
                    String contentType)
Add an uploaded file to the request. Use this to simulate a file that has been uploaded to a field.

Parameters:
fieldName - The fieldname of the upload field.
file - The file to upload.
contentType - The content type of the file. Must be a correct mimetype.

addHeader

public void addHeader(String name,
                      String value)
Add a header to the request.

Parameters:
name - The name of the header to add
value - The value

getAttribute

public Object getAttribute(String name)
Get an attribute.

Specified by:
getAttribute in interface ServletRequest
Parameters:
name - The attribute name
Returns:
The value, or null

getAttributeNames

public Enumeration getAttributeNames()
Get the names of all of the values.

Specified by:
getAttributeNames in interface ServletRequest
Returns:
The names

getAuthType

public String getAuthType()
Get the auth type.

Specified by:
getAuthType in interface HttpServletRequest
Returns:
The auth type

getCharacterEncoding

public String getCharacterEncoding()
Get the current character encoding.

Specified by:
getCharacterEncoding in interface ServletRequest
Returns:
The character encoding

setUseMultiPartContentType

public void setUseMultiPartContentType(boolean useMultiPartContentType)
True will force Request to generate multiPart ContentType and ContentLength.

Parameters:
useMultiPartContentType - true if the request is multi-part, false otherwise

getContentLength

public int getContentLength()
Return the length of the content. This is always -1 except if useMultiPartContentType set as true. Then the length will be the length of the generated request.

Specified by:
getContentLength in interface ServletRequest
Returns:
-1 if useMultiPartContentType is false. Else the length of the generated request.

getContentType

public String getContentType()
If useMultiPartContentType set as true return the correct content-type.

Specified by:
getContentType in interface ServletRequest
Returns:
The correct multipart content-type if useMultiPartContentType is true. Else null.

getCookies

public Cookie[] getCookies()
Get all of the cookies for this request.

Specified by:
getCookies in interface HttpServletRequest
Returns:
The cookies

getDateHeader

public long getDateHeader(String name)
                   throws IllegalArgumentException
Get the given header as a date.

Specified by:
getDateHeader in interface HttpServletRequest
Parameters:
name - The header name
Returns:
The date, or -1 if header not found
Throws:
IllegalArgumentException - If the header cannot be converted

getHeader

public String getHeader(String name)
Get the given header value.

Specified by:
getHeader in interface HttpServletRequest
Parameters:
name - The header name
Returns:
The header value or null

getHeaderNames

public Enumeration getHeaderNames()
Get the names of all of the headers.

Specified by:
getHeaderNames in interface HttpServletRequest
Returns:
The header names

getHeaders

public Enumeration getHeaders(String name)
Get enumeration of all header values with the given name.

Specified by:
getHeaders in interface HttpServletRequest
Parameters:
name - The name
Returns:
The header values

getInputStream

public ServletInputStream getInputStream()
                                  throws IOException
Returns an input stream if there has been added some uploaded files. Use addFile(String, File, String) to add some uploaded files.

Specified by:
getInputStream in interface ServletRequest
Returns:
The input stream
Throws:
IOException - If an I/O related problem occurs

getIntHeader

public int getIntHeader(String name)
Get the given header as an int.

Specified by:
getIntHeader in interface HttpServletRequest
Parameters:
name - The header name
Returns:
The header value or -1 if header not found
Throws:
NumberFormatException - If the header is not formatted correctly

getLocale

public Locale getLocale()
Get the locale of the request. Attempts to decode the Accept-Language header and if not found returns the default locale of the JVM.

Specified by:
getLocale in interface ServletRequest
Returns:
The locale

getLocales

public Enumeration getLocales()
Return all the accepted locales. This implementation always returns just one.

Specified by:
getLocales in interface ServletRequest
Returns:
The locales

getMethod

public String getMethod()
Get the method.

The returned string will be in upper case eg. POST.

Specified by:
getMethod in interface HttpServletRequest
Returns:
The method

getParameter

public String getParameter(String name)
Get the request parameter with the given name.

Specified by:
getParameter in interface ServletRequest
Parameters:
name - The parameter name
Returns:
The parameter value, or null

getParameterMap

public Map getParameterMap()
Get the map of all of the parameters.

Specified by:
getParameterMap in interface ServletRequest
Returns:
The parameters

getParameterNames

public Enumeration getParameterNames()
Get the names of all of the parameters.

Specified by:
getParameterNames in interface ServletRequest
Returns:
The parameter names

getParameterValues

public String[] getParameterValues(String name)
Get the values for the given parameter.

Specified by:
getParameterValues in interface ServletRequest
Parameters:
name - The name of the parameter
Returns:
The return values

getPathInfo

public String getPathInfo()
Get the path info.

Specified by:
getPathInfo in interface HttpServletRequest
Returns:
The path info

getPathTranslated

public String getPathTranslated()
Always returns null.

Specified by:
getPathTranslated in interface HttpServletRequest
Returns:
null

getProtocol

public String getProtocol()
Get the protocol.

Specified by:
getProtocol in interface ServletRequest
Returns:
Always HTTP/1.1

getQueryString

public String getQueryString()
Get the query string part of the request.

Specified by:
getQueryString in interface HttpServletRequest
Returns:
The query string

getReader

public BufferedReader getReader()
                         throws IOException
This feature is not implemented at this time as we are not supporting binary servlet input. This functionality may be added in the future.

Specified by:
getReader in interface ServletRequest
Returns:
The reader
Throws:
IOException - If an I/O related problem occurs

getRealPath

public String getRealPath(String name)
Deprecated. Use ServletContext.getRealPath(String) instead.

Deprecated method - should not be used.

Specified by:
getRealPath in interface ServletRequest
Parameters:
name - The name
Returns:
The path

getRemoteAddr

public String getRemoteAddr()
Get the remote address of the client.

Specified by:
getRemoteAddr in interface ServletRequest
Returns:
Always 127.0.0.1

getRemoteHost

public String getRemoteHost()
Get the remote host.

Specified by:
getRemoteHost in interface ServletRequest
Returns:
Always localhost

getRemoteUser

public String getRemoteUser()
Get the name of the remote user from the REMOTE_USER header.

Specified by:
getRemoteUser in interface HttpServletRequest
Returns:
The name of the remote user

getLocalAddr

public String getLocalAddr()
Return the local address, "127.0.0.1".

Returns:
"127.0.0.1" as the local address

getLocalName

public String getLocalName()
Return the local name, "127.0.0.1".

Returns:
"127.0.0.1" as the local name

getLocalPort

public int getLocalPort()
Return the local port, 80.

Returns:
80 as the local port

getRemotePort

public int getRemotePort()
Return the remote port, 80.

Returns:
80 as the remote port

getRequestDispatcher

public RequestDispatcher getRequestDispatcher(String path)
Returns a RequestDispatcher for the specified path. The dispatcher will not dispatch to the resource. It only records the specified path so that one can test if the correct path was dispatched to.

Specified by:
getRequestDispatcher in interface ServletRequest
Parameters:
path - a String specifying the pathname to the resource
Returns:
a dispatcher for the specified path

getRequestedSessionId

public String getRequestedSessionId()
Get the requested session id. Always returns the id of the current session.

Specified by:
getRequestedSessionId in interface HttpServletRequest
Returns:
The session id

getRequestURI

public String getRequestURI()
Returns context path and servlet path concatenated, typically /applicationClassName/applicationClassName.

Specified by:
getRequestURI in interface HttpServletRequest
Returns:
The path value
See Also:
HttpServletRequest.getRequestURI()

getRequestURL

public StringBuffer getRequestURL()
Returns (an attempt at) a reconstructed URL based on it's constituent parts.

Specified by:
getRequestURL in interface HttpServletRequest
Returns:
a StringBuffer object containing the reconstructed URL

isPost

public boolean isPost()
Return whether the request is a post or not.

Returns:
true if the request is a post, false otherwise

getScheme

public String getScheme()
Get the scheme http, https, or ftp.

Specified by:
getScheme in interface ServletRequest
Returns:
the scheme used by this request

setScheme

public void setScheme(String scheme)
Set the request's scheme, for example http, https, or ftp.

Parameters:
scheme - the request's scheme

getServerName

public String getServerName()
Get the host server name to which the request was sent.

Specified by:
getServerName in interface ServletRequest
Returns:
always the host server name

setServerName

public void setServerName(String serverName)
Sets the host server name to which the request was sent.

Parameters:
serverName - the server name the request was sent to

getServerPort

public int getServerPort()
Returns the port number to which the request was sent.

Specified by:
getServerPort in interface ServletRequest
Returns:
the server port to which the request was sent

setServerPort

public void setServerPort(int serverPort)
Set the port number to which the request was sent.

Parameters:
serverPort - the port number to which the request was sent

getContextPath

public String getContextPath()
Returns the portion of the request URI that indicates the context of the request.

Specified by:
getContextPath in interface HttpServletRequest
Returns:
the portion of the request URI that indicates the context of the request.

setContextPath

public void setContextPath(String contextPath)
Set the portion of the request URI that indicates the context of the request.

Parameters:
contextPath - the portion of the request URI that indicates the context of the request.

getServletPath

public String getServletPath()
Return a String containing the name or path of the servlet being called.

Specified by:
getServletPath in interface HttpServletRequest
Returns:
The servlet path

setServletPath

public void setServletPath(String servletPath)
Set the string containing the name or path of the servlet being called.

Parameters:
servletPath - a String containing the name or path of the servlet being called

getSession

public HttpSession getSession()
Returns the current HttpSession associated with this request.

Specified by:
getSession in interface HttpServletRequest
Returns:
the session associated with this request

setSession

public void setSession(HttpSession session)
Set the current HttpSession associated with this request.

Parameters:
session - the HttpSession to associate with this request

getSession

public HttpSession getSession(boolean create)
Returns the current HttpSession associated with this request.

Specified by:
getSession in interface HttpServletRequest
Parameters:
create - if true creates a new session if one does not exist
Returns:
the current HttpSession associated with this request.

getUserPrincipal

public Principal getUserPrincipal()
Get the user principal.

Specified by:
getUserPrincipal in interface HttpServletRequest
Returns:
A user principal

hasUploadedFiles

public boolean hasUploadedFiles()
Returns:
True if there has been added files to this request using addFile(String, File, String)

initialize

public final void initialize()
Reset the request back to a default state.


reset

public void reset()
Delegate to initialize method.


isRequestedSessionIdFromCookie

public boolean isRequestedSessionIdFromCookie()
Check whether session id is from a cookie. Always returns true.

Specified by:
isRequestedSessionIdFromCookie in interface HttpServletRequest
Returns:
Always true

isRequestedSessionIdFromUrl

public boolean isRequestedSessionIdFromUrl()
Check whether session id is from a url rewrite. Always returns false.

Specified by:
isRequestedSessionIdFromUrl in interface HttpServletRequest
Returns:
Always false

isRequestedSessionIdFromURL

public boolean isRequestedSessionIdFromURL()
Check whether session id is from a url rewrite. Always returns false.

Specified by:
isRequestedSessionIdFromURL in interface HttpServletRequest
Returns:
Always false

isRequestedSessionIdValid

public boolean isRequestedSessionIdValid()
Check whether the session id is valid.

Specified by:
isRequestedSessionIdValid in interface HttpServletRequest
Returns:
Always true

isSecure

public boolean isSecure()
Always returns false.

Specified by:
isSecure in interface ServletRequest
Returns:
Always false

isUserInRole

public boolean isUserInRole(String name)
NOT IMPLEMENTED.

Specified by:
isUserInRole in interface HttpServletRequest
Parameters:
name - The role name
Returns:
Always false

removeAttribute

public void removeAttribute(String name)
Remove the given attribute.

Specified by:
removeAttribute in interface ServletRequest
Parameters:
name - The name of the attribute

setAttribute

public void setAttribute(String name,
                         Object o)
Set the given attribute.

Specified by:
setAttribute in interface ServletRequest
Parameters:
name - The attribute name
o - The value to set

setAuthType

public void setAuthType(String authType)
Set the auth type.

Parameters:
authType - The auth type

setCharacterEncoding

public void setCharacterEncoding(String encoding)
                          throws UnsupportedEncodingException
Set the character encoding.

Specified by:
setCharacterEncoding in interface ServletRequest
Parameters:
encoding - The character encoding
Throws:
UnsupportedEncodingException - If encoding not supported

setCookies

public void setCookies(Cookie[] theCookies)
Set the cookies.

Parameters:
theCookies - The cookies

setMethod

public void setMethod(String method)
Set the method.

Parameters:
method - The method

setParameter

public void setParameter(String name,
                         String value)
Set a parameter.

Parameters:
name - The name
value - The value

setParameter

public void setParameter(String name,
                         String[] values)
Set the specified parameter name to the array of strings.

Parameters:
name - name of the parameter
values - the parameter values

removeParameter

public void removeParameter(String name)
Remove the specified parameter.

Parameters:
name - the parameter name to remove

setParameters

public void setParameters(Map parameters)
Sets a map of parameters.

Parameters:
parameters - the parameters to set

setPathInfo

public void setPathInfo(String path)
Set the path that this request is supposed to be serving. The path is relative to the web application root and should start with a / character

Parameters:
path - specifies the request path to serve

getForward

public String getForward()
Returns the url that was forwarded to, otherwise return null.

Returns:
url that was forwarded to
See Also:
MockRequestDispatcher.forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse)

getIncludes

public List getIncludes()
Returns the list of server side included url's.

Returns:
list of urls that were included
See Also:
MockRequestDispatcher.include(javax.servlet.ServletRequest, javax.servlet.ServletResponse)

toString

public String toString()
Returns the String representation of the mock request.

Returns:
string representation of the mock request