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