JETTY-1133 Handle multiple URL ; parameters test
git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@1328 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
parent
d5de0a20c5
commit
bb311d8970
|
@ -0,0 +1,579 @@
|
|||
package org.eclipse.jetty.server.session;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.security.Principal;
|
||||
import java.util.Enumeration;
|
||||
import java.util.EventListener;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.RequestDispatcher;
|
||||
import javax.servlet.ServletInputStream;
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.eclipse.jetty.http.HttpCookie;
|
||||
import org.eclipse.jetty.server.DispatcherType;
|
||||
import org.eclipse.jetty.server.Request;
|
||||
import org.eclipse.jetty.server.SessionIdManager;
|
||||
import org.eclipse.jetty.server.SessionManager;
|
||||
|
||||
public class SessionHandlerTest extends TestCase
|
||||
{
|
||||
|
||||
public void testRequestedIdFromCookies()
|
||||
{
|
||||
|
||||
final String cookieName = "SessionId";
|
||||
final String sessionId = "1234.host";
|
||||
HttpServletRequest httpRequest = new MockHttpServletRequest()
|
||||
{
|
||||
public Cookie[] getCookies()
|
||||
{
|
||||
return new Cookie[]
|
||||
{ new Cookie(cookieName,sessionId) };
|
||||
}
|
||||
};
|
||||
|
||||
Request baseRequest = new Request();
|
||||
baseRequest.setDispatcherType(DispatcherType.REQUEST);
|
||||
Assert.assertEquals(DispatcherType.REQUEST,baseRequest.getDispatcherType());
|
||||
|
||||
SessionHandler sessionHandler = new SessionHandler();
|
||||
sessionHandler.setSessionManager(new MockSessionManager()
|
||||
{
|
||||
public boolean isUsingCookies()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public String getSessionCookie()
|
||||
{
|
||||
return cookieName;
|
||||
}
|
||||
});
|
||||
sessionHandler.setRequestedId(baseRequest,httpRequest);
|
||||
|
||||
Assert.assertEquals(sessionId,baseRequest.getRequestedSessionId());
|
||||
Assert.assertTrue(baseRequest.isRequestedSessionIdFromCookie());
|
||||
|
||||
}
|
||||
|
||||
public void testRequestedIdFromURI()
|
||||
{
|
||||
|
||||
final String parameterName = "sessionid";
|
||||
final String sessionId = "1234.host";
|
||||
HttpServletRequest httpRequest = new MockHttpServletRequest()
|
||||
{
|
||||
@Override
|
||||
public String getRequestURI()
|
||||
{
|
||||
return "http://www.foo.net/app/action.do;" + parameterName + "=" + sessionId + ";p1=abc;p2=def";
|
||||
}
|
||||
};
|
||||
|
||||
Request baseRequest = new Request();
|
||||
baseRequest.setDispatcherType(DispatcherType.REQUEST);
|
||||
Assert.assertEquals(DispatcherType.REQUEST,baseRequest.getDispatcherType());
|
||||
|
||||
SessionHandler sessionHandler = new SessionHandler();
|
||||
sessionHandler.setSessionManager(new MockSessionManager()
|
||||
{
|
||||
@Override
|
||||
public String getSessionIdPathParameterName()
|
||||
{
|
||||
return parameterName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSessionIdPathParameterNamePrefix()
|
||||
{
|
||||
return ";"+parameterName+"=";
|
||||
}
|
||||
});
|
||||
|
||||
sessionHandler.setRequestedId(baseRequest,httpRequest);
|
||||
|
||||
Assert.assertEquals(sessionId,baseRequest.getRequestedSessionId());
|
||||
Assert.assertFalse(baseRequest.isRequestedSessionIdFromCookie());
|
||||
}
|
||||
|
||||
/**
|
||||
* Mock class for HttpServletRequest interface.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
class MockHttpServletRequest implements HttpServletRequest
|
||||
{
|
||||
|
||||
public String getRequestURI()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public Cookie[] getCookies()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getAuthType()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getContextPath()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public long getDateHeader(String name)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
public String getHeader(String name)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public Enumeration getHeaderNames()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public Enumeration getHeaders(String name)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public int getIntHeader(String name)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
public String getMethod()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getPathInfo()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getPathTranslated()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getQueryString()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getRemoteUser()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public StringBuffer getRequestURL()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getRequestedSessionId()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getServletPath()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public HttpSession getSession()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public HttpSession getSession(boolean create)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public Principal getUserPrincipal()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean isRequestedSessionIdFromCookie()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isRequestedSessionIdFromURL()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isRequestedSessionIdFromUrl()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isRequestedSessionIdValid()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isUserInRole(String role)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object getAttribute(String name)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public Enumeration getAttributeNames()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getCharacterEncoding()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public int getContentLength()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
public String getContentType()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public ServletInputStream getInputStream() throws IOException
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getLocalAddr()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getLocalName()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public int getLocalPort()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
public Locale getLocale()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public Enumeration getLocales()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getParameter(String name)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public Map getParameterMap()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public Enumeration getParameterNames()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public String[] getParameterValues(String name)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getProtocol()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public BufferedReader getReader() throws IOException
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getRealPath(String path)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getRemoteAddr()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getRemoteHost()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public int getRemotePort()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
public RequestDispatcher getRequestDispatcher(String path)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getScheme()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getServerName()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public int getServerPort()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
public boolean isSecure()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public void removeAttribute(String name)
|
||||
{
|
||||
}
|
||||
|
||||
public void setAttribute(String name, Object o)
|
||||
{
|
||||
}
|
||||
|
||||
public void setCharacterEncoding(String env) throws UnsupportedEncodingException
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Mock class for SessionManager interface.
|
||||
*/
|
||||
class MockSessionManager implements SessionManager
|
||||
{
|
||||
public HttpCookie access(HttpSession session, boolean secure)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public void addEventListener(EventListener listener)
|
||||
{
|
||||
}
|
||||
|
||||
public void clearEventListeners()
|
||||
{
|
||||
}
|
||||
|
||||
public void complete(HttpSession session)
|
||||
{
|
||||
}
|
||||
|
||||
public String getClusterId(HttpSession session)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean getHttpOnly()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public HttpSession getHttpSession(String id)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public SessionIdManager getIdManager()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public int getMaxCookieAge()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int getMaxInactiveInterval()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
public SessionIdManager getMetaManager()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getNodeId(HttpSession session)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean getSecureCookies()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public HttpCookie getSessionCookie(HttpSession session, String contextPath, boolean requestIsSecure)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getSessionCookie()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getSessionDomain()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getSessionIdPathParameterName()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getSessionIdPathParameterNamePrefix()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getSessionPath()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean isUsingCookies()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isValid(HttpSession session)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public HttpSession newHttpSession(HttpServletRequest request)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public void removeEventListener(EventListener listener)
|
||||
{
|
||||
}
|
||||
|
||||
public void setIdManager(SessionIdManager idManager)
|
||||
{
|
||||
}
|
||||
|
||||
public void setMaxCookieAge(int maxCookieAge)
|
||||
{
|
||||
}
|
||||
|
||||
public void setMaxInactiveInterval(int seconds)
|
||||
{
|
||||
}
|
||||
|
||||
public void setSessionCookie(String cookieName)
|
||||
{
|
||||
}
|
||||
|
||||
public void setSessionDomain(String domain)
|
||||
{
|
||||
}
|
||||
|
||||
public void setSessionHandler(SessionHandler handler)
|
||||
{
|
||||
}
|
||||
|
||||
public void setSessionIdPathParameterName(String parameterName)
|
||||
{
|
||||
}
|
||||
|
||||
public void setSessionPath(String path)
|
||||
{
|
||||
}
|
||||
|
||||
public void addLifeCycleListener(Listener listener)
|
||||
{
|
||||
}
|
||||
|
||||
public boolean isFailed()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isRunning()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isStarted()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isStarting()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isStopped()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isStopping()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public void removeLifeCycleListener(Listener listener)
|
||||
{
|
||||
}
|
||||
|
||||
public void start() throws Exception
|
||||
{
|
||||
}
|
||||
|
||||
public void stop() throws Exception
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,176 +0,0 @@
|
|||
// ========================================================================
|
||||
// Copyright (c) 2008-2009 Mort Bay Consulting Pty. Ltd.
|
||||
// ------------------------------------------------------------------------
|
||||
// All rights reserved. This program and the accompanying materials
|
||||
// are made available under the terms of the Eclipse Public License v1.0
|
||||
// and Apache License v2.0 which accompanies this distribution.
|
||||
// The Eclipse Public License is available at
|
||||
// http://www.eclipse.org/legal/epl-v10.html
|
||||
// The Apache License v2.0 is available at
|
||||
// http://www.opensource.org/licenses/apache2.0.php
|
||||
// You may elect to redistribute this code under either of these licenses.
|
||||
// ========================================================================
|
||||
|
||||
package org.eclipse.jetty.servlet;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public abstract class AbstractSessionTest extends TestCase
|
||||
{
|
||||
public static final String __host1 = "localhost";
|
||||
public static final String __host2 = __host1;
|
||||
public static final String __port1 = "8010";
|
||||
public static final String __port2 = "8011";
|
||||
SessionTestServer _server1;
|
||||
SessionTestServer _server2;
|
||||
|
||||
public abstract SessionTestServer newServer1 ();
|
||||
|
||||
public abstract SessionTestServer newServer2();
|
||||
|
||||
public void setUp () throws Exception
|
||||
{
|
||||
_server1 = newServer1();
|
||||
_server2 = newServer2();
|
||||
_server1.start();
|
||||
_server2.start();
|
||||
}
|
||||
|
||||
public void tearDown () throws Exception
|
||||
{
|
||||
if (_server1 != null)
|
||||
_server1.stop();
|
||||
if (_server2 != null)
|
||||
_server2.stop();
|
||||
|
||||
_server1=null;
|
||||
_server2=null;
|
||||
}
|
||||
|
||||
|
||||
public void testSessions () throws Exception
|
||||
{
|
||||
SessionTestClient client1 = new SessionTestClient("http://"+__host1+":"+__port1);
|
||||
SessionTestClient client2 = new SessionTestClient("http://"+__host2+":"+__port2);
|
||||
// confirm that user has no session
|
||||
assertFalse(client1.send("/contextA", null));
|
||||
String cookie1 = client1.newSession("/contextA");
|
||||
assertNotNull(cookie1);
|
||||
System.err.println("cookie1: " + cookie1);
|
||||
|
||||
// confirm that client2 has the same session attributes as client1
|
||||
assertTrue(client1.setAttribute("/contextA", cookie1, "foo", "bar"));
|
||||
assertTrue(client2.hasAttribute("/contextA", cookie1, "foo", "bar"));
|
||||
|
||||
// confirm that /contextA would share same sessionId as /contextB
|
||||
assertTrue(client1.send("/contextA/dispatch/forward/contextB", cookie1));
|
||||
assertTrue(client2.send("/contextA/dispatch/forward/contextB", cookie1));
|
||||
assertTrue(client1.send("/contextB", cookie1));
|
||||
|
||||
// verify that session attributes on /contextA is different from /contextB
|
||||
assertFalse(client1.hasAttribute("/contextB/action", cookie1, "foo", "bar"));
|
||||
|
||||
// add new session attributes on /contextB
|
||||
client1.setAttribute("/contextB/action", cookie1, "zzzzz", "yyyyy");
|
||||
assertTrue(client1.hasAttribute("/contextB/action", cookie1, "zzzzz", "yyyyy"));
|
||||
|
||||
// verify that client2 has same sessionAttributes on /contextB
|
||||
// client1's newly added attribute "zzzzz" needs to be flushed to the database first
|
||||
// saveInterval is configured at 10s... to test, uncomment the 2 lines below.
|
||||
//Thread.sleep(10000);
|
||||
//assertTrue(client2.hasAttribute("/contextB/action", cookie1, "zzzzz", "yyyyy"));
|
||||
|
||||
String cookie2 = client2.newSession("/contextA");
|
||||
assertNotNull(cookie2);
|
||||
System.err.println("cookie2: " + cookie2);
|
||||
|
||||
// confirm that client1 has same session attributes as client2
|
||||
assertTrue(client2.setAttribute("/contextA", cookie2, "hello", "world"));
|
||||
assertTrue(client1.hasAttribute("/contextA", cookie2, "hello", "world"));
|
||||
|
||||
// confirm that /contextA would share same sessionId as /contextB
|
||||
assertTrue(client1.send("/contextA/dispatch/forward/contextB", cookie2));
|
||||
assertTrue(client2.send("/contextA/dispatch/forward/contextB", cookie2));
|
||||
assertTrue(client1.send("/contextB", cookie2));
|
||||
|
||||
// Session invalidate on contextA
|
||||
assertTrue(client1.invalidate("/contextA", cookie1));
|
||||
|
||||
// confirm that session on contextB has not been invalidated after contextA has been invalidated
|
||||
assertTrue(client1.send("/contextB", cookie1));
|
||||
|
||||
// confirm that session on contextA has been deleted
|
||||
assertFalse(client1.send("/contextA", cookie1));
|
||||
|
||||
// Session invalidate on contextB
|
||||
assertTrue(client1.invalidate("/contextB/action", cookie1));
|
||||
|
||||
// confirm that session on contextB has been deleted
|
||||
assertFalse(client1.send("/contextB/action", cookie1));
|
||||
|
||||
// session will reflect after 10s, so node2 still would not be deleted.
|
||||
assertTrue(client2.send("/contextB/action", cookie1));
|
||||
|
||||
// wait for saveInterval and check if the session invalidation has been reflected to the other node
|
||||
// to test, uncomment 3 lines below
|
||||
//Thread.sleep(10000);
|
||||
//assertFalse(client2.send("/contextA", cookie1));
|
||||
//assertFalse(client2.send("/contextB/action", cookie1));
|
||||
}
|
||||
|
||||
public void testSessionManagerStop() throws Exception
|
||||
{
|
||||
SessionTestClient client1 = new SessionTestClient("http://"+__host1+":"+__port1);
|
||||
SessionTestClient client2 = new SessionTestClient("http://"+__host2+":"+__port2);
|
||||
// confirm that user has no session
|
||||
assertFalse(client1.send("/contextA", null));
|
||||
String cookie1 = client1.newSession("/contextA");
|
||||
assertNotNull(cookie1);
|
||||
System.err.println("cookie1: " + cookie1);
|
||||
|
||||
// creates a session for contextB
|
||||
assertTrue(client1.send("/contextB", cookie1));
|
||||
|
||||
// confirm that /contextA and /contextB sessions are available
|
||||
assertTrue(client1.send("/contextA", cookie1));
|
||||
assertTrue(client1.send("/contextB/action", cookie1));
|
||||
assertTrue(client1.setAttribute("/contextA", cookie1, "a", "b"));
|
||||
assertTrue(client1.setAttribute("/contextB/action", cookie1, "c", "d"));
|
||||
|
||||
// confirm that /contextA and /contextB sessions from client2 are available
|
||||
assertTrue(client2.send("/contextA", cookie1));
|
||||
assertTrue(client2.send("/contextB/action", cookie1));
|
||||
assertTrue(client2.hasAttribute("/contextA", cookie1, "a", "b"));
|
||||
assertTrue(client2.hasAttribute("/contextB/action", cookie1, "c", "d"));
|
||||
|
||||
// stop sessionManager from node1
|
||||
_server1._sessionMgr1.stop();
|
||||
|
||||
// verify session still exists for contextB
|
||||
assertTrue(client1.send("/contextB/action", cookie1));
|
||||
assertTrue(client1.hasAttribute("/contextB/action", cookie1, "c", "d"));
|
||||
|
||||
// stop sessionManager from node2
|
||||
_server2._sessionMgr2.stop();
|
||||
|
||||
// verfiy session still exists for contextA
|
||||
assertTrue(client2.send("/contextA", cookie1));
|
||||
assertTrue(client2.hasAttribute("/contextA", cookie1, "a", "b"));
|
||||
}
|
||||
|
||||
public void testFailover() throws Exception
|
||||
{
|
||||
SessionTestClient client1 = new SessionTestClient("http://"+__host1+":"+__port1);
|
||||
SessionTestClient client2 = new SessionTestClient("http://"+__host2+":"+__port2);
|
||||
// confirm that user has no session
|
||||
assertFalse(client1.send("/contextA", null));
|
||||
String cookie1 = client1.newSession("/contextA");
|
||||
|
||||
assertNotNull(cookie1);
|
||||
System.err.println("cookie1: " + cookie1);
|
||||
|
||||
assertTrue(client1.setAttribute("/contextA", cookie1, "a", "b"));
|
||||
|
||||
assertTrue(client2.hasAttribute("/contextA", cookie1, "a", "b"));
|
||||
}
|
||||
}
|
|
@ -1,125 +0,0 @@
|
|||
// ========================================================================
|
||||
// Copyright (c) 2008-2009 Mort Bay Consulting Pty. Ltd.
|
||||
// ------------------------------------------------------------------------
|
||||
// All rights reserved. This program and the accompanying materials
|
||||
// are made available under the terms of the Eclipse Public License v1.0
|
||||
// and Apache License v2.0 which accompanies this distribution.
|
||||
// The Eclipse Public License is available at
|
||||
// http://www.eclipse.org/legal/epl-v10.html
|
||||
// The Apache License v2.0 is available at
|
||||
// http://www.opensource.org/licenses/apache2.0.php
|
||||
// You may elect to redistribute this code under either of these licenses.
|
||||
// ========================================================================
|
||||
|
||||
|
||||
package org.eclipse.jetty.servlet;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
public class SessionTestClient
|
||||
{
|
||||
|
||||
private String _baseUrl;
|
||||
|
||||
// e.g http://localhost:8010
|
||||
public SessionTestClient(String baseUrl)
|
||||
{
|
||||
_baseUrl = baseUrl;
|
||||
}
|
||||
|
||||
public boolean send(String context, String cookie) throws Exception
|
||||
{
|
||||
HttpURLConnection conn = sendRequest("GET", new URL(_baseUrl + context + "/session/"),
|
||||
cookie);
|
||||
return isSessionAvailable(conn);
|
||||
}
|
||||
|
||||
public String newSession(String context) throws Exception
|
||||
{
|
||||
HttpURLConnection conn = sendRequest("POST", new URL(_baseUrl + context +
|
||||
"/session/?Action=New%20Session"), null);
|
||||
conn.disconnect();
|
||||
return getJSESSIONID(conn.getHeaderField("Set-Cookie"));
|
||||
}
|
||||
|
||||
public boolean setAttribute(String context, String cookie, String name, String value) throws Exception
|
||||
{
|
||||
// should be POST, GET for now
|
||||
HttpURLConnection conn = sendRequest("GET", new URL(_baseUrl + context +
|
||||
"/session/?Action=Set&Name=" + name + "&Value=" + value), cookie);
|
||||
return isAttributeSet(conn, name, value);
|
||||
}
|
||||
|
||||
public boolean hasAttribute(String context, String cookie, String name, String value) throws Exception
|
||||
{
|
||||
HttpURLConnection conn = sendRequest("GET", new URL(_baseUrl + context + "/session/"),
|
||||
cookie);
|
||||
return isAttributeSet(conn, name, value);
|
||||
}
|
||||
|
||||
public boolean invalidate(String context, String cookie) throws Exception
|
||||
{
|
||||
// should be POST, GET for now
|
||||
HttpURLConnection conn = sendRequest("GET", new URL(_baseUrl + context +
|
||||
"/session/?Action=Invalidate"), cookie);
|
||||
return !isSessionAvailable(conn);
|
||||
}
|
||||
|
||||
protected static boolean isSessionAvailable(HttpURLConnection conn) throws Exception
|
||||
{
|
||||
return !isTokenPresent(conn, "<H3>No Session</H3>");
|
||||
}
|
||||
|
||||
protected static boolean isAttributeSet(HttpURLConnection conn, String name, String value) throws Exception
|
||||
{
|
||||
return isTokenPresent(conn, "<b>" + name + ":</b> " + value + "<br/>");
|
||||
}
|
||||
|
||||
protected static boolean isTokenPresent(HttpURLConnection conn, String token) throws Exception
|
||||
{
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
||||
String line = null;
|
||||
boolean present = false;
|
||||
while((line=br.readLine())!=null)
|
||||
{
|
||||
|
||||
if(line.indexOf(token)!=-1)
|
||||
{
|
||||
present = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
conn.disconnect();
|
||||
return present;
|
||||
}
|
||||
|
||||
public HttpURLConnection sendRequest(String method, URL url, String cookie) throws Exception
|
||||
{
|
||||
return sendRequest(method, url, cookie, false);
|
||||
}
|
||||
|
||||
public HttpURLConnection sendRequest(String method, URL url, String cookie,
|
||||
boolean followRedirects) throws Exception
|
||||
{
|
||||
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
|
||||
conn.setRequestMethod(method);
|
||||
conn.setDoInput(true);
|
||||
conn.setInstanceFollowRedirects(followRedirects);
|
||||
if(cookie!=null)
|
||||
conn.setRequestProperty("Cookie", cookie);
|
||||
conn.connect();
|
||||
return conn;
|
||||
}
|
||||
|
||||
protected static String getJSESSIONID(String cookie)
|
||||
{
|
||||
System.err.println("COOKIE: " + cookie);
|
||||
int idx = cookie.indexOf("JSESSIONID");
|
||||
return cookie.substring(idx, cookie.indexOf(';', idx));
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,349 +0,0 @@
|
|||
// ========================================================================
|
||||
// Copyright (c) 2008-2009 Mort Bay Consulting Pty. Ltd.
|
||||
// ------------------------------------------------------------------------
|
||||
// All rights reserved. This program and the accompanying materials
|
||||
// are made available under the terms of the Eclipse Public License v1.0
|
||||
// and Apache License v2.0 which accompanies this distribution.
|
||||
// The Eclipse Public License is available at
|
||||
// http://www.eclipse.org/legal/epl-v10.html
|
||||
// The Apache License v2.0 is available at
|
||||
// http://www.opensource.org/licenses/apache2.0.php
|
||||
// You may elect to redistribute this code under either of these licenses.
|
||||
// ========================================================================
|
||||
|
||||
package org.eclipse.jetty.servlet;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.Date;
|
||||
import java.util.Enumeration;
|
||||
|
||||
import javax.servlet.RequestDispatcher;
|
||||
import javax.servlet.ServletConfig;
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
import org.eclipse.jetty.server.Request;
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.server.SessionIdManager;
|
||||
import org.eclipse.jetty.server.SessionManager;
|
||||
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
|
||||
|
||||
/**
|
||||
* SessionTestServer
|
||||
*
|
||||
* Base class for common backend to test various session plugin
|
||||
* implementations.
|
||||
*
|
||||
* The backend runs 2 jetty servers, with 2 contexts each:
|
||||
*
|
||||
* contextA/session - dumps and allows create/delete of a session
|
||||
* contextA/dispatch/forward/contextB/session - forwards to contextB
|
||||
* contextB/session - dumps and allows create/delete of a session
|
||||
*
|
||||
* Subclasses should implement the configureEnvironment(),
|
||||
* configureSessionIdManager(), configureSessionManager1(),
|
||||
* configureSessionManager2() in order to provide the session
|
||||
* management implementations to test.
|
||||
*/
|
||||
public abstract class SessionTestServer extends Server
|
||||
{
|
||||
protected SessionIdManager _sessionIdMgr;
|
||||
protected SessionManager _sessionMgr1;
|
||||
protected SessionManager _sessionMgr2;
|
||||
protected String _workerName;
|
||||
|
||||
|
||||
/**
|
||||
* ForwardingServlet
|
||||
* Do dispatch forward to test re-use of session id (BUT NOT CONTENTS!)
|
||||
*
|
||||
*/
|
||||
public class ForwardingServlet extends HttpServlet
|
||||
{
|
||||
public void doGet (HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException
|
||||
{
|
||||
String pathInfo = request.getPathInfo();
|
||||
|
||||
HttpSession session = request.getSession(false);
|
||||
|
||||
if (pathInfo.startsWith("/forward/"))
|
||||
{
|
||||
pathInfo = pathInfo.substring(8);
|
||||
String cpath = pathInfo.substring(0, pathInfo.indexOf('/', 1));
|
||||
pathInfo = pathInfo.substring(cpath.length());
|
||||
ServletContext context = ((Request)request).getServletContext().getContext(cpath);
|
||||
RequestDispatcher dispatcher = context.getRequestDispatcher(pathInfo);
|
||||
dispatcher.forward(request, response);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* SessionDumpServlet
|
||||
*
|
||||
* Servlet to dump the contents of the session.
|
||||
*/
|
||||
public class SessionDumpServlet extends HttpServlet
|
||||
{
|
||||
int redirectCount=0;
|
||||
|
||||
public void init(ServletConfig config)
|
||||
throws ServletException
|
||||
{
|
||||
super.init(config);
|
||||
}
|
||||
|
||||
|
||||
public void dump(HttpServletRequest request,
|
||||
HttpServletResponse response)
|
||||
throws ServletException, IOException
|
||||
{
|
||||
response.setContentType("text/html");
|
||||
|
||||
HttpSession session = request.getSession(getURI(request).indexOf("new")>0);
|
||||
try
|
||||
{
|
||||
if (session!=null)
|
||||
session.isNew();
|
||||
}
|
||||
catch(IllegalStateException e)
|
||||
{
|
||||
session=null;
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
PrintWriter out = response.getWriter();
|
||||
out.println("<h1>Session Dump Servlet:</h1>");
|
||||
String submitUrl = getServletContext().getContextPath();
|
||||
submitUrl = (submitUrl.equals("")?"/":submitUrl);
|
||||
submitUrl = (submitUrl.endsWith("/")?submitUrl:submitUrl+"/");
|
||||
submitUrl += "session/";
|
||||
out.println("<form action=\""+submitUrl+"\" method=\"post\">");
|
||||
|
||||
if (session==null)
|
||||
{
|
||||
out.println("<H3>No Session</H3>");
|
||||
out.println("<input type=\"submit\" name=\"Action\" value=\"New Session\"/>");
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
out.println("<b>ID:</b> "+session.getId()+"<br/>");
|
||||
out.println("<b>New:</b> "+session.isNew()+"<br/>");
|
||||
out.println("<b>Created:</b> "+new Date(session.getCreationTime())+"<br/>");
|
||||
out.println("<b>Last:</b> "+new Date(session.getLastAccessedTime())+"<br/>");
|
||||
out.println("<b>Max Inactive:</b> "+session.getMaxInactiveInterval()+"<br/>");
|
||||
out.println("<b>Context:</b> "+session.getServletContext()+"<br/>");
|
||||
|
||||
|
||||
Enumeration keys=session.getAttributeNames();
|
||||
while(keys.hasMoreElements())
|
||||
{
|
||||
String name=(String)keys.nextElement();
|
||||
String value=""+session.getAttribute(name);
|
||||
|
||||
out.println("<b>"+name+":</b> "+value+"<br/>");
|
||||
}
|
||||
|
||||
out.println("<b>Name:</b><input type=\"text\" name=\"Name\" /><br/>");
|
||||
out.println("<b>Value:</b><input type=\"text\" name=\"Value\" /><br/>");
|
||||
|
||||
out.println("<input type=\"submit\" name=\"Action\" value=\"Set\"/>");
|
||||
out.println("<input type=\"submit\" name=\"Action\" value=\"Remove\"/>");
|
||||
out.println("<input type=\"submit\" name=\"Action\" value=\"Refresh\"/>");
|
||||
out.println("<input type=\"submit\" name=\"Action\" value=\"Invalidate\"/><br/>");
|
||||
|
||||
out.println("</form><br/>");
|
||||
|
||||
if (request.isRequestedSessionIdFromCookie())
|
||||
out.println("<P>Turn off cookies in your browser to try url encoding<BR>");
|
||||
|
||||
if (request.isRequestedSessionIdFromURL())
|
||||
out.println("<P>Turn on cookies in your browser to try cookie encoding<BR>");
|
||||
out.println("<a href=\""+response.encodeURL(request.getRequestURI()+"?q=0")+"\">Encoded Link</a><BR>");
|
||||
|
||||
}
|
||||
catch (IllegalStateException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public String getServletInfo() {
|
||||
return "Session Dump Servlet";
|
||||
}
|
||||
|
||||
|
||||
public String getURI(HttpServletRequest request)
|
||||
{
|
||||
String uri=(String)request.getAttribute("javax.servlet.forward.request_uri");
|
||||
if (uri==null)
|
||||
uri=request.getRequestURI();
|
||||
return uri;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* SessionForwardedServlet
|
||||
*
|
||||
* Servlet that is target of a dispatch forward.
|
||||
* It will always try and make a new session, and then dump its
|
||||
* contents as html.
|
||||
*/
|
||||
public class SessionForwardedServlet extends SessionDumpServlet
|
||||
{
|
||||
|
||||
public void doPost(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException
|
||||
{
|
||||
handleForm(request, response);
|
||||
dump(request, response);
|
||||
}
|
||||
|
||||
protected void handleForm(HttpServletRequest request,
|
||||
HttpServletResponse response)
|
||||
{
|
||||
HttpSession session = request.getSession(false);
|
||||
String action = request.getParameter("Action");
|
||||
String name = request.getParameter("Name");
|
||||
String value = request.getParameter("Value");
|
||||
|
||||
if (action!=null)
|
||||
{
|
||||
if(action.equals("New Session"))
|
||||
{
|
||||
session = request.getSession(true);
|
||||
session.setAttribute("test","value");
|
||||
}
|
||||
else if (session!=null)
|
||||
{
|
||||
if (action.equals("Invalidate"))
|
||||
session.invalidate();
|
||||
else if (action.equals("Set") && name!=null && name.length()>0)
|
||||
session.setAttribute(name,value);
|
||||
else if (action.equals("Remove"))
|
||||
session.removeAttribute(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void doGet(HttpServletRequest request,
|
||||
HttpServletResponse response)
|
||||
throws ServletException, IOException
|
||||
{
|
||||
request.getSession(true);
|
||||
dump(request, response);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* SessionActionServlet
|
||||
*
|
||||
* Servlet to allow making a new session under user control
|
||||
* by clicking the "New Session" button (ie query params ?Action=New Session)
|
||||
*/
|
||||
public class SessionActionServlet extends SessionDumpServlet
|
||||
{
|
||||
protected void handleForm(HttpServletRequest request,
|
||||
HttpServletResponse response)
|
||||
{
|
||||
HttpSession session = request.getSession(false);
|
||||
String action = request.getParameter("Action");
|
||||
String name = request.getParameter("Name");
|
||||
String value = request.getParameter("Value");
|
||||
|
||||
if (action!=null)
|
||||
{
|
||||
if(action.equals("New Session"))
|
||||
{
|
||||
session = request.getSession(true);
|
||||
session.setAttribute("test","value");
|
||||
}
|
||||
else if (session!=null)
|
||||
{
|
||||
if (action.equals("Invalidate"))
|
||||
session.invalidate();
|
||||
else if (action.equals("Set") && name!=null && name.length()>0)
|
||||
session.setAttribute(name,value);
|
||||
else if (action.equals("Remove"))
|
||||
session.removeAttribute(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void doPost(HttpServletRequest request,
|
||||
HttpServletResponse response)
|
||||
throws ServletException, IOException
|
||||
{
|
||||
handleForm(request,response);
|
||||
String nextUrl = getURI(request)+"?R="+redirectCount++;
|
||||
String encodedUrl=response.encodeRedirectURL(nextUrl);
|
||||
response.sendRedirect(encodedUrl);
|
||||
}
|
||||
|
||||
public void doGet (HttpServletRequest request,
|
||||
HttpServletResponse response)
|
||||
throws ServletException, IOException
|
||||
{
|
||||
handleForm(request,response);
|
||||
dump(request, response);
|
||||
}
|
||||
}
|
||||
|
||||
public SessionTestServer(int port, String workerName)
|
||||
{
|
||||
super(port);
|
||||
_workerName = workerName;
|
||||
configureEnvironment();
|
||||
configureIdManager();
|
||||
configureSessionManager1();
|
||||
configureSessionManager2();
|
||||
configureServer();
|
||||
}
|
||||
|
||||
public abstract void configureEnvironment ();
|
||||
|
||||
public abstract void configureIdManager();
|
||||
|
||||
public abstract void configureSessionManager1();
|
||||
|
||||
public abstract void configureSessionManager2();
|
||||
|
||||
public void configureServer ()
|
||||
{
|
||||
if (_sessionIdMgr == null || _sessionMgr1 == null || _sessionMgr2 == null)
|
||||
throw new IllegalStateException ("Must set a SessionIdManager instance and 2 SessionManager instances");
|
||||
|
||||
setSessionIdManager(_sessionIdMgr);
|
||||
//set up 2 contexts and a filter than can forward between them
|
||||
ContextHandlerCollection contextsA = new ContextHandlerCollection();
|
||||
setHandler(contextsA);
|
||||
setSessionIdManager(_sessionIdMgr);
|
||||
|
||||
ServletContextHandler contextA1 = new ServletContextHandler(contextsA,"/contextA",ServletContextHandler.SESSIONS);
|
||||
contextA1.addServlet(new ServletHolder(new SessionActionServlet()), "/session/*");
|
||||
contextA1.addServlet(new ServletHolder(new ForwardingServlet()), "/dispatch/*");
|
||||
contextA1.getSessionHandler().setSessionManager(_sessionMgr1);
|
||||
_sessionMgr1.setIdManager(_sessionIdMgr);
|
||||
|
||||
|
||||
ServletContextHandler contextA2 = new ServletContextHandler(contextsA, "/contextB", ServletContextHandler.SESSIONS);
|
||||
contextA2.addServlet(new ServletHolder(new SessionForwardedServlet()), "/session/*");
|
||||
contextA2.addServlet(new ServletHolder(new SessionActionServlet()), "/action/session/*");
|
||||
contextA2.getSessionHandler().setSessionManager(_sessionMgr2);
|
||||
_sessionMgr2.setIdManager(_sessionIdMgr);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue