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.policy.rest;
23 import java.io.IOException;
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.entity.StringEntity;
33 import org.apache.http.impl.client.BasicCredentialsProvider;
34 import org.apache.http.impl.client.CloseableHttpClient;
35 import org.apache.http.impl.client.HttpClientBuilder;
36 import org.apache.http.util.EntityUtils;
38 public final class RESTManager {
40 public static class Pair<A, B> {
44 public Pair(A a, B b) {
50 public static Pair<Integer, String> post(String url, String username, String password, Map<String, String> headers, String contentType, String body) {
51 CredentialsProvider credentials = new BasicCredentialsProvider();
52 credentials.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
54 System.out.println("HTTP REQUEST: " + url + " -> " + username + ((password!=null)?password.length():"-") + " -> " + contentType);
55 if (headers != null) {
56 System.out.println("Headers: ");
57 headers.forEach((name, value) -> {
58 System.out.println(name + " -> " + value);
61 System.out.println(body);
63 try (CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(credentials).build()) {
65 HttpPost post = new HttpPost(url);
66 for (String key : headers.keySet()) {
67 post.addHeader(key, headers.get(key));
69 post.addHeader("Content-Type", contentType);
71 StringEntity input = new StringEntity(body);
72 input.setContentType(contentType);
73 post.setEntity(input);
75 HttpResponse response = client.execute(post);
77 String returnBody = EntityUtils.toString(response.getEntity(), "UTF-8");
78 System.out.println("HTTP POST Response Status Code: " + response.getStatusLine().getStatusCode());
79 System.out.println("HTTP POST Response Body:");
80 System.out.println(returnBody);
82 return new Pair<Integer, String>(response.getStatusLine().getStatusCode(), returnBody);
83 } catch (IOException e) {
84 System.err.println("Failed to POST to " + url + e.getLocalizedMessage());
90 public static Pair<Integer, String> get(String url, String username, String password, Map<String, String> headers) {
92 CredentialsProvider credentials = new BasicCredentialsProvider();
93 credentials.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
95 try (CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(credentials).build()) {
97 HttpGet get = new HttpGet(url);
98 for (String key : headers.keySet()) {
99 get.addHeader(key, headers.get(key));
102 HttpResponse response = client.execute(get);
104 String returnBody = EntityUtils.toString(response.getEntity(), "UTF-8");
105 System.out.println("HTTP GET Response Status Code: " + response.getStatusLine().getStatusCode());
106 System.out.println("HTTP GET Response Body:");
107 System.out.println(returnBody);
109 return new Pair<Integer, String>(response.getStatusLine().getStatusCode(), returnBody);
110 } catch (IOException e) {
111 System.err.println("Failed to GET to " + url + e.getLocalizedMessage());