27
27
28
28
import javax .net .ssl .SSLContext ;
29
29
30
+ import org .apache .commons .codec .binary .Base64 ;
30
31
import org .apache .http .Header ;
31
32
import org .apache .http .HttpHost ;
32
33
import org .apache .http .HttpStatus ;
34
+ import org .apache .http .auth .AUTH ;
33
35
import org .apache .http .auth .AuthScope ;
34
36
import org .apache .http .auth .NTCredentials ;
37
+ import org .apache .http .client .AuthCache ;
35
38
import org .apache .http .client .CredentialsProvider ;
36
39
import org .apache .http .client .config .RequestConfig ;
37
40
import org .apache .http .client .methods .CloseableHttpResponse ;
46
49
import org .apache .http .conn .ssl .NoopHostnameVerifier ;
47
50
import org .apache .http .conn .ssl .SSLConnectionSocketFactory ;
48
51
import org .apache .http .conn .ssl .TrustStrategy ;
52
+ import org .apache .http .impl .auth .BasicScheme ;
53
+ import org .apache .http .impl .client .BasicAuthCache ;
49
54
import org .apache .http .impl .client .BasicCredentialsProvider ;
50
55
import org .apache .http .impl .client .CloseableHttpClient ;
51
56
import org .apache .http .impl .client .HttpClients ;
@@ -73,6 +78,7 @@ public class DefaultServiceClient extends ServiceClient {
73
78
protected RequestConfig requestConfig ;
74
79
protected CredentialsProvider credentialsProvider ;
75
80
protected HttpHost proxyHttpHost ;
81
+ protected AuthCache authCache ;
76
82
77
83
public DefaultServiceClient (ClientConfiguration config ) {
78
84
super (config );
@@ -97,6 +103,9 @@ public DefaultServiceClient(ClientConfiguration config) {
97
103
this .credentialsProvider = new BasicCredentialsProvider ();
98
104
this .credentialsProvider .setCredentials (new AuthScope (proxyHost , proxyPort ),
99
105
new NTCredentials (proxyUsername , proxyPassword , proxyWorkstation , proxyDomain ));
106
+
107
+ this .authCache = new BasicAuthCache ();
108
+ authCache .put (this .proxyHttpHost , new BasicScheme ());
100
109
}
101
110
}
102
111
@@ -107,7 +116,8 @@ public DefaultServiceClient(ClientConfiguration config) {
107
116
public ResponseMessage sendRequestCore (ServiceClient .Request request , ExecutionContext context )
108
117
throws IOException {
109
118
HttpRequestBase httpRequest = httpRequestFactory .createHttpRequest (request , context );
110
- HttpClientContext httpContext = HttpClientContext .create ();
119
+ setProxyAuthorizationIfNeed (httpRequest );
120
+ HttpClientContext httpContext = createHttpContext ();
111
121
httpContext .setRequestConfig (this .requestConfig );
112
122
113
123
CloseableHttpResponse httpResponse = null ;
@@ -255,9 +265,19 @@ protected HttpClientContext createHttpContext() {
255
265
httpContext .setRequestConfig (this .requestConfig );
256
266
if (this .credentialsProvider != null ) {
257
267
httpContext .setCredentialsProvider (this .credentialsProvider );
268
+ httpContext .setAuthCache (this .authCache );
258
269
}
259
270
return httpContext ;
260
271
}
272
+
273
+ private void setProxyAuthorizationIfNeed (HttpRequestBase httpRequest ) {
274
+ if (this .credentialsProvider != null ) {
275
+ String auth = this .config .getProxyUsername () + ":" + this .config .getProxyPassword ();
276
+ byte [] encodedAuth = Base64 .encodeBase64 (auth .getBytes ());
277
+ String authHeader = "Basic " + new String (encodedAuth );
278
+ httpRequest .addHeader (AUTH .PROXY_AUTH_RESP , authHeader );
279
+ }
280
+ }
261
281
262
282
@ Override
263
283
public void shutdown () {
0 commit comments