org.apache.wicket.protocol.http
Class MockHttpServletRequest

java.lang.Object
  extended by org.apache.wicket.protocol.http.MockHttpServletRequest
All Implemented Interfaces:
javax.servlet.http.HttpServletRequest, javax.servlet.ServletRequest

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

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

Author:
Chris Turner

Field Summary
 
Fields inherited from interface javax.servlet.http.HttpServletRequest
BASIC_AUTH, CLIENT_CERT_AUTH, DIGEST_AUTH, FORM_AUTH
 
Constructor Summary
MockHttpServletRequest(Application application, javax.servlet.http.HttpSession session, javax.servlet.ServletContext context)
          Create the request using the supplied session object.
 
Method Summary
 void addCookie(javax.servlet.http.Cookie cookie)
          Add a new cookie.
 void addCookies(Iterable<javax.servlet.http.Cookie> cookies)
           
 void addDateHeader(String name, long date)
           
 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<String> 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()
          Get the context path.
 javax.servlet.http.Cookie[] getCookies()
          Get all of the cookies for this request.
 long getDateHeader(String name)
          Get the given header as a date.
 String getHeader(String name)
          Get the given header value.
 Enumeration<String> getHeaderNames()
          Get the names of all of the headers.
 Enumeration<String> getHeaders(String name)
          Get enumeration of all header values with the given name.
 javax.servlet.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()
           
 Locale getLocale()
          Get the locale of the request.
 Enumeration<Locale> getLocales()
          Return all the accepted locales.
 String getLocalName()
           
 int getLocalPort()
           
 String getMethod()
          Get the method.
 String getParameter(String name)
          Get the request parameter with the given name.
 Map<String,Object> getParameterMap()
          Get the map of all of the parameters.
 Enumeration<String> 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()
           
 String getRemoteUser()
          Get the name of the remote user from the REMOTE_USER header.
 javax.servlet.RequestDispatcher getRequestDispatcher(String name)
          Return a dummy dispatcher that just records that dispatch has occurred without actually doing anything.
 String getRequestedSessionId()
          Get the requested session id.
 String getRequestURI()
          Returns context path and servlet path concatenated, typically /applicationClassName/applicationClassName
 StringBuffer getRequestURL()
          Try to build a rough URL.
 String getScheme()
          Get the scheme.
 String getServerName()
          Get the server name.
 int getServerPort()
          Get the server port.
 String getServletPath()
          The servlet path may either be the application name or /.
 javax.servlet.http.HttpSession getSession()
          Get the sessions.
 javax.servlet.http.HttpSession getSession(boolean b)
          Get the session.
 Principal getUserPrincipal()
          Get the user principal.
 boolean hasUploadedFiles()
           
 void initialize()
          Reset the request back to a default state.
 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 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 setCookies(javax.servlet.http.Cookie[] theCookies)
          Set the cookies.
 void setMethod(String method)
          Set the method.
 void setParameter(String name, String value)
          Set a parameter.
 void setParameters(Map<String,String[]> parameters)
          Sets a map of parameters.
 void setPath(String path)
          Set the path that this request is supposed to be serving.
 void setRequestToBookmarkablePage(Page page, Map<String,Object> params)
          Initialize the request parameters to point to the given bookmarkable page.
 void setRequestToComponent(Component component)
          Initialize the request parameters to point to the given component.
 void setRequestToFormComponent(Form<?> form, Map<String,Object> values)
          Initialize the request parameters to point to the given form component.
 void setRequestToRedirectString(String redirect)
          Initialize the request parameters from the given redirect string that redirects back to a particular component for display.
 void setURL(String url)
          Set the complete url for this request.
 void setUseMultiPartContentType(boolean useMultiPartContentType)
          true will force Request generate multiPart ContentType and ContentLength
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MockHttpServletRequest

public MockHttpServletRequest(Application application,
                              javax.servlet.http.HttpSession session,
                              javax.servlet.ServletContext context)
Create the request using the supplied session object. Note that in order for temporary sessions to work, the supplied session must be an instance of MockHttpSession

Parameters:
application - The application that this request is for
session - The session object
context - The current servlet context
Method Detail

addCookie

public void addCookie(javax.servlet.http.Cookie cookie)
Add a new cookie.

Parameters:
cookie - The cookie

addCookies

public void addCookies(Iterable<javax.servlet.http.Cookie> cookies)

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

addDateHeader

public void addDateHeader(String name,
                          long date)
Parameters:
name -
date -

getAttribute

public Object getAttribute(String name)
Get an attribute.

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

getAttributeNames

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

Specified by:
getAttributeNames in interface javax.servlet.ServletRequest
Returns:
The names

getAuthType

public String getAuthType()
Get the auth type.

Specified by:
getAuthType in interface javax.servlet.http.HttpServletRequest
Returns:
The auth type

getCharacterEncoding

public String getCharacterEncoding()
Get the current character encoding.

Specified by:
getCharacterEncoding in interface javax.servlet.ServletRequest
Returns:
The character encoding

setUseMultiPartContentType

public void setUseMultiPartContentType(boolean useMultiPartContentType)
true will force Request generate multiPart ContentType and ContentLength

Parameters:
useMultiPartContentType -

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 javax.servlet.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 javax.servlet.ServletRequest
Returns:
The correct multipart content-type if useMultiPartContentType is true. Else null.

getContextPath

public String getContextPath()
Get the context path. For this mock implementation the name of the application is always returned.

Specified by:
getContextPath in interface javax.servlet.http.HttpServletRequest
Returns:
The context path

getCookies

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

Specified by:
getCookies in interface javax.servlet.http.HttpServletRequest
Returns:
The cookies

getDateHeader

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

Specified by:
getDateHeader in interface javax.servlet.http.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 javax.servlet.http.HttpServletRequest
Parameters:
name - The header name
Returns:
The header value or null

getHeaderNames

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

Specified by:
getHeaderNames in interface javax.servlet.http.HttpServletRequest
Returns:
The header names

getHeaders

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

Specified by:
getHeaders in interface javax.servlet.http.HttpServletRequest
Parameters:
name - The name
Returns:
The header values

getInputStream

public javax.servlet.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 javax.servlet.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 javax.servlet.http.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 javax.servlet.ServletRequest
Returns:
The locale

getLocales

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

Specified by:
getLocales in interface javax.servlet.ServletRequest
Returns:
The locales

getMethod

public String getMethod()
Get the method.

Specified by:
getMethod in interface javax.servlet.http.HttpServletRequest
Returns:
The method

getParameter

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

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

getParameterMap

public Map<String,Object> getParameterMap()
Get the map of all of the parameters.

Specified by:
getParameterMap in interface javax.servlet.ServletRequest
Returns:
The parameters

getParameterNames

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

Specified by:
getParameterNames in interface javax.servlet.ServletRequest
Returns:
The parameter names

getParameterValues

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

Specified by:
getParameterValues in interface javax.servlet.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 javax.servlet.http.HttpServletRequest
Returns:
The path info

getPathTranslated

public String getPathTranslated()
Always returns null.

Specified by:
getPathTranslated in interface javax.servlet.http.HttpServletRequest
Returns:
null

getProtocol

public String getProtocol()
Get the protocol.

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

getQueryString

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

Specified by:
getQueryString in interface javax.servlet.http.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 javax.servlet.ServletRequest
Returns:
The reader
Throws:
IOException - If an I/O related problem occurs

getRealPath

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

Deprecated method - should not be used.

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

getRemoteAddr

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

Specified by:
getRemoteAddr in interface javax.servlet.ServletRequest
Returns:
Always 127.0.0.1

getRemoteHost

public String getRemoteHost()
Get the remote host.

Specified by:
getRemoteHost in interface javax.servlet.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 javax.servlet.http.HttpServletRequest
Returns:
The name of the remote user

getRequestDispatcher

public javax.servlet.RequestDispatcher getRequestDispatcher(String name)
Return a dummy dispatcher that just records that dispatch has occurred without actually doing anything.

Specified by:
getRequestDispatcher in interface javax.servlet.ServletRequest
Parameters:
name - The name to dispatch to
Returns:
The dispatcher

getRequestedSessionId

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

Specified by:
getRequestedSessionId in interface javax.servlet.http.HttpServletRequest
Returns:
The session id

getRequestURI

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

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

getRequestURL

public StringBuffer getRequestURL()
Try to build a rough URL.

Specified by:
getRequestURL in interface javax.servlet.http.HttpServletRequest
Returns:
The url

getScheme

public String getScheme()
Get the scheme.

Specified by:
getScheme in interface javax.servlet.ServletRequest
Returns:
Always http

getServerName

public String getServerName()
Get the server name.

Specified by:
getServerName in interface javax.servlet.ServletRequest
Returns:
Always localhost

getServerPort

public int getServerPort()
Get the server port.

Specified by:
getServerPort in interface javax.servlet.ServletRequest
Returns:
Always 80

getServletPath

public String getServletPath()
The servlet path may either be the application name or /. For test purposes we always return the servlet name.

Specified by:
getServletPath in interface javax.servlet.http.HttpServletRequest
Returns:
The servlet path

getSession

public javax.servlet.http.HttpSession getSession()
Get the sessions.

Specified by:
getSession in interface javax.servlet.http.HttpServletRequest
Returns:
The session

getSession

public javax.servlet.http.HttpSession getSession(boolean b)
Get the session.

Specified by:
getSession in interface javax.servlet.http.HttpServletRequest
Parameters:
b - Ignored, there is always a session
Returns:
The session

getUserPrincipal

public Principal getUserPrincipal()
Get the user principal.

Specified by:
getUserPrincipal in interface javax.servlet.http.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 void initialize()
Reset the request back to a default state.


isRequestedSessionIdFromCookie

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

Specified by:
isRequestedSessionIdFromCookie in interface javax.servlet.http.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 javax.servlet.http.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 javax.servlet.http.HttpServletRequest
Returns:
Always false

isRequestedSessionIdValid

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

Specified by:
isRequestedSessionIdValid in interface javax.servlet.http.HttpServletRequest
Returns:
Always true

isSecure

public boolean isSecure()
Always returns false.

Specified by:
isSecure in interface javax.servlet.ServletRequest
Returns:
Always false

isUserInRole

public boolean isUserInRole(String name)
NOT IMPLEMENTED.

Specified by:
isUserInRole in interface javax.servlet.http.HttpServletRequest
Parameters:
name - The role name
Returns:
Always false

removeAttribute

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

Specified by:
removeAttribute in interface javax.servlet.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 javax.servlet.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 javax.servlet.ServletRequest
Parameters:
encoding - The character encoding
Throws:
UnsupportedEncodingException - If encoding not supported

setCookies

public void setCookies(javax.servlet.http.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

setParameters

public void setParameters(Map<String,String[]> parameters)
Sets a map of parameters.

Parameters:
parameters - the parameters to set

setPath

public void setPath(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 -

setURL

public void setURL(String url)
Set the complete url for this request. The url will be analyzed.

Parameters:
url -

setRequestToBookmarkablePage

public void setRequestToBookmarkablePage(Page page,
                                         Map<String,Object> params)
Initialize the request parameters to point to the given bookmarkable page.

Parameters:
page - The page to point to
params - Additional parameters

setRequestToComponent

public void setRequestToComponent(Component component)
Initialize the request parameters to point to the given component.

Parameters:
component - The component

setRequestToFormComponent

public void setRequestToFormComponent(Form<?> form,
                                      Map<String,Object> values)
Initialize the request parameters to point to the given form component. The additional map should contain mappings between individual components that appear in the form and the string value that should be submitted for each of these components.

Parameters:
form - The for to send the request to
values - The values for each of the form components

setRequestToRedirectString

public void setRequestToRedirectString(String redirect)
Initialize the request parameters from the given redirect string that redirects back to a particular component for display.

Parameters:
redirect - The redirect string to display from

getLocalAddr

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

getLocalName

public String getLocalName()
Specified by:
getLocalName in interface javax.servlet.ServletRequest
Returns:
local host name

getLocalPort

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

getRemotePort

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


Copyright © 2004-2011 Apache Software Foundation. All Rights Reserved.