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 * Modifications copyright (c) 2018 Nokia
21 * ===================================================================
25 package org.onap.clamp.clds.client;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
30 import java.io.BufferedReader;
31 import java.io.DataOutputStream;
32 import java.io.IOException;
33 import java.io.InputStreamReader;
34 import java.net.HttpURLConnection;
37 import javax.net.ssl.HttpsURLConnection;
38 import javax.ws.rs.BadRequestException;
40 import org.apache.commons.io.IOUtils;
41 import org.onap.clamp.clds.util.LoggingUtils;
42 import org.springframework.stereotype.Component;
45 * This class manages the HTTP and HTTPS connections to DCAE.
48 public class DcaeHttpConnectionManager {
49 protected static final EELFLogger logger = EELFManager.getInstance().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 String doHttpsQuery(URL url, String requestMethod, String payload, String contentType) throws IOException {
54 logger.info("Using HTTPS URL to contact DCAE:" + url.toString());
55 HttpsURLConnection secureConnection = (HttpsURLConnection) url.openConnection();
56 secureConnection.setRequestMethod(requestMethod);
57 secureConnection.setRequestProperty("X-ECOMP-RequestID", LoggingUtils.getRequestId());
58 if (payload != null && contentType != null) {
59 secureConnection.setRequestProperty("Content-Type", contentType);
60 secureConnection.setDoOutput(true);
61 try (DataOutputStream wr = new DataOutputStream(secureConnection.getOutputStream())) {
62 wr.writeBytes(payload);
66 int responseCode = secureConnection.getResponseCode();
67 logger.info("Response Code: " + responseCode);
68 if (responseCode < 400) {
69 try (BufferedReader reader = new BufferedReader(new InputStreamReader(secureConnection.getInputStream()))) {
70 String responseStr = IOUtils.toString(reader);
71 logger.info("Response Content: " + responseStr);
75 // In case of connection failure just check whether there is a
77 try (BufferedReader reader = new BufferedReader(new InputStreamReader(secureConnection.getErrorStream()))) {
78 String responseStr = IOUtils.toString(reader);
79 logger.error(DCAE_REQUEST_FAILED_LOG + responseStr);
80 throw new BadRequestException(responseStr);
85 private String doHttpQuery(URL url, String requestMethod, String payload, String contentType) throws IOException {
86 LoggingUtils utils = new LoggingUtils(logger);
87 logger.info("Using HTTP URL to contact DCAE:" + url);
88 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
89 connection = utils.invoke(connection, "DCAE", requestMethod);
90 connection.setRequestMethod(requestMethod);
91 connection.setRequestProperty("X-ECOMP-RequestID", LoggingUtils.getRequestId());
92 if (payload != null && contentType != null) {
93 connection.setRequestProperty("Content-Type", contentType);
94 connection.setDoOutput(true);
95 try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) {
96 wr.writeBytes(payload);
100 int responseCode = connection.getResponseCode();
101 logger.info("Response Code: " + responseCode);
102 if (responseCode < 400) {
103 try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
104 String responseStr = IOUtils.toString(reader);
105 logger.info("Response Content: " + responseStr);
106 utils.invokeReturn();
110 // In case of connection failure just check whether there is a
112 try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()))) {
113 String responseStr = IOUtils.toString(reader);
114 logger.error(DCAE_REQUEST_FAILED_LOG + responseStr);
115 utils.invokeReturn();
116 throw new BadRequestException(responseStr);
122 * This method does a HTTP/HTTPS query to DCAE with parameters specified.
125 * The string HTTP or HTTPS that mustr be used to connect
126 * @param requestMethod
127 * The Request Method (PUT, POST, GET, DELETE, etc ...)
129 * The payload if any, in that case an ouputstream is opened
131 * The "application/json or application/xml, or whatever"
132 * @return The payload of the answer
133 * @throws IOException
134 * In case of issue with the streams
136 public String doDcaeHttpQuery(String url, String requestMethod, String payload, String contentType)
138 URL urlObj = new URL(url);
139 if (url.contains("https://")) { // Support for HTTPS
140 return doHttpsQuery(urlObj, requestMethod, payload, contentType);
141 } else { // Support for HTTP
142 return doHttpQuery(urlObj, requestMethod, payload, contentType);