2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ================================================================================
20 package org.openecomp.portalsdk.core.onboarding.listener;
22 import java.util.HashMap;
23 import java.util.Hashtable;
26 import javax.servlet.ServletConfig;
27 import javax.servlet.ServletContext;
28 import javax.servlet.annotation.WebListener;
29 import javax.servlet.http.HttpSession;
30 import javax.servlet.http.HttpSessionEvent;
31 import javax.servlet.http.HttpSessionListener;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.openecomp.portalsdk.core.onboarding.util.PortalApiConstants;
38 * Listens to session-create and session-destroy events.
41 public class UserSessionListener implements HttpSessionListener {
43 private Log logger = LogFactory.getLog(getClass());
45 public static Map<String, HttpSession> activeSessions = new Hashtable<String, HttpSession>();
47 public void init(ServletConfig config) {
51 * Adds sessions to the context-scoped HashMap when they begin.
53 public void sessionCreated(HttpSessionEvent event) {
54 HttpSession session = event.getSession();
55 ServletContext context = session.getServletContext();
56 @SuppressWarnings("unchecked")
57 HashMap<String, HttpSession> activeUsers = (HashMap<String, HttpSession>) context
58 .getAttribute(PortalApiConstants.ACTIVE_USERS_NAME);
59 if (activeUsers != null)
60 activeUsers.put(session.getId(), session);
61 context.setAttribute(PortalApiConstants.ACTIVE_USERS_NAME, activeUsers);
62 activeSessions.put(session.getId(), session);
63 session.getServletContext().setAttribute(PortalApiConstants.MAX_IDLE_TIME, session.getMaxInactiveInterval());
67 * Removes sessions from the context-scoped HashMap when they expire or are
70 public void sessionDestroyed(HttpSessionEvent event) {
72 HttpSession session = event.getSession();
73 ServletContext context = session.getServletContext();
74 @SuppressWarnings("unchecked")
75 HashMap<String, HttpSession> activeUsers = (HashMap<String, HttpSession>) context
76 .getAttribute(PortalApiConstants.ACTIVE_USERS_NAME);
77 if (activeUsers != null)
78 activeUsers.remove(session.getId());
79 activeSessions.remove(session.getId());
80 PortalTimeoutHandler.sessionDestroyed(session);
81 } catch (Exception e) {
82 logger.warn(e.getMessage(), e);