This commit is contained in:
Jan Bartel 2017-06-06 17:00:22 +02:00 committed by Joakim Erdfelt
parent 8e3f950892
commit b2187b3b1c
12 changed files with 405 additions and 205 deletions

View File

@ -25,7 +25,6 @@ import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@ -33,11 +32,10 @@ import java.sql.Statement;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import org.eclipse.jetty.util.ClassLoadingObjectInputStream;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.annotation.ManagedAttribute;
import org.eclipse.jetty.util.annotation.ManagedObject;
import org.eclipse.jetty.util.log.Log;
@ -53,6 +51,11 @@ public class JDBCSessionDataStore extends AbstractSessionDataStore
{
final static Logger LOG = Log.getLogger("org.eclipse.jetty.server.session");
/**
* Used for Oracle and other databases where "" is treated as NULL
*/
public static final String NULL_CONTEXT_PATH = "/";
protected boolean _initialized = false;
private DatabaseAdaptor _dbAdaptor;
private SessionTableSchema _sessionTableSchema;
@ -275,27 +278,24 @@ public class JDBCSessionDataStore extends AbstractSessionDataStore
" values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
}
public PreparedStatement getUpdateSessionStatement(Connection connection, String canonicalContextPath)
public PreparedStatement getUpdateSessionStatement(Connection connection, String id, SessionContext context)
throws SQLException
{
String s = "update "+getSchemaTableName()+
" set "+getLastNodeColumn()+" = ?, "+getAccessTimeColumn()+" = ?, "+
getLastAccessTimeColumn()+" = ?, "+getLastSavedTimeColumn()+" = ?, "+getExpiryTimeColumn()+" = ?, "+
getMaxIntervalColumn()+" = ?, "+getMapColumn()+" = ? where ";
getMaxIntervalColumn()+" = ?, "+getMapColumn()+" = ? where "+getIdColumn()+" = ? and "+getContextPathColumn()+
" = ? and "+getVirtualHostColumn()+" = ?";
if (canonicalContextPath == null || "".equals(canonicalContextPath))
{
if (_dbAdaptor.isEmptyStringNull())
{
s = s+getIdColumn()+" = ? and "+
getContextPathColumn()+" is null and "+
getVirtualHostColumn()+" = ?";
return connection.prepareStatement(s);
}
}
return connection.prepareStatement(s+getIdColumn()+" = ? and "+getContextPathColumn()+
" = ? and "+getVirtualHostColumn()+" = ?");
String cp = context.getCanonicalContextPath();
if (_dbAdaptor.isEmptyStringNull() && StringUtil.isBlank(cp))
cp = NULL_CONTEXT_PATH;
PreparedStatement statement = connection.prepareStatement(s);
statement.setString(8, id);
statement.setString(9, cp);
statement.setString(10, context.getVhost());
return statement;
}
@ -305,27 +305,16 @@ public class JDBCSessionDataStore extends AbstractSessionDataStore
if (_dbAdaptor == null)
throw new IllegalStateException("No DB adaptor");
if (canonicalContextPath == null || "".equals(canonicalContextPath))
{
if (_dbAdaptor.isEmptyStringNull())
{
PreparedStatement statement = connection.prepareStatement("select "+getIdColumn()+", "+getExpiryTimeColumn()+
" from "+getSchemaTableName()+" where "+
getContextPathColumn()+" is null and "+
getVirtualHostColumn()+" = ? and "+getExpiryTimeColumn()+" >0 and "+getExpiryTimeColumn()+" <= ?");
statement.setString(1, vhost);
statement.setLong(2, expiry);
return statement;
}
}
String cp = canonicalContextPath;
if (_dbAdaptor.isEmptyStringNull() && StringUtil.isBlank(cp))
cp = NULL_CONTEXT_PATH;
PreparedStatement statement = connection.prepareStatement("select "+getIdColumn()+", "+getExpiryTimeColumn()+
" from "+getSchemaTableName()+" where "+getContextPathColumn()+" = ? and "+
getVirtualHostColumn()+" = ? and "+
getExpiryTimeColumn()+" >0 and "+getExpiryTimeColumn()+" <= ?");
statement.setString(1, canonicalContextPath);
statement.setString(1, cp);
statement.setString(2, vhost);
statement.setLong(3, expiry);
return statement;
@ -338,21 +327,9 @@ public class JDBCSessionDataStore extends AbstractSessionDataStore
if (_dbAdaptor == null)
throw new IllegalStateException("No DB adaptor");
if (sessionContext.getCanonicalContextPath() == null || "".equals(sessionContext.getCanonicalContextPath()))
{
if (_dbAdaptor.isEmptyStringNull())
{
PreparedStatement statement = connection.prepareStatement("select "+getIdColumn()+", "+getExpiryTimeColumn()+
" from "+getSchemaTableName()+" where "+
getLastNodeColumn() + " = ? and "+
getContextPathColumn()+" is null and "+
getVirtualHostColumn()+" = ? and "+getExpiryTimeColumn()+" >0 and "+getExpiryTimeColumn()+" <= ?");
statement.setString(1, sessionContext.getWorkerName());
statement.setString(2, sessionContext.getVhost());
statement.setLong(3, expiry);
return statement;
}
}
String cp = sessionContext.getCanonicalContextPath();
if (_dbAdaptor.isEmptyStringNull() && StringUtil.isBlank(cp))
cp = NULL_CONTEXT_PATH;
PreparedStatement statement = connection.prepareStatement("select "+getIdColumn()+", "+getExpiryTimeColumn()+
" from "+getSchemaTableName()+" where "+
@ -362,7 +339,7 @@ public class JDBCSessionDataStore extends AbstractSessionDataStore
getExpiryTimeColumn()+" >0 and "+getExpiryTimeColumn()+" <= ?");
statement.setString(1, sessionContext.getWorkerName());
statement.setString(2, sessionContext.getCanonicalContextPath());
statement.setString(2, cp);
statement.setString(3, sessionContext.getVhost());
statement.setLong(4, expiry);
return statement;
@ -382,52 +359,27 @@ public class JDBCSessionDataStore extends AbstractSessionDataStore
}
public PreparedStatement getCheckSessionExistsStatement (Connection connection, String canonicalContextPath)
public PreparedStatement getCheckSessionExistsStatement (Connection connection, SessionContext context)
throws SQLException
{
if (_dbAdaptor == null)
throw new IllegalStateException("No DB adaptor");
if (canonicalContextPath == null || "".equals(canonicalContextPath))
{
if (_dbAdaptor.isEmptyStringNull())
{
PreparedStatement statement = connection.prepareStatement("select "+getIdColumn()+", "+getExpiryTimeColumn()+
" from "+getSchemaTableName()+
" where "+getIdColumn()+" = ? and "+
getContextPathColumn()+" is null and "+
getVirtualHostColumn()+" = ?");
return statement;
}
}
String cp = context.getCanonicalContextPath();
if (_dbAdaptor.isEmptyStringNull() && StringUtil.isBlank(cp))
cp = NULL_CONTEXT_PATH;
PreparedStatement statement = connection.prepareStatement("select "+getIdColumn()+", "+getExpiryTimeColumn()+
" from "+getSchemaTableName()+
" where "+getIdColumn()+" = ? and "+
getContextPathColumn()+" = ? and "+
getVirtualHostColumn()+" = ?");
statement.setString(2, cp);
statement.setString(3, context.getVhost());
return statement;
}
public void fillCheckSessionExistsStatement (PreparedStatement statement, String id, SessionContext contextId)
throws SQLException
{
statement.clearParameters();
ParameterMetaData metaData = statement.getParameterMetaData();
if (metaData.getParameterCount() < 3)
{
statement.setString(1, id);
statement.setString(2, contextId.getVhost());
}
else
{
statement.setString(1, id);
statement.setString(2, contextId.getCanonicalContextPath());
statement.setString(3, contextId.getVhost());
}
}
public PreparedStatement getLoadStatement (Connection connection, String id, SessionContext contextId)
throws SQLException
@ -436,26 +388,15 @@ public class JDBCSessionDataStore extends AbstractSessionDataStore
throw new IllegalStateException("No DB adaptor");
if (contextId.getCanonicalContextPath() == null || "".equals(contextId.getCanonicalContextPath()))
{
if (_dbAdaptor.isEmptyStringNull())
{
PreparedStatement statement = connection.prepareStatement("select * from "+getSchemaTableName()+
" where "+getIdColumn()+" = ? and "+
getContextPathColumn()+" is null and "+
getVirtualHostColumn()+" = ?");
statement.setString(1, id);
statement.setString(2, contextId.getVhost());
return statement;
}
}
String cp = contextId.getCanonicalContextPath();
if (_dbAdaptor.isEmptyStringNull()&& StringUtil.isBlank(cp))
cp = NULL_CONTEXT_PATH;
PreparedStatement statement = connection.prepareStatement("select * from "+getSchemaTableName()+
" where "+getIdColumn()+" = ? and "+getContextPathColumn()+
" = ? and "+getVirtualHostColumn()+" = ?");
statement.setString(1, id);
statement.setString(2, contextId.getCanonicalContextPath());
statement.setString(2, cp);
statement.setString(3, contextId.getVhost());
return statement;
@ -469,28 +410,21 @@ public class JDBCSessionDataStore extends AbstractSessionDataStore
if (_dbAdaptor == null)
throw new IllegalStateException("No DB adaptor");
String cp = contextId.getCanonicalContextPath();
if (_dbAdaptor.isEmptyStringNull() && StringUtil.isBlank(cp))
cp = NULL_CONTEXT_PATH;
String s = "update "+getSchemaTableName()+
" set "+getLastNodeColumn()+" = ?, "+getAccessTimeColumn()+" = ?, "+
getLastAccessTimeColumn()+" = ?, "+getLastSavedTimeColumn()+" = ?, "+getExpiryTimeColumn()+" = ?, "+
getMaxIntervalColumn()+" = ?, "+getMapColumn()+" = ? where ";
getMaxIntervalColumn()+" = ?, "+getMapColumn()+" = ? where "+getIdColumn()+" = ? and "+getContextPathColumn()+
" = ? and "+getVirtualHostColumn()+" = ?";
if (contextId.getCanonicalContextPath() == null || "".equals(contextId.getCanonicalContextPath()))
{
if (_dbAdaptor.isEmptyStringNull())
{
PreparedStatement statement = connection.prepareStatement(s+getIdColumn()+" = ? and "+
getContextPathColumn()+" is null and "+
getVirtualHostColumn()+" = ?");
statement.setString(1, id);
statement.setString(2, contextId.getVhost());
return statement;
}
}
PreparedStatement statement = connection.prepareStatement(s+getIdColumn()+" = ? and "+getContextPathColumn()+
" = ? and "+getVirtualHostColumn()+" = ?");
statement.setString(1, id);
statement.setString(2, contextId.getCanonicalContextPath());
statement.setString(3, contextId.getVhost());
PreparedStatement statement = connection.prepareStatement(s);
statement.setString(8, id);
statement.setString(9, cp);
statement.setString(10, contextId.getVhost());
return statement;
}
@ -505,25 +439,15 @@ public class JDBCSessionDataStore extends AbstractSessionDataStore
throw new IllegalStateException("No DB adaptor");
if (contextId.getCanonicalContextPath() == null || "".equals(contextId.getCanonicalContextPath()))
{
if (_dbAdaptor.isEmptyStringNull())
{
PreparedStatement statement = connection.prepareStatement("delete from "+getSchemaTableName()+
" where "+getIdColumn()+" = ? and "+getContextPathColumn()+
" = ? and "+getVirtualHostColumn()+" = ?");
statement.setString(1, id);
statement.setString(2, contextId.getVhost());
return statement;
}
}
String cp = contextId.getCanonicalContextPath();
if (_dbAdaptor.isEmptyStringNull() && StringUtil.isBlank(cp))
cp = NULL_CONTEXT_PATH;
PreparedStatement statement = connection.prepareStatement("delete from "+getSchemaTableName()+
" where "+getIdColumn()+" = ? and "+getContextPathColumn()+
" = ? and "+getVirtualHostColumn()+" = ?");
statement.setString(1, id);
statement.setString(2, contextId.getCanonicalContextPath());
statement.setString(2, cp);
statement.setString(3, contextId.getVhost());
return statement;
@ -722,8 +646,8 @@ public class JDBCSessionDataStore extends AbstractSessionDataStore
data.setLastNode(result.getString(_sessionTableSchema.getLastNodeColumn()));
data.setLastSaved(result.getLong(_sessionTableSchema.getLastSavedTimeColumn()));
data.setExpiry(result.getLong(_sessionTableSchema.getExpiryTimeColumn()));
data.setContextPath(result.getString(_sessionTableSchema.getContextPathColumn())); //TODO needed? this is part of the key now
data.setVhost(result.getString(_sessionTableSchema.getVirtualHostColumn())); //TODO needed??? this is part of the key now
data.setContextPath(_context.getCanonicalContextPath());
data.setVhost(_context.getVhost());
try (InputStream is = _dbAdaptor.getBlobInputStream(result, _sessionTableSchema.getMapColumn());
ClassLoadingObjectInputStream ois = new ClassLoadingObjectInputStream(is))
@ -815,7 +739,13 @@ public class JDBCSessionDataStore extends AbstractSessionDataStore
try (PreparedStatement statement = connection.prepareStatement(s))
{
statement.setString(1, id); //session id
statement.setString(2, _context.getCanonicalContextPath()); //context path
String cp = _context.getCanonicalContextPath();
if (_dbAdaptor.isEmptyStringNull() && StringUtil.isBlank(cp))
cp = NULL_CONTEXT_PATH;
statement.setString(2, cp); //context path
statement.setString(3, _context.getVhost()); //first vhost
statement.setString(4, data.getLastNode());//my node id
statement.setLong(5, data.getAccessed());//accessTime
@ -845,11 +775,10 @@ public class JDBCSessionDataStore extends AbstractSessionDataStore
private void doUpdate (String id, SessionData data)
throws Exception
{
//TODO check if it is actually dirty && try to optimize the writing of lastAccessTime and expiryTime
try (Connection connection = _dbAdaptor.getConnection())
{
connection.setAutoCommit(true);
try (PreparedStatement statement = _sessionTableSchema.getUpdateSessionStatement(connection, _context.getCanonicalContextPath()))
try (PreparedStatement statement = _sessionTableSchema.getUpdateSessionStatement(connection, data.getId(), _context))
{
statement.setString(1, data.getLastNode());//should be my node id
statement.setLong(2, data.getAccessed());//accessTime
@ -865,19 +794,6 @@ public class JDBCSessionDataStore extends AbstractSessionDataStore
byte[] bytes = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
statement.setBinaryStream(7, bais, bytes.length);//attribute map as blob
if ((_context.getCanonicalContextPath() == null || "".equals(_context.getCanonicalContextPath())) && _dbAdaptor.isEmptyStringNull())
{
statement.setString(8, id);
statement.setString(9, _context.getVhost());
}
else
{
statement.setString(8, id);
statement.setString(9, _context.getCanonicalContextPath());
statement.setString(10, _context.getVhost());
}
statement.executeUpdate();
if (LOG.isDebugEnabled())
@ -968,11 +884,11 @@ public class JDBCSessionDataStore extends AbstractSessionDataStore
if (!notExpiredInDB.isEmpty())
{
//we have some sessions to check
try (PreparedStatement checkSessionExists = _sessionTableSchema.getCheckSessionExistsStatement(connection, _context.getCanonicalContextPath()))
try (PreparedStatement checkSessionExists = _sessionTableSchema.getCheckSessionExistsStatement(connection, _context))
{
for (String k: notExpiredInDB)
{
_sessionTableSchema.fillCheckSessionExistsStatement (checkSessionExists, k, _context);
checkSessionExists.setString(1, k);
try (ResultSet result = checkSessionExists.executeQuery())
{
if (!result.next())
@ -1044,9 +960,9 @@ public class JDBCSessionDataStore extends AbstractSessionDataStore
connection.setAutoCommit(true);
//non-expired session exists?
try (PreparedStatement checkSessionExists = _sessionTableSchema.getCheckSessionExistsStatement(connection, _context.getCanonicalContextPath()))
try (PreparedStatement checkSessionExists = _sessionTableSchema.getCheckSessionExistsStatement(connection, _context))
{
_sessionTableSchema.fillCheckSessionExistsStatement (checkSessionExists, id, _context);
checkSessionExists.setString(1, id);
try (ResultSet result = checkSessionExists.executeQuery())
{
if (!result.next())

View File

@ -18,11 +18,16 @@
package org.eclipse.jetty.server.session;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashSet;
import java.util.Set;
@ -87,32 +92,38 @@ public class JdbcTestHelper
*/
public static SessionDataStoreFactory newSessionDataStoreFactory()
{
JDBCSessionDataStoreFactory factory = new JDBCSessionDataStoreFactory();
DatabaseAdaptor da = new DatabaseAdaptor();
da.setDriverInfo(DRIVER_CLASS, DEFAULT_CONNECTION_URL);
factory.setDatabaseAdaptor(da);
JDBCSessionDataStore.SessionTableSchema sessionTableSchema = new JDBCSessionDataStore.SessionTableSchema();
sessionTableSchema.setTableName(TABLE);
sessionTableSchema.setIdColumn(ID_COL);
sessionTableSchema.setAccessTimeColumn(ACCESS_COL);
sessionTableSchema.setContextPathColumn(CONTEXT_COL);
sessionTableSchema.setCookieTimeColumn(COOKIE_COL);
sessionTableSchema.setCreateTimeColumn(CREATE_COL);
sessionTableSchema.setExpiryTimeColumn(EXPIRY_COL);
sessionTableSchema.setLastAccessTimeColumn(LAST_ACCESS_COL);
sessionTableSchema.setLastNodeColumn(LAST_NODE_COL);
sessionTableSchema.setLastSavedTimeColumn(LAST_SAVE_COL);
sessionTableSchema.setMapColumn(MAP_COL);
sessionTableSchema.setMaxIntervalColumn(MAX_IDLE_COL);
factory.setSessionTableSchema(sessionTableSchema);
return factory;
return newSessionDataStoreFactory(da);
}
public static SessionDataStoreFactory newSessionDataStoreFactory(DatabaseAdaptor da)
{
JDBCSessionDataStoreFactory factory = new JDBCSessionDataStoreFactory();
factory.setDatabaseAdaptor(da);
JDBCSessionDataStore.SessionTableSchema sessionTableSchema = newSessionTableSchema();
factory.setSessionTableSchema(sessionTableSchema);
return factory;
}
public static JDBCSessionDataStore.SessionTableSchema newSessionTableSchema()
{
JDBCSessionDataStore.SessionTableSchema sessionTableSchema = new JDBCSessionDataStore.SessionTableSchema();
sessionTableSchema.setTableName(TABLE);
sessionTableSchema.setIdColumn(ID_COL);
sessionTableSchema.setAccessTimeColumn(ACCESS_COL);
sessionTableSchema.setContextPathColumn(CONTEXT_COL);
sessionTableSchema.setCookieTimeColumn(COOKIE_COL);
sessionTableSchema.setCreateTimeColumn(CREATE_COL);
sessionTableSchema.setExpiryTimeColumn(EXPIRY_COL);
sessionTableSchema.setLastAccessTimeColumn(LAST_ACCESS_COL);
sessionTableSchema.setLastNodeColumn(LAST_NODE_COL);
sessionTableSchema.setLastSavedTimeColumn(LAST_SAVE_COL);
sessionTableSchema.setMapColumn(MAP_COL);
sessionTableSchema.setMaxIntervalColumn(MAX_IDLE_COL);
return sessionTableSchema;
}
public static boolean existsInSessionTable(String id, boolean verbose)
throws Exception
@ -147,6 +158,36 @@ public class JdbcTestHelper
}
public static void insertSession (String id, String contextPath, String vhost)
throws Exception
{
Class.forName(DRIVER_CLASS);
try (Connection con=DriverManager.getConnection(DEFAULT_CONNECTION_URL);)
{
PreparedStatement statement = con.prepareStatement("insert into "+TABLE+
" ("+ID_COL+", "+CONTEXT_COL+", virtualHost, "+LAST_NODE_COL+
", "+ACCESS_COL+", "+LAST_ACCESS_COL+", "+CREATE_COL+", "+COOKIE_COL+
", "+LAST_SAVE_COL+", "+EXPIRY_COL+" "+") "+
" values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
statement.setString(1, id);
statement.setString(2, contextPath);
statement.setString(3, vhost);
statement.setString(4, "0");
statement.setLong(5, System.currentTimeMillis());
statement.setLong(6, System.currentTimeMillis());
statement.setLong(7, System.currentTimeMillis());
statement.setLong(8, System.currentTimeMillis());
statement.setLong(9, System.currentTimeMillis());
statement.setLong(10, System.currentTimeMillis());
statement.execute();
assertEquals(1,statement.getUpdateCount());
}
}
public static Set<String> getSessionIds ()
throws Exception

View File

@ -0,0 +1,243 @@
//
// ========================================================================
// Copyright (c) 1995-2017 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.server.session;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* SessionTableSchemaTest
*
* Test the SessionTableSchema behaviour when the database treats "" as a NULL,
* like Oracle does.
*
*/
public class SessionTableSchemaTest
{
DatabaseAdaptor _da;
JDBCSessionDataStore.SessionTableSchema _tableSchema;
@Before
public void setUp() throws Exception
{
//pretend to be an Oracle-like database that treats "" as NULL
_da = new DatabaseAdaptor()
{
/**
* @see org.eclipse.jetty.server.session.DatabaseAdaptor#isEmptyStringNull()
*/
@Override
public boolean isEmptyStringNull()
{
return true; //test special handling for oracle
}
};
_da.setDriverInfo(JdbcTestHelper.DRIVER_CLASS, JdbcTestHelper.DEFAULT_CONNECTION_URL);
_tableSchema = JdbcTestHelper.newSessionTableSchema();
_tableSchema.setDatabaseAdaptor(_da);
}
@After
public void tearDown() throws Exception
{
JdbcTestHelper.shutdown(null);
}
@Test
public void testLoad()
throws Exception
{
//set up the db
_da.initialize();
_tableSchema.prepareTables();
//insert a fake session at the root context
JdbcTestHelper.insertSession("1234", "/", "0.0.0.0");
//test if it can be seen
try (Connection con = _da.getConnection())
{
//make a root context
ContextHandler handler = new ContextHandler();
handler.setContextPath("/");
SessionContext sc = new SessionContext("0", handler.getServletContext());
//test the load statement
PreparedStatement s = _tableSchema.getLoadStatement(con, "1234", sc);
ResultSet rs = s.executeQuery();
assertTrue(rs.next());
}
}
@Test
public void testExists()
throws Exception
{
//set up the db
_da.initialize();
_tableSchema.prepareTables();
//insert a fake session at the root context
JdbcTestHelper.insertSession("1234", "/", "0.0.0.0");
//test if it can be seen
try (Connection con = _da.getConnection())
{
ContextHandler handler = new ContextHandler();
handler.setContextPath("/");
SessionContext sc = new SessionContext("0", handler.getServletContext());
PreparedStatement s = _tableSchema.getCheckSessionExistsStatement(con, sc);
s.setString(1, "1234");
ResultSet rs = s.executeQuery();
assertTrue(rs.next());
}
}
@Test
public void testDelete()
throws Exception
{
//set up the db
_da.initialize();
_tableSchema.prepareTables();
//insert a fake session at the root context
JdbcTestHelper.insertSession("1234", "/", "0.0.0.0");
//test if it can be deleted
try (Connection con = _da.getConnection())
{
ContextHandler handler = new ContextHandler();
handler.setContextPath("/");
SessionContext sc = new SessionContext("0", handler.getServletContext());
PreparedStatement s = _tableSchema.getDeleteStatement(con, "1234", sc);
assertEquals(1,s.executeUpdate());
assertFalse(JdbcTestHelper.existsInSessionTable("1234", false));
}
}
@Test
public void testExpired()
throws Exception
{
//set up the db
_da.initialize();
_tableSchema.prepareTables();
//insert a fake session at the root context
JdbcTestHelper.insertSession("1234", "/", "0.0.0.0");
try (Connection con = _da.getConnection())
{
ContextHandler handler = new ContextHandler();
handler.setContextPath("/");
SessionContext sc = new SessionContext("0", handler.getServletContext());
PreparedStatement s = _tableSchema.getExpiredSessionsStatement(con,
sc.getCanonicalContextPath(),
sc.getVhost(),
(System.currentTimeMillis()+100L));
ResultSet rs = s.executeQuery();
assertTrue(rs.next());
assertEquals("1234", rs.getString(1));
}
}
@Test
public void testMyExpiredSessions ()
throws Exception
{
//set up the db
_da.initialize();
_tableSchema.prepareTables();
//insert a fake session at the root context
JdbcTestHelper.insertSession("1234", "/", "0.0.0.0");
try (Connection con = _da.getConnection())
{
ContextHandler handler = new ContextHandler();
handler.setContextPath("/");
SessionContext sc = new SessionContext("0", handler.getServletContext());
PreparedStatement s = _tableSchema.getMyExpiredSessionsStatement(con,
sc,
(System.currentTimeMillis()+100L));
ResultSet rs = s.executeQuery();
assertTrue(rs.next());
assertEquals("1234", rs.getString(1));
}
}
@Test
public void testUpdate()
throws Exception
{
//set up the db
_da.initialize();
_tableSchema.prepareTables();
//insert a fake session at the root context
JdbcTestHelper.insertSession("1234", "/", "0.0.0.0");
try (Connection con = _da.getConnection())
{
ContextHandler handler = new ContextHandler();
handler.setContextPath("/");
SessionContext sc = new SessionContext("0", handler.getServletContext());
PreparedStatement s = _tableSchema.getUpdateStatement(con,
"1234",
sc);
s.setString(1, "0");//should be my node id
s.setLong(2, System.currentTimeMillis());
s.setLong(3, System.currentTimeMillis());
s.setLong(4, System.currentTimeMillis());
s.setLong(5, System.currentTimeMillis());
s.setLong(6, 2000L);
byte[] bytes = new byte[3];
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
s.setBinaryStream(7, bais, bytes.length);//attribute map as blob
assertEquals(1, s.executeUpdate());
}
}
}

View File

@ -52,7 +52,7 @@ public abstract class AbstractClusteredInvalidationSessionTest extends AbstractT
@Test
public void testInvalidation() throws Exception
{
String contextPath = "";
String contextPath = "/";
String servletMapping = "/server";
int maxInactiveInterval = 30;
int scavengeInterval = 1;
@ -84,8 +84,8 @@ public abstract class AbstractClusteredInvalidationSessionTest extends AbstractT
try
{
String[] urls = new String[2];
urls[0] = "http://localhost:" + port1 + contextPath + servletMapping;
urls[1] = "http://localhost:" + port2 + contextPath + servletMapping;
urls[0] = "http://localhost:" + port1 + contextPath + servletMapping.substring(1);
urls[1] = "http://localhost:" + port2 + contextPath + servletMapping.substring(1);
// Create the session on node1
ContentResponse response1 = client.GET(urls[0] + "?action=init");

View File

@ -57,7 +57,7 @@ public abstract class AbstractClusteredLastAccessTimeTest extends AbstractTestBa
@Test
public void testLastAccessTime() throws Exception
{
String contextPath = "";
String contextPath = "/";
String servletMapping = "/server";
int maxInactivePeriod = 8; //session will timeout after 8 seconds
int scavengePeriod = 2; //scavenging occurs every 2 seconds
@ -97,7 +97,7 @@ public abstract class AbstractClusteredLastAccessTimeTest extends AbstractTestBa
try
{
// Perform one request to server1 to create a session
ContentResponse response1 = client.GET("http://localhost:" + port1 + contextPath + servletMapping + "?action=init");
ContentResponse response1 = client.GET("http://localhost:" + port1 + contextPath + servletMapping.substring(1) + "?action=init");
assertEquals(HttpServletResponse.SC_OK, response1.getStatus());
assertEquals("test", response1.getContentAsString());
String sessionCookie = response1.getHeaders().get("Set-Cookie");
@ -116,7 +116,7 @@ public abstract class AbstractClusteredLastAccessTimeTest extends AbstractTestBa
int requestInterval = 500;
for (int i = 0; i < maxInactivePeriod * (1000 / requestInterval); ++i)
{
Request request = client.newRequest("http://localhost:" + port2 + contextPath + servletMapping);
Request request = client.newRequest("http://localhost:" + port2 + contextPath + servletMapping.substring(1));
request.header("Cookie", sessionCookie);
ContentResponse response2 = request.send();
assertEquals(HttpServletResponse.SC_OK , response2.getStatus());

View File

@ -52,7 +52,7 @@ public abstract class AbstractClusteredOrphanedSessionTest extends AbstractTestB
public void testOrphanedSession() throws Exception
{
// Disable scavenging for the first server, so that we simulate its "crash".
String contextPath = "";
String contextPath = "/";
String servletMapping = "/server";
int inactivePeriod = 5;
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
@ -85,7 +85,7 @@ public abstract class AbstractClusteredOrphanedSessionTest extends AbstractTestB
try
{
// Connect to server1 to create a session and get its session cookie
ContentResponse response1 = client.GET("http://localhost:" + port1 + contextPath + servletMapping + "?action=init");
ContentResponse response1 = client.GET("http://localhost:" + port1 + contextPath + servletMapping.substring(1) + "?action=init");
assertEquals(HttpServletResponse.SC_OK,response1.getStatus());
String sessionCookie = response1.getHeaders().get("Set-Cookie");
assertTrue(sessionCookie != null);
@ -98,7 +98,7 @@ public abstract class AbstractClusteredOrphanedSessionTest extends AbstractTestB
Thread.sleep(TimeUnit.SECONDS.toMillis(inactivePeriod + 2L * scavengePeriod));
// Perform one request to server2 to be sure that the session has been expired
Request request = client.newRequest("http://localhost:" + port2 + contextPath + servletMapping + "?action=check");
Request request = client.newRequest("http://localhost:" + port2 + contextPath + servletMapping.substring(1) + "?action=check");
request.header("Cookie", sessionCookie);
ContentResponse response2 = request.send();
assertEquals(HttpServletResponse.SC_OK,response2.getStatus());

View File

@ -47,7 +47,7 @@ public abstract class AbstractClusteredSessionMigrationTest extends AbstractTest
@Test
public void testSessionMigration() throws Exception
{
String contextPath = "";
String contextPath = "/";
String servletMapping = "/server";
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
@ -79,7 +79,7 @@ public abstract class AbstractClusteredSessionMigrationTest extends AbstractTest
{
// Perform one request to server1 to create a session
int value = 1;
Request request1 = client.POST("http://localhost:" + port1 + contextPath + servletMapping + "?action=set&value=" + value);
Request request1 = client.POST("http://localhost:" + port1 + contextPath + servletMapping.substring(1) + "?action=set&value=" + value);
ContentResponse response1 = request1.send();
assertEquals(HttpServletResponse.SC_OK,response1.getStatus());
String sessionCookie = response1.getHeaders().get("Set-Cookie");
@ -89,7 +89,7 @@ public abstract class AbstractClusteredSessionMigrationTest extends AbstractTest
// Perform a request to server2 using the session cookie from the previous request
// This should migrate the session from server1 to server2.
Request request2 = client.newRequest("http://localhost:" + port2 + contextPath + servletMapping + "?action=get");
Request request2 = client.newRequest("http://localhost:" + port2 + contextPath + servletMapping.substring(1) + "?action=get");
request2.header("Cookie", sessionCookie);
ContentResponse response2 = request2.send();
assertEquals(HttpServletResponse.SC_OK,response2.getStatus());

View File

@ -82,7 +82,7 @@ public abstract class AbstractClusteredSessionScavengingTest extends AbstractTes
@Test
public void testNoScavenging() throws Exception
{
String contextPath = "";
String contextPath = "/";
String servletMapping = "/server";
int inactivePeriod = 3;
int scavengePeriod = 0;
@ -109,7 +109,7 @@ public abstract class AbstractClusteredSessionScavengingTest extends AbstractTes
client.start();
try
{
String url = "http://localhost:" + port1 + contextPath + servletMapping;
String url = "http://localhost:" + port1 + contextPath + servletMapping.substring(1);
// Create the session

View File

@ -82,7 +82,7 @@ public abstract class AbstractNonClusteredSessionScavengingTest extends Abstract
_dataStore = context.getSessionHandler().getSessionCache().getSessionDataStore();
context.addServlet(TestServlet.class, servletMapping);
String contextPath = "";
String contextPath = "/";
try
{
@ -92,7 +92,7 @@ public abstract class AbstractNonClusteredSessionScavengingTest extends Abstract
client.start();
try
{
ContentResponse response = client.GET("http://localhost:" + port + contextPath + servletMapping + "?action=create");
ContentResponse response = client.GET("http://localhost:" + port + contextPath + servletMapping.substring(1) + "?action=create");
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
String sessionCookie = response.getHeaders().get("Set-Cookie");
assertTrue(sessionCookie != null);
@ -106,7 +106,7 @@ public abstract class AbstractNonClusteredSessionScavengingTest extends Abstract
// The session should not be there anymore, but we present an old cookie
// The server should create a new session.
Request request = client.newRequest("http://localhost:" + port + contextPath + servletMapping + "?action=old-create");
Request request = client.newRequest("http://localhost:" + port + contextPath + servletMapping.substring(1) + "?action=old-create");
request.header("Cookie", sessionCookie);
response = request.send();
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
@ -139,7 +139,7 @@ public abstract class AbstractNonClusteredSessionScavengingTest extends Abstract
ServletContextHandler context = server.addContext("/");
_dataStore = context.getSessionHandler().getSessionCache().getSessionDataStore();
context.addServlet(TestServlet.class, servletMapping);
String contextPath = "";
String contextPath = "/";
try
{
@ -150,7 +150,7 @@ public abstract class AbstractNonClusteredSessionScavengingTest extends Abstract
try
{
//create an immortal session
ContentResponse response = client.GET("http://localhost:" + port + contextPath + servletMapping + "?action=create");
ContentResponse response = client.GET("http://localhost:" + port + contextPath + servletMapping.substring(1) + "?action=create");
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
String sessionCookie = response.getHeaders().get("Set-Cookie");
assertTrue(sessionCookie != null);
@ -163,7 +163,7 @@ public abstract class AbstractNonClusteredSessionScavengingTest extends Abstract
assertSession(TestServer.extractSessionId(sessionCookie), true);
// Test that the session is still there
Request request = client.newRequest("http://localhost:" + port + contextPath + servletMapping + "?action=old-test");
Request request = client.newRequest("http://localhost:" + port + contextPath + servletMapping.substring(1) + "?action=old-test");
request.header("Cookie", sessionCookie);
response = request.send();
assertEquals(HttpServletResponse.SC_OK,response.getStatus());

View File

@ -68,7 +68,7 @@ public abstract class AbstractProxySerializationTest extends AbstractTestBase
@Test
public void testProxySerialization() throws Exception
{
String contextPath = "";
String contextPath = "/";
String servletMapping = "/server";
int scavengePeriod = 10;
@ -104,7 +104,7 @@ public abstract class AbstractProxySerializationTest extends AbstractTestBase
client.start();
try
{
ContentResponse response = client.GET("http://localhost:" + port + contextPath + servletMapping + "?action=create");
ContentResponse response = client.GET("http://localhost:" + port + contextPath + servletMapping.substring(1) + "?action=create");
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
String sessionCookie = response.getHeaders().get("Set-Cookie");
assertTrue(sessionCookie != null);
@ -118,7 +118,7 @@ public abstract class AbstractProxySerializationTest extends AbstractTestBase
context.start();
// Make another request using the session id from before
Request request = client.newRequest("http://localhost:" + port + contextPath + servletMapping + "?action=test");
Request request = client.newRequest("http://localhost:" + port + contextPath + servletMapping.substring(1) + "?action=test");
request.header("Cookie", sessionCookie);
response = request.send();
assertEquals(HttpServletResponse.SC_OK,response.getStatus());

View File

@ -107,7 +107,7 @@ public abstract class AbstractSessionExpiryTest extends AbstractTestBase
@Test
public void testSessionExpiresWithListener() throws Exception
{
String contextPath = "";
String contextPath = "/";
String servletMapping = "/server";
int inactivePeriod = 3;
int scavengePeriod = 1;
@ -134,7 +134,7 @@ public abstract class AbstractSessionExpiryTest extends AbstractTestBase
{
HttpClient client = new HttpClient();
client.start();
String url = "http://localhost:" + port1 + contextPath + servletMapping;
String url = "http://localhost:" + port1 + contextPath + servletMapping.substring(1);
//make a request to set up a session on the server
ContentResponse response1 = client.GET(url + "?action=init");
@ -167,7 +167,7 @@ public abstract class AbstractSessionExpiryTest extends AbstractTestBase
@Test
public void testSessionNotExpired() throws Exception
{
String contextPath = "";
String contextPath = "/";
String servletMapping = "/server";
int inactivePeriod = 20;
int scavengePeriod = 10;
@ -190,7 +190,7 @@ public abstract class AbstractSessionExpiryTest extends AbstractTestBase
int port1 = server1.getPort();
client.start();
String url = "http://localhost:" + port1 + contextPath + servletMapping;
String url = "http://localhost:" + port1 + contextPath + servletMapping.substring(1);
//make a request to set up a session on the server
ContentResponse response = client.GET(url + "?action=init");
@ -207,7 +207,7 @@ public abstract class AbstractSessionExpiryTest extends AbstractTestBase
//start the server again, before the session times out
server1.start();
port1 = server1.getPort();
url = "http://localhost:" + port1 + contextPath + servletMapping;
url = "http://localhost:" + port1 + contextPath + servletMapping.substring(1);
//make another request, the session should not have expired
Request request = client.newRequest(url + "?action=notexpired");
@ -234,7 +234,7 @@ public abstract class AbstractSessionExpiryTest extends AbstractTestBase
{
String contextPath = "";
String contextPath = "/";
String servletMapping = "/server";
int inactivePeriod = 4;
int scavengePeriod = 1;
@ -261,7 +261,7 @@ public abstract class AbstractSessionExpiryTest extends AbstractTestBase
{
HttpClient client = new HttpClient();
client.start();
String url = "http://localhost:" + port1 + contextPath + servletMapping;
String url = "http://localhost:" + port1 + contextPath + servletMapping.substring(1);
//make a request to set up a session on the server
ContentResponse response1 = client.GET(url + "?action=init");
@ -288,7 +288,7 @@ public abstract class AbstractSessionExpiryTest extends AbstractTestBase
pause(inactivePeriod+(scavengePeriod*2));
port1 = server1.getPort();
url = "http://localhost:" + port1 + contextPath + servletMapping;
url = "http://localhost:" + port1 + contextPath + servletMapping.substring(1);
//make another request, the session should have expired
Request request = client.newRequest(url + "?action=test");
@ -310,7 +310,7 @@ public abstract class AbstractSessionExpiryTest extends AbstractTestBase
@Test
public void testRequestForSessionWithChangedTimeout () throws Exception
{
String contextPath = "";
String contextPath = "/";
String servletMapping = "/server";
int inactivePeriod = 5;
int scavengePeriod = 1;
@ -337,7 +337,7 @@ public abstract class AbstractSessionExpiryTest extends AbstractTestBase
{
HttpClient client = new HttpClient();
client.start();
String url = "http://localhost:" + port1 + contextPath + servletMapping;
String url = "http://localhost:" + port1 + contextPath + servletMapping.substring(1);
//make a request to set up a session on the server with the session manager's inactive timeout
ContentResponse response = client.GET(url + "?action=init");

View File

@ -87,7 +87,7 @@ public abstract class AbstractSessionInvalidateCreateScavengeTest extends Abstra
@Test
public void testSessionScavenge() throws Exception
{
String contextPath = "";
String contextPath = "/";
String servletMapping = "/server";
int inactivePeriod = 6;
int scavengePeriod = 3;
@ -115,7 +115,7 @@ public abstract class AbstractSessionInvalidateCreateScavengeTest extends Abstra
client.start();
try
{
String url = "http://localhost:" + port1 + contextPath + servletMapping;
String url = "http://localhost:" + port1 + contextPath + servletMapping.substring(1);
// Create the session