2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.onap.portalsdk.core.onboarding.listener;
40 import java.util.HashMap;
41 import java.util.Hashtable;
44 import javax.servlet.ServletConfig;
45 import javax.servlet.ServletContext;
46 import javax.servlet.annotation.WebListener;
47 import javax.servlet.http.HttpSession;
48 import javax.servlet.http.HttpSessionEvent;
49 import javax.servlet.http.HttpSessionListener;
51 import org.apache.commons.logging.Log;
52 import org.apache.commons.logging.LogFactory;
53 import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
56 * Listens to session-create and session-destroy events.
59 public class UserSessionListener implements HttpSessionListener {
61 private Log logger = LogFactory.getLog(getClass());
63 private static Map<String, HttpSession> activeSessions = new Hashtable<>();
65 public void init(ServletConfig config) {
69 * Adds sessions to the context-scoped HashMap when they begin.
72 public void sessionCreated(HttpSessionEvent event) {
73 HttpSession session = event.getSession();
74 ServletContext context = session.getServletContext();
75 @SuppressWarnings("unchecked")
76 HashMap<String, HttpSession> activeUsers = (HashMap<String, HttpSession>) context
77 .getAttribute(PortalApiConstants.ACTIVE_USERS_NAME);
78 if (activeUsers != null)
79 activeUsers.put(session.getId(), session);
80 context.setAttribute(PortalApiConstants.ACTIVE_USERS_NAME, activeUsers);
81 activeSessions.put(session.getId(), session);
82 session.getServletContext().setAttribute(PortalApiConstants.MAX_IDLE_TIME, session.getMaxInactiveInterval());
86 * Removes sessions from the context-scoped HashMap when they expire or are
90 public void sessionDestroyed(HttpSessionEvent event) {
92 HttpSession session = event.getSession();
93 ServletContext context = session.getServletContext();
94 @SuppressWarnings("unchecked")
95 HashMap<String, HttpSession> activeUsers = (HashMap<String, HttpSession>) context
96 .getAttribute(PortalApiConstants.ACTIVE_USERS_NAME);
97 if (activeUsers != null)
98 activeUsers.remove(session.getId());
99 activeSessions.remove(session.getId());
100 PortalTimeoutHandler.sessionDestroyed(session);
101 } catch (Exception e) {
102 logger.warn(e.getMessage(), e);