aa97d0b396948383a107ed751b4e4a067269faff
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / listener / UserSessionListener.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 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  * 
37  */
38
39 package org.onap.portalapp.portal.listener;
40
41 import java.util.HashMap;
42
43 import javax.servlet.ServletConfig;
44 import javax.servlet.ServletContext;
45 import javax.servlet.annotation.WebListener;
46 import javax.servlet.http.HttpSession;
47 import javax.servlet.http.HttpSessionEvent;
48 import javax.servlet.http.HttpSessionListener;
49
50 import org.onap.portalapp.portal.domain.EPUser;
51 import org.onap.portalapp.portal.service.SharedContextService;
52 import org.onap.portalsdk.core.domain.support.CollaborateList;
53 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
54 import org.onap.portalsdk.core.util.SystemProperties;
55 import org.springframework.beans.factory.annotation.Autowired;
56 import org.springframework.context.ApplicationContext;
57 import org.springframework.util.StringUtils;
58
59 /**
60  * Listens for session-create and session-destroy events.
61  */
62 @WebListener
63 @SuppressWarnings({ "unchecked", "rawtypes" })
64 public class UserSessionListener implements HttpSessionListener {
65         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(UserSessionListener.class);
66
67         /**
68          * Access to the database
69          */
70         @Autowired
71         SharedContextService sharedContextService;
72
73         public void init(ServletConfig config) {
74         }
75
76         /**
77          * Adds sessions to the context scoped HashMap when they begin.
78          */
79         public void sessionCreated(HttpSessionEvent event) {
80                 HttpSession session = event.getSession();
81                 ServletContext context = session.getServletContext();
82                 HashMap activeUsers = (HashMap) context.getAttribute("activeUsers");
83
84                 activeUsers.put(session.getId(), session);
85                 context.setAttribute("activeUsers", activeUsers);
86                 logger.info(EELFLoggerDelegate.debugLogger, "Session Created : " + session.getId());
87         }
88
89         /**
90          * Removes sessions from the context scoped HashMap when they expire or are
91          * invalidated.
92          */
93         public void sessionDestroyed(HttpSessionEvent event) {
94                 try {
95                         HttpSession session = event.getSession();
96                         ServletContext context = session.getServletContext();
97                         HashMap activeUsers = (HashMap) context.getAttribute("activeUsers");
98                         activeUsers.remove(session.getId());
99
100                         EPUser user = (EPUser) session
101                                         .getAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME));
102                         if (user != null && !StringUtils.isEmpty(user.getOrgUserId())) {
103                                 CollaborateList.delUserName(user.getOrgUserId());
104                         }
105
106                         // Remove any shared context entries for this session.
107                         if (getSharedContextService() != null) {
108                                 getSharedContextService().deleteSharedContexts(session.getId());
109
110                                 // Clean the shared context each time a session is destroyed.
111                                 // TODO: move the threshold to configuration file.
112                                 getSharedContextService().expireSharedContexts(60 * 60 * 8);
113                         }
114
115                         logger.info(EELFLoggerDelegate.debugLogger, "Session Destroyed : " + session.getId());
116
117                 } catch (Exception e) {
118                         logger.warn(EELFLoggerDelegate.errorLogger, "sessionDestroyed failed", e);
119                 }
120         }
121
122         private static ApplicationContext applicationContext;
123
124         public static void setApplicationContext(ApplicationContext _applicationContext) {
125                 applicationContext = _applicationContext;
126         }
127
128         public SharedContextService getSharedContextService() {
129                 if (sharedContextService == null) {
130                         if (applicationContext != null)
131                                 sharedContextService = (SharedContextService) applicationContext.getBean("sharedContextService");
132                 }
133
134                 return sharedContextService;
135         }
136
137         public void setSharedContextService(SharedContextService sharedContextService) {
138                 this.sharedContextService = sharedContextService;
139         }
140 }