-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathDefaultHttpClient.java
More file actions
168 lines (152 loc) · 6.75 KB
/
DefaultHttpClient.java
File metadata and controls
168 lines (152 loc) · 6.75 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
package com.icoderman.woocommerce;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.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 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 static final String WC_TOTAL_HEADER = "X-WP-Total";
private static final String WC_TOTAL_PAGES_HEADER = "X-WP-TotalPages";
private CloseableHttpClient httpClient;
private ObjectMapper mapper;
public DefaultHttpClient() {
this.httpClient = HttpClientBuilder.create().build();
this.mapper = new ObjectMapper();
}
@Override
public Map get(String url) {
HttpGet httpGet = new HttpGet(url);
return getEntityAndReleaseConnection(httpGet, Map.class);
}
@Override
public Page getAll(String url) {
HttpGet httpGet = new HttpGet(url);
Map result = getEntityWithHeaderAndReleaseConnection(httpGet, List.class);
List content = (List) result.get("content");
int total = Integer.parseInt(result.get("total").toString());
int totalPages = Integer.parseInt(result.get("totalPages").toString());
Page page = new Page<>(content, total, totalPages, content.size());
return page;
}
@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();
}
}
private <T> Map getEntityWithHeaderAndReleaseConnection(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)) {
Map<String, Object> map = new HashMap<>();
if (httpResponse.containsHeader(WC_TOTAL_HEADER)) {
map.put("total", httpResponse.getFirstHeader(WC_TOTAL_HEADER).getValue());
map.put("totalPages", httpResponse.getFirstHeader(WC_TOTAL_PAGES_HEADER).getValue());
}
map.put("content", objectClass.cast(result));
return map;
}
throw new RuntimeException("Can't parse retrieved object: " + result.toString());
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
httpRequest.releaseConnection();
}
}
}