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 * ================================================================================
20 package org.openecomp.portalapp.service.sessionmgt;
22 import java.util.Calendar;
23 import java.util.Hashtable;
26 import javax.servlet.http.HttpSession;
28 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
29 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
30 import org.openecomp.portalsdk.core.domain.sessionmgt.TimeoutVO;
31 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
32 import org.openecomp.portalsdk.core.onboarding.crossapi.PortalApiConstants;
33 import org.springframework.context.annotation.EnableAspectJAutoProxy;
34 import org.springframework.stereotype.Service;
36 import com.fasterxml.jackson.core.type.TypeReference;
37 import com.fasterxml.jackson.databind.ObjectMapper;
41 @org.springframework.context.annotation.Configuration
42 @EnableAspectJAutoProxy
44 public class CoreTimeoutHandler{
45 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CoreTimeoutHandler.class);
47 public static final Map<String, HttpSession> sessionMap = new Hashtable<String,HttpSession>();
48 public static final Integer repeatInterval = 15 * 60; // 15 minutes
49 ObjectMapper mapper = new ObjectMapper();
52 public static void sessionCreated(String portalJSessionId, String jSessionId, HttpSession session) {
54 storeMaxInactiveTime(session);
56 // this key is a combination of portal jsession id and app session id
57 session.setAttribute(PortalApiConstants.PORTAL_JSESSION_ID,jSessionKey(jSessionId, portalJSessionId));
58 sessionMap.put((String)session.getAttribute(PortalApiConstants.PORTAL_JSESSION_ID), session);
62 protected static void storeMaxInactiveTime(HttpSession session) {
64 if(session.getAttribute(PortalApiConstants.GLOBAL_SESSION_MAX_IDLE_TIME) == null)
65 session.setAttribute(PortalApiConstants.GLOBAL_SESSION_MAX_IDLE_TIME,session.getMaxInactiveInterval());
68 public static void sessionDestroyed(HttpSession session) {
71 sessionMap.remove((String)session.getAttribute(PortalApiConstants.PORTAL_JSESSION_ID));
73 logger.error(EELFLoggerDelegate.errorLogger, "************************ Session Management: Error while destroying session for " + session.getId() + " Details: " + EcompPortalUtils.getStackTrace(e));
78 public String gatherSessionExtenstions() {
80 Map<String,TimeoutVO> sessionTimeoutMap = new Hashtable<String,TimeoutVO>();
84 for(String jSessionKey: sessionMap.keySet()) {
87 // get the expirytime in seconds
88 HttpSession session = sessionMap.get(jSessionKey);
90 Long lastAccessedTimeMilliSec = session.getLastAccessedTime();
91 Long maxIntervalMilliSec = session.getMaxInactiveInterval() * 1000L;
92 //Long currentTimeMilliSec = Calendar.getInstance().getTimeInMillis() ;
93 //(maxIntervalMilliSec - (currentTimeMilliSec - lastAccessedTimeMilliSec) + ;
94 Calendar instance = Calendar.getInstance();
95 instance.setTimeInMillis(session.getLastAccessedTime());
96 logger.info(EELFLoggerDelegate.errorLogger, "************************ Session Management: Last Accessed time for "+ jSessionKey + ": " + instance.getTime());
98 Long sessionTimOutMilliSec = maxIntervalMilliSec + lastAccessedTimeMilliSec;
100 sessionTimeoutMap.put( portalJSessionId(jSessionKey), new TimeoutVO(jSessionId(jSessionKey),sessionTimOutMilliSec));
103 jsonMap = mapper.writeValueAsString(sessionTimeoutMap);
104 } catch(Exception e) {
105 logger.error(EELFLoggerDelegate.errorLogger, "************************ Session Management: Error during JsonSessionTimout conversion. Details: " + EcompPortalUtils.getStackTrace(e));
114 public void updateSessionExtensions(String sessionTimeoutMapStr) throws Exception {
116 //Map<String,Object> sessionTimeoutMap = mapper.readValue(sessionTimeoutMapStr, Map.class);
117 Map<String,TimeoutVO> sessionTimeoutMap;
119 TypeReference<Hashtable<String,TimeoutVO>> typeRef
120 = new TypeReference<Hashtable<String,TimeoutVO>>() {};
122 sessionTimeoutMap = mapper.readValue(sessionTimeoutMapStr, typeRef);
124 logger.error(EELFLoggerDelegate.errorLogger, "************************ Session Management: Error while to parse update session extension in portal", e);
127 for(String jPortalSessionId: sessionTimeoutMap.keySet()) {
130 TimeoutVO extendedTimeoutVO = mapper.readValue(mapper.writeValueAsString(sessionTimeoutMap.get(jPortalSessionId)),TimeoutVO.class);
131 HttpSession session = sessionMap.get(jSessionKey(extendedTimeoutVO.getjSessionId(), jPortalSessionId));
133 if(session == null) {
137 Long lastAccessedTimeMilliSec = session.getLastAccessedTime();
138 Long maxIntervalMilliSec = session.getMaxInactiveInterval() * 1000L;
139 Long sessionTimOutMilliSec = maxIntervalMilliSec + lastAccessedTimeMilliSec;
141 Long maxTimeoutTimeMilliSec = extendedTimeoutVO.getSessionTimOutMilliSec();
142 if(maxTimeoutTimeMilliSec>sessionTimOutMilliSec) {
143 logger.debug(EELFLoggerDelegate.debugLogger, "************************ Session Management: " + " updated session max idle time");
144 session.setMaxInactiveInterval((int)(maxTimeoutTimeMilliSec-lastAccessedTimeMilliSec)/1000);
146 } catch (Exception e) {
147 logger.error(EELFLoggerDelegate.errorLogger, "************************ Session Management: " + EcompPortalUtils.getStackTrace(e));
154 protected static String jSessionKey(String jSessionId, String portalJSessionId) {
155 return portalJSessionId + "-" + jSessionId;
158 protected String portalJSessionId(String jSessionKey) {
159 return jSessionKey.split("-")[0];
162 protected String jSessionId(String jSessionKey) {
163 return jSessionKey.split("-")[1];