2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
39 package org.onap.portalapp.portal.listener;
41 import java.util.HashMap;
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;
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;
60 * Listens for session-create and session-destroy events.
63 @SuppressWarnings({ "unchecked", "rawtypes" })
64 public class UserSessionListener implements HttpSessionListener {
65 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(UserSessionListener.class);
68 * Access to the database
71 SharedContextService sharedContextService;
73 public void init(ServletConfig config) {
77 * Adds sessions to the context scoped HashMap when they begin.
79 public void sessionCreated(HttpSessionEvent event) {
80 HttpSession session = event.getSession();
81 ServletContext context = session.getServletContext();
82 HashMap activeUsers = (HashMap) context.getAttribute("activeUsers");
84 activeUsers.put(session.getId(), session);
85 context.setAttribute("activeUsers", activeUsers);
86 logger.info(EELFLoggerDelegate.debugLogger, "Session Created : " + session.getId());
90 * Removes sessions from the context scoped HashMap when they expire or are
93 public void sessionDestroyed(HttpSessionEvent event) {
95 HttpSession session = event.getSession();
96 ServletContext context = session.getServletContext();
97 HashMap activeUsers = (HashMap) context.getAttribute("activeUsers");
98 activeUsers.remove(session.getId());
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());
106 // Remove any shared context entries for this session.
107 if (getSharedContextService() != null) {
108 getSharedContextService().deleteSharedContexts(session.getId());
110 // Clean the shared context each time a session is destroyed.
111 // TODO: move the threshold to configuration file.
112 //June2019:Commented as sharedContext is no more needed.
113 // getSharedContextService().expireSharedContexts(60 * 60 * 8);
116 logger.info(EELFLoggerDelegate.debugLogger, "Session Destroyed : " + session.getId());
118 } catch (Exception e) {
119 logger.warn(EELFLoggerDelegate.errorLogger, "sessionDestroyed failed", e);
123 private static ApplicationContext applicationContext;
125 public static void setApplicationContext(ApplicationContext _applicationContext) {
126 applicationContext = _applicationContext;
129 public SharedContextService getSharedContextService() {
130 if (sharedContextService == null) {
131 if (applicationContext != null)
132 sharedContextService = (SharedContextService) applicationContext.getBean("sharedContextService");
135 return sharedContextService;
138 public void setSharedContextService(SharedContextService sharedContextService) {
139 this.sharedContextService = sharedContextService;