[jboss-cvs] JBossAS SVN: r81244 - in trunk: testsuite/src/main/org/jboss/test/cluster/web/mocks and 1 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Tue Nov 18 12:26:00 EST 2008
Author: bstansberry at jboss.com
Date: 2008-11-18 12:26:00 -0500 (Tue, 18 Nov 2008)
New Revision: 81244
Added:
trunk/testsuite/src/main/org/jboss/test/cluster/defaultcfg/simpleweb/test/SetNewSessionCookieUnitTestCase.java
trunk/testsuite/src/main/org/jboss/test/cluster/web/mocks/MockConnector.java
trunk/testsuite/src/main/org/jboss/test/cluster/web/mocks/MockJBossManager.java
trunk/testsuite/src/main/org/jboss/test/cluster/web/mocks/MockResponse.java
Modified:
trunk/tomcat/src/main/org/jboss/web/tomcat/service/session/JBossManager.java
Log:
[JBAS-6206] Set session cookie after failover a la o.a.c.connector.Request
Added: trunk/testsuite/src/main/org/jboss/test/cluster/defaultcfg/simpleweb/test/SetNewSessionCookieUnitTestCase.java
===================================================================
--- trunk/testsuite/src/main/org/jboss/test/cluster/defaultcfg/simpleweb/test/SetNewSessionCookieUnitTestCase.java (rev 0)
+++ trunk/testsuite/src/main/org/jboss/test/cluster/defaultcfg/simpleweb/test/SetNewSessionCookieUnitTestCase.java 2008-11-18 17:26:00 UTC (rev 81244)
@@ -0,0 +1,219 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.test.cluster.defaultcfg.simpleweb.test;
+
+import javax.servlet.http.Cookie;
+
+import junit.framework.TestCase;
+
+import org.apache.catalina.Context;
+import org.apache.catalina.Globals;
+import org.apache.catalina.connector.Response;
+import org.apache.catalina.core.StandardContext;
+import org.apache.catalina.deploy.SessionCookie;
+import org.apache.tomcat.util.http.TomcatCookie;
+import org.jboss.test.cluster.web.mocks.MockConnector;
+import org.jboss.test.cluster.web.mocks.MockEngine;
+import org.jboss.test.cluster.web.mocks.MockHost;
+import org.jboss.test.cluster.web.mocks.MockJBossManager;
+import org.jboss.test.cluster.web.mocks.MockResponse;
+import org.jboss.web.tomcat.service.session.AbstractJBossManager;
+
+/**
+ * Test behavior of JBossManager.setNewSessionCookie()
+ *
+ * @author Brian Stansberry
+ */
+public class SetNewSessionCookieUnitTestCase extends TestCase
+{
+ private static final String ROOT_PATH = "/";
+ private static final String CONTEXT_PATH = "/test";
+ private static final String DOMAIN = "jboss.org";
+ private static final String SESSION_ID = "abc123";
+ private static final String COMMENT = "A comment";
+
+ /**
+ * Create a new SetNewSessionCookieUnitTestCase.
+ *
+ * @param name
+ */
+ public SetNewSessionCookieUnitTestCase(String name)
+ {
+ super(name);
+ }
+
+ public void testBasic() throws Exception
+ {
+ AbstractJBossManager mgr = getManager(getContext(CONTEXT_PATH));
+
+ Cookie cookie = validateResponse(executeRequest(mgr, false));
+ validateCookie(cookie, CONTEXT_PATH, false);
+ }
+
+ public void testRootContext() throws Exception
+ {
+ AbstractJBossManager mgr = getManager(getContext(ROOT_PATH));
+
+ Cookie cookie = validateResponse(executeRequest(mgr, false));
+ validateCookie(cookie, ROOT_PATH, false);
+ }
+
+ public void testEmptyContextPath() throws Exception
+ {
+ AbstractJBossManager mgr = getManager(getContext(""));
+
+ Cookie cookie = validateResponse(executeRequest(mgr, false));
+ validateCookie(cookie, ROOT_PATH, false);
+ }
+
+ public void testSecureConnector() throws Exception
+ {
+ AbstractJBossManager mgr = getManager(getContext(CONTEXT_PATH));
+
+ Cookie cookie = validateResponse(executeRequest(mgr, true));
+ validateCookie(cookie, CONTEXT_PATH, true);
+ }
+
+ public void testDomain() throws Exception
+ {
+ AbstractJBossManager mgr = getManager(getContext(CONTEXT_PATH, null, null, DOMAIN, false, false));
+
+ Cookie cookie = validateResponse(executeRequest(mgr, false));
+ validateCookie(cookie, CONTEXT_PATH, null, DOMAIN, false, false);
+ }
+
+ public void testCookiePath() throws Exception
+ {
+ AbstractJBossManager mgr = getManager(getContext(CONTEXT_PATH, ROOT_PATH, null, null, false, false));
+
+ Cookie cookie = validateResponse(executeRequest(mgr, false));
+ validateCookie(cookie, ROOT_PATH, false);
+ }
+
+ public void testComment() throws Exception
+ {
+ AbstractJBossManager mgr = getManager(getContext(CONTEXT_PATH, null, COMMENT, null, false, false));
+
+ Cookie cookie = validateResponse(executeRequest(mgr, false));
+ validateCookie(cookie, CONTEXT_PATH, COMMENT, null, false, false);
+ }
+
+ public void testSecureContext() throws Exception
+ {
+ AbstractJBossManager mgr = getManager(getContext(CONTEXT_PATH, null, null, null, false, true));
+
+ Cookie cookie = validateResponse(executeRequest(mgr, false));
+ validateCookie(cookie, CONTEXT_PATH, true);
+ }
+
+ public void testHttpOnly() throws Exception
+ {
+ AbstractJBossManager mgr = getManager(getContext(CONTEXT_PATH, null, null, null, true, false));
+
+ Cookie cookie = validateResponse(executeRequest(mgr, false));
+ validateCookie(cookie, CONTEXT_PATH, null, null, true, false);
+ }
+
+ private Context getContext(String contextName)
+ {
+ return getContext(contextName, null, null, null, false, false);
+ }
+
+ private Context getContext(String contextPath, String cookiePath,
+ String cookieComment, String cookieDomain,
+ boolean httpOnly, boolean secure)
+ {
+ MockEngine engine = new MockEngine();
+ MockHost host = new MockHost();
+ engine.addChild(host);
+ host.setName("localhost");
+ StandardContext container = new StandardContext();
+ container.setPath(contextPath);
+ host.addChild(container);
+
+ SessionCookie cookie = container.getSessionCookie();
+ if (cookiePath != null)
+ cookie.setPath(cookiePath);
+ if (cookieComment != null)
+ cookie.setComment(cookieComment);
+ if (cookieDomain != null)
+ cookie.setDomain(cookieDomain);
+ cookie.setHttpOnly(httpOnly);
+ cookie.setSecure(secure);
+
+ return container;
+ }
+
+ private AbstractJBossManager getManager(Context context)
+ {
+ MockJBossManager mgr = new MockJBossManager();
+ context.setManager(mgr);
+ return mgr;
+ }
+
+ private MockResponse executeRequest(AbstractJBossManager mgr, boolean secure) throws Exception
+ {
+ MockConnector connector = new MockConnector();
+ connector.setSecure(secure);
+ MockResponse response = new MockResponse(connector);
+ mgr.setNewSessionCookie(SESSION_ID, response);
+ return response;
+ }
+
+ private Cookie validateResponse(Response response)
+ {
+ Cookie[] cookies = response.getCookies();
+ assertNotNull(cookies);
+ assertEquals(1, cookies.length);
+ return cookies[0];
+ }
+
+ private void validateCookie(Cookie cookie, String cookiePath, boolean secure)
+ {
+ validateCookie(cookie, cookiePath, null, null, false, secure);
+ }
+
+ private void validateCookie(Cookie cookie, String cookiePath,
+ String cookieComment, String cookieDomain,
+ boolean httpOnly, boolean secure)
+ {
+ basicValidation(cookie);
+ assertEquals("path", cookiePath, cookie.getPath());
+ assertEquals("comment", cookieComment, cookie.getComment());
+ assertEquals("domain", cookieDomain, cookie.getDomain());
+ assertEquals("secure", secure, cookie.getSecure());
+ if (cookie instanceof TomcatCookie)
+ {
+ assertEquals("httpOnly", httpOnly, ((TomcatCookie) cookie).getHttpOnly());
+ }
+ }
+
+ private void basicValidation(Cookie cookie)
+ {
+ assertNotNull("cookie is null", cookie);
+ assertEquals("name", Globals.SESSION_COOKIE_NAME, cookie.getName());
+ assertEquals("value", SESSION_ID, cookie.getValue());
+ assertEquals("version", 0, cookie.getVersion());
+ assertEquals("maxAge", -1, cookie.getMaxAge());
+ }
+}
Added: trunk/testsuite/src/main/org/jboss/test/cluster/web/mocks/MockConnector.java
===================================================================
--- trunk/testsuite/src/main/org/jboss/test/cluster/web/mocks/MockConnector.java (rev 0)
+++ trunk/testsuite/src/main/org/jboss/test/cluster/web/mocks/MockConnector.java 2008-11-18 17:26:00 UTC (rev 81244)
@@ -0,0 +1,45 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.test.cluster.web.mocks;
+
+import org.apache.catalina.connector.Connector;
+
+/**
+ * @author Brian Stansberry
+ *
+ */
+public class MockConnector extends Connector
+{
+
+ /**
+ * Create a new MockConnector.
+ *
+ * @throws Exception
+ */
+ public MockConnector() throws Exception
+ {
+ super();
+ }
+
+
+}
Added: trunk/testsuite/src/main/org/jboss/test/cluster/web/mocks/MockJBossManager.java
===================================================================
--- trunk/testsuite/src/main/org/jboss/test/cluster/web/mocks/MockJBossManager.java (rev 0)
+++ trunk/testsuite/src/main/org/jboss/test/cluster/web/mocks/MockJBossManager.java 2008-11-18 17:26:00 UTC (rev 81244)
@@ -0,0 +1,109 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.test.cluster.web.mocks;
+
+import java.io.IOException;
+
+import org.apache.catalina.Session;
+import org.jboss.web.tomcat.service.session.JBossManager;
+
+/**
+ * Stubs out all the abstract JBossManager methods.
+ *
+ * @author Brian Stansberry
+ */
+public class MockJBossManager extends JBossManager
+{
+
+ /**
+ * Create a new MockJBossManager.
+ *
+ */
+ public MockJBossManager()
+ {
+ super();
+ }
+
+ @Override
+ protected int getTotalActiveSessions()
+ {
+ return 0;
+ }
+
+ @Override
+ protected void processExpirationPassivation()
+ {
+ // no-op
+ }
+
+ public void removeLocal(Session session)
+ {
+ // no-op
+ }
+
+ public boolean storeSession(Session session)
+ {
+ return false;
+ }
+
+ public void add(Session session)
+ {
+ // no-op
+ }
+
+ public Session createEmptySession()
+ {
+ return null;
+ }
+
+ public Session createSession()
+ {
+ return null;
+ }
+
+ public Session createSession(String sessionId)
+ {
+ return null;
+ }
+
+ public Session findSession(String id) throws IOException
+ {
+ return null;
+ }
+
+ public Session[] findSessions()
+ {
+ return null;
+ }
+
+ public String getInfo()
+ {
+ return "MockJBossManager";
+ }
+
+ public void remove(Session session)
+ {
+ // no-op
+ }
+
+}
Added: trunk/testsuite/src/main/org/jboss/test/cluster/web/mocks/MockResponse.java
===================================================================
--- trunk/testsuite/src/main/org/jboss/test/cluster/web/mocks/MockResponse.java (rev 0)
+++ trunk/testsuite/src/main/org/jboss/test/cluster/web/mocks/MockResponse.java 2008-11-18 17:26:00 UTC (rev 81244)
@@ -0,0 +1,71 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.test.cluster.web.mocks;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.servlet.http.Cookie;
+
+import org.apache.catalina.connector.Connector;
+import org.apache.catalina.connector.Response;
+
+/**
+ * @author Brian Stansberry
+ *
+ */
+public class MockResponse extends Response
+{
+ private Connector connector;
+ private final Set<Cookie> cookies = new HashSet<Cookie>();
+
+ /**
+ * Create a new MockResponse.
+ *
+ */
+ public MockResponse(Connector connector)
+ {
+ this.connector = connector;
+ }
+
+ @Override
+ public Connector getConnector()
+ {
+ return connector;
+ }
+
+ @Override
+ public void addCookieInternal(Cookie cookie)
+ {
+ cookies.add(cookie);
+ }
+
+ @Override
+ public Cookie[] getCookies()
+ {
+ return ((Cookie[]) cookies.toArray(new Cookie[cookies.size()]));
+ }
+
+
+
+}
Modified: trunk/tomcat/src/main/org/jboss/web/tomcat/service/session/JBossManager.java
===================================================================
--- trunk/tomcat/src/main/org/jboss/web/tomcat/service/session/JBossManager.java 2008-11-18 17:25:48 UTC (rev 81243)
+++ trunk/tomcat/src/main/org/jboss/web/tomcat/service/session/JBossManager.java 2008-11-18 17:26:00 UTC (rev 81244)
@@ -35,7 +35,6 @@
import javax.management.MBeanServer;
import javax.management.ObjectName;
-import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import org.apache.catalina.Container;
@@ -50,6 +49,7 @@
import org.apache.catalina.connector.Response;
import org.apache.catalina.core.ContainerBase;
import org.apache.catalina.util.LifecycleSupport;
+import org.apache.tomcat.util.http.TomcatCookie;
import org.apache.tomcat.util.modeler.Registry;
import org.jboss.logging.Logger;
import org.jboss.metadata.web.jboss.JBossWebMetaData;
@@ -284,22 +284,53 @@
if (response != null)
{
Context context = (Context) container_;
- Connector connector = ((Response)response).getConnector();
+ Connector connector = ((Response) response).getConnector();
if (context.getCookies())
{
// set a new session cookie
- Cookie newCookie = new Cookie(Globals.SESSION_COOKIE_NAME, sessionId);
+ TomcatCookie cookie = new TomcatCookie(Globals.SESSION_COOKIE_NAME, sessionId);
+ // JBAS-6206. Configure cookie a la o.a.c.connector.Request.configureSessionCookie()
+ cookie.setMaxAge(-1);
+ if (context.getSessionCookie().getPath() != null)
+ {
+ cookie.setPath(context.getSessionCookie().getPath());
+ }
+ else
+ {
+ String contextPath = context.getEncodedPath();
+ if ("".equals(contextPath))
+ {
+ contextPath = "/";
+ }
+ cookie.setPath(contextPath);
+ }
+ if (context.getSessionCookie().getComment() != null)
+ {
+ cookie.setComment(context.getSessionCookie().getComment());
+ }
+ if (context.getSessionCookie().getDomain() != null)
+ {
+ cookie.setDomain(context.getSessionCookie().getDomain());
+ }
+ if (context.getSessionCookie().isHttpOnly())
+ {
+ cookie.setHttpOnly(true);
+ }
+ if (context.getSessionCookie().isSecure())
+ {
+ cookie.setSecure(true);
+ }
+ if (connector.getSecure())
+ {
+ cookie.setSecure(true);
+ }
+
if (trace_)
{
log_.trace("Setting cookie with session id:" + sessionId + " & name:" + Globals.SESSION_COOKIE_NAME);
}
- newCookie.setPath("/");
- newCookie.setMaxAge(-1);
- if (connector.getSecure()) {
- newCookie.setSecure(true);
- }
- response.addCookie(newCookie);
+ response.addCookie(cookie);
}
}
}
More information about the jboss-cvs-commits
mailing list