1717package com .arangodb .http ;
1818
1919import java .io .IOException ;
20- import java .util .*;
20+ import java .util .ArrayList ;
21+ import java .util .HashMap ;
22+ import java .util .List ;
23+ import java .util .Map ;
2124import java .util .Map .Entry ;
25+ import java .util .TreeMap ;
2226
2327import org .apache .http .Header ;
28+ import org .apache .http .HeaderElement ;
29+ import org .apache .http .HeaderElementIterator ;
2430import org .apache .http .HttpEntity ;
2531import org .apache .http .HttpHost ;
2632import org .apache .http .HttpResponse ;
3036import org .apache .http .auth .Credentials ;
3137import org .apache .http .auth .UsernamePasswordCredentials ;
3238import org .apache .http .client .ClientProtocolException ;
39+ import org .apache .http .client .config .RequestConfig ;
40+ import org .apache .http .client .config .RequestConfig .Builder ;
3341import org .apache .http .client .methods .HttpDelete ;
3442import org .apache .http .client .methods .HttpEntityEnclosingRequestBase ;
3543import org .apache .http .client .methods .HttpGet ;
3846import org .apache .http .client .methods .HttpPost ;
3947import org .apache .http .client .methods .HttpPut ;
4048import org .apache .http .client .methods .HttpRequestBase ;
49+ import org .apache .http .client .protocol .HttpClientContext ;
4150import org .apache .http .client .utils .URLEncodedUtils ;
42- import org .apache .http .conn .params . ConnRoutePNames ;
51+ import org .apache .http .conn .ConnectionKeepAliveStrategy ;
4352import org .apache .http .entity .ContentType ;
4453import org .apache .http .entity .StringEntity ;
4554import org .apache .http .impl .auth .BasicScheme ;
46- import org .apache .http .impl .client .DefaultHttpClient ;
55+ import org .apache .http .impl .client .CloseableHttpClient ;
4756import org .apache .http .impl .client .DefaultHttpRequestRetryHandler ;
48- import org .apache .http .impl .conn .PoolingClientConnectionManager ;
57+ import org .apache .http .impl .client .HttpClientBuilder ;
58+ import org .apache .http .impl .conn .DefaultProxyRoutePlanner ;
59+ import org .apache .http .impl .conn .PoolingHttpClientConnectionManager ;
60+ import org .apache .http .message .BasicHeaderElementIterator ;
4961import org .apache .http .message .BasicNameValuePair ;
50- import org .apache .http .params .BasicHttpParams ;
51- import org .apache .http .params .HttpConnectionParams ;
52- import org .apache .http .params .HttpParams ;
62+ import org .apache .http .protocol .HTTP ;
63+ import org .apache .http .protocol .HttpContext ;
5364import org .slf4j .Logger ;
5465import org .slf4j .LoggerFactory ;
5566
@@ -68,8 +79,8 @@ public class HttpManager {
6879
6980 private Logger logger = LoggerFactory .getLogger (HttpManager .class );
7081
71- private PoolingClientConnectionManager cm ;
72- private DefaultHttpClient client ;
82+ private PoolingHttpClientConnectionManager cm ;
83+ private CloseableHttpClient client ;
7384
7485 private ArangoConfigure configure ;
7586
@@ -95,30 +106,77 @@ public ArangoConfigure getConfiguration() {
95106
96107 public void init () {
97108 // ConnectionManager
98- cm = new PoolingClientConnectionManager ();
109+ cm = new PoolingHttpClientConnectionManager ();
99110 cm .setDefaultMaxPerRoute (configure .getMaxPerConnection ());
100111 cm .setMaxTotal (configure .getMaxTotalConnection ());
101- // Params
102- HttpParams params = new BasicHttpParams ();
112+
113+ Builder custom = RequestConfig .custom ();
114+
115+ // RequestConfig
103116 if (configure .getConnectionTimeout () >= 0 ) {
104- HttpConnectionParams . setConnectionTimeout ( params , configure .getConnectionTimeout ());
117+ custom . setConnectTimeout ( configure .getConnectionTimeout ());
105118 }
106119 if (configure .getTimeout () >= 0 ) {
107- HttpConnectionParams .setSoTimeout (params , configure .getTimeout ());
120+ custom .setConnectionRequestTimeout (configure .getTimeout ());
121+ custom .setSocketTimeout (configure .getTimeout ());
108122 }
123+ custom .setStaleConnectionCheckEnabled (configure .isStaleConnectionCheck ());
124+
125+ RequestConfig requestConfig = custom .build ();
126+
127+ HttpClientBuilder builder = HttpClientBuilder .create ().setDefaultRequestConfig (requestConfig );
128+ builder .setConnectionManager (cm );
129+
130+ // KeepAlive Strategy
131+ ConnectionKeepAliveStrategy keepAliveStrategy = new ConnectionKeepAliveStrategy () {
132+
133+ public long getKeepAliveDuration (HttpResponse response ,
134+ HttpContext context ) {
135+ // Honor 'keep-alive' header
136+ HeaderElementIterator it = new BasicHeaderElementIterator (
137+ response .headerIterator (HTTP .CONN_KEEP_ALIVE ));
138+ while (it .hasNext ()) {
139+ HeaderElement he = it .nextElement ();
140+ String param = he .getName ();
141+ String value = he .getValue ();
142+ if (value != null && param .equalsIgnoreCase ("timeout" )) {
143+ try {
144+ return Long .parseLong (value ) * 1000 ;
145+ } catch (NumberFormatException ignore ) {
146+ }
147+ }
148+ }
149+ HttpHost target = (HttpHost ) context
150+ .getAttribute (HttpClientContext .HTTP_TARGET_HOST );
151+ if ("www.naughty-server.com" .equalsIgnoreCase (target .getHostName ())) {
152+ // Keep alive for 5 seconds only
153+ return 5 * 1000 ;
154+ } else {
155+ // otherwise keep alive for 30 seconds
156+ return 30 * 1000 ;
157+ }
158+ }
109159
110- HttpConnectionParams .setStaleCheckingEnabled (params , configure .isStaleConnectionCheck ());
160+ };
161+ builder .setKeepAliveStrategy (keepAliveStrategy );
111162
112- // Client
113- client = new DefaultHttpClient ( cm , params );
114- // TODO KeepAlive Strategy
163+ // Retry Handler
164+ builder . setRetryHandler ( new DefaultHttpRequestRetryHandler ( configure
165+ . getRetryCount (), false ));
115166
116167 // Proxy
117168 if (configure .getProxyHost () != null && configure .getProxyPort () != 0 ) {
118169 HttpHost proxy = new HttpHost (configure .getProxyHost (), configure .getProxyPort (), "http" );
119- client .getParams ().setParameter (ConnRoutePNames .DEFAULT_PROXY , proxy );
170+ // client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
171+
172+ DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner (
173+ proxy );
174+ builder .setRoutePlanner (routePlanner );
120175 }
121176
177+ // Client
178+ client = builder .build ();
179+
122180 // Basic Auth
123181 // if (configure.getUser() != null && configure.getPassword() != null) {
124182 // AuthScope scope = AuthScope.ANY; // TODO
@@ -128,9 +186,6 @@ public void init() {
128186 // client.getCredentialsProvider().setCredentials(scope, credentials);
129187 // }
130188
131- // Retry Handler
132- client .setHttpRequestRetryHandler (new DefaultHttpRequestRetryHandler (configure .getRetryCount (), false ));
133-
134189 }
135190
136191 public void destroy () {
@@ -295,7 +350,7 @@ public HttpResponseEntity execute(HttpRequestEntity requestEntity) throws Arango
295350
296351 // common-header
297352 String userAgent = "Mozilla/5.0 (compatible; ArangoDB-JavaDriver/1.0; +http://mt.orz.at/)" ; // TODO:
298- // 定数化
353+ // 定数化
299354 request .setHeader ("User-Agent" , userAgent );
300355 // request.setHeader("Content-Type", "binary/octet-stream");
301356
@@ -452,7 +507,7 @@ public static boolean is412Error(HttpResponseEntity res) {
452507 return res .getStatusCode () == HttpStatus .SC_PRECONDITION_FAILED ;
453508 }
454509
455- public DefaultHttpClient getClient () {
510+ public CloseableHttpClient getClient () {
456511 return client ;
457512 }
458513
0 commit comments