nexus site path corrected
[portal.git] / ecomp-portal-BE / src / main / java / org / openecomp / portalapp / service / sessionmgt / CoreTimeoutHandler.java
1 /*-
2  * ================================================================================
3  * eCOMP Portal
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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  * ================================================================================
19  */
20 package org.openecomp.portalapp.service.sessionmgt;
21
22 import java.util.Calendar;
23 import java.util.Hashtable;
24 import java.util.Map;
25
26 import javax.servlet.http.HttpSession;
27
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;
35
36 import com.fasterxml.jackson.core.type.TypeReference;
37 import com.fasterxml.jackson.databind.ObjectMapper;
38
39
40 @Service
41 @org.springframework.context.annotation.Configuration
42 @EnableAspectJAutoProxy
43 @EPMetricsLog
44 public class CoreTimeoutHandler{
45         private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CoreTimeoutHandler.class);
46         
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(); 
50         
51         
52         public static void sessionCreated(String portalJSessionId, String jSessionId, HttpSession session) {
53                 
54                 storeMaxInactiveTime(session);
55                 
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);
59                 
60         }
61         
62         protected static void storeMaxInactiveTime(HttpSession session) {
63                 
64                 if(session.getAttribute(PortalApiConstants.GLOBAL_SESSION_MAX_IDLE_TIME) == null)
65                         session.setAttribute(PortalApiConstants.GLOBAL_SESSION_MAX_IDLE_TIME,session.getMaxInactiveInterval());
66         }
67         
68         public static void sessionDestroyed(HttpSession session) {
69                 
70                 try{
71                 sessionMap.remove((String)session.getAttribute(PortalApiConstants.PORTAL_JSESSION_ID));
72                 }catch(Exception e){
73                         logger.error(EELFLoggerDelegate.errorLogger, "************************ Session Management: Error while destroying session for " + session.getId() + " Details: " + EcompPortalUtils.getStackTrace(e));
74                 }
75                 
76         }
77         
78         public String gatherSessionExtenstions() {
79                 
80                 Map<String,TimeoutVO> sessionTimeoutMap = new Hashtable<String,TimeoutVO>();
81                 String jsonMap = "";
82                 
83                 
84                 for(String jSessionKey: sessionMap.keySet()) {
85                         
86                         try{
87                                 // get the expirytime in seconds
88                                 HttpSession session = sessionMap.get(jSessionKey);
89                                 
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());
97                                 
98                                 Long sessionTimOutMilliSec = maxIntervalMilliSec + lastAccessedTimeMilliSec;
99                                 
100                                 sessionTimeoutMap.put( portalJSessionId(jSessionKey), new TimeoutVO(jSessionId(jSessionKey),sessionTimOutMilliSec));
101                                 
102                                 
103                                 jsonMap = mapper.writeValueAsString(sessionTimeoutMap);
104                         } catch(Exception e) {
105                                 logger.error(EELFLoggerDelegate.errorLogger, "************************ Session Management: Error during JsonSessionTimout conversion. Details: " + EcompPortalUtils.getStackTrace(e));
106                         }
107                 }
108                 
109                 return jsonMap;
110                 
111         }
112         
113         
114         public void updateSessionExtensions(String sessionTimeoutMapStr) throws Exception {
115                 
116                 //Map<String,Object> sessionTimeoutMap = mapper.readValue(sessionTimeoutMapStr, Map.class);
117                 Map<String,TimeoutVO> sessionTimeoutMap;
118                 try{
119                         TypeReference<Hashtable<String,TimeoutVO>> typeRef
120                          = new TypeReference<Hashtable<String,TimeoutVO>>() {};
121         
122                          sessionTimeoutMap  = mapper.readValue(sessionTimeoutMapStr, typeRef);
123                 }catch(Exception e){
124                         logger.error(EELFLoggerDelegate.errorLogger, "************************ Session Management: Error while to parse update session extension in portal", e);
125                         return;
126                 }
127                 for(String jPortalSessionId: sessionTimeoutMap.keySet()) {
128                         try {
129                                 
130                                 TimeoutVO extendedTimeoutVO = mapper.readValue(mapper.writeValueAsString(sessionTimeoutMap.get(jPortalSessionId)),TimeoutVO.class);
131                                 HttpSession session = sessionMap.get(jSessionKey(extendedTimeoutVO.getjSessionId(), jPortalSessionId));
132                                 
133                                 if(session == null) {
134                                         continue;
135                                 }
136                                 
137                                 Long lastAccessedTimeMilliSec = session.getLastAccessedTime();
138                                 Long maxIntervalMilliSec = session.getMaxInactiveInterval() * 1000L;
139                                 Long sessionTimOutMilliSec = maxIntervalMilliSec + lastAccessedTimeMilliSec;
140                                 
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);
145                                 }
146                         } catch (Exception e) {
147                                 logger.error(EELFLoggerDelegate.errorLogger, "************************ Session Management: " + EcompPortalUtils.getStackTrace(e));
148                         }
149                         
150                 }
151                         
152         }
153
154         protected  static String jSessionKey(String jSessionId, String portalJSessionId) {
155                 return portalJSessionId + "-" + jSessionId;
156         }
157         
158         protected  String portalJSessionId(String jSessionKey) {
159                 return jSessionKey.split("-")[0];
160         }
161         
162         protected  String jSessionId(String jSessionKey) {
163                 return jSessionKey.split("-")[1];
164         }
165
166 }