2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 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============================================
38 package org.onap.portalapp.service.sessionmgt;
40 import java.util.Hashtable;
41 import java.util.List;
44 import javax.servlet.http.HttpSession;
46 import org.quartz.DisallowConcurrentExecution;
47 import org.quartz.JobExecutionContext;
48 import org.quartz.JobExecutionException;
49 import org.quartz.PersistJobDataAfterExecution;
50 import org.springframework.beans.factory.annotation.Autowired;
51 import org.springframework.context.ApplicationContext;
52 import org.springframework.context.annotation.EnableAspectJAutoProxy;
53 import org.springframework.scheduling.quartz.QuartzJobBean;
54 import org.onap.portalapp.portal.logging.aop.EPMetricsLog;
55 import org.onap.portalapp.portal.logging.format.EPAppMessagesEnum;
56 import org.onap.portalapp.portal.logging.logic.EPLogUtil;
57 import org.onap.portalapp.portal.service.AppsCacheService;
58 import org.onap.portalapp.portal.service.EPAppService;
59 import org.onap.portalapp.portal.transport.OnboardingApp;
60 import org.onap.portalapp.portal.utils.EcompPortalUtils;
61 import org.onap.portalsdk.core.domain.sessionmgt.TimeoutVO;
62 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
63 import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
65 import com.fasterxml.jackson.core.JsonParseException;
66 import com.fasterxml.jackson.core.JsonProcessingException;
67 import com.fasterxml.jackson.core.type.TypeReference;
68 import com.fasterxml.jackson.databind.JsonMappingException;
69 import com.fasterxml.jackson.databind.ObjectMapper;
72 * Executed periodically by Quartz to discover remote application sessions and
73 * update timeouts suitably.
75 @PersistJobDataAfterExecution
76 @DisallowConcurrentExecution
77 @org.springframework.context.annotation.Configuration
78 @EnableAspectJAutoProxy
80 public class TimeoutHandler extends QuartzJobBean {
81 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(TimeoutHandler.class);
83 private ObjectMapper mapper = new ObjectMapper();
86 * Supports static call {@link #timeoutSessions(HttpSession)}
88 private static List<OnboardingApp> onboardedAppList = null;
91 private SessionCommunication sessionCommunication;
95 protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
97 //Create a request id if there is none available,
98 //and which will internally be used when making
99 //session extended timeout calls to the partner applications.
100 if (getSessionCommunication()!=null) {
101 getSessionCommunication().setRequestId();
103 logger.info(EELFLoggerDelegate.debugLogger, "Quartz Cronjob for Session Management begins");
105 ManageService manageService = (ManageService) applicationContext.getBean("manageService");
106 EPAppService appService = (EPAppService) applicationContext.getBean("epAppService");
107 AppsCacheService appsCacheService = (AppsCacheService)applicationContext.getBean("appsCacheService");
109 List<OnboardingApp> appList = appsCacheService.getAppsFullList();
110 onboardedAppList = appList;
111 TypeReference<Hashtable<String, TimeoutVO>> typeRef = new TypeReference<Hashtable<String, TimeoutVO>>() {
113 String portalJsonSessionStr;
114 Map<String, TimeoutVO> portalSessionTimeoutMap = null;
116 portalJsonSessionStr = manageService.gatherSessionExtenstions();
117 if (portalJsonSessionStr == null || portalJsonSessionStr == "") {
118 logger.error(EELFLoggerDelegate.errorLogger, "Session Management: Portal session information is empty.");
123 portalSessionTimeoutMap = mapper.readValue(portalJsonSessionStr, typeRef);
124 } catch (JsonMappingException | JsonParseException je) {
125 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeInvalidJsonInput, je);
126 logger.error(EELFLoggerDelegate.errorLogger, "Session Management: JSON Mapping Exception occurred while gathering the Session", je);
128 } catch (Exception e) {
129 logger.error(EELFLoggerDelegate.errorLogger, "Session Management: Error while gather Session from portal", e);
133 Map<Long, Map<String, TimeoutVO>> appSessionTimeOutMap = new Hashtable<Long, Map<String, TimeoutVO>>();
134 // determine the Max TimeOut Time for each of the managed sessions
135 for (OnboardingApp app : appList) {
136 if (app.restUrl == null) {
137 logger.info(EELFLoggerDelegate.debugLogger, "Session Management: null restUrl, not fetching from app " + app.name);
140 logger.info(EELFLoggerDelegate.debugLogger, "Session Management: Calling App " + app.name + " at URL " + app.restUrl);
141 String jsonSessionStr = fetchAppSessions(app);
142 logger.info(EELFLoggerDelegate.debugLogger, "Session Management: App " + app.name + " returned " + jsonSessionStr);
143 if (jsonSessionStr == null || jsonSessionStr.isEmpty())
147 Map<String, TimeoutVO> sessionTimeoutMap = mapper.readValue(jsonSessionStr, typeRef);
148 appSessionTimeOutMap.put(app.id, sessionTimeoutMap);
149 for (String portalJSessionId : sessionTimeoutMap.keySet()) {
150 final TimeoutVO maxTimeoutVO = portalSessionTimeoutMap.get(portalJSessionId);
151 final TimeoutVO compareTimeoutVO = sessionTimeoutMap.get(portalJSessionId);
152 if (maxTimeoutVO != null && compareTimeoutVO != null) {
153 if (maxTimeoutVO.compareTo(compareTimeoutVO) < 0)
154 portalSessionTimeoutMap.get(portalJSessionId)
155 .setSessionTimOutMilliSec(compareTimeoutVO.getSessionTimOutMilliSec());
158 } catch (JsonParseException | JsonMappingException e) {
159 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeInvalidJsonInput, e);
160 logger.error(EELFLoggerDelegate.errorLogger,
161 "JSON Mapping/Processing Exception occurred while mapping/parsing the jsonSessionStr", e);
163 } catch (Exception e) {
164 logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while mapping/parsing the jsonSessionStr", e);
170 // post the updated session timeouts back to the Apps
171 for (OnboardingApp app : appList) {
172 if (app.restUrl == null) {
173 logger.warn(EELFLoggerDelegate.errorLogger, "Session Management: null restUrl, not posting back to app " + app.name);
177 Map<String, TimeoutVO> sessionTimeoutMap = appSessionTimeOutMap.get(app.id);
178 if (sessionTimeoutMap == null || sessionTimeoutMap.isEmpty())
181 for (String portalJSessionId : sessionTimeoutMap.keySet()) {
183 final TimeoutVO maxTimeoutVO = portalSessionTimeoutMap.get(portalJSessionId);
184 final TimeoutVO setTimeoutVO = sessionTimeoutMap.get(portalJSessionId);
185 if (maxTimeoutVO == null || setTimeoutVO == null) {
186 String message = String.format(
187 "Session Management: Failed to update the session timeouts for the app: %s and the sessionId: %s.",
188 app.name, portalJSessionId);
189 logger.warn(EELFLoggerDelegate.errorLogger, message);
192 setTimeoutVO.setSessionTimOutMilliSec(maxTimeoutVO.getSessionTimOutMilliSec());
193 } catch (Exception e) {
194 logger.error(EELFLoggerDelegate.errorLogger, "Session Management: error while updating the session timeout map", e);
198 logger.info(EELFLoggerDelegate.debugLogger, "Session Management: Updating App " + app.restUrl);
199 String sessionTimeoutMapStr = "";
201 sessionTimeoutMapStr = mapper.writeValueAsString(sessionTimeoutMap);
202 } catch (JsonProcessingException je) {
203 logger.error(EELFLoggerDelegate.errorLogger, "executeInternal failed while processing sessionTimeOutMap object to a String", je);
204 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeInvalidJsonInput, je);
206 pingAppSessions(app, sessionTimeoutMapStr);
208 String portalSessionTimeoutMapStr = "";
210 portalSessionTimeoutMapStr = mapper.writeValueAsString(portalSessionTimeoutMap);
211 } catch (JsonProcessingException je) {
212 logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while processing portalSessionTimeOutMap object to a String", je);
213 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeInvalidJsonInput, je);
215 manageService.updateSessionExtensions(portalSessionTimeoutMapStr);
216 } catch (Exception e) {
217 logger.error(EELFLoggerDelegate.errorLogger, "************************ Session Management: error in managing session timeouts", e);
219 getSessionCommunication().clear(true);
223 private String fetchAppSessions(OnboardingApp app) throws Exception {
224 String jsonSessionValue = getSessionCommunication().sendGet(app);
225 getSessionCommunication().clear(false);
226 return jsonSessionValue;
229 private void pingAppSessions(OnboardingApp app, String sessionTimeoutMapStr) throws Exception {
230 getSessionCommunication().pingSession(app, sessionTimeoutMapStr);
231 getSessionCommunication().clear(false);
234 public void timeoutSessions(HttpSession session) throws Exception {
235 String portalJSessionId = portalJSessionId(session);
236 if (onboardedAppList == null)
239 for (OnboardingApp app : onboardedAppList) {
240 getSessionCommunication().timeoutSession(app, portalJSessionId);
241 getSessionCommunication().clear(false);
245 protected static String portalJSessionId(HttpSession session) {
246 final Object attribute = session.getAttribute(PortalApiConstants.PORTAL_JSESSION_ID);
247 if (attribute == null)
249 String jSessionKey = (String) attribute;
250 return jSessionKey.split("-")[0];
251 //return jSessionKey;
254 private static ApplicationContext applicationContext;
256 public static void setApplicationContext(ApplicationContext _applicationContext) {
257 applicationContext = _applicationContext;
260 public SessionCommunication getSessionCommunication() {
261 if(sessionCommunication == null){
262 if (applicationContext != null)
263 sessionCommunication = (SessionCommunication)applicationContext.getBean("sessionCommunication");
266 return sessionCommunication;
269 public void setSessionCommunication(SessionCommunication sessionCommunication) {
270 this.sessionCommunication = sessionCommunication;