-
Notifications
You must be signed in to change notification settings - Fork 643
/
ApacheHttpClientDelegate.java
223 lines (179 loc) · 8.02 KB
/
ApacheHttpClientDelegate.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
package io.fabric8.maven.docker.access.hc;
import java.io.File;
import java.io.IOException;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ResponseHandler;
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.HttpUriRequest;
import org.apache.http.entity.FileEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import io.fabric8.maven.docker.access.hc.http.HttpRequestException;
import io.fabric8.maven.docker.access.hc.util.ClientBuilder;
public class ApacheHttpClientDelegate {
private final ClientBuilder clientBuilder;
private final CloseableHttpClient httpClient;
public ApacheHttpClientDelegate(ClientBuilder clientBuilder, boolean pooled) throws IOException {
this.clientBuilder = clientBuilder;
this.httpClient = pooled ? clientBuilder.buildPooledClient() : clientBuilder.buildBasicClient();
}
public CloseableHttpClient createBasicClient() {
try {
return clientBuilder.buildBasicClient();
} catch (IOException exp) {
throw new IllegalStateException("Cannot create single HTTP client: " + exp,exp);
}
}
public CloseableHttpClient getHttpClient() {
return httpClient;
}
public void close() throws IOException {
httpClient.close();
}
public int delete(String url, int... statusCodes) throws IOException {
return delete(url, new StatusCodeResponseHandler(), statusCodes);
}
public static class StatusCodeResponseHandler implements ResponseHandler<Integer> {
@Override
public Integer handleResponse(HttpResponse response) {
return response.getStatusLine().getStatusCode();
}
}
public <T> T delete(String url, ResponseHandler<T> responseHandler, int... statusCodes)
throws IOException {
return httpClient.execute(newDelete(url),
new StatusCodeCheckerResponseHandler<>(responseHandler,
statusCodes));
}
public String get(String url, int... statusCodes) throws IOException {
return httpClient.execute(newGet(url), new StatusCodeCheckerResponseHandler<>(
new BodyResponseHandler(), statusCodes));
}
public <T> T get(String url, ResponseHandler<T> responseHandler, int... statusCodes)
throws IOException {
return httpClient
.execute(newGet(url), new StatusCodeCheckerResponseHandler<>(responseHandler, statusCodes));
}
public static class BodyResponseHandler implements ResponseHandler<String> {
@Override
public String handleResponse(HttpResponse response)
throws IOException {
return getResponseMessage(response);
}
}
private static String getResponseMessage(HttpResponse response) throws IOException {
return (response.getEntity() == null) ? null
: EntityUtils.toString(response.getEntity()).trim();
}
public <T> T post(String url, Object body, Map<String, String> headers,
ResponseHandler<T> responseHandler, int... statusCodes) throws IOException {
HttpUriRequest request = newPost(url, body);
for (Entry<String, String> entry : headers.entrySet()) {
request.addHeader(entry.getKey(), entry.getValue());
}
return httpClient.execute(request, new StatusCodeCheckerResponseHandler<>(responseHandler, statusCodes));
}
public <T> T post(String url, Object body, ResponseHandler<T> responseHandler,
int... statusCodes) throws IOException {
return httpClient.execute(newPost(url, body),
new StatusCodeCheckerResponseHandler<>(responseHandler,
statusCodes));
}
public int post(String url, int... statusCodes) throws IOException {
return post(url, null, new StatusCodeResponseHandler(), statusCodes);
}
public int put(String url, Object body, int... statusCodes) throws IOException {
return httpClient.execute(newPut(url, body),
new StatusCodeCheckerResponseHandler<>(new StatusCodeResponseHandler(), statusCodes));
}
// =========================================================================================
private HttpUriRequest addDefaultHeaders(HttpUriRequest req, Object body) {
req.addHeader(HttpHeaders.ACCEPT, "*/*");
if (body instanceof File) {
req.addHeader(HttpHeaders.CONTENT_TYPE, URLConnection.guessContentTypeFromName(((File)body).getName()));
}
if (body != null && !req.containsHeader(HttpHeaders.CONTENT_TYPE)) {
req.addHeader(HttpHeaders.CONTENT_TYPE, "application/json");
}
return req;
}
private HttpUriRequest newDelete(String url) {
return addDefaultHeaders(new HttpDelete(url), null);
}
private HttpUriRequest newGet(String url) {
return addDefaultHeaders(new HttpGet(url), null);
}
private HttpUriRequest newPut(String url, Object body) {
HttpPut put = new HttpPut(url);
setEntityIfGiven(put, body);
return addDefaultHeaders(put, body);
}
private HttpUriRequest newPost(String url, Object body) {
HttpPost post = new HttpPost(url);
setEntityIfGiven(post, body);
return addDefaultHeaders(post, body);
}
private void setEntityIfGiven(HttpEntityEnclosingRequestBase request, Object entity) {
if (entity != null) {
if (entity instanceof File) {
request.setEntity(new FileEntity((File) entity));
} else {
request.setEntity(new StringEntity((String) entity, Charset.defaultCharset()));
}
}
}
static class StatusCodeCheckerResponseHandler<T> implements ResponseHandler<T> {
int[] statusCodes;
ResponseHandler<T> delegate;
StatusCodeCheckerResponseHandler(ResponseHandler<T> delegate, int... statusCodes) {
this.statusCodes = statusCodes;
this.delegate = delegate;
}
@Override
public T handleResponse(HttpResponse response) throws IOException {
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
for (int code : statusCodes) {
if (statusCode == code) {
return delegate.handleResponse(response);
}
}
String reason = statusLine.getReasonPhrase().trim();
throw new HttpRequestException(String.format("%s (%s: %d)", getResponseMessage(response),
reason, statusCode));
}
}
public static class BodyAndStatusResponseHandler implements ResponseHandler<HttpBodyAndStatus> {
@Override
public HttpBodyAndStatus handleResponse(HttpResponse response)
throws IOException {
return new HttpBodyAndStatus(response.getStatusLine().getStatusCode(),
getResponseMessage(response));
}
}
public static class HttpBodyAndStatus {
private final int statusCode;
private final String body;
public HttpBodyAndStatus(int statusCode, String body) {
this.statusCode = statusCode;
this.body = body;
}
public int getStatusCode() {
return statusCode;
}
public String getBody() {
return body;
}
}
}