|
13 | 13 | */ |
14 | 14 | package feign; |
15 | 15 |
|
| 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; |
16 | 24 | import static java.lang.String.format; |
| 25 | + |
17 | 26 | import java.io.IOException; |
18 | 27 | import java.io.InputStream; |
19 | 28 | import java.io.OutputStream; |
20 | 29 | import java.net.HttpURLConnection; |
| 30 | +import java.net.Proxy; |
21 | 31 | import java.net.URL; |
| 32 | +import java.nio.charset.StandardCharsets; |
| 33 | +import java.util.Base64; |
22 | 34 | import java.util.Collection; |
23 | 35 | import java.util.LinkedHashMap; |
24 | 36 | import java.util.List; |
25 | 37 | import java.util.Map; |
26 | 38 | import java.util.zip.DeflaterOutputStream; |
27 | 39 | import java.util.zip.GZIPOutputStream; |
| 40 | + |
28 | 41 | import javax.net.ssl.HostnameVerifier; |
29 | 42 | import javax.net.ssl.HttpsURLConnection; |
30 | 43 | import javax.net.ssl.SSLSocketFactory; |
| 44 | + |
31 | 45 | 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; |
36 | 46 |
|
37 | 47 | /** |
38 | 48 | * 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 { |
68 | 78 | return convertResponse(connection, request); |
69 | 79 | } |
70 | 80 |
|
| 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 | + |
71 | 121 | 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); |
74 | 124 | if (connection instanceof HttpsURLConnection) { |
75 | 125 | HttpsURLConnection sslCon = (HttpsURLConnection) connection; |
76 | 126 | if (sslContextFactory != null) { |
@@ -138,41 +188,50 @@ HttpURLConnection convertAndSend(Request request, Options options) throws IOExce |
138 | 188 | } |
139 | 189 | return connection; |
140 | 190 | } |
| 191 | + } |
141 | 192 |
|
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 { |
145 | 197 |
|
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; |
150 | 201 |
|
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 | + } |
158 | 208 |
|
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); |
168 | 222 | } |
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; |
176 | 235 | } |
177 | 236 | } |
178 | 237 | } |
0 commit comments