/
StdHttpClient.java
503 lines (437 loc) · 13.8 KB
/
StdHttpClient.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
package org.ektorp.http;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
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.HttpHead;
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.methods.HttpUriRequest;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DecompressingHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.cache.CacheConfig;
import org.apache.http.impl.client.cache.CachingHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.ektorp.util.Exceptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author henrik lundgren
*
*/
public class StdHttpClient implements HttpClient {
private final org.apache.http.client.HttpClient client;
private final org.apache.http.client.HttpClient backend;
private final static Logger LOG = LoggerFactory
.getLogger(StdHttpClient.class);
public StdHttpClient(org.apache.http.client.HttpClient hc) {
this(hc, hc);
}
public StdHttpClient(org.apache.http.client.HttpClient hc,
org.apache.http.client.HttpClient backend) {
this.client = hc;
this.backend = backend;
}
public HttpResponse delete(String uri) {
return executeRequest(new HttpDelete(uri));
}
public HttpResponse get(String uri) {
return executeRequest(new HttpGet(uri));
}
@Override
public HttpResponse get(String uri, Map<String, String> headers) {
return executeRequest(new HttpGet(uri), headers);
}
public HttpResponse getUncached(String uri) {
return executeRequest(new HttpGet(uri), true);
}
public HttpResponse postUncached(String uri, String content) {
return executePutPost(new HttpPost(uri), content, true);
}
public HttpResponse post(String uri, String content) {
return executePutPost(new HttpPost(uri), content, false);
}
public HttpResponse post(String uri, InputStream content) {
InputStreamEntity e = new InputStreamEntity(content, -1);
e.setContentType("application/json");
HttpPost post = new HttpPost(uri);
post.setEntity(e);
return executeRequest(post, true);
}
public HttpResponse put(String uri, String content) {
return executePutPost(new HttpPut(uri), content, false);
}
public HttpResponse put(String uri) {
return executeRequest(new HttpPut(uri));
}
public HttpResponse put(String uri, InputStream data, String contentType,
long contentLength) {
InputStreamEntity e = new InputStreamEntity(data, contentLength);
e.setContentType(contentType);
HttpPut hp = new HttpPut(uri);
hp.setEntity(e);
return executeRequest(hp);
}
public HttpResponse head(String uri) {
return executeRequest(new HttpHead(uri));
}
private HttpResponse executePutPost(HttpEntityEnclosingRequestBase request,
String content, boolean useBackend) {
try {
if (LOG.isTraceEnabled()) {
LOG.trace("Content: {}", content);
}
StringEntity e = new StringEntity(content, "UTF-8");
e.setContentType("application/json");
request.setEntity(e);
return executeRequest(request, useBackend);
} catch (Exception e) {
throw Exceptions.propagate(e);
}
}
private HttpResponse executeRequest(HttpRequestBase request, Map<String, String> headers) {
for(Map.Entry<String, String> header : headers.entrySet()) {
request.setHeader(header.getKey(), header.getValue());
}
return executeRequest(request);
}
private HttpResponse executeRequest(HttpUriRequest request, boolean useBackend) {
try {
org.apache.http.HttpResponse rsp;
if (useBackend) {
rsp = backend.execute(request);
} else {
rsp = client.execute((HttpHost)client.getParams().getParameter(ClientPNames.DEFAULT_HOST), request);
}
if (LOG.isTraceEnabled()) {
LOG.trace(String.format("%s %s %s %s", request.getMethod(),
request.getURI(), rsp.getStatusLine().getStatusCode(),
rsp.getStatusLine().getReasonPhrase()));
}
return StdHttpResponse.of(rsp, request);
} catch (Exception e) {
throw Exceptions.propagate(e);
}
}
private HttpResponse executeRequest(HttpRequestBase request) {
return executeRequest(request, false);
}
@Override
public HttpResponse copy(String sourceUri, String destination) {
return executeRequest(new HttpCopyRequest(sourceUri, destination), true);
}
public void shutdown() {
client.getConnectionManager().shutdown();
}
public static class Builder {
String host = "localhost";
int port = 5984;
int maxConnections = 20;
int connectionTimeout = 1000;
int socketTimeout = 10000;
ClientConnectionManager conman;
int proxyPort = -1;
String proxy = null;
boolean enableSSL = false;
boolean relaxedSSLSettings = false;
SSLSocketFactory sslSocketFactory;
String username;
String password;
boolean cleanupIdleConnections = true;
boolean useExpectContinue = true;
boolean caching = true;
boolean compression; // Default is false;
int maxObjectSizeBytes = 8192;
int maxCacheEntries = 1000;
public Builder url(String s) throws MalformedURLException {
if (s == null) return this;
return this.url(new URL(s));
}
/**
* Will set host, port and possible enables SSL based on the properties if the supplied URL.
* This method overrides the properties: host, port and enableSSL.
* @param url
* @return
*/
public Builder url(URL url){
this.host = url.getHost();
this.port = url.getPort();
if (url.getUserInfo() != null) {
String[] userInfoParts = url.getUserInfo().split(":");
if (userInfoParts.length == 2) {
this.username = userInfoParts[0];
this.password = userInfoParts[1];
}
}
enableSSL("https".equals(url.getProtocol()));
if (this.port == -1) {
if (this.enableSSL) {
this.port = 443;
} else {
this.port = 80;
}
}
return this;
}
public Builder host(String s) {
host = s;
return this;
}
public Builder proxyPort(int p) {
proxyPort = p;
return this;
}
public Builder proxy(String s) {
proxy = s;
return this;
}
/**
* Controls if the http client should send Accept-Encoding: gzip,deflate
* header and handle Content-Encoding responses. This enable compression
* on the server; although not supported natively by CouchDB, you can
* use a reverse proxy, such as nginx, in front of CouchDB to achieve
* this.
* <p>
* Disabled by default (for backward compatibility).
*
* @param b
* @return This builder
*/
public Builder compression(boolean b){
compression = b;
return this;
}
/**
* Controls if the http client should cache response entities.
* Default is true.
* @param b
* @return
*/
public Builder caching(boolean b) {
caching = b;
return this;
}
public Builder maxCacheEntries(int m) {
maxCacheEntries = m;
return this;
}
public Builder maxObjectSizeBytes(int m) {
maxObjectSizeBytes = m;
return this;
}
public ClientConnectionManager configureConnectionManager(
HttpParams params) {
if (conman == null) {
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(configureScheme());
PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
cm.setMaxTotal(maxConnections);
cm.setDefaultMaxPerRoute(maxConnections);
conman = cm;
}
if (cleanupIdleConnections) {
IdleConnectionMonitor.monitor(conman);
}
return conman;
}
private Scheme configureScheme() {
if (enableSSL) {
try {
if (sslSocketFactory == null) {
SSLContext context = SSLContext.getInstance("TLS");
if (relaxedSSLSettings) {
context.init(
null,
new TrustManager[] { new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs,
String authType) {
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs,
String authType) {
}
} }, null);
} else {
context.init(null, null, null);
}
sslSocketFactory = relaxedSSLSettings ? new SSLSocketFactory(context, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER) : new SSLSocketFactory(context);
}
return new Scheme("https", port, sslSocketFactory);
} catch (Exception e) {
throw Exceptions.propagate(e);
}
} else {
return new Scheme("http", port, PlainSocketFactory.getSocketFactory());
}
}
public org.apache.http.client.HttpClient configureClient() {
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setUseExpectContinue(params, useExpectContinue);
HttpConnectionParams
.setConnectionTimeout(params, connectionTimeout);
HttpConnectionParams.setSoTimeout(params, socketTimeout);
HttpConnectionParams.setTcpNoDelay(params, Boolean.TRUE);
String protocol = "http";
if (enableSSL)
protocol = "https";
params.setParameter(ClientPNames.DEFAULT_HOST, new HttpHost(host,
port, protocol));
if (proxy != null) {
params.setParameter(ConnRoutePNames.DEFAULT_PROXY,
new HttpHost(proxy, proxyPort, protocol));
}
ClientConnectionManager connectionManager = configureConnectionManager(params);
DefaultHttpClient client = new DefaultHttpClient(connectionManager, params);
if (username != null && password != null) {
client.getCredentialsProvider().setCredentials(
new AuthScope(host, port, AuthScope.ANY_REALM),
new UsernamePasswordCredentials(username, password));
client.addRequestInterceptor(
new PreemptiveAuthRequestInterceptor(), 0);
}
if (compression) {
return new DecompressingHttpClient(client);
}
return client;
}
public Builder port(int i) {
port = i;
return this;
}
public Builder username(String s) {
username = s;
return this;
}
public Builder password(String s) {
password = s;
return this;
}
public Builder maxConnections(int i) {
maxConnections = i;
return this;
}
public Builder connectionTimeout(int i) {
connectionTimeout = i;
return this;
}
public Builder socketTimeout(int i) {
socketTimeout = i;
return this;
}
/**
* If set to true, a monitor thread will be started that cleans up idle
* connections every 30 seconds.
*
* @param b
* @return
*/
public Builder cleanupIdleConnections(boolean b) {
cleanupIdleConnections = b;
return this;
}
/**
* Bring your own Connection Manager. If this parameters is set, the
* parameters port, maxConnections, connectionTimeout and socketTimeout
* are ignored.
*
* @param cm
* @return
*/
public Builder connectionManager(ClientConnectionManager cm) {
conman = cm;
return this;
}
/**
* Set to true in order to enable SSL sockets. Note that the CouchDB
* host must be accessible through a https:// path Default is false.
*
* @param s
* @return
*/
public Builder enableSSL(boolean b) {
enableSSL = b;
return this;
}
/**
* Bring your own SSLSocketFactory. Note that schemeName must be also be
* configured to "https". Will override any setting of
* relaxedSSLSettings.
*
* @param f
* @return
*/
public Builder sslSocketFactory(SSLSocketFactory f) {
sslSocketFactory = f;
return this;
}
/**
* If set to true all SSL certificates and hosts will be trusted. This
* might be handy during development. default is false.
*
* @param b
* @return
*/
public Builder relaxedSSLSettings(boolean b) {
relaxedSSLSettings = b;
return this;
}
/**
* Activates 'Expect: 100-Continue' handshake with CouchDB.
* Using expect continue can reduce stale connection problems for PUT / POST operations.
* body. Enabled by default.
*
* @param b
* @return
*/
public Builder useExpectContinue(boolean b) {
useExpectContinue = b;
return this;
}
public HttpClient build() {
org.apache.http.client.HttpClient client = configureClient();
org.apache.http.client.HttpClient cachingHttpClient = client;
if (caching) {
cachingHttpClient = WithCachingBuilder.withCaching(client, maxCacheEntries, maxObjectSizeBytes);
}
return new StdHttpClient(cachingHttpClient, client);
}
}
// separate class to avoid runtime dependency to httpclient-cache unless using caching
private static class WithCachingBuilder {
public static org.apache.http.client.HttpClient withCaching(org.apache.http.client.HttpClient client, int maxCacheEntries, int maxObjectSizeBytes) {
CacheConfig cacheConfig = new CacheConfig();
cacheConfig.setMaxCacheEntries(maxCacheEntries);
cacheConfig.setMaxObjectSizeBytes(maxObjectSizeBytes);
return new CachingHttpClient(client, cacheConfig);
}
}
}