public static WebFilterConfig create(FilterConfig filterConfig, Properties properties) { boolean useClient = getBoolean(filterConfig, properties, USE_CLIENT, false); validateHazelcastConfigParameters(filterConfig, properties, useClient); String clientConfigLocation = getString(filterConfig, properties, CLIENT_CONFIG_LOCATION, null); int sessionTtlSeconds = getInt(filterConfig, properties, SESSION_TTL_CONFIG, SESSION_TTL_DEFAULT_SECONDS); String configLocation = getString(filterConfig, properties, CONFIG_LOCATION, null); URL configUrl = validateAndGetConfigUrl(filterConfig.getServletContext(), useClient, configLocation, clientConfigLocation); String instanceName = getString(filterConfig, properties, INSTANCE_NAME, null); String mapName = getString(filterConfig, properties, MAP_NAME, "_web_" + filterConfig.getServletContext().getServletContextName()); boolean stickySession = getBoolean(filterConfig, properties, STICKY_SESSION_CONFIG, true); boolean shutdownOnDestroy = getBoolean(filterConfig, properties, SHUTDOWN_ON_DESTROY, true); boolean deferredWrite = getBoolean(filterConfig, properties, DEFERRED_WRITE, false); boolean useRequestParameter = getBoolean(filterConfig, properties, USE_REQUEST_PARAMETER, false); Set<String> transientAttributes = getStringSet(filterConfig, properties, TRANSIENT_ATTRIBUTES); String cookieName = getString(filterConfig, properties, COOKIE_NAME, "hazelcast.sessionId"); String cookieDomain = getString(filterConfig, properties, COOKIE_DOMAIN, null); boolean cookieSecure = getBoolean(filterConfig, properties, COOKIE_SECURE, false); boolean cookieHttpOnly = getBoolean(filterConfig, properties, COOKIE_HTTP_ONLY, false); String cookiePath = getString(filterConfig, properties, COOKIE_PATH, null); int cookieMaxAge = getInt(filterConfig, properties, COOKIE_MAX_AGE, -1); WebFilterConfig wfc = new WebFilterConfig(); wfc.useClient = useClient;
private static URL validateAndGetConfigUrl(ServletContext ctx, boolean useClient, String configLocation, String clientConfigLocation) { if (!useClient && configLocation != null) { return getConfigUrl(ctx, configLocation); } else if (useClient && clientConfigLocation != null) { return getConfigUrl(ctx, clientConfigLocation); } else { return null; } }
private String findHazelcastSessionIdFromRequest() { String hzSessionId = null; final Cookie[] cookies = getCookies(); if (cookies != null) { for (final Cookie cookie : cookies) { final String name = cookie.getName(); final String value = cookie.getValue(); if (name.equalsIgnoreCase(config.getCookieName())) { hzSessionId = value; break; } } } // if hazelcast session id is not found on the cookie and using request parameter is enabled, look into // request parameters if (hzSessionId == null && config.isUseRequestParameter()) { hzSessionId = getParameter(config.getCookieName()); } return hzSessionId; } }
static HazelcastInstance loadInstance(ClusteredSessionService sessionService, WebFilterConfig filterConfig) throws ServletException { if (filterConfig.getInstanceName() != null) { if (filterConfig.isUseClient()) { return loadExistingClient(sessionService, filterConfig.getInstanceName()); } else { return loadExistingInstance(sessionService, filterConfig.getInstanceName()); } } else { if (filterConfig.isUseClient()) { return createClient(sessionService, filterConfig); } else { return createInstance(sessionService, filterConfig); } } }
private void addSessionCookie(final HazelcastRequestWrapper req, final String sessionId) { final Cookie sessionCookie = new Cookie(config.getCookieName(), sessionId); if (!isNullOrEmptyAfterTrim(config.getCookiePath())) { path = config.getCookiePath(); } else { path = req.getContextPath(); sessionCookie.setMaxAge(config.getCookieMaxAge()); if (config.getCookieDomain() != null) { sessionCookie.setDomain(config.getCookieDomain()); if (config.isCookieHttpOnly()) { try { sessionCookie.setHttpOnly(true); sessionCookie.setSecure(config.isCookieSecure()); req.res.addCookie(sessionCookie);
private static HazelcastInstance createInstance(ClusteredSessionService sessionService, WebFilterConfig filterConfig) throws ServletException { LOGGER.info("Creating a new HazelcastInstance for session replication"); Config config; if (filterConfig.getConfigUrl() == null) { config = new XmlConfigBuilder().build(); } else { try { config = new UrlXmlConfig(filterConfig.getConfigUrl()); } catch (IOException e) { throw new ServletException(e); } } config.getMapConfig(filterConfig.getMapName()).setMaxIdleSeconds(filterConfig.getSessionTtlSeconds()); config.addListenerConfig(new ListenerConfig(new ServerLifecycleListener(sessionService))); return Hazelcast.newHazelcastInstance(config); }
private static HazelcastInstance createClient(ClusteredSessionService sessionService, WebFilterConfig filterConfig) throws ServletException { LOGGER.warning("Creating a new HazelcastClient for session replication..."); LOGGER.warning("make sure this client has access to an already running cluster..."); ClientConfig clientConfig; if (filterConfig.getConfigUrl() == null) { clientConfig = new ClientConfig(); } else { try { clientConfig = new XmlClientConfigBuilder(filterConfig.getConfigUrl()).build(); } catch (IOException e) { throw new ServletException("Failed to load client config XML file [" + filterConfig.getConfigUrl() + "]:" + e.getMessage(), e); } } if (filterConfig.isStickySession()) { clientConfig.getNetworkConfig().setConnectionAttemptLimit(1); } clientConfig.addListenerConfig(new ListenerConfig(new ClientLifecycleListener(sessionService))); return HazelcastClient.newHazelcastClient(clientConfig); }
@Override public final void init(final FilterConfig filterConfig) throws ServletException { this.config = WebFilterConfig.create(filterConfig, this.properties); // Register the WebFilter with the ServletContext so SessionListener can look it up. The name // here is WebFilter.class instead of getClass() because WebFilter can have subclasses servletContext = filterConfig.getServletContext(); servletContext.setAttribute(WEB_FILTER_ATTRIBUTE_KEY, this); clusteredSessionService = new ClusteredSessionService(this.config); if (LOGGER.isLoggable(Level.FINEST)) { LOGGER.finest(this.config.toString()); } }
private void reconnectHZInstance() throws ServletException { LOGGER.info("Retrying the connection!!"); lastConnectionTry = System.currentTimeMillis(); hazelcastInstance = HazelcastInstanceLoader.loadInstance(this, filterConfig); clusterMap = hazelcastInstance.getMap(filterConfig.getMapName()); sss = (SerializationServiceSupport) hazelcastInstance; setFailedConnection(false); LOGGER.info("Successfully Connected!"); }
private void addSessionCookie(final HazelcastRequestWrapper req, final String sessionId) { final Cookie sessionCookie = new Cookie(config.getCookieName(), sessionId); if (!isNullOrEmptyAfterTrim(config.getCookiePath())) { path = config.getCookiePath(); } else { path = req.getContextPath(); if (config.getCookieDomain() != null) { sessionCookie.setDomain(config.getCookieDomain()); if (config.isCookieHttpOnly()) { try { sessionCookie.setHttpOnly(true); sessionCookie.setSecure(config.isCookieSecure()); req.res.addCookie(sessionCookie);
private static HazelcastInstance createInstance(ClusteredSessionService sessionService, WebFilterConfig filterConfig) throws ServletException { LOGGER.info("Creating a new HazelcastInstance for session replication"); Config config; if (filterConfig.getConfigUrl() == null) { config = new XmlConfigBuilder().build(); } else { try { config = new UrlXmlConfig(filterConfig.getConfigUrl()); } catch (IOException e) { throw new ServletException(e); } } config.getMapConfig(filterConfig.getMapName()).setMaxIdleSeconds(filterConfig.getSessionTtlSeconds()); config.addListenerConfig(new ListenerConfig(new ServerLifecycleListener(sessionService))); return Hazelcast.newHazelcastInstance(config); }
static HazelcastInstance loadInstance(ClusteredSessionService sessionService, WebFilterConfig filterConfig) throws ServletException { if (filterConfig.getInstanceName() != null) { if (filterConfig.isUseClient()) { return loadExistingClient(sessionService, filterConfig.getInstanceName()); } else { return loadExistingInstance(sessionService, filterConfig.getInstanceName()); } } else { if (filterConfig.isUseClient()) { return createClient(sessionService, filterConfig); } else { return createInstance(sessionService, filterConfig); } } }
private static HazelcastInstance createClient(ClusteredSessionService sessionService, WebFilterConfig filterConfig) throws ServletException { LOGGER.warning("Creating a new HazelcastClient for session replication..."); LOGGER.warning("make sure this client has access to an already running cluster..."); ClientConfig clientConfig; if (filterConfig.getConfigUrl() == null) { clientConfig = new ClientConfig(); } else { try { clientConfig = new XmlClientConfigBuilder(filterConfig.getConfigUrl()).build(); } catch (IOException e) { throw new ServletException("Failed to load client config XML file [" + filterConfig.getConfigUrl() + "]:" + e.getMessage(), e); } } if (filterConfig.isStickySession()) { clientConfig.getNetworkConfig().setConnectionAttemptLimit(1); } clientConfig.addListenerConfig(new ListenerConfig(new ClientLifecycleListener(sessionService))); return HazelcastClient.newHazelcastClient(clientConfig); }
@Override public final void init(final FilterConfig filterConfig) throws ServletException { this.config = WebFilterConfig.create(filterConfig, this.properties); // Register the WebFilter with the ServletContext so SessionListener can look it up. The name // here is WebFilter.class instead of getClass() because WebFilter can have subclasses servletContext = filterConfig.getServletContext(); servletContext.setAttribute(WEB_FILTER_ATTRIBUTE_KEY, this); clusteredSessionService = new ClusteredSessionService(this.config); if (LOGGER.isLoggable(Level.FINEST)) { LOGGER.finest(this.config.toString()); } }
private void reconnectHZInstance() throws ServletException { LOGGER.info("Retrying the connection!!"); lastConnectionTry = System.currentTimeMillis(); hazelcastInstance = HazelcastInstanceLoader.loadInstance(this, filterConfig); clusterMap = hazelcastInstance.getMap(filterConfig.getMapName()); sss = (SerializationServiceSupport) hazelcastInstance; setFailedConnection(false); LOGGER.info("Successfully Connected!"); }
public static WebFilterConfig create(FilterConfig filterConfig, Properties properties) { boolean useClient = getBoolean(filterConfig, properties, USE_CLIENT, false); validateHazelcastConfigParameters(filterConfig, properties, useClient); String clientConfigLocation = getString(filterConfig, properties, CLIENT_CONFIG_LOCATION, null); int sessionTtlSeconds = getInt(filterConfig, properties, SESSION_TTL_CONFIG, SESSION_TTL_DEFAULT_SECONDS); String configLocation = getString(filterConfig, properties, CONFIG_LOCATION, null); URL configUrl = validateAndGetConfigUrl(filterConfig.getServletContext(), useClient, configLocation, clientConfigLocation); String instanceName = getString(filterConfig, properties, INSTANCE_NAME, null); String mapName = getString(filterConfig, properties, MAP_NAME, "_web_" + filterConfig.getServletContext().getServletContextName()); boolean stickySession = getBoolean(filterConfig, properties, STICKY_SESSION_CONFIG, true); boolean shutdownOnDestroy = getBoolean(filterConfig, properties, SHUTDOWN_ON_DESTROY, true); boolean deferredWrite = getBoolean(filterConfig, properties, DEFERRED_WRITE, false); boolean useRequestParameter = getBoolean(filterConfig, properties, USE_REQUEST_PARAMETER, false); Set<String> transientAttributes = getStringSet(filterConfig, properties, TRANSIENT_ATTRIBUTES); String cookieName = getString(filterConfig, properties, COOKIE_NAME, "hazelcast.sessionId"); String cookieDomain = getString(filterConfig, properties, COOKIE_DOMAIN, null); boolean cookieSecure = getBoolean(filterConfig, properties, COOKIE_SECURE, false); boolean cookieHttpOnly = getBoolean(filterConfig, properties, COOKIE_HTTP_ONLY, false); String cookiePath = getString(filterConfig, properties, COOKIE_PATH, null); WebFilterConfig wfc = new WebFilterConfig(); wfc.useClient = useClient; wfc.configUrl = configUrl;
private String findHazelcastSessionIdFromRequest() { String hzSessionId = null; final Cookie[] cookies = getCookies(); if (cookies != null) { for (final Cookie cookie : cookies) { final String name = cookie.getName(); final String value = cookie.getValue(); if (name.equalsIgnoreCase(config.getCookieName())) { hzSessionId = value; break; } } } // if hazelcast session id is not found on the cookie and using request parameter is enabled, look into // request parameters if (hzSessionId == null && config.isUseRequestParameter()) { hzSessionId = getParameter(config.getCookieName()); } return hzSessionId; } }
private static URL validateAndGetConfigUrl(ServletContext ctx, boolean useClient, String configLocation, String clientConfigLocation) { if (!useClient && configLocation != null) { return getConfigUrl(ctx, configLocation); } else if (useClient && clientConfigLocation != null) { return getConfigUrl(ctx, clientConfigLocation); } else { return null; } }