/** * <p>Configuration properties to connect HTTP session storage * to the Hazelcast IMDG server. * </p> * <p>Meaning of each: * <ul> * <li><b>use-client</b> Are we a Hazecastl IMDG client or server * </li> * <li><b>client-config-location</b> How to configure the client * </li> * <li><b>map-name</b> Where to save the sessions * </li> * <li><b>sticky-session</b> Can we assume that all requests from the same * user will come to this process, and make optimisations on that basis. * </li> * </ul> * </p> * * @return Properties */ @Bean public WebFilter webFilter() { Properties properties = new Properties(); properties.put("use-client", "true"); properties.put("client-config-location", "hazelcast-client.xml"); properties.put("map-name", Constants.IMAP_NAME_JSESSIONID); properties.put("sticky-session", "false"); log.info("Web properties {}" , properties); return new WebFilter(properties); }
protected HazelcastHttpSession createNewSession(HazelcastRequestWrapper requestWrapper, boolean create, String existingSessionId) { // use existing hazelcast session id for the new session only if the session info exists in the cluster boolean sessionExistsInTheCluster = sessionExistsInTheCluster(existingSessionId); if (!create && !sessionExistsInTheCluster) { return null; } String id = sessionExistsInTheCluster ? existingSessionId : generateSessionId(); if (requestWrapper.getOriginalSession(false) != null) { LOGGER.finest("Original session exists!!!"); } HttpSession originalSession = requestWrapper.getOriginalSession(true); HazelcastHttpSession hazelcastSession = createHazelcastHttpSession(id, originalSession); if (existingSessionId == null) { hazelcastSession.setClusterWideNew(true); // If the session is being created for the first time, add its initial reference in the cluster-wide map. } updateSessionMaps(id, originalSession, hazelcastSession); addSessionCookie(requestWrapper, id); return hazelcastSession; }
@Override public HazelcastHttpSession getSession(final boolean create) { hazelcastSession = readSessionFromLocal(); String hazelcastSessionId = findHazelcastSessionIdFromRequest(); if (hazelcastSession == null && !res.isCommitted() && (create || hazelcastSessionId != null)) { hazelcastSession = createNewSession(HazelcastRequestWrapper.this, create, hazelcastSessionId); } return hazelcastSession; }
void destroy(boolean invalidate) { valid = false; webFilter.getClusteredSessionService().deleteSession(id, invalidate); }
public void invalidate() { // we must invalidate hazelcast session first // invalidating original session will trigger another // invalidation as our SessionListener will be triggered. webFilter.destroySession(this, true); originalSession.invalidate(); invalidatedOriginalSessionId = originalSession.getId(); }
public void sessionDestroyed(HttpSessionEvent event) { HttpSession session = event.getSession(); ServletContext servletContext = session.getServletContext(); WebFilter webFilter = (WebFilter) servletContext.getAttribute(WebFilter.class.getName()); if (webFilter == null) { LOGGER.warning("The " + WebFilter.class.getName() + " could not be found. " + getClass().getName() + " should be paired with a " + WebFilter.class.getName() + "."); } else { webFilter.destroyOriginalSession(session); } } }
public String changeSessionId() { Method changeSessionIdMethod = getChangeSessionIdMethod(); if (changeSessionIdMethod == null) { return ""; } HttpServletRequest nonWrappedHttpServletRequest = getNonWrappedHttpServletRequest(); if (nonWrappedHttpServletRequest.getSession() == null) { throw new IllegalStateException("changeSessionId requested for request with no session"); } originalSessions.remove(nonWrappedHttpServletRequest.getSession().getId()); HazelcastHttpSession hazelcastHttpSession = getSession(false); sessions.remove(hazelcastHttpSession.getId()); hazelcastHttpSession.destroy(true); String newHazelcastSessionId = generateSessionId(); String newJSessionId = invokeChangeSessionId(nonWrappedHttpServletRequest, changeSessionIdMethod); HttpSession originalSession = nonWrappedHttpServletRequest.getSession(); HazelcastHttpSession hazelcastSession = createHazelcastHttpSession(newHazelcastSessionId, originalSession); hazelcastSession.setClusterWideNew(true); updateSessionMaps(newJSessionId, hazelcastSession); addSessionCookie(this, newHazelcastSessionId); return newJSessionId; }
void destroy(boolean invalidate) { valid = false; webFilter.getClusteredSessionService().deleteSession(id, invalidate); }
public void invalidate() { // we must invalidate hazelcast session first // invalidating original session will trigger another // invalidation as our SessionListener will be triggered. webFilter.destroySession(this, true); originalSession.invalidate(); invalidatedOriginalSessionId = originalSession.getId(); }
public void sessionDestroyed(HttpSessionEvent event) { HttpSession session = event.getSession(); ServletContext servletContext = session.getServletContext(); WebFilter webFilter = (WebFilter) servletContext.getAttribute(WebFilter.class.getName()); if (webFilter == null) { LOGGER.warning("The " + WebFilter.class.getName() + " could not be found. " + getClass().getName() + " should be paired with a " + WebFilter.class.getName() + "."); } else { webFilter.destroyOriginalSession(session); } } }
protected HazelcastHttpSession createNewSession(HazelcastRequestWrapper requestWrapper, boolean create, String existingSessionId) { // use existing hazelcast session id for the new session only if the session info exists in the cluster boolean sessionExistsInTheCluster = sessionExistsInTheCluster(existingSessionId); if (!create && !sessionExistsInTheCluster) { return null; } String id = sessionExistsInTheCluster ? existingSessionId : generateSessionId(); if (requestWrapper.getOriginalSession(false) != null) { LOGGER.finest("Original session exists!!!"); } HttpSession originalSession = requestWrapper.getOriginalSession(true); HazelcastHttpSession hazelcastSession = createHazelcastHttpSession(id, originalSession); if (existingSessionId == null) { hazelcastSession.setClusterWideNew(true); // If the session is being created for the first time, add its initial reference in the cluster-wide map. } updateSessionMaps(originalSession.getId(), hazelcastSession); addSessionCookie(requestWrapper, id); return hazelcastSession; }
private Set<String> selectKeys() { Set<String> keys = new HashSet<String>(); if (!deferredWrite) { Set<String> attributeNames = null; try { attributeNames = webFilter.getClusteredSessionService().getAttributeNames(id); } catch (Exception ignored) { for (Map.Entry<String, LocalCacheEntry> entry : localCache.entrySet()) { if (!entry.getValue().isRemoved() && entry.getValue().getValue() != null) { keys.add(entry.getKey()); } } } if (attributeNames != null) { keys.addAll(attributeNames); } } else { for (Map.Entry<String, LocalCacheEntry> entry : localCache.entrySet()) { if (!entry.getValue().isRemoved() && entry.getValue().getValue() != null) { keys.add(entry.getKey()); } } } return keys; }
void destroyOriginalSession(HttpSession originalSession) { String hazelcastSessionId = originalSessions.remove(originalSession.getId()); if (hazelcastSessionId != null) { HazelcastHttpSession hazelSession = sessions.get(hazelcastSessionId); if (hazelSession != null) { destroySession(hazelSession, false); } } }
@Override public HazelcastHttpSession getSession(final boolean create) { hazelcastSession = readSessionFromLocal(); String hazelcastSessionId = findHazelcastSessionIdFromRequest(); if (hazelcastSession == null && !res.isCommitted() && (create || hazelcastSessionId != null)) { hazelcastSession = createNewSession(HazelcastRequestWrapper.this, create, hazelcastSessionId); } return hazelcastSession; }
/** * Create a web filter. Parameterize this with two properties, * * <ol> * <li><i>instance-name</i> * Direct the web filter to use the existing Hazelcast instance rather than * to create a new one.</li> * <li><i>sticky-session</i> * As the HTTP session will be accessed from multiple processes, deactivate * the optimization that assumes each user's traffic is routed to the same * process for that user.</li> * </ol> * * Spring will assume dispatcher types of {@code FORWARD}, {@code INCLUDE} * and {@code REQUEST}, and a context pattern of "{@code /*}". * * @param hazelcastInstance Created by Spring * @return The web filter for Tomcat */ @Bean public WebFilter webFilter(HazelcastInstance hazelcastInstance) { Properties properties = new Properties(); properties.put("instance-name", hazelcastInstance.getName()); properties.put("sticky-session", "false"); return new WebFilter(properties); } }
private Set<String> selectKeys() { Set<String> keys = new HashSet<String>(); if (!deferredWrite) { Set<String> attributeNames = null; try { attributeNames = webFilter.getClusteredSessionService().getAttributeNames(id); } catch (Exception ignored) { for (Map.Entry<String, LocalCacheEntry> entry : localCache.entrySet()) { if (!entry.getValue().isRemoved() && entry.getValue().getValue() != null) { keys.add(entry.getKey()); } } } if (attributeNames != null) { keys.addAll(attributeNames); } } else { for (Map.Entry<String, LocalCacheEntry> entry : localCache.entrySet()) { if (!entry.getValue().isRemoved() && entry.getValue().getValue() != null) { keys.add(entry.getKey()); } } } return keys; }
void destroyOriginalSession(HttpSession originalSession) { String hazelcastSessionId = originalSessions.remove(originalSession.getId()); if (hazelcastSessionId != null) { HazelcastHttpSession hazelSession = sessions.get(hazelcastSessionId); if (hazelSession != null) { destroySession(hazelSession, false); } } }
boolean create, String existingSessionId) { HazelcastHttpSession session = super.createNewSession(requestWrapper, create, existingSessionId); ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
void sessionDeferredWrite() { if (sessionChanged() || isNew()) { Map<String, Object> updates = new HashMap<String, Object>(); Iterator<Map.Entry<String, LocalCacheEntry>> iterator = localCache.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<String, LocalCacheEntry> entry = iterator.next(); LocalCacheEntry cacheEntry = entry.getValue(); if (cacheEntry.isDirty() && !cacheEntry.isTransient()) { if (cacheEntry.isRemoved()) { updates.put(entry.getKey(), null); } else { updates.put(entry.getKey(), cacheEntry.getValue()); } cacheEntry.setDirty(false); } } try { webFilter.getClusteredSessionService().updateAttributes(id, updates); } catch (Exception ignored) { EmptyStatement.ignore(ignored); } } }
private HazelcastHttpSession getSessionWithId(final String sessionId) { HazelcastHttpSession session = sessions.get(sessionId); if (session != null && !session.isValid()) { destroySession(session, true); session = null; } return session; }