2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019-2020 Nordix Foundation.
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=========================================================
22 package org.onap.policy.rest;
24 import java.nio.charset.StandardCharsets;
26 import java.util.Map.Entry;
27 import javax.xml.bind.DatatypeConverter;
28 import org.apache.commons.lang3.tuple.Pair;
29 import org.apache.http.HttpHeaders;
30 import org.apache.http.HttpResponse;
31 import org.apache.http.client.methods.HttpDelete;
32 import org.apache.http.client.methods.HttpGet;
33 import org.apache.http.client.methods.HttpPatch;
34 import org.apache.http.client.methods.HttpPost;
35 import org.apache.http.client.methods.HttpPut;
36 import org.apache.http.client.methods.HttpRequestBase;
37 import org.apache.http.conn.ssl.NoopHostnameVerifier;
38 import org.apache.http.entity.StringEntity;
39 import org.apache.http.impl.client.CloseableHttpClient;
40 import org.apache.http.impl.client.HttpClientBuilder;
41 import org.apache.http.util.EntityUtils;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 public class RestManager {
46 private static final Logger logger = LoggerFactory.getLogger(RestManager.class);
48 // Constants for string literals
49 private static final String CONTENT_TYPE = "Content-Type";
55 * @param username the user name
56 * @param password the password
57 * @param headers any headers
58 * @param contentType what the content type is
59 * @param body body to send
60 * @return the response status code and the body
62 public Pair<Integer, String> put(String url, String username, String password, Map<String, String> headers,
63 String contentType, String body) {
64 HttpPut put = new HttpPut(url);
65 addHeaders(put, username, password, headers);
66 put.addHeader(CONTENT_TYPE, contentType);
68 StringEntity input = new StringEntity(body);
69 input.setContentType(contentType);
71 } catch (Exception e) {
72 logger.error("put threw: ", e);
75 return sendRequest(put);
82 * @param username the user name
83 * @param password the password
84 * @param headers any headers
85 * @param contentType what the content type is
86 * @param body body to send
87 * @return the response status code and the body
89 public Pair<Integer, String> post(String url, String username, String password, Map<String, String> headers,
90 String contentType, String body) {
91 HttpPost post = new HttpPost(url);
92 addHeaders(post, username, password, headers);
93 post.addHeader(CONTENT_TYPE, contentType);
95 StringEntity input = new StringEntity(body);
96 input.setContentType(contentType);
97 post.setEntity(input);
98 } catch (Exception e) {
99 logger.error("post threw: ", e);
102 return sendRequest(post);
109 * @param username user name
110 * @param password password
111 * @param headers any headers to add
112 * @return a Pair for the response status and the body
114 public Pair<Integer, String> get(String url, String username, String password, Map<String, String> headers) {
115 HttpGet get = new HttpGet(url);
116 addHeaders(get, username, password, headers);
117 return sendRequest(get);
121 * Perform REST Delete. <br/>
122 * <i>Note: Many REST endpoints will return a 400 error for delete requests with a non-empty body</i>
125 * @param username the user name
126 * @param password the password
127 * @param headers any headers
128 * @param contentType what the content type is
129 * @param body body (optional) to send
130 * @return the response status code and the body
132 public Pair<Integer, String> delete(String url, String username, String password, Map<String, String> headers,
133 String contentType, String body) {
134 HttpDeleteWithBody delete = new HttpDeleteWithBody(url);
135 addHeaders(delete, username, password, headers);
136 if (body != null && !body.isEmpty()) {
137 delete.addHeader(CONTENT_TYPE, contentType);
139 StringEntity input = new StringEntity(body);
140 input.setContentType(contentType);
141 delete.setEntity(input);
142 } catch (Exception e) {
143 logger.error("delete threw: ", e);
147 return sendRequest(delete);
151 * Perform REST Delete.
154 * @param username the user name
155 * @param password the password
156 * @param headers any headers
157 * @return the response status code and the body
159 public Pair<Integer, String> delete(String url, String username, String password, Map<String, String> headers) {
160 HttpDelete delete = new HttpDelete(url);
161 addHeaders(delete, username, password, headers);
162 return sendRequest(delete);
166 * Perform REST Patch.
169 * @param username the user name
170 * @param password the password
171 * @param headers any headers
172 * @param body body to send
173 * @return the response status code and the body
175 public Pair<Integer, String> patch(String url, String username, String password, Map<String, String> headers,
177 String contentType = "application/merge-patch+json";
178 HttpPatch patch = new HttpPatch(url);
179 addHeaders(patch, username, password, headers);
180 patch.addHeader(CONTENT_TYPE, contentType);
182 StringEntity input = new StringEntity(body);
183 input.setContentType(contentType);
184 patch.setEntity(input);
185 } catch (Exception e) {
186 logger.error("patch threw: ", e);
189 return sendRequest(patch);
195 * @param request http request to send
196 * @return the response status code and the body
198 private Pair<Integer, String> sendRequest(HttpRequestBase request) {
199 if (logger.isDebugEnabled()) {
200 logger.debug("***** sendRequest to url {}:", request.getURI());
203 try (CloseableHttpClient client =
204 HttpClientBuilder.create().setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).build()) {
205 HttpResponse response = client.execute(request);
206 if (response != null) {
207 String returnBody = EntityUtils.toString(response.getEntity(), "UTF-8");
208 logger.debug("HTTP Response Status Code: {}", response.getStatusLine().getStatusCode());
209 logger.debug("HTTP Response Body:");
210 logger.debug(returnBody);
212 return Pair.of(response.getStatusLine().getStatusCode(), returnBody);
214 logger.error("Response from {} is null", request.getURI());
217 } catch (Exception e) {
218 logger.error("Request failed to {}", request.getURI(), e);
224 * Add header to the request.
226 * @param request http request to send
227 * @param username the user name
228 * @param password the password
229 * @param headers any headers
231 private void addHeaders(HttpRequestBase request, String username, String password, Map<String, String> headers) {
232 String authHeader = makeAuthHeader(username, password);
233 if (headers != null) {
234 for (Entry<String, String> entry : headers.entrySet()) {
235 request.addHeader(entry.getKey(), headers.get(entry.getKey()));
238 if (authHeader != null) {
239 request.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
243 private String makeAuthHeader(String username, String password) {
244 if (username == null || username.isEmpty()) {
248 String auth = username + ":" + (password == null ? "" : password);
249 return "Basic " + DatatypeConverter.printBase64Binary(auth.getBytes(StandardCharsets.ISO_8859_1));