/*-
- * ================================================================================
- * ECOMP Portal
- * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property
- * ================================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
+ * ============LICENSE_START==========================================
+ * ONAP Portal
+ * ===================================================================
+ * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
+ * ===================================================================
+ *
+ * Unless otherwise specified, all software contained herein is licensed
+ * under the Apache License, Version 2.0 (the “License”);
+ * you may not use this software except in compliance with the License.
* You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
- * ================================================================================
+ *
+ * Unless otherwise specified, all documentation contained herein is licensed
+ * under the Creative Commons License, Attribution 4.0 Intl. (the “License”);
+ * you may not use this documentation except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://creativecommons.org/licenses/by/4.0/
+ *
+ * Unless required by applicable law or agreed to in writing, documentation
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * ============LICENSE_END============================================
+ *
+ * ECOMP is a trademark and service mark of AT&T Intellectual Property.
*/
package org.openecomp.portalapp.portal.service;
@org.springframework.context.annotation.Configuration
@EnableAspectJAutoProxy
@EPAuditLog
-public class ApplicationsRestClientServiceImpl implements ApplicationsRestClientService{
-
+public class ApplicationsRestClientServiceImpl implements ApplicationsRestClientService {
+
private static final String PASSWORD_HEADER = "password";
private static final String APP_USERNAME_HEADER = "username";
private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ApplicationsRestClientServiceImpl.class);
-
+
@Autowired
private AppsCacheService appsCacheService;
Gson gson = null;
-
+
private final ObjectMapper mapper = new ObjectMapper();
-
+
@PostConstruct
- private void init(){
+ private void init() {
logger.debug(EELFLoggerDelegate.debugLogger, "initializing");
- GsonBuilder builder = new GsonBuilder();
+ GsonBuilder builder = new GsonBuilder();
- // Register an adapter to manage the date types as long values
- builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
- public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
- return new Date(json.getAsJsonPrimitive().getAsLong());
- }
+ // Register an adapter to manage the date types as long values
+ builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
+ public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
+ throws JsonParseException {
+ return new Date(json.getAsJsonPrimitive().getAsLong());
+ }
});
- gson = builder.create();
+ gson = builder.create();
}
-
+
// TODO: do we need to do additional logging for remote API calls?
private static WebClient createClientForPath(String baseUri, String path) {
- logger.info(EELFLoggerDelegate.debugLogger, "Creating web client for " + baseUri + " + " + path);
+ logger.info(EELFLoggerDelegate.debugLogger, "Creating web client for " + baseUri + " + " + path);
WebClient client = WebClient.create(baseUri);
client.type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON);
client.path(path);
logger.debug(EELFLoggerDelegate.debugLogger, "http response status=" + status);
MDC.put(EPSystemProperties.EXTERNAL_API_RESPONSE_CODE, Integer.toString(status));
if (!isHttpSuccess(status)) {
- String errMsg = "Failed. Status=" + status + "; [" + response.getStatusInfo().getReasonPhrase().toString() + "]";
+ String errMsg = "Failed. Status=" + status + "; [" + response.getStatusInfo().getReasonPhrase().toString()
+ + "]";
URL url = null;
try {
// must not be null to avoid NPE in HTTPException constructor
throw new HTTPException(status, errMsg, url);
}
}
-
- private static boolean isHttpSuccess(int status){
+
+ private static boolean isHttpSuccess(int status) {
return status / 100 == 2;
}
@EPMetricsLog
private WebClient createClientForApp(long appId, String restPath) {
- logger.debug(EELFLoggerDelegate.debugLogger, "creating client for appId=" + appId + "; restPath=" + restPath);
+ logger.debug(EELFLoggerDelegate.debugLogger, "creating client for appId=" + appId + "; restPath=" + restPath);
EPApp externalApp = appsCacheService.getApp(appId);
- if(externalApp != null){
+ if (externalApp != null) {
String appBaseUri = externalApp.getAppRestEndpoint();
- String username = externalApp.getUsername();
+ String username = externalApp.getUsername();
String encriptedPwd = externalApp.getAppPassword();
String decreptedAppPwd = "";
-
- //Set local context
+
+ // Set local context
MDC.put(EPSystemProperties.PROTOCOL, EPSystemProperties.HTTP);
- if (appBaseUri!=null && appBaseUri.contains("https")) {
+ if (appBaseUri != null && appBaseUri.contains("https")) {
MDC.put(EPSystemProperties.PROTOCOL, EPSystemProperties.HTTPS);
}
MDC.put(EPSystemProperties.FULL_URL, appBaseUri + restPath);
MDC.put(EPSystemProperties.TARGET_ENTITY, externalApp.getName());
MDC.put(EPSystemProperties.TARGET_SERVICE_NAME, restPath);
-
+
try {
- decreptedAppPwd = CipherUtil.decrypt(encriptedPwd, SystemProperties.getProperty(SystemProperties.Decryption_Key));
- } catch (Exception e) {
- logger.error(EELFLoggerDelegate.errorLogger, "Unable to decrypt App name = " + externalApp, EcompPortalUtils.getStackTrace(e));
- logger.error(EELFLoggerDelegate.errorLogger, "Unable to decrypt App name = " + externalApp, EcompPortalUtils.getStackTrace(e));
+ decreptedAppPwd = CipherUtil.decrypt(encriptedPwd,
+ SystemProperties.getProperty(SystemProperties.Decryption_Key));
+ } catch (Exception e) {
+ logger.error(EELFLoggerDelegate.errorLogger, "createClientForApp failed to decrypt", e);
}
- logger.debug(EELFLoggerDelegate.debugLogger, String.format("App %d found, baseUri=[%s], Headers: [%s=%s, %s=%s]", appId, appBaseUri, APP_USERNAME_HEADER, username, PASSWORD_HEADER, encriptedPwd));
+ logger.debug(EELFLoggerDelegate.debugLogger,
+ String.format("App %d found, baseUri=[%s], Headers: [%s=%s, %s=%s]", appId, appBaseUri,
+ APP_USERNAME_HEADER, username, PASSWORD_HEADER, encriptedPwd));
WebClient client = createClientForPath(appBaseUri, restPath);
client.header(APP_USERNAME_HEADER, username);
client.header(PASSWORD_HEADER, decreptedAppPwd);
client.header(SystemProperties.ECOMP_REQUEST_ID, MDC.get(MDC_KEY_REQUEST_ID));
client.header(SystemProperties.USERAGENT_NAME, EPSystemProperties.ECOMP_PORTAL_BE);
-
+
return client;
}
return null;
}
-
+
@Override
public <T> T get(Class<T> clazz, long appId, String restPath) throws HTTPException {
-
+
WebClient webClient = null;
Response response = null;
T t = null;
-
+
webClient = createClientForApp(appId, restPath);
EcompPortalUtils.logAndSerializeObject(restPath, "GET request =", "no-payload");
-
+
try {
- if (webClient!=null) {
+ if (webClient != null) {
response = webClient.get();
} else {
- logger.error(EELFLoggerDelegate.errorLogger, "Unable to create the Webclient to make the '" + restPath + "' API call.");
+ logger.error(EELFLoggerDelegate.errorLogger,
+ "Unable to create the Webclient to make the '" + restPath + "' API call.");
}
} catch (Exception e) {
- MDC.put(EPSystemProperties.EXTERNAL_API_RESPONSE_CODE, Integer.toString(HttpServletResponse.SC_INTERNAL_SERVER_ERROR));
+ MDC.put(EPSystemProperties.EXTERNAL_API_RESPONSE_CODE,
+ Integer.toString(HttpServletResponse.SC_INTERNAL_SERVER_ERROR));
EPLogUtil.logEcompError(EPAppMessagesEnum.BeRestApiGeneralError);
- logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while making the GET REST API call, Details: " + EcompPortalUtils.getStackTrace(e));
+ logger.error(EELFLoggerDelegate.errorLogger, "get 1 failed", e);
}
-
- if (response!=null) {
+
+ if (response != null) {
verifyResponse(response);
String str = response.readEntity(String.class);
EcompPortalUtils.logAndSerializeObject(restPath, "GET result =", str);
- try {
- t = mapper.readValue(str, clazz);
- } catch(Exception e) {
- logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
- EPLogUtil.logEcompError(EPAppMessagesEnum.BeInvalidJsonInput);
- }
+ try {
+ t = mapper.readValue(str, clazz);
+ } catch (Exception e) {
+ logger.error(EELFLoggerDelegate.errorLogger, "get 2 failed", e);
+ EPLogUtil.logEcompError(EPAppMessagesEnum.BeInvalidJsonInput);
+ }
}
-
+
return t;
}
WebClient client = null;
Response response = null;
T t = null;
-
+
client = createClientForApp(appId, restPath);
EcompPortalUtils.logAndSerializeObject(restPath, "POST request =", payload);
-
-
+
try {
- if (client!=null) {
+ if (client != null) {
response = client.post(payload);
} else {
- logger.error(EELFLoggerDelegate.errorLogger, "Unable to create the Webclient to make the '" + restPath + "' API call.");
+ logger.error(EELFLoggerDelegate.errorLogger,
+ "Unable to create the Webclient to make the {} API call", restPath);
}
} catch (Exception e) {
- MDC.put(EPSystemProperties.EXTERNAL_API_RESPONSE_CODE, Integer.toString(HttpServletResponse.SC_INTERNAL_SERVER_ERROR));
+ MDC.put(EPSystemProperties.EXTERNAL_API_RESPONSE_CODE,
+ Integer.toString(HttpServletResponse.SC_INTERNAL_SERVER_ERROR));
EPLogUtil.logEcompError(EPAppMessagesEnum.BeRestApiGeneralError);
- logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while making the POST REST API call, Details: " + EcompPortalUtils.getStackTrace(e));
+ logger.error(EELFLoggerDelegate.errorLogger, "post 1 failed", e);
}
-
- if (response!=null) {
+
+ if (response != null) {
verifyResponse(response);
-
- //String contentType = response.getHeaderString("Content-Type");
- if(clazz != null) {
+
+ // String contentType = response.getHeaderString("Content-Type");
+ if (clazz != null) {
String str = response.readEntity(String.class);
EcompPortalUtils.logAndSerializeObject(restPath, "POST result =", str);
- try { t = gson.fromJson(str, clazz); } catch (Exception e) {
- logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
+ try {
+ t = gson.fromJson(str, clazz);
+ } catch (Exception e) {
+ logger.error(EELFLoggerDelegate.errorLogger, "post 2 failed", e);
EPLogUtil.logEcompError(EPAppMessagesEnum.BeInvalidJsonInput);
- }
+ }
}
}
return t;
}
@Override
- public <T> T put(Class<T> clazz, long appId, Object payload, String restPath) throws HTTPException {
+ public <T> T put(Class<T> clazz, long appId, Object payload, String restPath) throws HTTPException {
WebClient client = null;
Response response = null;
T t = null;
-
+
client = createClientForApp(appId, restPath);
EcompPortalUtils.logAndSerializeObject(restPath, "PUT request =", payload);
-
+
try {
- if (client!=null) {
+ if (client != null) {
response = client.put(payload);
} else {
- logger.error(EELFLoggerDelegate.errorLogger, "Unable to create the Webclient to make the '" + restPath + "' API call.");
+ logger.error(EELFLoggerDelegate.errorLogger,
+ "Unable to create the Webclient to make the '" + restPath + "' API call.");
}
- } catch(Exception e) {
- MDC.put(EPSystemProperties.EXTERNAL_API_RESPONSE_CODE, Integer.toString(HttpServletResponse.SC_INTERNAL_SERVER_ERROR));
+ } catch (Exception e) {
+ MDC.put(EPSystemProperties.EXTERNAL_API_RESPONSE_CODE,
+ Integer.toString(HttpServletResponse.SC_INTERNAL_SERVER_ERROR));
EPLogUtil.logEcompError(EPAppMessagesEnum.BeRestApiGeneralError);
- logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while making the PUT REST API call, Details: " + EcompPortalUtils.getStackTrace(e));
+ logger.error(EELFLoggerDelegate.errorLogger, "put 1 failed", e);
}
-
- if (response!=null) {
+
+ if (response != null) {
verifyResponse(response);
String str = response.readEntity(String.class);
EcompPortalUtils.logAndSerializeObject(restPath, "PUT result =", str);
- try { t = gson.fromJson(str, clazz); } catch (Exception e) {
- logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
+ try {
+ t = gson.fromJson(str, clazz);
+ } catch (Exception e) {
+ logger.error(EELFLoggerDelegate.errorLogger, "put 2 failed", e);
EPLogUtil.logEcompError(EPAppMessagesEnum.BeInvalidJsonInput);
- }
+ }
}
return t;
}