Skip to content

Commit

Permalink
Issue #1591
Browse files Browse the repository at this point in the history
  • Loading branch information
janbartel authored and joakime committed Jul 29, 2017
1 parent 40b5150 commit aadbb2d
Show file tree
Hide file tree
Showing 12 changed files with 405 additions and 205 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -87,32 +92,38 @@ public static void shutdown (String connectionUrl)
*/
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
Expand Down Expand Up @@ -147,6 +158,36 @@ public static boolean existsInSessionTable(String id, boolean verbose)
}


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
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -84,8 +84,8 @@ public void testInvalidation() throws Exception
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");
Expand Down

0 comments on commit aadbb2d

Please sign in to comment.