aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSébastien Determe <sebastien.determe@intl.att.com>2019-03-29 08:41:02 +0000
committerGerrit Code Review <gerrit@onap.org>2019-03-29 08:41:02 +0000
commite10910d9070a80e47e81a6cd7c350a627080b6a2 (patch)
treec8cfa1172852dc858ec520e6bb205f9bcc2220d0
parent76c74f505d71c5bcd97dfb3b3560e69f37a7a83f (diff)
parent3a8e6a29c2332a3d5a7fd9b06e16ee22a2747f6a (diff)
Merge "Create common library for REST"
-rw-r--r--src/main/java/org/onap/clamp/clds/client/DcaeDispatcherServices.java9
-rw-r--r--src/main/java/org/onap/clamp/util/HttpConnectionManager.java146
2 files changed, 151 insertions, 4 deletions
diff --git a/src/main/java/org/onap/clamp/clds/client/DcaeDispatcherServices.java b/src/main/java/org/onap/clamp/clds/client/DcaeDispatcherServices.java
index 63caf5a93..e2a4c2f48 100644
--- a/src/main/java/org/onap/clamp/clds/client/DcaeDispatcherServices.java
+++ b/src/main/java/org/onap/clamp/clds/client/DcaeDispatcherServices.java
@@ -36,6 +36,7 @@ import org.json.simple.parser.ParseException;
import org.onap.clamp.clds.config.ClampProperties;
import org.onap.clamp.clds.exception.dcae.DcaeDeploymentException;
import org.onap.clamp.clds.util.LoggingUtils;
+import org.onap.clamp.util.HttpConnectionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -49,7 +50,7 @@ public class DcaeDispatcherServices {
protected static final EELFLogger logger = EELFManager.getInstance().getLogger(DcaeDispatcherServices.class);
protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
private final ClampProperties refProp;
- private final DcaeHttpConnectionManager dcaeHttpConnectionManager;
+ private final HttpConnectionManager dcaeHttpConnectionManager;
private static final String STATUS_URL_LOG = "Status URL extracted: ";
private static final String DCAE_URL_PREFIX = "/dcae-deployments/";
private static final String DCAE_URL_PROPERTY_NAME = "dcae.dispatcher.url";
@@ -58,7 +59,7 @@ public class DcaeDispatcherServices {
private static final String DCAE_STATUS_FIELD = "status";
@Autowired
- public DcaeDispatcherServices(ClampProperties refProp, DcaeHttpConnectionManager dcaeHttpConnectionManager) {
+ public DcaeDispatcherServices(ClampProperties refProp, HttpConnectionManager dcaeHttpConnectionManager) {
this.refProp = refProp;
this.dcaeHttpConnectionManager = dcaeHttpConnectionManager;
}
@@ -98,7 +99,7 @@ public class DcaeDispatcherServices {
Date startTime = new Date();
LoggingUtils.setTargetContext("DCAE", "getOperationStatus");
try {
- String responseStr = dcaeHttpConnectionManager.doDcaeHttpQuery(statusUrl, "GET", null, null);
+ String responseStr = dcaeHttpConnectionManager.doGeneralHttpQuery(statusUrl, "GET", null, null, "DCAE");
JSONObject jsonObj = parseResponse(responseStr);
String operationType = (String) jsonObj.get("operationType");
String status = (String) jsonObj.get(DCAE_STATUS_FIELD);
@@ -189,7 +190,7 @@ public class DcaeDispatcherServices {
String nodeAttr) throws IOException, ParseException {
Date startTime = new Date();
try {
- String responseStr = dcaeHttpConnectionManager.doDcaeHttpQuery(url, requestMethod, payload, contentType);
+ String responseStr = dcaeHttpConnectionManager.doGeneralHttpQuery(url, requestMethod, payload, contentType, "DCAE");
JSONObject jsonObj = parseResponse(responseStr);
JSONObject linksObj = (JSONObject) jsonObj.get(node);
String statusUrl = (String) linksObj.get(nodeAttr);
diff --git a/src/main/java/org/onap/clamp/util/HttpConnectionManager.java b/src/main/java/org/onap/clamp/util/HttpConnectionManager.java
new file mode 100644
index 000000000..9443301b1
--- /dev/null
+++ b/src/main/java/org/onap/clamp/util/HttpConnectionManager.java
@@ -0,0 +1,146 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP CLAMP
+ * ================================================================================
+ * Copyright (C) 2019 AT&T Intellectual Property. All rights
+ * reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * 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.
+ * ============LICENSE_END============================================
+ * Modifications copyright (c) 2018 Nokia
+ * ===================================================================
+ *
+ */
+
+package org.onap.clamp.util;
+
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
+
+import java.io.BufferedReader;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+import javax.net.ssl.HttpsURLConnection;
+import javax.ws.rs.BadRequestException;
+
+import org.apache.commons.io.IOUtils;
+import org.onap.clamp.clds.util.LoggingUtils;
+import org.springframework.stereotype.Component;
+
+/**
+ * This class manages the HTTP and HTTPS connections.
+ */
+@Component
+public class HttpConnectionManager {
+ protected static final EELFLogger logger = EELFManager.getInstance().getLogger(HttpConnectionManager.class);
+ protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
+ private static final String REQUEST_FAILED_LOG = "Request Failed - response payload=";
+
+ private String doHttpsQuery(URL url, String requestMethod, String payload, String contentType, String target) throws IOException {
+ LoggingUtils utils = new LoggingUtils(logger);
+ logger.info("Using HTTPS URL:" + url.toString());
+ HttpsURLConnection secureConnection = (HttpsURLConnection) url.openConnection();
+ secureConnection = utils.invokeHttps(secureConnection, target, requestMethod);
+ secureConnection.setRequestMethod(requestMethod);
+ secureConnection.setRequestProperty("X-ECOMP-RequestID", LoggingUtils.getRequestId());
+ if (payload != null && contentType != null) {
+ secureConnection.setRequestProperty("Content-Type", contentType);
+ secureConnection.setDoOutput(true);
+ try (DataOutputStream wr = new DataOutputStream(secureConnection.getOutputStream())) {
+ wr.writeBytes(payload);
+ wr.flush();
+ }
+ }
+ int responseCode = secureConnection.getResponseCode();
+ logger.info("Response Code: " + responseCode);
+ if (responseCode < 400) {
+ try (BufferedReader reader = new BufferedReader(new InputStreamReader(secureConnection.getInputStream()))) {
+ String responseStr = IOUtils.toString(reader);
+ logger.info("Response Content: " + responseStr);
+ return responseStr;
+ }
+ } else {
+ // In case of connection failure just check whether there is a
+ // content or not
+ try (BufferedReader reader = new BufferedReader(new InputStreamReader(secureConnection.getErrorStream()))) {
+ String responseStr = IOUtils.toString(reader);
+ logger.error(REQUEST_FAILED_LOG + responseStr);
+ throw new BadRequestException(responseStr);
+ }
+ }
+ }
+
+ private String doHttpQuery(URL url, String requestMethod, String payload, String contentType, String target) throws IOException {
+ LoggingUtils utils = new LoggingUtils(logger);
+ logger.info("Using HTTP URL:" + url);
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+ connection = utils.invoke(connection, target, requestMethod);
+ connection.setRequestMethod(requestMethod);
+ if (payload != null && contentType != null) {
+ connection.setRequestProperty("Content-Type", contentType);
+ connection.setDoOutput(true);
+ try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) {
+ wr.writeBytes(payload);
+ wr.flush();
+ }
+ }
+ int responseCode = connection.getResponseCode();
+ logger.info("Response Code: " + responseCode);
+ if (responseCode < 400) {
+ try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
+ String responseStr = IOUtils.toString(reader);
+ logger.info("Response Content: " + responseStr);
+ utils.invokeReturn();
+ return responseStr;
+ }
+ } else {
+ // In case of connection failure just check whether there is a
+ // content or not
+ try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()))) {
+ String responseStr = IOUtils.toString(reader);
+ logger.error(REQUEST_FAILED_LOG + responseStr);
+ utils.invokeReturn();
+ throw new BadRequestException(responseStr);
+ }
+ }
+ }
+
+ /**
+ * This method does a HTTP/HTTPS query with parameters specified.
+ *
+ * @param url
+ * The string HTTP or HTTPS that mustr be used to connect
+ * @param requestMethod
+ * The Request Method (PUT, POST, GET, DELETE, etc ...)
+ * @param payload
+ * The payload if any, in that case an ouputstream is opened
+ * @param contentType
+ * The "application/json or application/xml, or whatever"
+ * @return The payload of the answer
+ * @throws IOException
+ * In case of issue with the streams
+ */
+ public String doGeneralHttpQuery(String url, String requestMethod, String payload, String contentType, String target)
+ throws IOException {
+ URL urlObj = new URL(url);
+ if (url.contains("https://")) { // Support for HTTPS
+ return doHttpsQuery(urlObj, requestMethod, payload, contentType, target);
+ } else { // Support for HTTP
+ return doHttpQuery(urlObj, requestMethod, payload, contentType, target);
+ }
+ }
+}