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.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;
43 * Listens for session-create and session-destroy events.
46 @SuppressWarnings({ "unchecked", "rawtypes" })
47 public class UserSessionListener implements HttpSessionListener {
48 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(UserSessionListener.class);
51 * Access to the database
54 SharedContextService sharedContextService;
56 public void init(ServletConfig config) {
60 * Adds sessions to the context scoped HashMap when they begin.
62 public void sessionCreated(HttpSessionEvent event) {
63 HttpSession session = event.getSession();
64 ServletContext context = session.getServletContext();
65 HashMap activeUsers = (HashMap) context.getAttribute("activeUsers");
67 activeUsers.put(session.getId(), session);
68 context.setAttribute("activeUsers", activeUsers);
69 logger.info(EELFLoggerDelegate.debugLogger, "Session Created : " + session.getId());
73 * Removes sessions from the context scoped HashMap when they expire or are
76 public void sessionDestroyed(HttpSessionEvent event) {
79 HttpSession session = event.getSession();
80 ServletContext context = session.getServletContext();
81 HashMap activeUsers = (HashMap) context.getAttribute("activeUsers");
82 activeUsers.remove(session.getId());
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());
90 // Remove any shared context entries for this session.
91 if (getSharedContextService()!=null) {
92 getSharedContextService().deleteSharedContexts(session.getId());
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);
99 logger.info(EELFLoggerDelegate.debugLogger, "Session Destroyed : " + session.getId());
101 } catch (Exception e) {
102 logger.warn(EELFLoggerDelegate.errorLogger, "Exception occurred while executing sessionDestroyed. Details: " + EcompPortalUtils.getStackTrace(e));
106 private static ApplicationContext applicationContext;
108 public static void setApplicationContext(ApplicationContext _applicationContext) {
109 applicationContext = _applicationContext;
112 public SharedContextService getSharedContextService() {
113 if(sharedContextService == null){
114 if (applicationContext != null)
115 sharedContextService = (SharedContextService)applicationContext.getBean("sharedContextService");
118 return sharedContextService;
121 public void setSharedContextService(SharedContextService sharedContextService) {
122 this.sharedContextService = sharedContextService;