Skip to content

Commit 7ef4ec1

Browse files
committed
proxy
1 parent 35addcc commit 7ef4ec1

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

src/main/java/com/aliyun/oss/common/comm/DefaultServiceClient.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,14 @@
2727

2828
import javax.net.ssl.SSLContext;
2929

30+
import org.apache.commons.codec.binary.Base64;
3031
import org.apache.http.Header;
3132
import org.apache.http.HttpHost;
3233
import org.apache.http.HttpStatus;
34+
import org.apache.http.auth.AUTH;
3335
import org.apache.http.auth.AuthScope;
3436
import org.apache.http.auth.NTCredentials;
37+
import org.apache.http.client.AuthCache;
3538
import org.apache.http.client.CredentialsProvider;
3639
import org.apache.http.client.config.RequestConfig;
3740
import org.apache.http.client.methods.CloseableHttpResponse;
@@ -46,6 +49,8 @@
4649
import org.apache.http.conn.ssl.NoopHostnameVerifier;
4750
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
4851
import org.apache.http.conn.ssl.TrustStrategy;
52+
import org.apache.http.impl.auth.BasicScheme;
53+
import org.apache.http.impl.client.BasicAuthCache;
4954
import org.apache.http.impl.client.BasicCredentialsProvider;
5055
import org.apache.http.impl.client.CloseableHttpClient;
5156
import org.apache.http.impl.client.HttpClients;
@@ -73,6 +78,7 @@ public class DefaultServiceClient extends ServiceClient {
7378
protected RequestConfig requestConfig;
7479
protected CredentialsProvider credentialsProvider;
7580
protected HttpHost proxyHttpHost;
81+
protected AuthCache authCache;
7682

7783
public DefaultServiceClient(ClientConfiguration config) {
7884
super(config);
@@ -97,6 +103,9 @@ public DefaultServiceClient(ClientConfiguration config) {
97103
this.credentialsProvider = new BasicCredentialsProvider();
98104
this.credentialsProvider.setCredentials(new AuthScope(proxyHost, proxyPort),
99105
new NTCredentials(proxyUsername, proxyPassword, proxyWorkstation, proxyDomain));
106+
107+
this.authCache = new BasicAuthCache();
108+
authCache.put(this.proxyHttpHost, new BasicScheme());
100109
}
101110
}
102111

@@ -107,7 +116,8 @@ public DefaultServiceClient(ClientConfiguration config) {
107116
public ResponseMessage sendRequestCore(ServiceClient.Request request, ExecutionContext context)
108117
throws IOException {
109118
HttpRequestBase httpRequest = httpRequestFactory.createHttpRequest(request, context);
110-
HttpClientContext httpContext = HttpClientContext.create();
119+
setProxyAuthorizationIfNeed(httpRequest);
120+
HttpClientContext httpContext = createHttpContext();
111121
httpContext.setRequestConfig(this.requestConfig);
112122

113123
CloseableHttpResponse httpResponse = null;
@@ -255,9 +265,19 @@ protected HttpClientContext createHttpContext() {
255265
httpContext.setRequestConfig(this.requestConfig);
256266
if (this.credentialsProvider != null) {
257267
httpContext.setCredentialsProvider(this.credentialsProvider);
268+
httpContext.setAuthCache(this.authCache);
258269
}
259270
return httpContext;
260271
}
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+
}
261281

262282
@Override
263283
public void shutdown() {

src/test/java/com/aliyun/oss/integrationtests/TestBase.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,27 @@ public static void resetTestConfig() {
359359
if (TestConfig.STS_TEST_BUCKET == null) {
360360
TestConfig.STS_TEST_BUCKET = System.getenv().get("STS_TEST_BUCKET");
361361
}
362+
363+
// proxy test
364+
if (TestConfig.PROXY_HOST == null) {
365+
TestConfig.PROXY_HOST = System.getenv().get("PROXY_HOST");
366+
}
367+
368+
if (TestConfig.PROXY_PORT == -1) {
369+
TestConfig.PROXY_PORT = 3128;
370+
String portStr = System.getenv().get("PROXY_PORT");
371+
if (portStr != null) {
372+
TestConfig.PROXY_PORT = Integer.parseInt(portStr);
373+
}
374+
}
375+
376+
if (TestConfig.PROXY_USER == null) {
377+
TestConfig.PROXY_USER = System.getenv().get("PROXY_USER");
378+
}
379+
380+
if (TestConfig.PROXY_PASSWORD == null) {
381+
TestConfig.PROXY_PASSWORD = System.getenv().get("PROXY_PASSWORD");
382+
}
362383
}
363384

364385
}

src/test/java/com/aliyun/oss/integrationtests/TestConfig.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,10 @@ public final class TestConfig {
3939
public static String STS_TEST_ROLE = null;
4040
public static String STS_TEST_BUCKET = null;
4141

42+
// OSS proxy test
43+
public static String PROXY_HOST = null;
44+
public static int PROXY_PORT = -1;
45+
public static String PROXY_USER = null;
46+
public static String PROXY_PASSWORD = null;
47+
4248
}

0 commit comments

Comments
 (0)