Repository: wicket Updated Branches: refs/heads/wicket-8.x 122ddac9d -> bc90a114b
[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/bc90a114 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/bc90a114 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/bc90a114 Branch: refs/heads/wicket-8.x Commit: bc90a114bbc3e526030bd70eb3c091d33626eb56 Parents: 122ddac Author: Maxim Solodovnik <[email protected]> Authored: Thu Sep 20 23:40:32 2018 +0700 Committer: Maxim Solodovnik <[email protected]> Committed: Fri Sep 21 14:17:21 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/bc90a114/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 ce3bae0..8648203 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 @@ -53,6 +53,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; @@ -69,7 +70,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 @@ -81,7 +82,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 */ @@ -89,12 +90,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 @@ -132,7 +227,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"); @@ -147,7 +242,7 @@ public class MockServletContext implements ServletContext /** * Add an init parameter. - * + * * @param name * The parameter name * @param value @@ -162,7 +257,7 @@ public class MockServletContext implements ServletContext /** * Add a new recognized mime type. - * + * * @param fileExtension * The file extension (e.g. "jpg") * @param mimeType @@ -175,7 +270,7 @@ public class MockServletContext implements ServletContext /** * Get an attribute with the given name. - * + * * @param name * The attribute name * @return The value, or null @@ -188,7 +283,7 @@ public class MockServletContext implements ServletContext /** * Get all of the attribute names. - * + * * @return The attribute names */ @Override @@ -201,7 +296,7 @@ public class MockServletContext implements ServletContext /** * Get the context for the given URL path - * + * * @param name * The url path * @return Always returns this @@ -214,7 +309,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 @@ -227,7 +322,7 @@ public class MockServletContext implements ServletContext /** * Get the name of all of the init parameters. - * + * * @return The init parameter names */ @Override @@ -245,7 +340,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 @@ -290,7 +385,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 @@ -303,7 +398,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 @@ -334,7 +429,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 @@ -362,7 +457,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 @@ -400,7 +495,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 @@ -440,7 +535,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 @@ -514,7 +609,7 @@ public class MockServletContext implements ServletContext /** * Get the server info. - * + * * @return The server info */ @Override @@ -525,7 +620,7 @@ public class MockServletContext implements ServletContext /** * NOT USED - Servlet Spec requires that this always returns null. - * + * * @param name * Not used * @return null @@ -540,7 +635,7 @@ public class MockServletContext implements ServletContext /** * Return the name of the servlet context. - * + * * @return The name */ @Override @@ -567,9 +662,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; } @@ -650,7 +745,7 @@ public class MockServletContext implements ServletContext @Override public SessionCookieConfig getSessionCookieConfig() { - return null; + return sessionCookieConfig; } @Override @@ -716,7 +811,7 @@ public class MockServletContext implements ServletContext /** * NOT USED - Servlet spec requires that this always returns null. - * + * * @return null */ @Override @@ -727,7 +822,7 @@ public class MockServletContext implements ServletContext /** * NOT USED - Servlet spec requires that this always returns null. - * + * * @return null */ @Override @@ -738,7 +833,7 @@ public class MockServletContext implements ServletContext /** * As part of testing we always log to the console. - * + * * @param e * The exception to log * @param msg @@ -752,7 +847,7 @@ public class MockServletContext implements ServletContext /** * As part of testing we always log to the console. - * + * * @param msg * The message to log */ @@ -764,7 +859,7 @@ public class MockServletContext implements ServletContext /** * As part of testing we always log to the console. - * + * * @param msg * The message to log * @param cause @@ -778,7 +873,7 @@ public class MockServletContext implements ServletContext /** * Remove an attribute with the given name. - * + * * @param name * The name */ @@ -790,7 +885,7 @@ public class MockServletContext implements ServletContext /** * Set an attribute. - * + * * @param name * The name of the attribute * @param o @@ -810,26 +905,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 { @@ -837,7 +932,7 @@ public class MockServletContext implements ServletContext { return servletName; } - + return null; } } http://git-wip-us.apache.org/repos/asf/wicket/blob/bc90a114/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.
