bf1732a0219a10870997831469d947d8e30513f9
[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         private static Map<String, HttpSession> activeSessions = new Hashtable<>();
64
65         public void init(ServletConfig config) {
66         }
67
68         /**
69          * Adds sessions to the context-scoped HashMap when they begin.
70          */
71         @Override
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());
83         }
84
85         /**
86          * Removes sessions from the context-scoped HashMap when they expire or are
87          * invalidated.
88          */
89         @Override
90         public void sessionDestroyed(HttpSessionEvent event) {
91                 try {
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);
103                 }
104         }
105 }