Removal of dead code
[clamp.git] / src / main / java / org / onap / clamp / clds / client / DcaeHttpConnectionManager.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                             reserved.
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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  * ===================================================================
22  *
23  */
24
25 package org.onap.clamp.clds.client;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29
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;
35 import java.net.URL;
36
37 import javax.net.ssl.HttpsURLConnection;
38 import javax.ws.rs.BadRequestException;
39
40 import org.apache.commons.io.IOUtils;
41 import org.onap.clamp.clds.util.LoggingUtils;
42 import org.springframework.stereotype.Component;
43
44 /**
45  *
46  * This class manages the HTTP and HTTPS connections to DCAE.
47  *
48  */
49 @Component
50 public class DcaeHttpConnectionManager {
51     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(DcaeHttpConnectionManager.class);
52     protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
53     private static final String DCAE_REQUEST_FAILED_LOG = "Request Failed - response payload=";
54
55     private String doHttpsQuery(URL url, String requestMethod, String payload, String contentType) throws IOException {
56         logger.info("Using HTTPS URL to contact DCAE:" + url.toString());
57         HttpsURLConnection secureConnection = (HttpsURLConnection) url.openConnection();
58         secureConnection.setRequestMethod(requestMethod);
59         secureConnection.setRequestProperty("X-ECOMP-RequestID", LoggingUtils.getRequestId());
60         if (payload != null && contentType != null) {
61             secureConnection.setRequestProperty("Content-Type", contentType);
62             secureConnection.setDoOutput(true);
63             try (DataOutputStream wr = new DataOutputStream(secureConnection.getOutputStream())) {
64                 wr.writeBytes(payload);
65                 wr.flush();
66             }
67         }
68         int responseCode = secureConnection.getResponseCode();
69         logger.info("Response Code: " + responseCode);
70         if (responseCode < 400) {
71             try (BufferedReader reader = new BufferedReader(new InputStreamReader(secureConnection.getInputStream()))) {
72                 String responseStr = IOUtils.toString(reader);
73                 logger.info("Response Content: " + responseStr);
74                 return responseStr;
75             }
76         } else {
77             // In case of connection failure just check whether there is a
78             // content or not
79             try (BufferedReader reader = new BufferedReader(new InputStreamReader(secureConnection.getErrorStream()))) {
80                 String responseStr = IOUtils.toString(reader);
81                 logger.error(DCAE_REQUEST_FAILED_LOG + responseStr);
82                 throw new BadRequestException(responseStr);
83             }
84         }
85     }
86
87     private String doHttpQuery(URL url, String requestMethod, String payload, String contentType) throws IOException {
88         LoggingUtils utils = new LoggingUtils(logger);
89         logger.info("Using HTTP URL to contact DCAE:" + url);
90         HttpURLConnection connection = (HttpURLConnection) url.openConnection();
91         connection = utils.invoke(connection, "DCAE", requestMethod);
92         connection.setRequestMethod(requestMethod);
93         connection.setRequestProperty("X-ECOMP-RequestID", LoggingUtils.getRequestId());
94         if (payload != null && contentType != null) {
95             connection.setRequestProperty("Content-Type", contentType);
96             connection.setDoOutput(true);
97             try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) {
98                 wr.writeBytes(payload);
99                 wr.flush();
100             }
101         }
102         int responseCode = connection.getResponseCode();
103         logger.info("Response Code: " + responseCode);
104         if (responseCode < 400) {
105             try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
106                 String responseStr = IOUtils.toString(reader);
107                 logger.info("Response Content: " + responseStr);
108                 utils.invokeReturn();
109                 return responseStr;
110             }
111         } else {
112             // In case of connection failure just check whether there is a
113             // content or not
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                 utils.invokeReturn();
118                 throw new BadRequestException(responseStr);
119             }
120         }
121     }
122
123     /**
124      * This method does a HTTP/HTTPS query to DCAE with parameters specified.
125      *
126      * @param url
127      *        The string HTTP or HTTPS that mustr be used to connect
128      * @param requestMethod
129      *        The Request Method (PUT, POST, GET, DELETE, etc ...)
130      * @param payload
131      *        The payload if any, in that case an ouputstream is opened
132      * @param contentType
133      *        The "application/json or application/xml, or whatever"
134      * @return The payload of the answer
135      * @throws IOException
136      *         In case of issue with the streams
137      */
138     public String doDcaeHttpQuery(String url, String requestMethod, String payload, String contentType)
139         throws IOException {
140         URL urlObj = new URL(url);
141         if (url.contains("https://")) { // Support for HTTPS
142             return doHttpsQuery(urlObj, requestMethod, payload, contentType);
143         } else { // Support for HTTP
144             return doHttpQuery(urlObj, requestMethod, payload, contentType);
145         }
146     }
147 }