88934282a91534ccd274c1d4aa540cf948c2584d
[portal/sdk.git] /
1 /*-
2  * ================================================================================
3  * ECOMP Portal SDK
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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  * ================================================================================
19  */
20 package org.openecomp.portalsdk.core.onboarding.listener;
21
22 import java.util.HashMap;
23 import java.util.Hashtable;
24 import java.util.Map;
25
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;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.openecomp.portalsdk.core.onboarding.util.PortalApiConstants;
36
37 /**
38  * Listens to session-create and session-destroy events.
39  */
40 @WebListener
41 public class UserSessionListener implements HttpSessionListener {
42
43         private Log logger = LogFactory.getLog(getClass());
44
45         public static Map<String, HttpSession> activeSessions = new Hashtable<String, HttpSession>();
46
47         public void init(ServletConfig config) {
48         }
49
50         /**
51          * Adds sessions to the context-scoped HashMap when they begin.
52          */
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());
64         }
65
66         /**
67          * Removes sessions from the context-scoped HashMap when they expire or are
68          * invalidated.
69          */
70         public void sessionDestroyed(HttpSessionEvent event) {
71                 try {
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);
83                 }
84         }
85 }