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.Hashtable;
23 import java.util.List;
26 import javax.servlet.http.HttpSession;
28 import org.quartz.DisallowConcurrentExecution;
29 import org.quartz.JobExecutionContext;
30 import org.quartz.JobExecutionException;
31 import org.quartz.PersistJobDataAfterExecution;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.context.ApplicationContext;
34 import org.springframework.context.annotation.EnableAspectJAutoProxy;
35 import org.springframework.scheduling.quartz.QuartzJobBean;
37 import org.openecomp.portalsdk.core.domain.sessionmgt.TimeoutVO;
38 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
39 import org.openecomp.portalsdk.core.onboarding.util.PortalApiConstants;
40 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
41 import org.openecomp.portalapp.portal.logging.format.EPAppMessagesEnum;
42 import org.openecomp.portalapp.portal.logging.logic.EPLogUtil;
43 import org.openecomp.portalapp.portal.service.EPAppService;
44 import org.openecomp.portalapp.portal.transport.OnboardingApp;
45 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
46 import com.fasterxml.jackson.core.JsonParseException;
47 import com.fasterxml.jackson.core.JsonProcessingException;
48 import com.fasterxml.jackson.core.type.TypeReference;
49 import com.fasterxml.jackson.databind.JsonMappingException;
50 import com.fasterxml.jackson.databind.ObjectMapper;
53 * Executed periodically by Quartz to discover remote application sessions and
54 * update timeouts suitably.
56 @PersistJobDataAfterExecution
57 @DisallowConcurrentExecution
58 @org.springframework.context.annotation.Configuration
59 @EnableAspectJAutoProxy
61 public class TimeoutHandler extends QuartzJobBean {
62 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(TimeoutHandler.class);
64 ObjectMapper mapper = new ObjectMapper();
67 * Supports static call {@link #timeoutSessions(HttpSession)}
69 private static List<OnboardingApp> onboardedAppList = null;
72 SessionCommunication sessionCommunication;
75 protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
77 //Create a request id if there is none available,
78 //and which will internally be used when making
79 //session extended timeout calls to the partner applications.
80 if (getSessionCommunication()!=null) {
81 getSessionCommunication().setRequestId();
83 logger.info(EELFLoggerDelegate.debugLogger, "Quartz Cronjob for Session Management begins");
85 ManageService manageService = (ManageService) applicationContext.getBean("manageService");
86 EPAppService appService = (EPAppService) applicationContext.getBean("epAppService");
88 List<OnboardingApp> appList = appService.getEnabledNonOpenOnboardingApps();
89 onboardedAppList = appList;
90 TypeReference<Hashtable<String, TimeoutVO>> typeRef = new TypeReference<Hashtable<String, TimeoutVO>>() {
92 String portalJsonSessionStr;
93 Map<String, TimeoutVO> portalSessionTimeoutMap = null;
95 portalJsonSessionStr = manageService.gatherSessionExtenstions();
96 if (portalJsonSessionStr == null || portalJsonSessionStr == "") {
97 logger.error(EELFLoggerDelegate.errorLogger, "Session Management: Portal session information is empty.");
102 portalSessionTimeoutMap = mapper.readValue(portalJsonSessionStr, typeRef);
103 } catch (JsonMappingException | JsonParseException je) {
104 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeInvalidJsonInput, je);
105 logger.error(EELFLoggerDelegate.errorLogger, "Session Management: JSON Mapping Exception occurred while gathering the Session", je);
107 } catch (Exception e) {
108 logger.error(EELFLoggerDelegate.errorLogger, "Session Management: Error while gather Session from portal", e);
112 Map<Long, Map<String, TimeoutVO>> appSessionTimeOutMap = new Hashtable<Long, Map<String, TimeoutVO>>();
113 // determine the Max TimeOut Time for each of the managed sessions
114 for (OnboardingApp app : appList) {
115 if (app.restUrl == null) {
116 logger.info(EELFLoggerDelegate.debugLogger, "Session Management: null restUrl, not fetching from app " + app.name);
119 logger.info(EELFLoggerDelegate.debugLogger, "Session Management: Calling App " + app.name + " at URL " + app.restUrl);
120 String jsonSessionStr = fetchAppSessions(app);
121 logger.info(EELFLoggerDelegate.debugLogger, "Session Management: App " + app.name + " returned " + jsonSessionStr);
122 if (jsonSessionStr == null || jsonSessionStr.isEmpty())
126 Map<String, TimeoutVO> sessionTimeoutMap = mapper.readValue(jsonSessionStr, typeRef);
127 appSessionTimeOutMap.put(app.id, sessionTimeoutMap);
128 for (String portalJSessionId : sessionTimeoutMap.keySet()) {
129 final TimeoutVO maxTimeoutVO = portalSessionTimeoutMap.get(portalJSessionId);
130 final TimeoutVO compareTimeoutVO = sessionTimeoutMap.get(portalJSessionId);
131 if (maxTimeoutVO != null && compareTimeoutVO != null) {
132 if (maxTimeoutVO.compareTo(compareTimeoutVO) < 0)
133 portalSessionTimeoutMap.get(portalJSessionId)
134 .setSessionTimOutMilliSec(compareTimeoutVO.getSessionTimOutMilliSec());
137 } catch (JsonParseException | JsonMappingException e) {
138 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeInvalidJsonInput, e);
139 logger.error(EELFLoggerDelegate.errorLogger,
140 "JSON Mapping/Processing Exception occurred while mapping/parsing the jsonSessionStr", e);
142 } catch (Exception e) {
143 logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while mapping/parsing the jsonSessionStr", e);
149 // post the updated session timeouts back to the Apps
150 for (OnboardingApp app : appList) {
151 if (app.restUrl == null) {
152 logger.warn(EELFLoggerDelegate.errorLogger, "Session Management: null restUrl, not posting back to app " + app.name);
156 Map<String, TimeoutVO> sessionTimeoutMap = appSessionTimeOutMap.get(app.id);
157 if (sessionTimeoutMap == null || sessionTimeoutMap.isEmpty())
160 for (String portalJSessionId : sessionTimeoutMap.keySet()) {
162 final TimeoutVO maxTimeoutVO = portalSessionTimeoutMap.get(portalJSessionId);
163 final TimeoutVO setTimeoutVO = sessionTimeoutMap.get(portalJSessionId);
164 if (maxTimeoutVO == null || setTimeoutVO == null) {
165 String message = String.format(
166 "Session Management: Failed to update the session timeouts for the app: %s and the sessionId: %s.",
167 app.name, portalJSessionId);
168 logger.warn(EELFLoggerDelegate.errorLogger, message);
171 setTimeoutVO.setSessionTimOutMilliSec(maxTimeoutVO.getSessionTimOutMilliSec());
172 } catch (Exception e) {
173 logger.error(EELFLoggerDelegate.errorLogger, "Session Management: error while updating the session timeout map", e);
177 logger.info(EELFLoggerDelegate.debugLogger, "Session Management: Updating App " + app.restUrl);
178 String sessionTimeoutMapStr = "";
180 sessionTimeoutMapStr = mapper.writeValueAsString(sessionTimeoutMap);
181 } catch (JsonProcessingException je) {
182 logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while processing sessionTimeOutMap object to a String. Details: "
183 + EcompPortalUtils.getStackTrace(je));
184 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeInvalidJsonInput, je);
186 pingAppSessions(app, sessionTimeoutMapStr);
188 String portalSessionTimeoutMapStr = "";
190 portalSessionTimeoutMapStr = mapper.writeValueAsString(portalSessionTimeoutMap);
191 } catch (JsonProcessingException je) {
192 logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while processing portalSessionTimeOutMap object to a String", je);
193 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeInvalidJsonInput, je);
195 manageService.updateSessionExtensions(portalSessionTimeoutMapStr);
196 } catch (Exception e) {
197 logger.error(EELFLoggerDelegate.errorLogger, "************************ Session Management: error in managing session timeouts", e);
199 getSessionCommunication().clear(true);
203 private String fetchAppSessions(OnboardingApp app) throws Exception {
204 String jsonSessionValue = getSessionCommunication().sendGet(app);
205 getSessionCommunication().clear(false);
206 return jsonSessionValue;
209 private void pingAppSessions(OnboardingApp app, String sessionTimeoutMapStr) throws Exception {
210 getSessionCommunication().pingSession(app, sessionTimeoutMapStr);
211 getSessionCommunication().clear(false);
214 public void timeoutSessions(HttpSession session) throws Exception {
215 String portalJSessionId = portalJSessionId(session);
216 if (onboardedAppList == null)
219 for (OnboardingApp app : onboardedAppList) {
220 getSessionCommunication().timeoutSession(app, portalJSessionId);
221 getSessionCommunication().clear(false);
225 protected static String portalJSessionId(HttpSession session) {
226 final Object attribute = session.getAttribute(PortalApiConstants.PORTAL_JSESSION_ID);
227 if (attribute == null)
229 String jSessionKey = (String) attribute;
230 return jSessionKey.split("-")[0];
233 private static ApplicationContext applicationContext;
235 public static void setApplicationContext(ApplicationContext _applicationContext) {
236 applicationContext = _applicationContext;
239 public SessionCommunication getSessionCommunication() {
240 if(sessionCommunication == null){
241 if (applicationContext != null)
242 sessionCommunication = (SessionCommunication)applicationContext.getBean("sessionCommunication");
245 return sessionCommunication;
248 public void setSessionCommunication(SessionCommunication sessionCommunication) {
249 this.sessionCommunication = sessionCommunication;