-
Notifications
You must be signed in to change notification settings - Fork 773
/
HTTPSClientUtils.java
300 lines (277 loc) · 11.5 KB
/
HTTPSClientUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package org.wso2.carbon.apimgt.samples.utils;
/*
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* This class is high level implementation for the HTTP client for Secure connection
*/
public class HTTPSClientUtils {
/**
* do HTTP GET operation for the given URL
*
* @param url request URL
* @param headers headers to be send
* @return org.wso2.carbon.apimgt.samples.utils.HttpResponse
* @throws IOException if connection issue occurred
*/
public static org.wso2.carbon.apimgt.samples.utils.HttpResponse doGet(String url,
Map<String, String> headers) throws IOException {
CloseableHttpClient httpClient = getHttpsClient();
org.apache.http.HttpResponse response = sendGetRequest(httpClient, url, headers);
return constructResponse(response);
}
/**
* do HTTP POST operation for the given URL
*
* @param url request URL
* @param headers headers to be send
* @param urlParameters parameters to be sent as payload
* @return org.wso2.carbon.apimgt.samples.utils.HttpResponse
* @throws IOException if connection issue occurred
*/
public static org.wso2.carbon.apimgt.samples.utils.HttpResponse doPost(String url,
Map<String, String> headers, List<NameValuePair> urlParameters) throws IOException {
CloseableHttpClient httpClient = getHttpsClient();
org.apache.http.HttpResponse response = sendPOSTMessage(httpClient, url, headers, urlParameters);
return constructResponse(response);
}
/**
* do HTTP POST operation for the given URL
*
* @param url request URL
* @param headers headers to be sent
* @return org.wso2.carbon.apimgt.samples.utils.HttpResponse
* @throws IOException if connection issue occurred
*/
public static org.wso2.carbon.apimgt.samples.utils.HttpResponse doPost(String url,
Map<String, String> headers, String payload) throws IOException {
CloseableHttpClient httpClient = getHttpsClient();
org.apache.http.HttpResponse response = sendPOSTMessage(httpClient, url, headers, payload);
return constructResponse(response);
}
/**
* do HTTP PUT operation for the given URL
*
* @param url request URL
* @param headers headers to be sent
* @return org.wso2.carbon.apimgt.samples.utils.HttpResponse
* @throws IOException if connection issue occurred
*/
public static org.wso2.carbon.apimgt.samples.utils.HttpResponse doPut(String url,
Map<String, String> headers, String payload) throws IOException {
CloseableHttpClient httpClient = getHttpsClient();
org.apache.http.HttpResponse response = sendPUTMessage(httpClient, url, headers, payload);
return constructResponse(response);
}
/**
* do HTTP POST operation for the given URL with JSON entity
* @param url
* @param headers
* @param json
* @return org.wso2.carbon.apimgt.samples.utils.HttpResponse
* @throws IOException
*/
public static org.wso2.carbon.apimgt.samples.utils.HttpResponse doPost(URL url,
Map<String, String> headers, String json) throws IOException {
CloseableHttpClient httpClient = getHttpsClient();
org.apache.http.HttpResponse response = sendPOSTMessage(httpClient, url.toString(), headers, json);
return constructResponse(response);
}
/**
* do HTTP POST operation for the given URL
*
* @param url request URL
* @param headers headers to be send
* @param urlParams parameter string to be sent as payload
* @return org.wso2.carbon.apimgt.samples.utils.HttpResponse
* @throws IOException if connection issue occurred
*/
public static org.wso2.carbon.apimgt.samples.utils.HttpResponse doPost(URL url, String urlParams,
Map<String, String> headers) throws IOException {
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
if (urlParams != null && urlParams.contains("=")) {
String[] paramList = urlParams.split("&");
for (String pair : paramList) {
if (pair.contains("=")) {
String[] pairList = pair.split("=");
String key = pairList[0];
String value = (pairList.length > 1) ? pairList[1] : "";
urlParameters.add(new BasicNameValuePair(key, URLDecoder.decode(value, "UTF-8")));
}
}
}
return doPost(url.toString(), headers, urlParameters);
}
/**
* get the HTTP Client
*
* @return CloseableHttpClient
*/
public static CloseableHttpClient getHttpsClient() {
int timeout = 7;
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(timeout * 1000)
.setConnectionRequestTimeout(timeout * 1000)
.setSocketTimeout(timeout * 1000).build();
CloseableHttpClient httpClient = HttpClients.custom().disableRedirectHandling()
.setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER).
setDefaultRequestConfig(config).build();
return httpClient;
}
/**
* GET function implementation
*
* @param httpClient http client to use
* @param url request URL
* @param headers headers to be send
* @return org.apache.http.HttpResponse
* @throws IOException if connection issue occurred
*/
private static org.apache.http.HttpResponse sendGetRequest(CloseableHttpClient httpClient, String url, Map<String, String> headers)
throws IOException {
HttpGet request = new HttpGet(url);
if (headers != null) {
for (Map.Entry<String, String> head : headers.entrySet()) {
request.addHeader(head.getKey(), head.getValue());
}
}
return httpClient.execute(request);
}
/**
* POST function implementation
*
* @param httpClient http client to use
* @param url request URL
* @param headers headers to be send
* @param urlParameters parameters to be sent as payload
* @return org.apache.http.HttpResponse
* @throws IOException if connection issue occurred
*/
private static org.apache.http.HttpResponse sendPOSTMessage(CloseableHttpClient httpClient, String url, Map<String, String> headers,
List<NameValuePair> urlParameters) throws IOException {
HttpPost post = new HttpPost(url);
if (headers != null) {
for (Map.Entry<String, String> head : headers.entrySet()) {
post.addHeader(head.getKey(), head.getValue());
}
}
post.setEntity(new UrlEncodedFormEntity(urlParameters));
return httpClient.execute(post);
}
/**
* POST function implementation
*
* @param httpClient http client to use
* @param url request URL
* @param headers headers to be send
* @param body payload to be send
* @return org.apache.http.HttpResponse
* @throws IOException if connection issue occurred
*/
private static org.apache.http.HttpResponse sendPOSTMessage(CloseableHttpClient httpClient, String url, Map<String, String> headers,
String body) throws IOException {
HttpPost post = new HttpPost(url);
if (headers != null) {
for (Map.Entry<String, String> head : headers.entrySet()) {
post.addHeader(head.getKey(), head.getValue());
}
}
post.setEntity(new StringEntity(body));
return httpClient.execute(post);
}
/**
* PUT function implementation
*
* @param httpClient http client to use
* @param url request URL
* @param headers headers to be send
* @param body payload to be send
* @return org.apache.http.HttpResponse
* @throws IOException if connection issue occurred
*/
private static org.apache.http.HttpResponse sendPUTMessage(CloseableHttpClient httpClient, String url, Map<String, String> headers,
String body) throws IOException {
HttpPut put = new HttpPut(url);
if (headers != null) {
for (Map.Entry<String, String> head : headers.entrySet()) {
put.addHeader(head.getKey(), head.getValue());
}
}
put.setEntity(new StringEntity(body));
return httpClient.execute(put);
}
/**
* Construct the org.wso2.carbon.apimgt.samples.utils.HttpResponse
*
* @param response org.apache.http.HttpResponse
* @return org.wso2.carbon.apimgt.samples.utils.HttpResponse
* @throws IOException if any exception occurred when reading payload
*/
private static org.wso2.carbon.apimgt.samples.utils.HttpResponse constructResponse(
org.apache.http.HttpResponse response) throws IOException {
int code = response.getStatusLine().getStatusCode();
String body = getResponseBody(response);
Header[] headers = response.getAllHeaders();
Map<String, String> heads = new HashMap<String, String>();
for (Header header : headers) {
heads.put(header.getName(), header.getValue());
}
org.wso2.carbon.apimgt.samples.utils.HttpResponse res = new org.wso2.carbon.apimgt.samples.utils.HttpResponse(
body, code, heads);
return res;
}
/**
* read the response body as String
*
* @param response http response with type org.apache.http.HttpResponse
* @return String of the response body
* @throws IOException throws if any error occurred
*/
public static String getResponseBody(HttpResponse response) throws IOException {
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String line;
StringBuffer sb = new StringBuffer();
while ((line = rd.readLine()) != null) {
sb.append(line);
}
rd.close();
return sb.toString();
}
}