2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 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============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.openecomp.portalapp.service.sessionmgt;
40 import java.util.Calendar;
41 import java.util.Hashtable;
44 import javax.servlet.http.HttpSession;
46 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
47 import org.openecomp.portalsdk.core.domain.sessionmgt.TimeoutVO;
48 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
49 import org.openecomp.portalsdk.core.onboarding.util.PortalApiConstants;
50 import org.springframework.context.annotation.EnableAspectJAutoProxy;
51 import org.springframework.stereotype.Service;
53 import com.fasterxml.jackson.core.type.TypeReference;
54 import com.fasterxml.jackson.databind.ObjectMapper;
57 @org.springframework.context.annotation.Configuration
58 @EnableAspectJAutoProxy
60 public class CoreTimeoutHandler {
61 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CoreTimeoutHandler.class);
63 public static final Map<String, HttpSession> sessionMap = new Hashtable<String, HttpSession>();
64 public static final Integer repeatInterval = 15 * 60; // 15 minutes
65 ObjectMapper mapper = new ObjectMapper();
67 public static void sessionCreated(String portalJSessionId, String jSessionId, HttpSession session) {
69 storeMaxInactiveTime(session);
71 // this key is a combination of portal jsession id and app session id
72 session.setAttribute(PortalApiConstants.PORTAL_JSESSION_ID, jSessionKey(jSessionId, portalJSessionId));
73 sessionMap.put((String) session.getAttribute(PortalApiConstants.PORTAL_JSESSION_ID), session);
77 protected static void storeMaxInactiveTime(HttpSession session) {
79 if (session.getAttribute(PortalApiConstants.GLOBAL_SESSION_MAX_IDLE_TIME) == null)
80 session.setAttribute(PortalApiConstants.GLOBAL_SESSION_MAX_IDLE_TIME, session.getMaxInactiveInterval());
83 public static void sessionDestroyed(HttpSession session) {
86 sessionMap.remove((String) session.getAttribute(PortalApiConstants.PORTAL_JSESSION_ID));
87 } catch (Exception e) {
88 logger.error(EELFLoggerDelegate.errorLogger, "sessionDestroyed failed on session " + session.getId(), e);
93 public String gatherSessionExtenstions() {
95 Map<String, TimeoutVO> sessionTimeoutMap = new Hashtable<String, TimeoutVO>();
98 for (String jSessionKey : sessionMap.keySet()) {
101 // get the expirytime in seconds
102 HttpSession session = sessionMap.get(jSessionKey);
104 Long lastAccessedTimeMilliSec = session.getLastAccessedTime();
105 Long maxIntervalMilliSec = session.getMaxInactiveInterval() * 1000L;
106 // Long currentTimeMilliSec = Calendar.getInstance().getTimeInMillis() ;
107 // (maxIntervalMilliSec - (currentTimeMilliSec - lastAccessedTimeMilliSec) + ;
108 Calendar instance = Calendar.getInstance();
109 instance.setTimeInMillis(session.getLastAccessedTime());
110 logger.info(EELFLoggerDelegate.errorLogger,
111 "gatherSessionExtenstions: Session Management: Last Accessed time for " + jSessionKey + ": "
112 + instance.getTime());
114 Long sessionTimOutMilliSec = maxIntervalMilliSec + lastAccessedTimeMilliSec;
116 sessionTimeoutMap.put(portalJSessionId(jSessionKey),
117 new TimeoutVO(jSessionId(jSessionKey), sessionTimOutMilliSec));
119 jsonMap = mapper.writeValueAsString(sessionTimeoutMap);
120 } catch (Exception e) {
121 logger.error(EELFLoggerDelegate.errorLogger, "gatherSessionExtenstions failed", e);
129 public void updateSessionExtensions(String sessionTimeoutMapStr) throws Exception {
131 Map<String, TimeoutVO> sessionTimeoutMap;
133 TypeReference<Hashtable<String, TimeoutVO>> typeRef = new TypeReference<Hashtable<String, TimeoutVO>>() {
136 sessionTimeoutMap = mapper.readValue(sessionTimeoutMapStr, typeRef);
137 } catch (Exception e) {
138 logger.error(EELFLoggerDelegate.errorLogger, "updateSessionExtensions failed 1", e);
141 for (String jPortalSessionId : sessionTimeoutMap.keySet()) {
144 TimeoutVO extendedTimeoutVO = mapper
145 .readValue(mapper.writeValueAsString(sessionTimeoutMap.get(jPortalSessionId)), TimeoutVO.class);
146 HttpSession session = sessionMap.get(jSessionKey(extendedTimeoutVO.getjSessionId(), jPortalSessionId));
148 if (session == null) {
152 Long lastAccessedTimeMilliSec = session.getLastAccessedTime();
153 Long maxIntervalMilliSec = session.getMaxInactiveInterval() * 1000L;
154 Long sessionTimOutMilliSec = maxIntervalMilliSec + lastAccessedTimeMilliSec;
156 Long maxTimeoutTimeMilliSec = extendedTimeoutVO.getSessionTimOutMilliSec();
157 if (maxTimeoutTimeMilliSec > sessionTimOutMilliSec) {
158 logger.debug(EELFLoggerDelegate.debugLogger,
159 "updateSessionExtensions: Session Management: updated session max idle time");
160 session.setMaxInactiveInterval((int) (maxTimeoutTimeMilliSec - lastAccessedTimeMilliSec) / 1000);
162 } catch (Exception e) {
163 logger.error(EELFLoggerDelegate.errorLogger,
164 "updateSessionExtensions failed", e);
171 protected static String jSessionKey(String jSessionId, String portalJSessionId) {
172 return portalJSessionId + "-" + jSessionId;
175 protected String portalJSessionId(String jSessionKey) {
176 return jSessionKey.split("-")[0];
179 protected String jSessionId(String jSessionKey) {
180 return jSessionKey.split("-")[1];