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