2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.rest;
23 import java.nio.charset.Charset;
25 import java.util.Map.Entry;
26 import javax.xml.bind.DatatypeConverter;
28 import org.apache.http.HttpHeaders;
29 import org.apache.http.HttpResponse;
30 import org.apache.http.client.methods.HttpGet;
31 import org.apache.http.client.methods.HttpPost;
32 import org.apache.http.client.methods.HttpRequestBase;
33 import org.apache.http.conn.ssl.NoopHostnameVerifier;
34 import org.apache.http.entity.StringEntity;
35 import org.apache.http.impl.client.CloseableHttpClient;
36 import org.apache.http.impl.client.HttpClientBuilder;
37 import org.apache.http.util.EntityUtils;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public class RestManager {
43 private static final Logger logger = LoggerFactory.getLogger(RestManager.class);
45 public class Pair<A, B> {
47 public final B second;
49 public Pair(A first, B second) {
59 * @param username the user name
60 * @param password the password
61 * @param headers any headers
62 * @param contentType what the content type is
63 * @param body body to send
64 * @return the response status code and the body
66 public Pair<Integer, String> post(String url, String username, String password,
67 Map<String, String> headers, String contentType, String body) {
68 HttpPost post = new HttpPost(url);
69 addHeaders(post, username, password, headers);
70 post.addHeader("Content-Type", contentType);
72 StringEntity input = new StringEntity(body);
73 input.setContentType(contentType);
74 post.setEntity(input);
75 } catch (Exception e) {
76 logger.error("post threw: ", e);
79 return sendRequest(post);
86 * @param username user name
87 * @param password password
88 * @param headers any headers to add
89 * @return a Pair for the response status and the body
91 public Pair<Integer, String> get(String url, String username, String password,
92 Map<String, String> headers) {
93 HttpGet get = new HttpGet(url);
94 addHeaders(get, username, password, headers);
95 return sendRequest(get);
99 * Perform REST Delete.
102 * @param username the user name
103 * @param password the password
104 * @param headers any headers
105 * @param contentType what the content type is
106 * @param body body (optional) to send
107 * @return the response status code and the body
109 public Pair<Integer, String> delete(String url, String username, String password, Map<String, String> headers,
110 String contentType, String body) {
111 HttpDeleteWithBody delete = new HttpDeleteWithBody(url);
112 addHeaders(delete, username, password, headers);
113 delete.addHeader("Content-Type", contentType);
114 if (body != null && !body.isEmpty()) {
116 StringEntity input = new StringEntity(body);
117 input.setContentType(contentType);
118 delete.setEntity(input);
119 } catch (Exception e) {
120 logger.error("delete threw: ", e);
124 return sendRequest(delete);
130 * @param request http request to send
131 * @return the response status code and the body
133 private Pair<Integer, String> sendRequest(HttpRequestBase request) {
134 if (logger.isDebugEnabled()) {
135 logger.debug("***** sendRequest to url {}:", request.getURI());
138 try (CloseableHttpClient client =
141 .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
143 HttpResponse response = client.execute(request);
144 if (response != null) {
145 String returnBody = EntityUtils.toString(response.getEntity(), "UTF-8");
146 logger.debug("HTTP Response Status Code: {}",
147 response.getStatusLine().getStatusCode());
148 logger.debug("HTTP Response Body:");
149 logger.debug(returnBody);
151 return new Pair<>(response.getStatusLine().getStatusCode(),
154 logger.error("Response from {} is null", request.getURI());
157 } catch (Exception e) {
158 logger.error("Request failed to {}", request.getURI(), e);
164 * Add header to the request.
166 * @param request http request to send
167 * @param username the user name
168 * @param password the password
169 * @param headers any headers
171 private void addHeaders(HttpRequestBase request, String username, String password, Map<String,
173 String authHeader = makeAuthHeader(username, password);
174 if (headers != null) {
175 for (Entry<String, String> entry : headers.entrySet()) {
176 request.addHeader(entry.getKey(), headers.get(entry.getKey()));
179 if (authHeader != null) {
180 request.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
184 private String makeAuthHeader(String username, String password) {
185 if (username == null || username.isEmpty()) {
189 String auth = username + ":" + (password == null ? "" : password);
190 return "Basic " + DatatypeConverter.printBase64Binary(auth.getBytes(Charset.forName("ISO-8859-1")));