95b2d44e0166cab2f802bad070be2fc6f68ae8ab
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalsdk.core.onboarding.listener;
39
40 import java.util.HashMap;
41 import java.util.Hashtable;
42 import java.util.Map;
43
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;
50
51 import org.apache.commons.logging.Log;
52 import org.apache.commons.logging.LogFactory;
53 import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
54
55 /**
56  * Listens to session-create and session-destroy events.
57  */
58 @WebListener
59 public class UserSessionListener implements HttpSessionListener {
60
61         private Log logger = LogFactory.getLog(getClass());
62
63         public static Map<String, HttpSession> activeSessions = new Hashtable<String, HttpSession>();
64
65         public void init(ServletConfig config) {
66         }
67
68         /**
69          * Adds sessions to the context-scoped HashMap when they begin.
70          */
71         public void sessionCreated(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.put(session.getId(), session);
79                 context.setAttribute(PortalApiConstants.ACTIVE_USERS_NAME, activeUsers);
80                 activeSessions.put(session.getId(), session);
81                 session.getServletContext().setAttribute(PortalApiConstants.MAX_IDLE_TIME, session.getMaxInactiveInterval());
82         }
83
84         /**
85          * Removes sessions from the context-scoped HashMap when they expire or are
86          * invalidated.
87          */
88         public void sessionDestroyed(HttpSessionEvent event) {
89                 try {
90                         HttpSession session = event.getSession();
91                         ServletContext context = session.getServletContext();
92                         @SuppressWarnings("unchecked")
93                         HashMap<String, HttpSession> activeUsers = (HashMap<String, HttpSession>) context
94                                         .getAttribute(PortalApiConstants.ACTIVE_USERS_NAME);
95                         if (activeUsers != null)
96                                 activeUsers.remove(session.getId());
97                         activeSessions.remove(session.getId());
98                         PortalTimeoutHandler.sessionDestroyed(session);
99                 } catch (Exception e) {
100                         logger.warn(e.getMessage(), e);
101                 }
102         }
103 }