2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.vnf.health;
23 import java.io.IOException;
25 import java.util.Map.Entry;
26 import org.apache.http.HttpResponse;
27 import org.apache.http.auth.AuthScope;
28 import org.apache.http.auth.UsernamePasswordCredentials;
29 import org.apache.http.client.CredentialsProvider;
30 import org.apache.http.client.methods.HttpGet;
31 import org.apache.http.client.methods.HttpPost;
32 import org.apache.http.conn.ssl.NoopHostnameVerifier;
33 import org.apache.http.entity.StringEntity;
34 import org.apache.http.impl.client.BasicCredentialsProvider;
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> {
49 public Pair(A a, B b) {
55 public Pair<Integer, String> post(String url, String username, String password,
56 Map<String, String> headers, String contentType, String body) {
57 CredentialsProvider credentials = new BasicCredentialsProvider();
58 credentials.setCredentials(AuthScope.ANY,
59 new UsernamePasswordCredentials(username, password));
61 logger.debug("HTTP REQUEST: {} -> {} {} -> {}", url, username,
62 ((password != null) ? password.length() : "-"), contentType);
63 if (headers != null) {
64 logger.debug("Headers: ");
65 headers.forEach((name, value) -> logger.debug("{} -> {}", name, value));
69 try (CloseableHttpClient client =
72 .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
73 .setDefaultCredentialsProvider(credentials)
76 HttpPost post = new HttpPost(url);
77 if (headers != null) {
78 for (Entry<String, String> entry : headers.entrySet()) {
79 post.addHeader(entry.getKey(), headers.get(entry.getKey()));
82 post.addHeader("Content-Type", contentType);
84 StringEntity input = new StringEntity(body);
85 input.setContentType(contentType);
86 post.setEntity(input);
88 HttpResponse response = client.execute(post);
89 if (response != null) {
90 String returnBody = EntityUtils.toString(response.getEntity(), "UTF-8");
91 logger.debug("HTTP POST Response Status Code: {}",
92 response.getStatusLine().getStatusCode());
93 logger.debug("HTTP POST Response Body:");
94 logger.debug(returnBody);
96 return new Pair<>(response.getStatusLine().getStatusCode(),
100 logger.error("Response from {} is null", url);
104 catch (Exception e) {
105 logger.error("Failed to POST to {}", url, e);
110 public Pair<Integer, String> get(String url, String username, String password,
111 Map<String, String> headers) {
113 CredentialsProvider credentials = new BasicCredentialsProvider();
114 credentials.setCredentials(AuthScope.ANY,
115 new UsernamePasswordCredentials(username, password));
117 try (CloseableHttpClient client =
120 .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
121 .setDefaultCredentialsProvider(credentials)
124 HttpGet get = new HttpGet(url);
125 if (headers != null) {
126 for (Entry<String, String> entry : headers.entrySet()) {
127 get.addHeader(entry.getKey(), headers.get(entry.getKey()));
131 HttpResponse response = client.execute(get);
133 String returnBody = EntityUtils.toString(response.getEntity(), "UTF-8");
135 logger.debug("HTTP GET Response Status Code: {}",
136 response.getStatusLine().getStatusCode());
137 logger.debug("HTTP GET Response Body:");
138 logger.debug(returnBody);
140 return new Pair<>(response.getStatusLine().getStatusCode(), returnBody);
142 catch (IOException e) {
143 logger.error("Failed to GET to {}", url, e);