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============================================
 
  36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 
  38 package org.onap.portalapp.service.sessionmgt;
 
  40 import java.io.BufferedReader;
 
  41 import java.io.InputStreamReader;
 
  42 import java.net.HttpURLConnection;
 
  44 import java.util.UUID;
 
  46 import javax.servlet.http.HttpServletResponse;
 
  48 import org.onap.portalapp.portal.logging.aop.EPAuditLog;
 
  49 import org.onap.portalapp.portal.logging.aop.EPMetricsLog;
 
  50 import org.onap.portalapp.portal.logging.format.EPAppMessagesEnum;
 
  51 import org.onap.portalapp.portal.logging.logic.EPLogUtil;
 
  52 import org.onap.portalapp.portal.transport.OnboardingApp;
 
  53 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
 
  54 import org.onap.portalapp.portal.utils.EcompPortalUtils;
 
  55 import org.onap.portalsdk.core.exception.UrlAccessRestrictedException;
 
  56 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
 
  58 import org.springframework.context.annotation.EnableAspectJAutoProxy;
 
  59 import org.springframework.stereotype.Service;
 
  60 import org.springframework.util.StringUtils;
 
  62 import com.att.eelf.configuration.Configuration;
 
  64 @Service("sessionCommunication")
 
  65 @org.springframework.context.annotation.Configuration
 
  66 @EnableAspectJAutoProxy
 
  67 public class SessionCommunication {
 
  68         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SessionCommunication.class);
 
  71         public String sendGet(OnboardingApp app) throws Exception {
 
  72                 String appResponse = "";
 
  75                 if (app != null && app.name != null && app.name != "") {
 
  78                                 String url = app.restUrl + "/sessionTimeOuts";
 
  79                                 String encriptedPwdDB = app.appPassword;
 
  80                                 String appUserName = app.username;
 
  82                                 setLocalMDCContext(app, "/sessionTimeOuts", url);
 
  84                                 URL obj = new URL(url);
 
  86                                 HttpURLConnection con = (HttpURLConnection) obj.openConnection();
 
  88                                 // optional default is GET
 
  89                                 con.setRequestMethod("GET");
 
  90                                 con.setConnectTimeout(3000);
 
  91                                 con.setReadTimeout(8000);
 
  93                                 con.setRequestProperty("username", appUserName);
 
  94                                 con.setRequestProperty("password", encriptedPwdDB);
 
  97                                 responseCode = con.getResponseCode();
 
  98                                 logger.debug(EELFLoggerDelegate.debugLogger, "Response Code : " + responseCode);
 
 100                                 BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
 
 102                                 StringBuffer response = new StringBuffer();
 
 104                                 while ((inputLine = in.readLine()) != null) {
 
 105                                         response.append(inputLine);
 
 109                                 appResponse = response.toString();
 
 110                         } catch (UrlAccessRestrictedException e) {
 
 111                                 responseCode = HttpServletResponse.SC_UNAUTHORIZED;
 
 112                                 logger.error(EELFLoggerDelegate.errorLogger, String.format(
 
 113                                                 "SessionCommunication.sendGet received an un-authorized exception. AppName: %s", appName));
 
 114                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeRestApiAuthenticationError, e);
 
 115                         } catch (Exception e) {
 
 116                                 responseCode = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
 
 117                                 String message = String.format(
 
 118                                                 "SessionCommunication.sendGet encountered an Exception. AppName: %s, Details: %s", appName,
 
 120                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeHttpConnectionError, e);
 
 121                                 logger.error(EELFLoggerDelegate.errorLogger, message, e);
 
 123                                 EcompPortalUtils.setExternalAppResponseCode(responseCode);
 
 126                         logger.error(EELFLoggerDelegate.errorLogger, "SessionCommunication sendGet: app is null");
 
 132         public Boolean pingSession(OnboardingApp app, String sessionTimeoutMap) throws Exception {
 
 134                 int responseCode = 0;
 
 137                                 throw new Exception("SessionCommunication.pingSession: app is null");
 
 138                         if (app != null && app.name != null && app.name != "") {
 
 141                         String url = app.restUrl + "/updateSessionTimeOuts";
 
 142                         String encriptedPwdDB = app.appPassword;
 
 143                         String appUserName = app.username;
 
 145                         setLocalMDCContext(app, "/updateSessionTimeOuts", url);
 
 147                         URL obj = new URL(url);
 
 149                         HttpURLConnection con = (HttpURLConnection) obj.openConnection();
 
 151                         // optional default is GET
 
 152                         con.setRequestMethod("POST");
 
 153                         con.setConnectTimeout(3000);
 
 154                         con.setReadTimeout(15000);
 
 156                         // add request header
 
 157                         con.setRequestProperty("username", appUserName);
 
 158                         con.setRequestProperty("password", encriptedPwdDB);
 
 160                         con.setRequestProperty("sessionMap", sessionTimeoutMap);
 
 161                         con.setDoInput(true);
 
 162                         con.setDoOutput(true);
 
 163                         con.getOutputStream().write(sessionTimeoutMap.getBytes());
 
 164                         con.getOutputStream().flush();
 
 165                         con.getOutputStream().close();
 
 167                         responseCode = con.getResponseCode();
 
 168                         logger.debug(EELFLoggerDelegate.debugLogger, "Response Code : " + responseCode);
 
 169                 } catch (UrlAccessRestrictedException e) {
 
 170                         responseCode = HttpServletResponse.SC_UNAUTHORIZED;
 
 171                         String message = String.format(
 
 172                                         "SessionCommunication.pingSession received an un-authorized exception. AppName: %s", appName);
 
 173                         logger.error(EELFLoggerDelegate.errorLogger, message);
 
 174                         EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeRestApiAuthenticationError, e);
 
 175                 } catch (Exception e) {
 
 176                         responseCode = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
 
 177                         String message = String.format(
 
 178                                         "SessionCommunication.pingSession encountered an Exception. AppName: %s, Details: %s", appName, e.toString());
 
 179                         EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeHttpConnectionError, e);
 
 180                         logger.error(EELFLoggerDelegate.errorLogger, message, e);
 
 182                         EcompPortalUtils.setExternalAppResponseCode(responseCode);
 
 189         public Boolean timeoutSession(OnboardingApp app, String portalJSessionId) throws Exception {
 
 190                 String appName = "Unknwon";
 
 191                 int responseCode = 0;
 
 192                 if (app != null && app.name != null && app.name != "") {
 
 195                                 String url = app.restUrl + "/timeoutSession" + "?portalJSessionId=" + portalJSessionId;
 
 197                                 String encriptedPwdDB = app.appPassword;
 
 198                                 String appUserName = app.username;
 
 199                                 // String decreptedPwd = CipherUtil.decrypt(encriptedPwdDB,
 
 200                                 // SystemProperties.getProperty(SystemProperties.Decryption_Key));
 
 202                                 setLocalMDCContext(app, "/timeoutSession", url);
 
 204                                 URL obj = new URL(url);
 
 205                                 HttpURLConnection con = (HttpURLConnection) obj.openConnection();
 
 207                                 // optional default is GET
 
 208                                 con.setRequestMethod("POST");
 
 209                                 con.setConnectTimeout(3000);
 
 210                                 con.setReadTimeout(15000);
 
 212                                 // add request header
 
 213                                 con.setRequestProperty("username", appUserName);
 
 214                                 con.setRequestProperty("password", encriptedPwdDB);
 
 216                                 // con.setRequestProperty("portalJSessionId", portalJSessionId);
 
 217                                 con.setDoInput(true);
 
 218                                 con.setDoOutput(true);
 
 219                                 con.getOutputStream().flush();
 
 220                                 con.getOutputStream().close();
 
 222                                 responseCode = con.getResponseCode();
 
 223                                 logger.debug(EELFLoggerDelegate.debugLogger, "Response Code : " + responseCode);
 
 224                         } catch (UrlAccessRestrictedException e) {
 
 225                                 responseCode = HttpServletResponse.SC_UNAUTHORIZED;
 
 226                                 String message = String.format(
 
 227                                                 "SessionCommunication.timeoutSession received an un-authorized exception. AppName: %s",
 
 229                                 logger.error(EELFLoggerDelegate.errorLogger, message);
 
 230                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeRestApiAuthenticationError, e);
 
 231                         } catch (Exception e) {
 
 232                                 responseCode = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
 
 233                                 String message = String.format(
 
 234                                                 "SessionCommunication.timeoutSession encountered an Exception. AppName: %s, Details: %s", 
 
 235                                                 appName, e.toString());
 
 236                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeHttpConnectionError, e);
 
 237                                 logger.error(EELFLoggerDelegate.errorLogger, message, e);
 
 239                                 EcompPortalUtils.setExternalAppResponseCode(responseCode);
 
 242                         logger.error(EELFLoggerDelegate.errorLogger, "SessionCommunication pingSession: app is null");
 
 248         private void setLocalMDCContext(OnboardingApp app, String restPath, String url) {
 
 250                 MDC.put(EPCommonSystemProperties.PROTOCOL, EPCommonSystemProperties.HTTP);
 
 251                 if (url != null && url.contains("https")) {
 
 252                         MDC.put(EPCommonSystemProperties.PROTOCOL, EPCommonSystemProperties.HTTPS);
 
 254                 MDC.put(EPCommonSystemProperties.FULL_URL, url);
 
 255                 MDC.put(EPCommonSystemProperties.TARGET_ENTITY, app.myLoginsAppName);
 
 256                 MDC.put(EPCommonSystemProperties.TARGET_SERVICE_NAME, restPath);
 
 260          * Generates request id, service name fields and loads them into MDC, as these
 
 261          * values could be empty as these session timeout requests are generated at
 
 262          * scheduled intervals using quartz scheduler.
 
 265         public void setRequestId() {
 
 266                 String requestId = MDC.get(Configuration.MDC_KEY_REQUEST_ID);
 
 267                 if (StringUtils.isEmpty(requestId)) {
 
 268                         MDC.put(Configuration.MDC_KEY_REQUEST_ID, UUID.randomUUID().toString());
 
 271                 MDC.put(Configuration.MDC_SERVICE_NAME, "/quartz/keepSessionAlive");
 
 272                 MDC.put(EPCommonSystemProperties.PARTNER_NAME, EPCommonSystemProperties.ECOMP_PORTAL_BE);
 
 276          * Remove the values from MDC as these requests are executed at regular
 
 277          * intervals based on quartz rather incoming REST API requests.
 
 282         public void clear(Boolean bAll) {
 
 283                 MDC.remove(EPCommonSystemProperties.EXTERNAL_API_RESPONSE_CODE);
 
 285                         MDC.remove(Configuration.MDC_KEY_REQUEST_ID);
 
 286                         MDC.remove(Configuration.MDC_SERVICE_NAME);
 
 287                         MDC.remove(EPCommonSystemProperties.PARTNER_NAME);