Skip to content

Commit 83e1532

Browse files
authored
Merge pull request #46 from IBM/proxy-auth-v4
(v4) Allow for proxy auth configuration
2 parents 80b8099 + 83de034 commit 83e1532

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

src/main/java/com/ibm/cloud/sdk/core/http/HttpClientSingleton.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.ibm.cloud.sdk.core.service.BaseService;
1616
import com.ibm.cloud.sdk.core.service.security.DelegatingSSLSocketFactory;
1717
import com.ibm.cloud.sdk.core.util.HttpLogging;
18+
import okhttp3.Authenticator;
1819
import okhttp3.ConnectionSpec;
1920
import okhttp3.OkHttpClient;
2021
import okhttp3.OkHttpClient.Builder;
@@ -176,6 +177,19 @@ private OkHttpClient setProxy(OkHttpClient client, Proxy proxy) {
176177
return builder.build();
177178
}
178179

180+
/**
181+
* Sets a proxy authenticator for the specified {@link OkHttpClient} instance and returns
182+
* a new instance with the authentication configured as requested.
183+
*
184+
* @param client the {@link OkHttpClient} instance to set the proxy authenticator on
185+
* @param proxyAuthenticator the {@link Authenticator}
186+
* @return the new {@link OkHttpClient} instance with the authenticator configured
187+
*/
188+
private OkHttpClient setProxyAuthenticator(OkHttpClient client, Authenticator proxyAuthenticator) {
189+
OkHttpClient.Builder builder = client.newBuilder().proxyAuthenticator(proxyAuthenticator);
190+
return builder.build();
191+
}
192+
179193
/**
180194
* Specifically enable all TLS protocols. See: https://github.com/watson-developer-cloud/java-sdk/issues/610
181195
*
@@ -259,7 +273,11 @@ public OkHttpClient configureClient(OkHttpClient client, HttpConfigOptions optio
259273
if (options.getProxy() != null) {
260274
client = setProxy(client, options.getProxy());
261275
}
276+
if (options.getProxyAuthenticator() != null) {
277+
client = setProxyAuthenticator(client, options.getProxyAuthenticator());
278+
}
262279
}
263280
return client;
264281
}
282+
265283
}

src/main/java/com/ibm/cloud/sdk/core/http/HttpConfigOptions.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.ibm.cloud.sdk.core.http;
22

3+
import okhttp3.Authenticator;
4+
35
import java.net.Proxy;
46

57
/**
@@ -8,6 +10,7 @@
810
public class HttpConfigOptions {
911
private boolean disableSslVerification;
1012
private Proxy proxy;
13+
private Authenticator proxyAuthenticator;
1114

1215
public boolean shouldDisableSslVerification() {
1316
return this.disableSslVerification;
@@ -17,9 +20,14 @@ public Proxy getProxy() {
1720
return this.proxy;
1821
}
1922

23+
public Authenticator getProxyAuthenticator() {
24+
return this.proxyAuthenticator;
25+
}
26+
2027
public static class Builder {
2128
private boolean disableSslVerification;
2229
private Proxy proxy;
30+
private Authenticator proxyAuthenticator;
2331

2432
public HttpConfigOptions build() {
2533
return new HttpConfigOptions(this);
@@ -47,10 +55,23 @@ public Builder proxy(Proxy proxy) {
4755
this.proxy = proxy;
4856
return this;
4957
}
58+
59+
/**
60+
* Sets HTTP proxy authentication to be used by connections with the current client.
61+
*
62+
* @param proxyAuthenticator the desired {@link Authenticator}
63+
* @return the builder
64+
*/
65+
public Builder proxyAuthenticator(Authenticator proxyAuthenticator) {
66+
this.proxyAuthenticator = proxyAuthenticator;
67+
return this;
68+
}
69+
5070
}
5171

5272
private HttpConfigOptions(Builder builder) {
5373
this.disableSslVerification = builder.disableSslVerification;
5474
this.proxy = builder.proxy;
75+
this.proxyAuthenticator = builder.proxyAuthenticator;
5576
}
5677
}

src/test/java/com/ibm/cloud/sdk/core/test/http/HttpConfigTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package com.ibm.cloud.sdk.core.test.http;
22

3+
import okhttp3.Authenticator;
4+
import okhttp3.Request;
5+
import okhttp3.Response;
6+
import okhttp3.Route;
37
import org.junit.Test;
48

59
import com.ibm.cloud.sdk.core.http.HttpConfigOptions;
610

11+
import javax.annotation.Nullable;
12+
import java.io.IOException;
713
import java.net.InetSocketAddress;
814
import java.net.Proxy;
915

@@ -16,13 +22,23 @@ public class HttpConfigTest {
1622

1723
@Test
1824
public void testHttpConfigOptions() {
25+
Authenticator authenticator = new Authenticator() {
26+
@Nullable
27+
@Override
28+
public Request authenticate(@Nullable Route route, Response response) throws IOException {
29+
return null;
30+
}
31+
};
32+
1933
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxyHost", 8080));
2034
HttpConfigOptions configOptions = new HttpConfigOptions.Builder()
2135
.disableSslVerification(true)
2236
.proxy(proxy)
37+
.proxyAuthenticator(authenticator)
2338
.build();
2439

2540
assertEquals(true, configOptions.shouldDisableSslVerification());
41+
assertEquals(authenticator, configOptions.getProxyAuthenticator());
2642
assertEquals(proxy, configOptions.getProxy());
2743
}
2844
}

0 commit comments

Comments
 (0)