You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by so...@apache.org on 2018/09/20 16:40:40 UTC
wicket git commit: [WICKET-6588] MockServletContext is updated to
return non-null sessionCookieConfig
Repository: wicket
Updated Branches:
refs/heads/master 5fc86bdd8 -> 056d726a5
[WICKET-6588] MockServletContext is updated to return non-null sessionCookieConfig
Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/056d726a
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/056d726a
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/056d726a
Branch: refs/heads/master
Commit: 056d726a5d8a1506c3ae180774802ad1e243f08d
Parents: 5fc86bd
Author: Maxim Solodovnik <so...@gmail.com>
Authored: Thu Sep 20 23:40:32 2018 +0700
Committer: Maxim Solodovnik <so...@gmail.com>
Committed: Thu Sep 20 23:40:32 2018 +0700
----------------------------------------------------------------------
.../protocol/http/mock/MockServletContext.java | 173 ++++++++++++++-----
.../apache/wicket/util/cookies/CookieUtils.java | 64 +++----
2 files changed, 166 insertions(+), 71 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/wicket/blob/056d726a/wicket-core/src/main/java/org/apache/wicket/protocol/http/mock/MockServletContext.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/protocol/http/mock/MockServletContext.java b/wicket-core/src/main/java/org/apache/wicket/protocol/http/mock/MockServletContext.java
index f7b6579..8904e96 100755
--- a/wicket-core/src/main/java/org/apache/wicket/protocol/http/mock/MockServletContext.java
+++ b/wicket-core/src/main/java/org/apache/wicket/protocol/http/mock/MockServletContext.java
@@ -54,6 +54,7 @@ import javax.servlet.descriptor.JspConfigDescriptor;
import org.apache.wicket.Application;
import org.apache.wicket.WicketRuntimeException;
+import org.apache.wicket.util.cookies.CookieUtils;
import org.apache.wicket.util.string.Strings;
import org.apache.wicket.util.value.ValueMap;
import org.slf4j.Logger;
@@ -70,7 +71,7 @@ import org.slf4j.LoggerFactory;
* value allows all of the resource location functionality to work as in a fully functioning web
* application. This value is not set then not resource location functionality will work and instead
* null will always be returned.
- *
+ *
* @author Chris Turner
*/
public class MockServletContext implements ServletContext
@@ -82,7 +83,7 @@ public class MockServletContext implements ServletContext
private final ValueMap attributes = new ValueMap();
private final ValueMap initParameters = new ValueMap();
-
+
private final Map<String, ServletRegistration.Dynamic> servletRegistration = new HashMap<>();
/** Map of mime types */
@@ -90,12 +91,106 @@ public class MockServletContext implements ServletContext
private File webappRoot;
+ private final SessionCookieConfig sessionCookieConfig = new SessionCookieConfig()
+ {
+ private boolean secure;
+ private String path;
+ private String name = CookieUtils.DEFAULT_SESSIONID_COOKIE_NAME;
+ private int maxAge;
+ private boolean httpOnly;
+ private String domain;
+ private String comment;
+
+ @Override
+ public void setSecure(boolean secure)
+ {
+ this.secure = secure;
+ }
+
+ @Override
+ public void setPath(String path)
+ {
+ this.path = path;
+ }
+
+ @Override
+ public void setName(String name)
+ {
+ this.name = name;
+ }
+
+ @Override
+ public void setMaxAge(int maxAge)
+ {
+ this.maxAge = maxAge;
+ }
+
+ @Override
+ public void setHttpOnly(boolean httpOnly)
+ {
+ this.httpOnly = httpOnly;
+ }
+
+ @Override
+ public void setDomain(String domain)
+ {
+ this.domain = domain;
+ }
+
+ @Override
+ public void setComment(String comment)
+ {
+ this.comment = comment;
+ }
+
+ @Override
+ public boolean isSecure()
+ {
+ return secure;
+ }
+
+ @Override
+ public boolean isHttpOnly()
+ {
+ return httpOnly;
+ }
+
+ @Override
+ public String getPath()
+ {
+ return path;
+ }
+
+ @Override
+ public String getName()
+ {
+ return name;
+ }
+
+ @Override
+ public int getMaxAge()
+ {
+ return maxAge;
+ }
+
+ @Override
+ public String getDomain()
+ {
+ return domain;
+ }
+
+ @Override
+ public String getComment()
+ {
+ return comment;
+ }
+ };
/**
* Create the mock object. As part of the creation, the context sets the root directory where
* web application content is stored. This must be an ABSOLUTE directory relative to where the
* tests are being executed. For example: <code>System.getProperty("user.dir") +
* "/src/webapp"</code>
- *
+ *
* @param application
* The application that this context is for
* @param path
@@ -133,7 +228,7 @@ public class MockServletContext implements ServletContext
String tmpDir = System.getProperty("java.io.tmpdir");
file = new File(tmpDir);
}
-
+
attributes.put("javax.servlet.context.tempdir", file);
mimeTypes.put("html", "text/html");
@@ -148,7 +243,7 @@ public class MockServletContext implements ServletContext
/**
* Add an init parameter.
- *
+ *
* @param name
* The parameter name
* @param value
@@ -163,7 +258,7 @@ public class MockServletContext implements ServletContext
/**
* Add a new recognized mime type.
- *
+ *
* @param fileExtension
* The file extension (e.g. "jpg")
* @param mimeType
@@ -176,7 +271,7 @@ public class MockServletContext implements ServletContext
/**
* Get an attribute with the given name.
- *
+ *
* @param name
* The attribute name
* @return The value, or null
@@ -189,7 +284,7 @@ public class MockServletContext implements ServletContext
/**
* Get all of the attribute names.
- *
+ *
* @return The attribute names
*/
@Override
@@ -202,7 +297,7 @@ public class MockServletContext implements ServletContext
/**
* Get the context for the given URL path
- *
+ *
* @param name
* The url path
* @return Always returns this
@@ -215,7 +310,7 @@ public class MockServletContext implements ServletContext
/**
* Get the init parameter with the given name.
- *
+ *
* @param name
* The name
* @return The parameter, or null if no such parameter
@@ -228,7 +323,7 @@ public class MockServletContext implements ServletContext
/**
* Get the name of all of the init parameters.
- *
+ *
* @return The init parameter names
*/
@Override
@@ -246,7 +341,7 @@ public class MockServletContext implements ServletContext
/**
* Get the mime type for the given file. Uses a hardcoded map of mime types set at
* Initialization time.
- *
+ *
* @param name
* The name to get the mime type for
* @return The mime type
@@ -291,7 +386,7 @@ public class MockServletContext implements ServletContext
/**
* Wicket does not use the RequestDispatcher, so this implementation just returns a dummy value.
- *
+ *
* @param name
* The name of the servlet or JSP
* @return The dispatcher
@@ -304,7 +399,7 @@ public class MockServletContext implements ServletContext
/**
* Get the real file path of the given resource name.
- *
+ *
* @param name
* The name
* @return The real path or null
@@ -335,7 +430,7 @@ public class MockServletContext implements ServletContext
/**
* Wicket does not use the RequestDispatcher, so this implementation just returns a dummy value.
- *
+ *
* @param name
* The name of the resource to get the dispatcher for
* @return The dispatcher
@@ -363,7 +458,7 @@ public class MockServletContext implements ServletContext
/**
* Get the URL for a particular resource that is relative to the web app root directory.
- *
+ *
* @param name
* The name of the resource to get
* @return The resource, or null if resource not found
@@ -401,7 +496,7 @@ public class MockServletContext implements ServletContext
/**
* Get an input stream for a particular resource that is relative to the web app root directory.
- *
+ *
* @param name
* The name of the resource to get
* @return The input stream for the resource, or null of resource is not found
@@ -441,7 +536,7 @@ public class MockServletContext implements ServletContext
/**
* Get the resource paths starting from the web app root directory and then relative to the the
* given name.
- *
+ *
* @param name
* The starting name
* @return The set of resource paths at this location
@@ -515,7 +610,7 @@ public class MockServletContext implements ServletContext
/**
* Get the server info.
- *
+ *
* @return The server info
*/
@Override
@@ -526,7 +621,7 @@ public class MockServletContext implements ServletContext
/**
* NOT USED - Servlet Spec requires that this always returns null.
- *
+ *
* @param name
* Not used
* @return null
@@ -541,7 +636,7 @@ public class MockServletContext implements ServletContext
/**
* Return the name of the servlet context.
- *
+ *
* @return The name
*/
@Override
@@ -568,9 +663,9 @@ public class MockServletContext implements ServletContext
{
Dynamic mockRegistration = (Dynamic)Proxy.newProxyInstance(Dynamic.class.getClassLoader(),
new Class<?>[]{Dynamic.class}, new MockedServletRegistationHandler(servletName));
-
+
servletRegistration.put(servletName, mockRegistration);
-
+
return mockRegistration;
}
@@ -651,7 +746,7 @@ public class MockServletContext implements ServletContext
@Override
public SessionCookieConfig getSessionCookieConfig()
{
- return null;
+ return sessionCookieConfig;
}
@Override
@@ -717,7 +812,7 @@ public class MockServletContext implements ServletContext
/**
* NOT USED - Servlet spec requires that this always returns null.
- *
+ *
* @return null
*/
@Override
@@ -728,7 +823,7 @@ public class MockServletContext implements ServletContext
/**
* NOT USED - Servlet spec requires that this always returns null.
- *
+ *
* @return null
*/
@Override
@@ -739,7 +834,7 @@ public class MockServletContext implements ServletContext
/**
* As part of testing we always log to the console.
- *
+ *
* @param e
* The exception to log
* @param msg
@@ -753,7 +848,7 @@ public class MockServletContext implements ServletContext
/**
* As part of testing we always log to the console.
- *
+ *
* @param msg
* The message to log
*/
@@ -765,7 +860,7 @@ public class MockServletContext implements ServletContext
/**
* As part of testing we always log to the console.
- *
+ *
* @param msg
* The message to log
* @param cause
@@ -779,7 +874,7 @@ public class MockServletContext implements ServletContext
/**
* Remove an attribute with the given name.
- *
+ *
* @param name
* The name
*/
@@ -791,7 +886,7 @@ public class MockServletContext implements ServletContext
/**
* Set an attribute.
- *
+ *
* @param name
* The name of the attribute
* @param o
@@ -811,26 +906,26 @@ public class MockServletContext implements ServletContext
{
return "";
}
-
-
+
+
/**
* Invocation handler for proxy interface of {@link javax.servlet.ServletRegistration.Dynamic}.
- * This class intercepts invocation for method {@link javax.servlet.ServletRegistration.Dynamic#getMappings}
+ * This class intercepts invocation for method {@link javax.servlet.ServletRegistration.Dynamic#getMappings}
* and returns the servlet name.
- *
+ *
* @author andrea del bene
*
*/
class MockedServletRegistationHandler implements InvocationHandler
{
-
+
private final Collection<String> servletName;
-
+
public MockedServletRegistationHandler(String servletName)
{
this.servletName = Arrays.asList(servletName);
}
-
+
@Override
public Object invoke(Object object, Method method, Object[] args) throws Throwable
{
@@ -838,7 +933,7 @@ public class MockServletContext implements ServletContext
{
return servletName;
}
-
+
return null;
}
}
http://git-wip-us.apache.org/repos/asf/wicket/blob/056d726a/wicket-core/src/main/java/org/apache/wicket/util/cookies/CookieUtils.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/util/cookies/CookieUtils.java b/wicket-core/src/main/java/org/apache/wicket/util/cookies/CookieUtils.java
index fc7a639..fb6cfc0 100644
--- a/wicket-core/src/main/java/org/apache/wicket/util/cookies/CookieUtils.java
+++ b/wicket-core/src/main/java/org/apache/wicket/util/cookies/CookieUtils.java
@@ -32,7 +32,7 @@ import org.slf4j.LoggerFactory;
/**
* Helper class to simplify Cookie handling.
- *
+ *
* @author Juergen Donnerstag
* @author Jonathan Locke
*/
@@ -40,7 +40,7 @@ public class CookieUtils
{
private final static Logger log = LoggerFactory.getLogger(CookieUtils.class);
- private static final String DEFAULT_SESSIONID_COOKIE_NAME = "JSESSIONID";
+ public static final String DEFAULT_SESSIONID_COOKIE_NAME = "JSESSIONID";
private final CookieDefaults settings;
@@ -54,7 +54,7 @@ public class CookieUtils
/**
* Construct.
- *
+ *
* @param settings
* the default settings for the saved cookies
*/
@@ -73,7 +73,7 @@ public class CookieUtils
/**
* Remove the cookie identified by the key
- *
+ *
* @param key
* The cookie name
*/
@@ -88,7 +88,7 @@ public class CookieUtils
/**
* Remove the cookie identified by the form component
- *
+ *
* @param formComponent
*/
public final void remove(final FormComponent<?> formComponent)
@@ -99,7 +99,7 @@ public class CookieUtils
/**
* This method gets used when a cookie key needs to be derived from a form component. By default
* the component's page relative path is used.
- *
+ *
* @param component
* @return cookie key
*/
@@ -128,7 +128,7 @@ public class CookieUtils
/**
* Retrieve the cookie value associated with the formComponent and load the model object with
* the cookie value.
- *
+ *
* @param formComponent
* @return The Cookie value which has also been used to set the component's model value
*/
@@ -145,7 +145,7 @@ public class CookieUtils
/**
* Create a Cookie with key and value and save it in the browser with the next response
- *
+ *
* @param key
* The cookie name
* @param value
@@ -168,7 +168,7 @@ public class CookieUtils
/**
* Save the form components model value in a cookie
- *
+ *
* @param formComponent
*/
public final void save(final FormComponent<?> formComponent)
@@ -178,7 +178,7 @@ public class CookieUtils
/**
* Make sure the 'key' does not contain any illegal chars. E.g. for cookies ':' is not allowed.
- *
+ *
* @param key
* The key to be validated
* @return The save key
@@ -200,7 +200,7 @@ public class CookieUtils
/**
* Convenience method for deleting a cookie by name. Delete the cookie by setting its maximum
* age to zero.
- *
+ *
* @param cookie
* The cookie to delete
*/
@@ -223,10 +223,10 @@ public class CookieUtils
/**
* Gets the cookie with 'name' attached to the latest WebRequest.
- *
+ *
* @param name
* The name of the cookie to be looked up
- *
+ *
* @return Any cookies for this request
*/
public Cookie getCookie(final String name)
@@ -260,26 +260,26 @@ public class CookieUtils
return null;
}
-
-
+
+
/**
- * Gets the name of the cookie where the session id is stored.
- *
- * @param application
- * The current we application holding the {@link javax.servlet.ServletContext}.
- *
- * @return The name set in {@link javax.servlet.SessionCookieConfig} or the default value 'JSESSIONID' if not set
- */
- public String getSessionIdCookieName(WebApplication application)
+ * Gets the name of the cookie where the session id is stored.
+ *
+ * @param application
+ * The current we application holding the {@link javax.servlet.ServletContext}.
+ *
+ * @return The name set in {@link javax.servlet.SessionCookieConfig} or the default value 'JSESSIONID' if not set
+ */
+ public String getSessionIdCookieName(WebApplication application)
{
- String jsessionCookieName = application.getServletContext().getSessionCookieConfig().getName();
-
- return jsessionCookieName == null ? DEFAULT_SESSIONID_COOKIE_NAME : jsessionCookieName;
- }
+ String jsessionCookieName = application.getServletContext().getSessionCookieConfig().getName();
+
+ return jsessionCookieName == null ? DEFAULT_SESSIONID_COOKIE_NAME : jsessionCookieName;
+ }
/**
* Persist/save the data using Cookies.
- *
+ *
* @param cookie
* The Cookie to be persisted.
* @return The cookie provided
@@ -307,7 +307,7 @@ public class CookieUtils
/**
* Is called before the Cookie is saved. May be subclassed for different (dynamic) Cookie
* parameters. Static parameters can also be changed via {@link CookieDefaults}.
- *
+ *
* @param cookie
*/
protected void initializeCookie(final Cookie cookie)
@@ -337,7 +337,7 @@ public class CookieUtils
/**
* Convenience method to get the http request.
- *
+ *
* @return WebRequest related to the RequestCycle
*/
private WebRequest getWebRequest()
@@ -347,7 +347,7 @@ public class CookieUtils
/**
* Convenience method to get the http response.
- *
+ *
* @return WebResponse related to the RequestCycle
*/
private WebResponse getWebResponse()
@@ -363,7 +363,7 @@ public class CookieUtils
/**
* Gets debug info as a string for the given cookie.
- *
+ *
* @param cookie
* the cookie to debug.
* @return a string that represents the internals of the cookie.