X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=ecomp-portal-BE-common%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fportalapp%2Fportal%2Fservice%2FApplicationsRestClientServiceImpl.java;h=73003659c6dcd47d5f5b21e60ebb0b1d5667efd6;hb=54c5ed82662ef681375675e58abc8058e3203466;hp=1460a61c5ae3ce2a9b4b29a64159e2f4346a2130;hpb=0d2d6a5ba5a81b19497f5ce77506c1e7aed7b776;p=portal.git diff --git a/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/service/ApplicationsRestClientServiceImpl.java b/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/service/ApplicationsRestClientServiceImpl.java index 1460a61c..73003659 100644 --- a/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/service/ApplicationsRestClientServiceImpl.java +++ b/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/service/ApplicationsRestClientServiceImpl.java @@ -2,7 +2,9 @@ * ============LICENSE_START========================================== * ONAP Portal * =================================================================== - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * Modifications Copyright (c) 2019 Samsung * =================================================================== * * Unless otherwise specified, all software contained herein is licensed @@ -33,7 +35,7 @@ * * ============LICENSE_END============================================ * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * */ package org.onap.portalapp.portal.service; @@ -52,6 +54,7 @@ import javax.ws.rs.core.Response; import org.apache.commons.lang.StringUtils; import org.apache.cxf.jaxrs.client.WebClient; +import org.apache.cxf.jaxrs.impl.ResponseImpl; import org.apache.cxf.transport.http.HTTPException; import org.onap.portalapp.portal.domain.EPApp; import org.onap.portalapp.portal.logging.aop.EPAuditLog; @@ -62,7 +65,10 @@ import org.onap.portalapp.portal.utils.EPCommonSystemProperties; import org.onap.portalapp.portal.utils.EcompPortalUtils; import org.onap.portalapp.util.SystemType; import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.onap.portalsdk.core.onboarding.exception.CipherUtilException; import org.onap.portalsdk.core.onboarding.util.CipherUtil; +import org.onap.portalsdk.core.onboarding.util.KeyConstants; +import org.onap.portalsdk.core.onboarding.util.KeyProperties; import org.onap.portalsdk.core.util.SystemProperties; import org.slf4j.MDC; import org.springframework.beans.factory.annotation.Autowired; @@ -124,24 +130,24 @@ public class ApplicationsRestClientServiceImpl implements ApplicationsRestClient } @EPMetricsLog - private void verifyResponse(Response response) throws HTTPException { + private void verifyResponse(Response response,String restPath) throws HTTPException { int status = response.getStatus(); logger.debug(EELFLoggerDelegate.debugLogger, "http response status=" + status); MDC.put(EPCommonSystemProperties.EXTERNAL_API_RESPONSE_CODE, Integer.toString(status)); if (!isHttpSuccess(status)) { - String errMsg = "Failed. Status=" + status + "; [" + response.getStatusInfo().getReasonPhrase().toString() + String errMsg = "Failed. Status=" + status + restPath +"; [" + ((ResponseImpl)response).getStatusInfo().getReasonPhrase() + "]"; URL url = null; try { // must not be null to avoid NPE in HTTPException constructor url = new URL("http://null"); - if (response.getLocation() != null) - url = response.getLocation().toURL(); + if (((ResponseImpl)response).getLocation() != null) + url = ((ResponseImpl)response).getLocation().toURL(); } catch (MalformedURLException e) { // never mind. it is only for the debug message. logger.warn(EELFLoggerDelegate.errorLogger, "Failed to build URL", e); } - logger.error(EELFLoggerDelegate.errorLogger, "http response failed. " + errMsg + "; url=" + url); + logger.error(EELFLoggerDelegate.errorLogger, "http response failed. " + restPath + errMsg + "; url=" + url); EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeIncorrectHttpStatusError); throw new HTTPException(status, errMsg, url); } @@ -156,6 +162,8 @@ public class ApplicationsRestClientServiceImpl implements ApplicationsRestClient return createClientFor(appId, restPath, SystemType.APPLICATION); } + + //TODO Need to implement the mylogins once the endpoint is confirmed @EPMetricsLog private WebClient createClientFor(long appSystemId, String restPath, SystemType type) { @@ -170,8 +178,9 @@ public class ApplicationsRestClientServiceImpl implements ApplicationsRestClient if (externalApp != null) { String appBaseUri = (type == SystemType.APPLICATION) ? externalApp.getAppRestEndpoint() : ""; - String username = (type == SystemType.APPLICATION) ? externalApp.getUsername(): ""; - String encriptedPwd = (type == SystemType.APPLICATION) ? externalApp.getAppPassword(): ""; + String username = (type == SystemType.APPLICATION) ? externalApp.getAppBasicAuthUsername(): ""; + String encriptedPwd = (type == SystemType.APPLICATION) ? externalApp.getAppBasicAuthPassword(): ""; + String appName = (type == SystemType.APPLICATION) ? externalApp.getName(): ""; String decreptedAppPwd = StringUtils.EMPTY; @@ -184,31 +193,59 @@ public class ApplicationsRestClientServiceImpl implements ApplicationsRestClient MDC.put(EPCommonSystemProperties.TARGET_ENTITY, appName); MDC.put(EPCommonSystemProperties.TARGET_SERVICE_NAME, restPath); + if(!encriptedPwd.isEmpty() || encriptedPwd != null || StringUtils.isEmpty(encriptedPwd)){ try { decreptedAppPwd = CipherUtil.decryptPKC(encriptedPwd, - SystemProperties.getProperty(SystemProperties.Decryption_Key)); + KeyProperties.getProperty(KeyConstants.CIPHER_ENCRYPTION_KEY)); } catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger, "createClientFor failed to decrypt", e); + logger.error(EELFLoggerDelegate.errorLogger, "createClientFor failed to decrypt", e.getMessage()); + } } - WebClient client = createClientForPath(appBaseUri, restPath); + + + if(externalApp.getAppBasicAuthPassword().isEmpty() || externalApp.getAppBasicAuthPassword()==null){ + logger.debug(EELFLoggerDelegate.debugLogger, "Entering in the externalApp get app password contains null : {}"); + + externalApp = appsCacheService.getApp(1L); + logger.debug(EELFLoggerDelegate.debugLogger, "external App Information : {}",externalApp); + + String mechidUsername=externalApp.getAppBasicAuthUsername(); + logger.debug(EELFLoggerDelegate.debugLogger, "external App mechidUsername Information : {}",mechidUsername); + + String password=externalApp.getAppBasicAuthPassword(); + String decreptedexternalAppPwd = StringUtils.EMPTY; + try { + decreptedexternalAppPwd = CipherUtil.decryptPKC(password, + KeyProperties.getProperty(KeyConstants.CIPHER_ENCRYPTION_KEY)); + } catch (CipherUtilException e) { + logger.error(EELFLoggerDelegate.errorLogger, "failed to decreptedexternalAppPwd when external app pwd is null", e); + } + + username =mechidUsername; + decreptedAppPwd = decreptedexternalAppPwd; + + }else{ + logger.debug(EELFLoggerDelegate.debugLogger, "Entering in the externalApp get app password is not null : {}" ); // support basic authentication for some partners String encoding = Base64.getEncoder().encodeToString((username + ":" + decreptedAppPwd).getBytes()); String encodingStr = "Basic " + encoding; client.header(BASIC_AUTHENTICATION_HEADER, encodingStr); - + } + // But still keep code downward compatible for non compliant apps client.header(APP_USERNAME_HEADER, username); client.header(PASSWORD_HEADER, decreptedAppPwd); + String encoding = Base64.getEncoder().encodeToString((username + ":" + decreptedAppPwd).getBytes()); + String encodingStr = "Basic " + encoding; + client.header(BASIC_AUTHENTICATION_HEADER, encodingStr); client.header(SystemProperties.ECOMP_REQUEST_ID, MDC.get(MDC_KEY_REQUEST_ID)); client.header(SystemProperties.USERAGENT_NAME, EPCommonSystemProperties.ECOMP_PORTAL_BE); - logger.debug(EELFLoggerDelegate.debugLogger, - String.format("App %d found, baseUri=[%s], Headers: [%s=%s, %s=%s, %s=%s]", appSystemId, appBaseUri, - APP_USERNAME_HEADER, username, PASSWORD_HEADER, encriptedPwd, BASIC_AUTHENTICATION_HEADER, encodingStr)); - + String.format("check the partner application URL App %d found, baseUri=[%s], Headers: [%s=%s]", appSystemId, appBaseUri, + APP_USERNAME_HEADER, username)); return client; } return null; @@ -220,8 +257,12 @@ public class ApplicationsRestClientServiceImpl implements ApplicationsRestClient Response response = getResponse(appId, restPath); if (response != null) { - verifyResponse(response); - String str = response.readEntity(String.class); + verifyResponse(response,restPath); + /* It is not recommendable to use the implementation class org.apache.cxf.jaxrs.impl.ResponseImpl in the code, + but had to force this in-order to prevent conflict with the ResponseImpl class of Jersey Client which + doesn't work as expected. Created Portal-253 for tracking */ + String str = ((ResponseImpl)response).readEntity(String.class); + EcompPortalUtils.logAndSerializeObject(logger, restPath, "GET result =", str); try { t = gson.fromJson(str, clazz); @@ -238,9 +279,11 @@ public class ApplicationsRestClientServiceImpl implements ApplicationsRestClient Response response = getResponse(appId, restPath); if (response != null) { - verifyResponse(response); - String incomingJson = response.readEntity(String.class); - return incomingJson; + verifyResponse(response,restPath); + /* It is not recommendable to use the implementation class org.apache.cxf.jaxrs.impl.ResponseImpl in the code, + but had to force this in-order to prevent conflict with the ResponseImpl class of Jersey Client which + doesn't work as expected. Created Portal-253 for tracking */ + return ((ResponseImpl)response).readEntity(String.class); } return ""; @@ -264,14 +307,13 @@ public class ApplicationsRestClientServiceImpl implements ApplicationsRestClient Response response = getResponse(appId, restPath); if (response != null) { - verifyResponse(response); - String str = response.readEntity(String.class); + verifyResponse(response,restPath); + String str = ((ResponseImpl)response).readEntity(String.class); EcompPortalUtils.logAndSerializeObject(logger, restPath, "GET result =", str); try { t = mapper.readValue(str, clazz); } catch (Exception e) { - e.printStackTrace(); EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeInvalidJsonInput, e); } } @@ -297,12 +339,13 @@ public class ApplicationsRestClientServiceImpl implements ApplicationsRestClient MDC.put(EPCommonSystemProperties.EXTERNAL_API_RESPONSE_CODE, Integer.toString(HttpServletResponse.SC_INTERNAL_SERVER_ERROR)); EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeRestApiGeneralError, e); - logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while making the GET REST API call", e); + logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while making the GET REST API call", e.getMessage()); } return response; } + @SuppressWarnings({ "unchecked", "null" }) @Override public T post(Class clazz, long appId, Object payload, String restPath, SystemType type) throws HTTPException { WebClient client = null; @@ -323,18 +366,16 @@ public class ApplicationsRestClientServiceImpl implements ApplicationsRestClient MDC.put(EPCommonSystemProperties.EXTERNAL_API_RESPONSE_CODE, Integer.toString(HttpServletResponse.SC_INTERNAL_SERVER_ERROR)); EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeRestApiGeneralError, e); - logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while making the POST REST API call", e); + logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while making the POST REST API call", e.getMessage()); } if (response != null) { - verifyResponse(response); - - // String contentType = response.getHeaderString("Content-Type"); + verifyResponse(response,restPath); if (clazz != null) { - String str = response.readEntity(String.class); + String str = ((ResponseImpl)response).readEntity(String.class); EcompPortalUtils.logAndSerializeObject(logger, restPath, "POST result =", str); try { - t = gson.fromJson(str, clazz); + t = (T) gson.fromJson(str, clazz); } catch (Exception e) { EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeInvalidJsonInput, e); } @@ -348,48 +389,6 @@ public class ApplicationsRestClientServiceImpl implements ApplicationsRestClient return post( clazz, appId, payload, restPath, SystemType.APPLICATION); } - //@Override - public T postForClass(Class clazz, long appId, Object payload, String restPath, Class forClass) throws HTTPException { - WebClient client = null; - Response response = null; - T t = null; - logger.debug(EELFLoggerDelegate.debugLogger, "Entering to createClientForApp method for payload: {} and restPath: {} and appId: {}", payload.toString(), restPath, appId); - client = createClientForApp(appId, restPath); - EcompPortalUtils.logAndSerializeObject(logger, restPath, "POST request =", payload); - logger.debug(EELFLoggerDelegate.debugLogger, "Finished createClientForApp method for payload: {} and restPath: {} and appId: {}", payload.toString(), restPath, appId); - try { - if (client != null) { - logger.debug(EELFLoggerDelegate.debugLogger, "Entering to POST for payload: {} and restPath: {} and appId: {}", payload.toString(), restPath, appId); - response = client.post(payload); - logger.debug(EELFLoggerDelegate.debugLogger, "Finished to POST for payload: {} and restPath: {} and appId: {}", payload.toString(), restPath, appId); - } else { - logger.error(EELFLoggerDelegate.errorLogger, - "Unable to create the Webclient to make the '" + restPath + "' API call."); - } - } catch (Exception e) { - MDC.put(EPCommonSystemProperties.EXTERNAL_API_RESPONSE_CODE, - Integer.toString(HttpServletResponse.SC_INTERNAL_SERVER_ERROR)); - EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeRestApiGeneralError, e); - logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while making the POST REST API call", e); - } - - if (response != null) { - verifyResponse(response); - - // String contentType = response.getHeaderString("Content-Type"); - if (clazz != null) { - String str = response.readEntity(String.class); - EcompPortalUtils.logAndSerializeObject(logger, restPath, "POST result =", str); - try { - t = gson.fromJson(str, clazz); - } catch (Exception e) { - EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeInvalidJsonInput, e); - } - } - } - return t; - } - @Override public T put(Class clazz, long appId, Object payload, String restPath) throws HTTPException { @@ -420,12 +419,12 @@ public class ApplicationsRestClientServiceImpl implements ApplicationsRestClient MDC.put(EPCommonSystemProperties.EXTERNAL_API_RESPONSE_CODE, Integer.toString(HttpServletResponse.SC_INTERNAL_SERVER_ERROR)); EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeRestApiGeneralError, e); - logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while making the PUT REST API call", e); + logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while making the PUT REST API call", e.getMessage()); } if (response != null) { - verifyResponse(response); - String str = response.readEntity(String.class); + verifyResponse(response,restPath); + String str = ((ResponseImpl)response).readEntity(String.class); EcompPortalUtils.logAndSerializeObject(logger, restPath, "PUT result =", str); try { t = gson.fromJson(str, clazz);