@Override protected Serializable doCreate(final Session session) { final Serializable sessionId = jdbcSessionSqlDao.inTransaction(new Transaction<Long, JDBCSessionSqlDao>() { @Override public Long inTransaction(final JDBCSessionSqlDao transactional, final TransactionStatus status) throws Exception { transactional.create(new SessionModelDao(session)); return transactional.getLastInsertId(); } }); assignSessionId(session, sessionId); return sessionId; }
@Override protected void doDelete(final Session session) { jdbcSessionSqlDao.delete(new SessionModelDao(session)); }
@Override public Long inTransaction(final JDBCSessionSqlDao transactional, final TransactionStatus status) throws Exception { transactional.create(new SessionModelDao(session)); return transactional.getLastInsertId(); } });
@Test(groups = "slow") public void testCRUD() throws Exception { // Note! We are testing the do* methods here to bypass the caching layer final JDBCSessionDao jdbcSessionDao = new JDBCSessionDao((DBI) dbi); // Retrieve final SimpleSession session = createSession(); Assert.assertNull(jdbcSessionDao.doReadSession(session.getId())); // Create final Serializable sessionId = jdbcSessionDao.doCreate(session); final Session retrievedSession = jdbcSessionDao.doReadSession(sessionId); Assert.assertEquals(retrievedSession, session); // Update final String newHost = UUID.randomUUID().toString(); Assert.assertNotEquals(retrievedSession.getHost(), newHost); session.setHost(newHost); jdbcSessionDao.doUpdate(session); Assert.assertEquals(jdbcSessionDao.doReadSession(sessionId).getHost(), newHost); // Delete jdbcSessionDao.doDelete(session); Assert.assertNull(jdbcSessionDao.doReadSession(session.getId())); }
@Test(groups = "fast") public void testRoundTrip() throws Exception { final SimpleSession simpleSession = new SimpleSession(); simpleSession.setStartTimestamp(new Date(System.currentTimeMillis() - 5000)); simpleSession.setLastAccessTime(new Date(System.currentTimeMillis())); simpleSession.setTimeout(493934L); simpleSession.setHost(UUID.randomUUID().toString()); simpleSession.setAttribute(UUID.randomUUID(), Short.MIN_VALUE); simpleSession.setAttribute(UUID.randomUUID(), Integer.MIN_VALUE); simpleSession.setAttribute(UUID.randomUUID(), Long.MIN_VALUE); simpleSession.setAttribute(UUID.randomUUID().toString(), UUID.randomUUID().toString()); // Test with Serializable objects simpleSession.setAttribute(UUID.randomUUID().toString(), UUID.randomUUID()); simpleSession.setAttribute(UUID.randomUUID().toString(), new Date(1242)); final SessionModelDao sessionModelDao = new SessionModelDao(simpleSession); Assert.assertEquals(sessionModelDao.getTimeout(), simpleSession.getTimeout()); Assert.assertEquals(sessionModelDao.getHost(), simpleSession.getHost()); Assert.assertTrue(sessionModelDao.getSessionData().length > 0); final Session retrievedSession = sessionModelDao.toSimpleSession(); Assert.assertEquals(retrievedSession, simpleSession); } }
@Override protected void doUpdate(final Session session) { jdbcSessionSqlDao.update(new SessionModelDao(session)); }
@Override protected Session doReadSession(final Serializable sessionId) { final SessionModelDao sessionModelDao = jdbcSessionSqlDao.read(sessionId); if (sessionModelDao == null) { return null; } try { return sessionModelDao.toSimpleSession(); } catch (IOException e) { log.warn("Corrupted cookie", e); return null; } } }
@Override public JDBCSessionDao get() { final JDBCSessionDao jdbcSessionDao = new JDBCSessionDao(dbi); if (sessionManager instanceof DefaultSessionManager) { final DefaultSessionManager defaultSessionManager = (DefaultSessionManager) sessionManager; defaultSessionManager.setSessionDAO(jdbcSessionDao); defaultSessionManager.setGlobalSessionTimeout(rbacConfig.getGlobalSessionTimeout().getMillis()); } return jdbcSessionDao; } }
public SessionModelDao(final Session session) { this.recordId = (Long) session.getId(); this.startTimestamp = new DateTime(session.getStartTimestamp(), DateTimeZone.UTC); this.lastAccessTime = new DateTime(session.getLastAccessTime(), DateTimeZone.UTC); this.timeout = session.getTimeout(); this.host = session.getHost(); try { this.sessionData = serializeSessionData(session); } catch (IOException e) { this.sessionData = new byte[]{}; } }