2 * ================================================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
21 package org.openecomp.portalapp.portal.listener;
23 import java.util.HashMap;
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;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.context.ApplicationContext;
34 import org.springframework.util.StringUtils;
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;
44 * Listens for session-create and session-destroy events.
47 @SuppressWarnings({ "unchecked", "rawtypes" })
48 public class UserSessionListener implements HttpSessionListener {
49 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(UserSessionListener.class);
52 * Access to the database
55 SharedContextService sharedContextService;
57 public void init(ServletConfig config) {
61 * Adds sessions to the context scoped HashMap when they begin.
63 public void sessionCreated(HttpSessionEvent event) {
64 HttpSession session = event.getSession();
65 ServletContext context = session.getServletContext();
66 HashMap activeUsers = (HashMap) context.getAttribute("activeUsers");
68 activeUsers.put(session.getId(), session);
69 context.setAttribute("activeUsers", activeUsers);
70 logger.info(EELFLoggerDelegate.debugLogger, "Session Created : " + session.getId());
74 * Removes sessions from the context scoped HashMap when they expire or are
77 public void sessionDestroyed(HttpSessionEvent event) {
80 HttpSession session = event.getSession();
81 ServletContext context = session.getServletContext();
82 HashMap activeUsers = (HashMap) context.getAttribute("activeUsers");
83 activeUsers.remove(session.getId());
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());
91 // Remove any shared context entries for this session.
92 if (getSharedContextService()!=null) {
93 getSharedContextService().deleteSharedContexts(session.getId());
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);
100 logger.info(EELFLoggerDelegate.debugLogger, "Session Destroyed : " + session.getId());
102 } catch (Exception e) {
103 logger.warn(EELFLoggerDelegate.errorLogger, "Exception occurred while executing sessionDestroyed. Details: " + EcompPortalUtils.getStackTrace(e));
107 private static ApplicationContext applicationContext;
109 public static void setApplicationContext(ApplicationContext _applicationContext) {
110 applicationContext = _applicationContext;
113 public SharedContextService getSharedContextService() {
114 if(sharedContextService == null){
115 if (applicationContext != null)
116 sharedContextService = (SharedContextService)applicationContext.getBean("sharedContextService");
119 return sharedContextService;
122 public void setSharedContextService(SharedContextService sharedContextService) {
123 this.sharedContextService = sharedContextService;