org.apache.wicket.mock
Class MockWebRequest

java.lang.Object
  extended by org.apache.wicket.request.Request
      extended by org.apache.wicket.request.http.WebRequest
          extended by org.apache.wicket.mock.MockWebRequest

public class MockWebRequest
extends WebRequest

Mutable mock WebRequest.

Author:
Matej Knopp

Field Summary
 
Fields inherited from class org.apache.wicket.request.http.WebRequest
HEADER_AJAX, HEADER_AJAX_BASE_URL, PARAM_AJAX, PARAM_AJAX_BASE_URL
 
Constructor Summary
MockWebRequest(Url url)
          Construct.
MockWebRequest(Url url, java.lang.String contextPath, java.lang.String filterPath, java.lang.String prefixToContextPath)
          Construct.
 
Method Summary
 void addCookie(javax.servlet.http.Cookie cookie)
           
 void addDateHeader(java.lang.String name, Time value)
          Adds date header for given name.
 void addHeader(java.lang.String name, java.lang.String value)
          Adds header for given name.
 MockWebRequest cloneWithUrl(Url url)
          Returns request with specified URL and same POST parameters as this request.
 java.nio.charset.Charset getCharset()
          Gets charset of the request
 Url getClientUrl()
          Returns the url against which the client, usually the browser, will resolve relative urls in the rendered markup.
 java.lang.Object getContainerRequest()
          Provides access to the low-level container request object that implementaion of this Request delegate to.
 java.lang.String getContextPath()
          Returns the context path or an empty string if the application is running under root context.
 java.util.List<javax.servlet.http.Cookie> getCookies()
           
 Time getDateHeader(java.lang.String name)
          Returns the value of the specified request header as a long value that represents a Date object.
 java.lang.String getFilterPath()
          Returns the path to which wicket Filter is mapped or an empty string if the filter is mapped to /*.
 java.lang.String getHeader(java.lang.String name)
          Returns the value of the specified request header as a String
 java.util.List<java.lang.String> getHeaders(java.lang.String name)
          Returns all the values of the specified request header.
 java.util.Locale getLocale()
          Returns locale for this request.
 MockRequestParameters getPostParameters()
           
 java.lang.String getPrefixToContextPath()
          Returns prefix from Wicket Filter mapping to context path.
 Url getUrl()
          Returns the URL for this request.
 void removeHeader(java.lang.String header)
          Removes header with specified name.
 MockWebRequest setContextPath(java.lang.String contextPath)
           
 void setCookies(java.util.List<javax.servlet.http.Cookie> cookies)
          Sets cookies for current request.
 void setDateHeader(java.lang.String name, Time value)
          Sets date header for given name.
 MockWebRequest setFilterPath(java.lang.String filterPath)
           
 void setHeader(java.lang.String name, java.lang.String value)
          Sets header for given name.
 void setLocale(java.util.Locale locale)
          Sets request locale.
 MockWebRequest setPrefixToContextPath(java.lang.String prefixToContextPath)
           
 void setUrl(Url url)
           
 java.lang.String toString()
           
 
Methods inherited from class org.apache.wicket.request.http.WebRequest
getCookie, getIfModifiedSinceHeader, isAjax, shouldPreserveClientUrl
 
Methods inherited from class org.apache.wicket.request.Request
getOriginalUrl, getQueryParameters, getRequestParameters
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MockWebRequest

public MockWebRequest(Url url)
Construct.

Parameters:
url -

MockWebRequest

public MockWebRequest(Url url,
                      java.lang.String contextPath,
                      java.lang.String filterPath,
                      java.lang.String prefixToContextPath)
Construct.

Parameters:
url -
contextPath -
filterPath -
prefixToContextPath -
Method Detail

cloneWithUrl

public MockWebRequest cloneWithUrl(Url url)
Description copied from class: WebRequest
Returns request with specified URL and same POST parameters as this request.

Overrides:
cloneWithUrl in class WebRequest
Parameters:
url - Url instance
Returns:
request with specified URL.

setUrl

public void setUrl(Url url)
Parameters:
url -

getUrl

public Url getUrl()
Description copied from class: Request
Returns the URL for this request. URL is relative to Wicket filter path.

Specified by:
getUrl in class Request
Returns:
Url instance

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

setCookies

public void setCookies(java.util.List<javax.servlet.http.Cookie> cookies)
Sets cookies for current request.

Parameters:
cookies -

addCookie

public void addCookie(javax.servlet.http.Cookie cookie)
Parameters:
cookie -

getCookies

public java.util.List<javax.servlet.http.Cookie> getCookies()
Specified by:
getCookies in class WebRequest
Returns:
request cookies

getDateHeader

public Time getDateHeader(java.lang.String name)
Description copied from class: WebRequest
Returns the value of the specified request header as a long value that represents a Date object. Use this method with headers that contain dates, such as If-Modified-Since.

Specified by:
getDateHeader in class WebRequest
Returns:
date value of request header or null if not found

setDateHeader

public void setDateHeader(java.lang.String name,
                          Time value)
Sets date header for given name.

Parameters:
name -
value -

addDateHeader

public void addDateHeader(java.lang.String name,
                          Time value)
Adds date header for given name.

Parameters:
name -
value -

getHeader

public java.lang.String getHeader(java.lang.String name)
Description copied from class: WebRequest
Returns the value of the specified request header as a String

Specified by:
getHeader in class WebRequest
Returns:
string value of request header

setHeader

public void setHeader(java.lang.String name,
                      java.lang.String value)
Sets header for given name.

Parameters:
name -
value -

addHeader

public void addHeader(java.lang.String name,
                      java.lang.String value)
Adds header for given name.

Parameters:
name -
value -

setLocale

public void setLocale(java.util.Locale locale)
Sets request locale.

Parameters:
locale -

getLocale

public java.util.Locale getLocale()
Description copied from class: Request
Returns locale for this request.

Specified by:
getLocale in class Request
Returns:
locale

getHeaders

public java.util.List<java.lang.String> getHeaders(java.lang.String name)
Description copied from class: WebRequest
Returns all the values of the specified request header.

Specified by:
getHeaders in class WebRequest
Returns:
unmodifiable list of header values

removeHeader

public void removeHeader(java.lang.String header)
Removes header with specified name.

Parameters:
header -

getPostParameters

public MockRequestParameters getPostParameters()
Overrides:
getPostParameters in class Request
Returns:
POST request parameters for this request.

getCharset

public java.nio.charset.Charset getCharset()
Description copied from class: Request
Gets charset of the request

Specified by:
getCharset in class Request
Returns:
request charset

getClientUrl

public Url getClientUrl()
Description copied from class: Request
Returns the url against which the client, usually the browser, will resolve relative urls in the rendered markup. If the client is a browser this is the url in the browser's address bar.

Under normal circumstances the client and request (Request.getUrl()) urls are the same. Handling an Ajax request, however, is a good example of when they may be different. Technologies such as XHR are free to request whatever url they wish without modifying the client url; however, any produced urls will be evaluated by the client against the client url - not against the request url.

Lets take a simple example:
Suppose we are on a client detail page. This page contains an Ajax link which opens a list of client's orders. Each order has a link that goes to the order detail page. The client detail page is located at

 /detail/customer/15
 
and the order detail page is located at
 /order/22
 
The Ajax link which renders the detail section is located at
  /detail/wicket?page 3
 
Lets run through the execution and see what happens when the XHR request is processed:
 request 1: /details/customer/15
 client url: details/customer/15 (the url in the browser's address bar)
 request url: details/customer/15
 Wicket renders relative Ajax details anchor as ../../wicket/page?3  
 
 ../../wicket/page?3 resolved against current client url details/customer/15 yields:
 request 2: /wicket/page?3
 client url: customer/15 (unchanged since XHRs requests dont change it)
 request url: wicket/ajax/page?3
 
 now Wicket has to render a relative url to /details/order/22. If Wicket renders 
 it against the request url it will be: ../order/22, and later evaluated on the
 client will result in /customer/order/22 which is incorrect.
 
This is why implementations of Request must track the client url, so that relative urls can be rendered against the same url they will be evaluated against on the client - which is not always the same as the request url. For example, Wicket's Ajax implementation always sends the current client url in a header along with the XHR request so that Wicket can correctly render relative urls against it.

Specified by:
getClientUrl in class Request
Returns:
client url

getContainerRequest

public java.lang.Object getContainerRequest()
Description copied from class: Request
Provides access to the low-level container request object that implementaion of this Request delegate to. This allows users to access features provided by the container requests but not by generalized Wicket Request objects.

Specified by:
getContainerRequest in class Request
Returns:
low-level container request object, or null if none

getContextPath

public java.lang.String getContextPath()
Description copied from class: Request
Returns the context path or an empty string if the application is running under root context. Returned path, unless an empty string, will always start with a slash and will never end with a slash.

Overrides:
getContextPath in class Request
Returns:
context path

setContextPath

public MockWebRequest setContextPath(java.lang.String contextPath)
Parameters:
contextPath -
Returns:
this

getFilterPath

public java.lang.String getFilterPath()
Description copied from class: Request
Returns the path to which wicket Filter is mapped or an empty string if the filter is mapped to /*. Returned path, unless an empty string, will always start with a slash and will never end with a slash.

Overrides:
getFilterPath in class Request
Returns:
filter path

setFilterPath

public MockWebRequest setFilterPath(java.lang.String filterPath)
Parameters:
filterPath -
Returns:
this

getPrefixToContextPath

public java.lang.String getPrefixToContextPath()
Description copied from class: Request
Returns prefix from Wicket Filter mapping to context path. This method does not take the actual URL into account.

For example if Wicket filter is mapped to hello/* this method should return ../ regardless of actual URL (after Wicket filter)

Overrides:
getPrefixToContextPath in class Request
Returns:
prefix to context path for this request.

setPrefixToContextPath

public MockWebRequest setPrefixToContextPath(java.lang.String prefixToContextPath)
Parameters:
prefixToContextPath -
Returns:
this


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