/** * {@inheritDoc} */ @Override public boolean removeSession(final HttpSessionImpl session) { File file = new File(getSessionStoragePath(session.getId())); return file.delete(); }
/** * {@inheritDoc} */ @Override public void persistSession(final HttpSessionImpl session) throws IOException { if (!isSessionIdValid(session.getId())) { throw new IllegalArgumentException("Session ID can not be empty and must be composed of 32 characters"); } File file = new File(getSessionStoragePath(session.getId())); if (!file.exists() && !file.createNewFile()) { throw new IOException("Unable to create new file " + file.getAbsolutePath()); } writeSession(session, file); LOGGER.log(Level.FINE, "Persisted session {0} in {1}", new Object[]{session.getId(), tempPath}); }
/** * Creates a new session. * * @return */ public HttpSessionImpl createNewSession() { HttpSessionImpl session = new HttpSessionImpl(StringUtilities.generateRandom(), System.currentTimeMillis()); session.setServletContext(this); LOGGER.log(Level.FINE, "Created a new session {0}", new Object[]{session.getId()}); return session; }
/** * Handles session storage/invalidation, sets session cookies. * * @param session * @param response * @throws IOException */ public void handleSession(final HttpSessionImpl session, final HttpServletResponseImpl response) throws IOException { Cookie cookie = new Cookie(HttpSessionImpl.COOKIE_NAME, ""); if (session.isInvalidated()) { cookie.setMaxAge(MAX_AGE_IN_PAST); sessionStorage.removeSession(session); LOGGER.log(Level.FINE, "Invalidated session {0}", new Object[]{session.getId()}); } else { cookie.setValue(session.getId()); sessionStorage.persistSession(session); } response.addCookie(cookie); }
/** * Gets session for the given id. * * @param id * @return */ public HttpSessionImpl getSession(final String id) { HttpSessionImpl session = null; try { session = sessionStorage.getSession(id); if (session != null) { session.setServletContext(this); if (isSessionExpired(session)) { sessionStorage.removeSession(session); LOGGER.log(Level.FINE, "Removed expired session {0}", new Object[]{session.getId()}); session = null; } } } catch (IOException e) { } return session; }
@Test public void shouldReturnTheSameValuesByNull() { assertThat(session.getId(), is("123")); session.setLastAccessedTime(3344l); assertThat(session.getLastAccessedTime(), is(3344l)); session.setMaxInactiveInterval(6677); assertThat(session.getMaxInactiveInterval(), is(6677)); ServletContext servletContext = mock(ServletContext.class); session.setServletContext(servletContext); assertThat(session.getServletContext(), is(servletContext)); }