[POLICY-22] Reorganizing drools-apps
[policy/drools-applications.git] / controlloop / common / model-impl / rest / src / main / java / org / onap / policy / rest / RESTManager.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * rest
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.policy.rest;
22
23 import java.io.IOException;
24 import java.util.Map;
25
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;
37
38 public final class RESTManager {
39
40         public static class Pair<A, B> {
41                 public final A a;
42                 public final B b;
43                 
44                 public Pair(A a, B b) {
45                         this.a = a;
46                         this.b = b;
47                 }
48         }
49
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));
53                 
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);
59                         });
60                 }
61                 System.out.println(body);
62                 
63                 try (CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(credentials).build()) {
64
65                         HttpPost post = new HttpPost(url);
66                         for (String key : headers.keySet()) {
67                                 post.addHeader(key, headers.get(key));
68                         }
69                         post.addHeader("Content-Type", contentType);
70                         
71                         StringEntity input = new StringEntity(body);
72                         input.setContentType(contentType);
73                         post.setEntity(input);
74                         
75                         HttpResponse response = client.execute(post);
76                         
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);
81
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());
85                         return null;
86                 }
87                 
88         }
89
90         public static Pair<Integer, String> get(String url, String username, String password, Map<String, String> headers) {
91                 
92                 CredentialsProvider credentials = new BasicCredentialsProvider();
93                 credentials.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
94                 
95                 try (CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(credentials).build()) {
96
97                         HttpGet get = new HttpGet(url);
98                         for (String key : headers.keySet()) {
99                                 get.addHeader(key, headers.get(key));
100                         }
101                         
102                         HttpResponse response = client.execute(get);
103                         
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);
108
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());
112                         return null;
113                 }
114         }
115 }