Skip to content

Commit 91e9882

Browse files
authored
OpenFeignGH-801: Adding support for JDK Proxy (OpenFeign#1045)
Fixes OpenFeign#801 Adding a `Proxied` client implementation that extends the `Default` client allowing for a JDK Proxy, along with explict credential support.
1 parent 9a4b21b commit 91e9882

3 files changed

Lines changed: 141 additions & 37 deletions

File tree

‎core/src/main/java/feign/Client.java‎

Lines changed: 95 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,36 @@
1313
*/
1414
package feign;
1515

16+
import static feign.Util.CONTENT_ENCODING;
17+
import static feign.Util.CONTENT_LENGTH;
18+
import static feign.Util.ENCODING_DEFLATE;
19+
import static feign.Util.ENCODING_GZIP;
20+
import static feign.Util.checkArgument;
21+
import static feign.Util.checkNotNull;
22+
import static feign.Util.isBlank;
23+
import static feign.Util.isNotBlank;
1624
import static java.lang.String.format;
25+
1726
import java.io.IOException;
1827
import java.io.InputStream;
1928
import java.io.OutputStream;
2029
import java.net.HttpURLConnection;
30+
import java.net.Proxy;
2131
import java.net.URL;
32+
import java.nio.charset.StandardCharsets;
33+
import java.util.Base64;
2234
import java.util.Collection;
2335
import java.util.LinkedHashMap;
2436
import java.util.List;
2537
import java.util.Map;
2638
import java.util.zip.DeflaterOutputStream;
2739
import java.util.zip.GZIPOutputStream;
40+
2841
import javax.net.ssl.HostnameVerifier;
2942
import javax.net.ssl.HttpsURLConnection;
3043
import javax.net.ssl.SSLSocketFactory;
44+
3145
import feign.Request.Options;
32-
import static feign.Util.CONTENT_ENCODING;
33-
import static feign.Util.CONTENT_LENGTH;
34-
import static feign.Util.ENCODING_DEFLATE;
35-
import static feign.Util.ENCODING_GZIP;
3646

3747
/**
3848
* Submits HTTP {@link Request requests}. Implementations are expected to be thread-safe.
@@ -68,9 +78,49 @@ public Response execute(Request request, Options options) throws IOException {
6878
return convertResponse(connection, request);
6979
}
7080

81+
Response convertResponse(HttpURLConnection connection, Request request) throws IOException {
82+
int status = connection.getResponseCode();
83+
String reason = connection.getResponseMessage();
84+
85+
if (status < 0) {
86+
throw new IOException(format("Invalid status(%s) executing %s %s", status,
87+
connection.getRequestMethod(), connection.getURL()));
88+
}
89+
90+
Map<String, Collection<String>> headers = new LinkedHashMap<>();
91+
for (Map.Entry<String, List<String>> field : connection.getHeaderFields().entrySet()) {
92+
// response message
93+
if (field.getKey() != null) {
94+
headers.put(field.getKey(), field.getValue());
95+
}
96+
}
97+
98+
Integer length = connection.getContentLength();
99+
if (length == -1) {
100+
length = null;
101+
}
102+
InputStream stream;
103+
if (status >= 400) {
104+
stream = connection.getErrorStream();
105+
} else {
106+
stream = connection.getInputStream();
107+
}
108+
return Response.builder()
109+
.status(status)
110+
.reason(reason)
111+
.headers(headers)
112+
.request(request)
113+
.body(stream, length)
114+
.build();
115+
}
116+
117+
public HttpURLConnection getConnection(final URL url) throws IOException {
118+
return (HttpURLConnection) url.openConnection();
119+
}
120+
71121
HttpURLConnection convertAndSend(Request request, Options options) throws IOException {
72-
final HttpURLConnection connection =
73-
(HttpURLConnection) new URL(request.url()).openConnection();
122+
final URL url = new URL(request.url());
123+
final HttpURLConnection connection = this.getConnection(url);
74124
if (connection instanceof HttpsURLConnection) {
75125
HttpsURLConnection sslCon = (HttpsURLConnection) connection;
76126
if (sslContextFactory != null) {
@@ -138,41 +188,50 @@ HttpURLConnection convertAndSend(Request request, Options options) throws IOExce
138188
}
139189
return connection;
140190
}
191+
}
141192

142-
Response convertResponse(HttpURLConnection connection, Request request) throws IOException {
143-
int status = connection.getResponseCode();
144-
String reason = connection.getResponseMessage();
193+
/**
194+
* Client that supports a {@link java.net.Proxy}.
195+
*/
196+
class Proxied extends Default {
145197

146-
if (status < 0) {
147-
throw new IOException(format("Invalid status(%s) executing %s %s", status,
148-
connection.getRequestMethod(), connection.getURL()));
149-
}
198+
public static final String PROXY_AUTHORIZATION = "Proxy-Authorization";
199+
private final Proxy proxy;
200+
private String credentials;
150201

151-
Map<String, Collection<String>> headers = new LinkedHashMap<String, Collection<String>>();
152-
for (Map.Entry<String, List<String>> field : connection.getHeaderFields().entrySet()) {
153-
// response message
154-
if (field.getKey() != null) {
155-
headers.put(field.getKey(), field.getValue());
156-
}
157-
}
202+
public Proxied(SSLSocketFactory sslContextFactory, HostnameVerifier hostnameVerifier,
203+
Proxy proxy) {
204+
super(sslContextFactory, hostnameVerifier);
205+
checkNotNull(proxy, "a proxy is required.");
206+
this.proxy = proxy;
207+
}
158208

159-
Integer length = connection.getContentLength();
160-
if (length == -1) {
161-
length = null;
162-
}
163-
InputStream stream;
164-
if (status >= 400) {
165-
stream = connection.getErrorStream();
166-
} else {
167-
stream = connection.getInputStream();
209+
public Proxied(SSLSocketFactory sslContextFactory, HostnameVerifier hostnameVerifier,
210+
Proxy proxy, String proxyUser, String proxyPassword) {
211+
this(sslContextFactory, hostnameVerifier, proxy);
212+
checkArgument(isNotBlank(proxyUser), "proxy user is required.");
213+
checkArgument(isNotBlank(proxyPassword), "proxy password is required.");
214+
this.credentials = basic(proxyUser, proxyPassword);
215+
}
216+
217+
@Override
218+
public HttpURLConnection getConnection(URL url) throws IOException {
219+
HttpURLConnection connection = (HttpURLConnection) url.openConnection(this.proxy);
220+
if (isNotBlank(this.credentials)) {
221+
connection.addRequestProperty(PROXY_AUTHORIZATION, this.credentials);
168222
}
169-
return Response.builder()
170-
.status(status)
171-
.reason(reason)
172-
.headers(headers)
173-
.request(request)
174-
.body(stream, length)
175-
.build();
223+
return connection;
224+
}
225+
226+
public String getCredentials() {
227+
return this.credentials;
228+
}
229+
230+
private String basic(String username, String password) {
231+
String token = username + ":" + password;
232+
byte[] bytes = token.getBytes(StandardCharsets.ISO_8859_1);
233+
String encoded = Base64.getEncoder().encodeToString(bytes);
234+
return "Basic " + encoded;
176235
}
177236
}
178237
}

‎core/src/test/java/feign/client/DefaultClientTest.java‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,19 @@
1313
*/
1414
package feign.client;
1515

16+
import static org.assertj.core.api.Assertions.assertThat;
1617
import static org.hamcrest.core.Is.isA;
1718
import static org.junit.Assert.assertEquals;
19+
20+
import feign.Client.Proxied;
1821
import java.io.IOException;
22+
import java.net.HttpURLConnection;
23+
import java.net.InetSocketAddress;
1924
import java.net.ProtocolException;
25+
import java.net.Proxy;
26+
import java.net.Proxy.Type;
27+
import java.net.SocketAddress;
28+
import java.net.URL;
2029
import javax.net.ssl.HostnameVerifier;
2130
import javax.net.ssl.SSLSession;
2231
import org.junit.Test;
@@ -32,7 +41,7 @@
3241
*/
3342
public class DefaultClientTest extends AbstractClientTest {
3443

35-
Client disableHostnameVerification =
44+
protected Client disableHostnameVerification =
3645
new Client.Default(TrustingSSLSocketFactory.get(), new HostnameVerifier() {
3746
@Override
3847
public boolean verify(String s, SSLSession sslSession) {
@@ -103,4 +112,37 @@ public void canOverrideHostnameVerifier() throws IOException, InterruptedExcepti
103112
api.post("foo");
104113
}
105114

115+
private final SocketAddress proxyAddress =
116+
new InetSocketAddress("proxy.example.com", 8080);
117+
118+
/**
119+
* Test that the proxy is being used, but don't check the credentials. Credentials can still
120+
* be used, but they must be set using the appropriate system properties and testing that is
121+
* not what we are looking to do here.
122+
*/
123+
@Test
124+
public void canCreateWithImplicitOrNoCredentials() throws Exception {
125+
Proxied proxied = new Proxied(
126+
TrustingSSLSocketFactory.get(), null,
127+
new Proxy(Type.HTTP, proxyAddress));
128+
assertThat(proxied).isNotNull();
129+
assertThat(proxied.getCredentials()).isNullOrEmpty();
130+
131+
/* verify that the proxy */
132+
HttpURLConnection connection = proxied.getConnection(new URL("http://www.example.com"));
133+
assertThat(connection).isNotNull().isInstanceOf(HttpURLConnection.class);
134+
}
135+
136+
@Test
137+
public void canCreateWithExplicitCredentials() throws Exception {
138+
Proxied proxied = new Proxied(
139+
TrustingSSLSocketFactory.get(), null,
140+
new Proxy(Type.HTTP, proxyAddress), "user", "password");
141+
assertThat(proxied).isNotNull();
142+
assertThat(proxied.getCredentials()).isNotBlank();
143+
144+
HttpURLConnection connection = proxied.getConnection(new URL("http://www.example.com"));
145+
assertThat(connection).isNotNull().isInstanceOf(HttpURLConnection.class);
146+
}
147+
106148
}

‎pom.xml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,8 +495,11 @@
495495
<exclude>**/.idea/**</exclude>
496496
<exclude>**/target/**</exclude>
497497
<exclude>LICENSE</exclude>
498+
<exclude>NOTICE</exclude>
499+
<exclude>OSSMETADATA</exclude>
498500
<exclude>**/*.md</exclude>
499501
<exclude>bnd.bnd</exclude>
502+
<exclude>travis/**</exclude>
500503
<exclude>src/test/resources/**</exclude>
501504
<exclude>src/main/resources/**</exclude>
502505
</excludes>

0 commit comments

Comments
 (0)