152c68b113d7341a39a72e5839a6ddf4f74317a8
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / service / sessionmgt / CoreTimeoutHandler.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.openecomp.portalapp.service.sessionmgt;
39
40 import java.util.Calendar;
41 import java.util.Hashtable;
42 import java.util.Map;
43
44 import javax.servlet.http.HttpSession;
45
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;
52
53 import com.fasterxml.jackson.core.type.TypeReference;
54 import com.fasterxml.jackson.databind.ObjectMapper;
55
56 @Service
57 @org.springframework.context.annotation.Configuration
58 @EnableAspectJAutoProxy
59 @EPMetricsLog
60 public class CoreTimeoutHandler {
61         private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CoreTimeoutHandler.class);
62
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();
66
67         public static void sessionCreated(String portalJSessionId, String jSessionId, HttpSession session) {
68
69                 storeMaxInactiveTime(session);
70
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);
74
75         }
76
77         protected static void storeMaxInactiveTime(HttpSession session) {
78
79                 if (session.getAttribute(PortalApiConstants.GLOBAL_SESSION_MAX_IDLE_TIME) == null)
80                         session.setAttribute(PortalApiConstants.GLOBAL_SESSION_MAX_IDLE_TIME, session.getMaxInactiveInterval());
81         }
82
83         public static void sessionDestroyed(HttpSession session) {
84
85                 try {
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);
89                 }
90
91         }
92
93         public String gatherSessionExtenstions() {
94
95                 Map<String, TimeoutVO> sessionTimeoutMap = new Hashtable<String, TimeoutVO>();
96                 String jsonMap = "";
97
98                 for (String jSessionKey : sessionMap.keySet()) {
99
100                         try {
101                                 // get the expirytime in seconds
102                                 HttpSession session = sessionMap.get(jSessionKey);
103
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());
113
114                                 Long sessionTimOutMilliSec = maxIntervalMilliSec + lastAccessedTimeMilliSec;
115
116                                 sessionTimeoutMap.put(portalJSessionId(jSessionKey),
117                                                 new TimeoutVO(jSessionId(jSessionKey), sessionTimOutMilliSec));
118
119                                 jsonMap = mapper.writeValueAsString(sessionTimeoutMap);
120                         } catch (Exception e) {
121                                 logger.error(EELFLoggerDelegate.errorLogger, "gatherSessionExtenstions failed", e);
122                         }
123                 }
124
125                 return jsonMap;
126
127         }
128
129         public void updateSessionExtensions(String sessionTimeoutMapStr) throws Exception {
130
131                 Map<String, TimeoutVO> sessionTimeoutMap;
132                 try {
133                         TypeReference<Hashtable<String, TimeoutVO>> typeRef = new TypeReference<Hashtable<String, TimeoutVO>>() {
134                         };
135
136                         sessionTimeoutMap = mapper.readValue(sessionTimeoutMapStr, typeRef);
137                 } catch (Exception e) {
138                         logger.error(EELFLoggerDelegate.errorLogger, "updateSessionExtensions failed 1", e);
139                         return;
140                 }
141                 for (String jPortalSessionId : sessionTimeoutMap.keySet()) {
142                         try {
143
144                                 TimeoutVO extendedTimeoutVO = mapper
145                                                 .readValue(mapper.writeValueAsString(sessionTimeoutMap.get(jPortalSessionId)), TimeoutVO.class);
146                                 HttpSession session = sessionMap.get(jSessionKey(extendedTimeoutVO.getjSessionId(), jPortalSessionId));
147
148                                 if (session == null) {
149                                         continue;
150                                 }
151
152                                 Long lastAccessedTimeMilliSec = session.getLastAccessedTime();
153                                 Long maxIntervalMilliSec = session.getMaxInactiveInterval() * 1000L;
154                                 Long sessionTimOutMilliSec = maxIntervalMilliSec + lastAccessedTimeMilliSec;
155
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);
161                                 }
162                         } catch (Exception e) {
163                                 logger.error(EELFLoggerDelegate.errorLogger,
164                                                 "updateSessionExtensions failed", e);
165                         }
166
167                 }
168
169         }
170
171         protected static String jSessionKey(String jSessionId, String portalJSessionId) {
172                 return portalJSessionId + "-" + jSessionId;
173         }
174
175         protected String portalJSessionId(String jSessionKey) {
176                 return jSessionKey.split("-")[0];
177         }
178
179         protected String jSessionId(String jSessionKey) {
180                 return jSessionKey.split("-")[1];
181         }
182
183 }