1616package com .stormpath .sdk .impl .http .httpclient ;
1717
1818import com .stormpath .sdk .client .AuthenticationScheme ;
19- import com .stormpath .sdk .impl .authc .credentials .ClientCredentials ;
2019import com .stormpath .sdk .client .Proxy ;
20+ import com .stormpath .sdk .impl .authc .credentials .ClientCredentials ;
2121import com .stormpath .sdk .impl .http .HttpHeaders ;
2222import com .stormpath .sdk .impl .http .MediaType ;
2323import com .stormpath .sdk .impl .http .QueryString ;
3232import com .stormpath .sdk .impl .http .support .DefaultRequest ;
3333import com .stormpath .sdk .impl .http .support .DefaultResponse ;
3434import com .stormpath .sdk .lang .Assert ;
35+ import org .apache .http .Consts ;
3536import org .apache .http .Header ;
3637import org .apache .http .HeaderElement ;
3738import org .apache .http .HttpEntity ;
4243import org .apache .http .HttpVersion ;
4344import org .apache .http .NoHttpResponseException ;
4445import org .apache .http .auth .AuthScope ;
46+ import org .apache .http .auth .Credentials ;
4547import org .apache .http .auth .UsernamePasswordCredentials ;
48+ import org .apache .http .client .CredentialsProvider ;
49+ import org .apache .http .client .HttpClient ;
50+ import org .apache .http .client .config .RequestConfig ;
4651import org .apache .http .client .entity .GzipDecompressingEntity ;
4752import org .apache .http .client .methods .HttpRequestBase ;
48- import org .apache .http .client .params .AllClientPNames ;
49- import org .apache .http .client .params .ClientPNames ;
53+ import org .apache .http .config .ConnectionConfig ;
5054import org .apache .http .conn .ConnectTimeoutException ;
51- import org .apache .http .conn . params . ConnRoutePNames ;
52- import org .apache .http .impl .client .DefaultHttpClient ;
53- import org .apache .http .impl .conn .PoolingClientConnectionManager ;
55+ import org .apache .http .impl . client . BasicCredentialsProvider ;
56+ import org .apache .http .impl .client .HttpClientBuilder ;
57+ import org .apache .http .impl .conn .PoolingHttpClientConnectionManager ;
5458import org .apache .http .util .EntityUtils ;
5559import org .slf4j .Logger ;
5660import org .slf4j .LoggerFactory ;
@@ -93,7 +97,7 @@ public class HttpClientRequestExecutor implements RequestExecutor {
9397
9498 private final RequestAuthenticator requestAuthenticator ;
9599
96- private DefaultHttpClient httpClient ;
100+ private HttpClient httpClient ;
97101
98102 private BackoffStrategy backoffStrategy ;
99103
@@ -150,9 +154,8 @@ public HttpClientRequestExecutor(ClientCredentials clientCredentials, Proxy prox
150154
151155 this .requestAuthenticator = factory .create (authenticationScheme , clientCredentials );
152156
153- this . httpClientRequestFactory = new HttpClientRequestFactory ();
157+ PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager ();
154158
155- PoolingClientConnectionManager connMgr = new PoolingClientConnectionManager ();
156159 if (MAX_CONNECTIONS_TOTAL >= MAX_CONNECTIONS_PER_ROUTE ) {
157160 connMgr .setDefaultMaxPerRoute (MAX_CONNECTIONS_PER_ROUTE );
158161 connMgr .setMaxTotal (MAX_CONNECTIONS_TOTAL );
@@ -173,25 +176,31 @@ public HttpClientRequestExecutor(ClientCredentials clientCredentials, Proxy prox
173176 // Therefore, multiply it by 1000 to be milliseconds since DefaultHttpClient expects milliseconds.
174177 int connectionTimeoutAsMilliseconds = connectionTimeout * 1000 ;
175178
176- this .httpClient = new DefaultHttpClient (connMgr );
177- httpClient .getParams ().setParameter (AllClientPNames .PROTOCOL_VERSION , HttpVersion .HTTP_1_1 );
178- httpClient .getParams ().setParameter (AllClientPNames .SO_TIMEOUT , connectionTimeoutAsMilliseconds );
179- httpClient .getParams ().setParameter (AllClientPNames .CONNECTION_TIMEOUT , connectionTimeoutAsMilliseconds );
180- httpClient .getParams ().setParameter (ClientPNames .HANDLE_REDIRECTS , false );
181- httpClient .getParams ().setParameter ("http.protocol.content-charset" , "UTF-8" );
179+ RequestConfig requestConfig = RequestConfig .custom ().setConnectTimeout (connectionTimeoutAsMilliseconds )
180+ .setSocketTimeout (connectionTimeoutAsMilliseconds ).setRedirectsEnabled (false ).build ();
181+
182+ ConnectionConfig connectionConfig = ConnectionConfig .custom ().setCharset (Consts .UTF_8 ).build ();
183+
184+ HttpClientBuilder httpClientBuilder = HttpClientBuilder .create ().setDefaultRequestConfig (requestConfig )
185+ .disableCookieManagement ().setDefaultConnectionConfig (connectionConfig ).setConnectionManager (connMgr );
186+
187+ this .httpClientRequestFactory = new HttpClientRequestFactory (requestConfig );
182188
183189 if (proxy != null ) {
184190 //We have some proxy setting to use!
185191 HttpHost httpProxyHost = new HttpHost (proxy .getHost (), proxy .getPort ());
186- httpClient . getParams (). setParameter ( ConnRoutePNames . DEFAULT_PROXY , httpProxyHost );
192+ httpClientBuilder . setProxy ( httpProxyHost );
187193
188194 if (proxy .isAuthenticationRequired ()) {
189- httpClient .getCredentialsProvider ().setCredentials (
190- new AuthScope (proxy .getHost (), proxy .getPort ()),
191- new UsernamePasswordCredentials (proxy .getUsername (), proxy .getPassword ()));
195+ AuthScope authScope = new AuthScope (proxy .getHost (), proxy .getPort ());
196+ Credentials credentials = new UsernamePasswordCredentials (proxy .getUsername (), proxy .getPassword ());
197+ CredentialsProvider credentialsProviderProvider = new BasicCredentialsProvider ();
198+ credentialsProviderProvider .setCredentials (authScope , credentials );
199+ httpClientBuilder .setDefaultCredentialsProvider (credentialsProviderProvider );
192200 }
193-
194201 }
202+
203+ this .httpClient = httpClientBuilder .build ();
195204 }
196205
197206 public int getNumRetries () {
@@ -213,7 +222,7 @@ public void setBackoffStrategy(BackoffStrategy backoffStrategy) {
213222 this .backoffStrategy = backoffStrategy ;
214223 }
215224
216- public void setHttpClient (DefaultHttpClient httpClient ) {
225+ public void setHttpClient (HttpClient httpClient ) {
217226 this .httpClient = httpClient ;
218227 }
219228
0 commit comments