-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathDefaultHttpClient.java
More file actions
177 lines (153 loc) · 6.18 KB
/
DefaultHttpClient.java
File metadata and controls
177 lines (153 loc) · 6.18 KB
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
package com.icoderman.woocommerce;
import java.io.IOException;
import java.net.URISyntaxException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.net.ssl.SSLContext;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
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.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.TrustStrategy;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class DefaultHttpClient implements HttpClient {
private static final String CONTENT_TYPE = "Content-Type";
private static final String APPLICATION_JSON = "application/json";
private CloseableHttpClient httpClient;
private ObjectMapper mapper;
public DefaultHttpClient() {
super();
createDefaultHttpClient(true);
}
public DefaultHttpClient(Boolean sslTrusted) {
super();
createDefaultHttpClient(true);
}
private void createDefaultHttpClient(Boolean sslTrusted) {
SSLContext sslContext = getSslContext();
if (sslTrusted)
this.httpClient = HttpClientBuilder.create().setSSLContext(sslContext)
.build();
else
this.httpClient = HttpClientBuilder.create().setSSLContext(sslContext)
.build();
this.mapper = new ObjectMapper();
}
private SSLContext getSslContext() {
TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
public boolean isTrusted(X509Certificate[] certificate, String authType) {
return true;
}
};
SSLContext sslContext = null;
try {
sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
} catch (Exception e) {
// Handle error
}
return sslContext;
}
@Override
public Map get(String url) {
HttpGet httpGet = new HttpGet(url);
return getEntityAndReleaseConnection(httpGet, Map.class);
}
@Override
public List getAll(String url) {
HttpGet httpGet = new HttpGet(url);
return getEntityAndReleaseConnection(httpGet, List.class);
}
@Override
public Map post(String url, Map<String, String> params, Map<String, Object> object) {
List<NameValuePair> postParameters = getParametersAsList(params);
HttpPost httpPost;
try {
URIBuilder uriBuilder = new URIBuilder(url);
uriBuilder.addParameters(postParameters);
httpPost = new HttpPost(uriBuilder.build());
httpPost.setHeader(CONTENT_TYPE, APPLICATION_JSON);
return postEntity(object, httpPost);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
@Override
public Map put(String url, Map<String, String> params, Map<String, Object> object) {
List<NameValuePair> postParameters = getParametersAsList(params);
HttpPut httpPut;
try {
URIBuilder uriBuilder = new URIBuilder(url);
uriBuilder.addParameters(postParameters);
httpPut = new HttpPut(uriBuilder.build());
httpPut.setHeader(CONTENT_TYPE, APPLICATION_JSON);
return postEntity(object, httpPut);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
@Override
public Map delete(String url, Map<String, String> params) {
List<NameValuePair> postParameters = getParametersAsList(params);
HttpDelete httpDelete;
try {
URIBuilder uriBuilder = new URIBuilder(url);
uriBuilder.addParameters(postParameters);
httpDelete = new HttpDelete(uriBuilder.build());
return getEntityAndReleaseConnection(httpDelete, Map.class);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
private Map postEntity(Map<String, Object> objectForJson, HttpEntityEnclosingRequestBase httpPost) {
try {
HttpEntity entity = new ByteArrayEntity(this.mapper.writeValueAsBytes(objectForJson), ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
return getEntityAndReleaseConnection(httpPost, Map.class);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
private List<NameValuePair> getParametersAsList(Map<String, String> params) {
List<NameValuePair> postParameters = new ArrayList<>();
if (params != null && params.size() > 0) {
for (String key : params.keySet()) {
postParameters.add(new BasicNameValuePair(key, params.get(key)));
}
}
return postParameters;
}
private <T> T getEntityAndReleaseConnection(HttpRequestBase httpRequest, Class<T> objectClass) {
try {
HttpResponse httpResponse = httpClient.execute(httpRequest);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity == null) {
throw new RuntimeException("Error retrieving results from http request");
}
Object result = mapper.readValue(httpEntity.getContent(), Object.class);
if (objectClass.isInstance(result)) {
return objectClass.cast(result);
}
throw new RuntimeException("Can't parse retrieved object: " + result.toString());
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
httpRequest.releaseConnection();
}
}
}