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 ;
3839import org .apache .http .HttpEntityEnclosingRequest ;
3940import org .apache .http .HttpHost ;
4041import org .apache .http .HttpResponse ;
4142import org .apache .http .HttpStatus ;
42- import org .apache .http .HttpVersion ;
4343import org .apache .http .NoHttpResponseException ;
4444import org .apache .http .auth .AuthScope ;
45+ import org .apache .http .auth .Credentials ;
4546import org .apache .http .auth .UsernamePasswordCredentials ;
47+ import org .apache .http .client .CredentialsProvider ;
48+ import org .apache .http .client .HttpClient ;
49+ import org .apache .http .client .config .RequestConfig ;
4650import org .apache .http .client .entity .GzipDecompressingEntity ;
4751import org .apache .http .client .methods .HttpRequestBase ;
48- import org .apache .http .client .params .AllClientPNames ;
49- import org .apache .http .client .params .ClientPNames ;
52+ import org .apache .http .config .ConnectionConfig ;
5053import 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 ;
54+ import org .apache .http .impl . client . BasicCredentialsProvider ;
55+ import org .apache .http .impl .client .HttpClientBuilder ;
56+ import org .apache .http .impl .conn .PoolingHttpClientConnectionManager ;
5457import org .apache .http .util .EntityUtils ;
5558import org .slf4j .Logger ;
5659import org .slf4j .LoggerFactory ;
@@ -93,7 +96,7 @@ public class HttpClientRequestExecutor implements RequestExecutor {
9396
9497 private final RequestAuthenticator requestAuthenticator ;
9598
96- private DefaultHttpClient httpClient ;
99+ private HttpClient httpClient ;
97100
98101 private BackoffStrategy backoffStrategy ;
99102
@@ -150,9 +153,8 @@ public HttpClientRequestExecutor(ClientCredentials clientCredentials, Proxy prox
150153
151154 this .requestAuthenticator = factory .create (authenticationScheme , clientCredentials );
152155
153- this . httpClientRequestFactory = new HttpClientRequestFactory ();
156+ PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager ();
154157
155- PoolingClientConnectionManager connMgr = new PoolingClientConnectionManager ();
156158 if (MAX_CONNECTIONS_TOTAL >= MAX_CONNECTIONS_PER_ROUTE ) {
157159 connMgr .setDefaultMaxPerRoute (MAX_CONNECTIONS_PER_ROUTE );
158160 connMgr .setMaxTotal (MAX_CONNECTIONS_TOTAL );
@@ -170,28 +172,34 @@ public HttpClientRequestExecutor(ClientCredentials clientCredentials, Proxy prox
170172 }
171173
172174 // The connectionTimeout value is specified in seconds in Stormpath configuration settings.
173- // Therefore, multiply it by 1000 to be milliseconds since DefaultHttpClient expects milliseconds.
175+ // Therefore, multiply it by 1000 to be milliseconds since RequestConfig expects milliseconds.
174176 int connectionTimeoutAsMilliseconds = connectionTimeout * 1000 ;
175177
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" );
178+ RequestConfig requestConfig = RequestConfig .custom ().setConnectTimeout (connectionTimeoutAsMilliseconds )
179+ .setSocketTimeout (connectionTimeoutAsMilliseconds ).setRedirectsEnabled (false ).build ();
180+
181+ ConnectionConfig connectionConfig = ConnectionConfig .custom ().setCharset (Consts .UTF_8 ).build ();
182+
183+ HttpClientBuilder httpClientBuilder = HttpClientBuilder .create ().setDefaultRequestConfig (requestConfig )
184+ .disableCookieManagement ().setDefaultConnectionConfig (connectionConfig ).setConnectionManager (connMgr );
185+
186+ this .httpClientRequestFactory = new HttpClientRequestFactory (requestConfig );
182187
183188 if (proxy != null ) {
184189 //We have some proxy setting to use!
185190 HttpHost httpProxyHost = new HttpHost (proxy .getHost (), proxy .getPort ());
186- httpClient . getParams (). setParameter ( ConnRoutePNames . DEFAULT_PROXY , httpProxyHost );
191+ httpClientBuilder . setProxy ( httpProxyHost );
187192
188193 if (proxy .isAuthenticationRequired ()) {
189- httpClient .getCredentialsProvider ().setCredentials (
190- new AuthScope (proxy .getHost (), proxy .getPort ()),
191- new UsernamePasswordCredentials (proxy .getUsername (), proxy .getPassword ()));
194+ AuthScope authScope = new AuthScope (proxy .getHost (), proxy .getPort ());
195+ Credentials credentials = new UsernamePasswordCredentials (proxy .getUsername (), proxy .getPassword ());
196+ CredentialsProvider credentialsProviderProvider = new BasicCredentialsProvider ();
197+ credentialsProviderProvider .setCredentials (authScope , credentials );
198+ httpClientBuilder .setDefaultCredentialsProvider (credentialsProviderProvider );
192199 }
193-
194200 }
201+
202+ this .httpClient = httpClientBuilder .build ();
195203 }
196204
197205 public int getNumRetries () {
@@ -213,7 +221,7 @@ public void setBackoffStrategy(BackoffStrategy backoffStrategy) {
213221 this .backoffStrategy = backoffStrategy ;
214222 }
215223
216- public void setHttpClient (DefaultHttpClient httpClient ) {
224+ public void setHttpClient (HttpClient httpClient ) {
217225 this .httpClient = httpClient ;
218226 }
219227
0 commit comments