2121import org .apache .http .HttpRequest ;
2222import org .apache .http .HttpResponse ;
2323import org .apache .http .ProtocolException ;
24+ import org .apache .http .auth .AuthScope ;
25+ import org .apache .http .auth .Credentials ;
26+ import org .apache .http .client .CredentialsProvider ;
2427import org .apache .http .client .HttpClient ;
2528import org .apache .http .client .RedirectStrategy ;
2629import org .apache .http .client .config .RequestConfig ;
3336import org .apache .http .conn .socket .ConnectionSocketFactory ;
3437import org .apache .http .conn .socket .PlainConnectionSocketFactory ;
3538import org .apache .http .conn .ssl .SSLConnectionSocketFactory ;
39+ import org .apache .http .impl .client .BasicCredentialsProvider ;
3640import org .apache .http .impl .client .CloseableHttpClient ;
3741import org .apache .http .impl .client .HttpClientBuilder ;
3842import org .apache .http .impl .conn .DefaultSchemePortResolver ;
@@ -51,13 +55,7 @@ public class HttpClientFactory {
5155 getClientConnectionManager ();
5256
5357 public HttpClientFactory () {
54- httpClient = HttpClientBuilder .create ()
55- .setConnectionManager (getClientConnectionManager ())
56- .setDefaultSocketConfig (createSocketConfig ())
57- .setDefaultSocketConfig (createSocketConfig ())
58- .setRoutePlanner (createRoutePlanner ())
59- .setDefaultRequestConfig (createRequestConfig ())
60- .build ();
58+ httpClient = createHttpClient (null );
6159 }
6260
6361 private static HttpClientConnectionManager getClientConnectionManager () {
@@ -78,6 +76,23 @@ public HttpClient getHttpClient() {
7876 return httpClient ;
7977 }
8078
79+ public CloseableHttpClient createHttpClient (Credentials credentials ) {
80+ HttpClientBuilder builder = HttpClientBuilder .create ()
81+ .setConnectionManager (getClientConnectionManager ())
82+ .setDefaultSocketConfig (createSocketConfig ())
83+ .setDefaultSocketConfig (createSocketConfig ())
84+ .setRoutePlanner (createRoutePlanner ())
85+ .setDefaultRequestConfig (createRequestConfig ());
86+
87+ if (credentials != null ) {
88+ CredentialsProvider provider = new BasicCredentialsProvider ();
89+ provider .setCredentials (AuthScope .ANY , credentials );
90+ builder .setDefaultCredentialsProvider (provider );
91+ }
92+
93+ return builder .build ();
94+ }
95+
8196 public HttpClient getGridHttpClient (int connection_timeout , int socket_timeout ) {
8297 gridClientConnectionManager .closeIdleConnections (100 , MILLISECONDS );
8398
0 commit comments