2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
20 * ===================================================================
24 package org.onap.clamp.clds.client;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
29 import java.io.BufferedReader;
30 import java.io.DataOutputStream;
31 import java.io.IOException;
32 import java.io.InputStreamReader;
33 import java.net.HttpURLConnection;
36 import javax.net.ssl.HttpsURLConnection;
37 import javax.ws.rs.BadRequestException;
39 import org.apache.commons.io.IOUtils;
40 import org.onap.clamp.clds.util.LoggingUtils;
44 * This class manages the HTTP and HTTPS connections to DCAE.
47 public class DcaeHttpConnectionManager {
48 protected static final EELFLogger logger = EELFManager.getInstance()
49 .getLogger(DcaeHttpConnectionManager.class);
50 protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
51 private static final String DCAE_REQUEST_FAILED_LOG = "Request Failed - response payload=";
53 private DcaeHttpConnectionManager() {
56 private static String doHttpsQuery(URL url, String requestMethod, String payload, String contentType)
58 logger.info("Using HTTPS URL to contact DCAE:" + url.toString());
59 HttpsURLConnection secureConnection = (HttpsURLConnection) url.openConnection();
60 secureConnection.setRequestMethod(requestMethod);
61 secureConnection.setRequestProperty("X-ECOMP-RequestID", LoggingUtils.getRequestId());
62 if (payload != null && contentType != null) {
63 secureConnection.setRequestProperty("Content-Type", contentType);
64 secureConnection.setDoOutput(true);
65 try (DataOutputStream wr = new DataOutputStream(secureConnection.getOutputStream())) {
66 wr.writeBytes(payload);
70 int responseCode = secureConnection.getResponseCode();
71 logger.info("Response Code: " + responseCode);
72 if (responseCode < 400) {
73 try (BufferedReader reader = new BufferedReader(new InputStreamReader(secureConnection.getInputStream()))) {
74 String responseStr = IOUtils.toString(reader);
75 logger.info("Response Content: " + responseStr);
79 // In case of connection failure just check whether there is a
81 try (BufferedReader reader = new BufferedReader(new InputStreamReader(secureConnection.getErrorStream()))) {
82 String responseStr = IOUtils.toString(reader);
83 logger.error(DCAE_REQUEST_FAILED_LOG + responseStr);
84 throw new BadRequestException(responseStr);
89 private static String doHttpQuery(URL url, String requestMethod, String payload, String contentType)
91 logger.info("Using HTTP URL to contact DCAE:" + url);
92 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
93 connection.setRequestMethod(requestMethod);
94 connection.setRequestProperty("X-ECOMP-RequestID", LoggingUtils.getRequestId());
95 if (payload != null && contentType != null) {
96 connection.setRequestProperty("Content-Type", contentType);
97 connection.setDoOutput(true);
98 try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) {
99 wr.writeBytes(payload);
103 int responseCode = connection.getResponseCode();
104 logger.info("Response Code: " + responseCode);
105 if (responseCode < 400) {
106 try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
107 String responseStr = IOUtils.toString(reader);
108 logger.info("Response Content: " + responseStr);
112 // In case of connection failure just check whether there is a
114 try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()))) {
115 String responseStr = IOUtils.toString(reader);
116 logger.error(DCAE_REQUEST_FAILED_LOG + responseStr);
117 throw new BadRequestException(responseStr);
123 * This method does a HTTP/HTTPS query to DCAE with parameters specified.
126 * The string HTTP or HTTPS that mustr be used to connect
127 * @param requestMethod
128 * The Request Method (PUT, POST, GET, DELETE, etc ...)
130 * The payload if any, in that case an ouputstream is opened
132 * The "application/json or application/xml, or whatever"
133 * @return The payload of the answer
134 * @throws IOException
135 * In case of issue with the streams
137 public static String doDcaeHttpQuery(String url, String requestMethod, String payload, String contentType)
139 URL urlObj = new URL(url);
140 if (url.contains("https://")) { // Support for HTTPS
141 return doHttpsQuery(urlObj, requestMethod, payload, contentType);
142 } else { // Support for HTTP
143 return doHttpQuery(urlObj, requestMethod, payload, contentType);