|
42 | 42 | import java.util.concurrent.Future; |
43 | 43 | import java.util.logging.Level; |
44 | 44 | import java.util.logging.Logger; |
| 45 | +import java.util.regex.Pattern; |
45 | 46 | import java.util.stream.Collectors; |
46 | 47 |
|
47 | 48 | import javax.net.ssl.HostnameVerifier; |
|
53 | 54 | import javax.ws.rs.core.MultivaluedMap; |
54 | 55 | import javax.ws.rs.core.Response; |
55 | 56 |
|
| 57 | +import org.glassfish.jersey.ExternalProperties; |
56 | 58 | import org.glassfish.jersey.client.ClientProperties; |
57 | 59 | import org.glassfish.jersey.client.ClientRequest; |
58 | 60 | import org.glassfish.jersey.client.ClientResponse; |
@@ -338,34 +340,55 @@ private static URI getProxyUriValue(Object proxy) { |
338 | 340 | * @return the URI or null |
339 | 341 | */ |
340 | 342 | public static URI getProxyUri(Configuration config) { |
| 343 | + URI uri = null; |
341 | 344 | Object proxyUri = config.getProperty(ClientProperties.PROXY_URI); |
342 | 345 | if (proxyUri == null) { |
343 | | - String proxyHost = System.getProperty("http.proxyHost"); |
344 | | - String proxyPort = System.getProperty("http.proxyPort"); |
| 346 | + String proxyHost = System.getProperty(ExternalProperties.HTTP_PROXY_HOST); |
| 347 | + String proxyPort = System.getProperty(ExternalProperties.HTTP_PROXY_PORT); |
345 | 348 | if (proxyHost != null && proxyPort != null) { |
346 | | - return URI.create(proxyHost + ":" + proxyPort); |
| 349 | + if (proxyHost.startsWith("http://")) { |
| 350 | + uri = URI.create(proxyHost + ":" + proxyPort); |
| 351 | + } else { |
| 352 | + uri = URI.create("http://" + proxyHost + ":" + proxyPort); |
| 353 | + } |
347 | 354 | } |
348 | 355 | } else { |
349 | | - return getProxyUriValue(proxyUri); |
| 356 | + uri = getProxyUriValue(proxyUri); |
350 | 357 | } |
351 | | - return null; |
| 358 | + return uri; |
352 | 359 | } |
353 | 360 |
|
354 | 361 | private ClientResponse _apply(final ClientRequest request) throws IOException { |
355 | 362 | final HttpURLConnection uc; |
356 | 363 | Proxy proxy = null; |
357 | | - URI proxyUri = getProxyUri(request.getConfiguration()); |
358 | | - if (proxyUri != null) { |
359 | | - String username = ClientProperties.getValue(request.getConfiguration().getProperties(), |
360 | | - ClientProperties.PROXY_USERNAME, "http.proxyUser"); |
361 | | - String password = ClientProperties.getValue(request.getConfiguration().getProperties(), |
362 | | - ClientProperties.PROXY_PASSWORD, "http.proxyPassword"); |
363 | | - if (username != null && password != null) { |
364 | | - StringBuilder auth = new StringBuilder().append(username).append(":").append(password); |
365 | | - String encoded = "Basic " + Base64.getEncoder().encodeToString(auth.toString().getBytes()); |
366 | | - request.getHeaders().put("Proxy-Authorization", Arrays.asList(encoded)); |
| 364 | + boolean skipProxy = false; |
| 365 | + // Evaluate HTTP_NON_PROXY_HOSTS if HTTP_PROXY_HOST is also set |
| 366 | + if (System.getProperty(ExternalProperties.HTTP_PROXY_HOST) != null |
| 367 | + && System.getProperty(ExternalProperties.HTTP_NON_PROXY_HOSTS) != null) { |
| 368 | + String[] nonProxyHosts = System.getProperty(ExternalProperties.HTTP_NON_PROXY_HOSTS) |
| 369 | + .trim().split("\\|"); |
| 370 | + String currentHost = request.getUri().getHost(); |
| 371 | + for (String nonProxyHost : nonProxyHosts) { |
| 372 | + if (Pattern.matches(nonProxyHost, currentHost)) { |
| 373 | + skipProxy = true; |
| 374 | + break; |
| 375 | + } |
| 376 | + } |
| 377 | + } |
| 378 | + if (!skipProxy) { |
| 379 | + URI proxyUri = getProxyUri(request.getConfiguration()); |
| 380 | + if (proxyUri != null) { |
| 381 | + String username = ClientProperties.getValue(request.getConfiguration().getProperties(), |
| 382 | + ClientProperties.PROXY_USERNAME, "http.proxyUser"); |
| 383 | + String password = ClientProperties.getValue(request.getConfiguration().getProperties(), |
| 384 | + ClientProperties.PROXY_PASSWORD, "http.proxyPassword"); |
| 385 | + if (username != null && password != null) { |
| 386 | + StringBuilder auth = new StringBuilder().append(username).append(":").append(password); |
| 387 | + String encoded = "Basic " + Base64.getEncoder().encodeToString(auth.toString().getBytes()); |
| 388 | + request.getHeaders().put("Proxy-Authorization", Arrays.asList(encoded)); |
| 389 | + } |
| 390 | + proxy = new Proxy(Type.HTTP, new InetSocketAddress(proxyUri.getHost(), proxyUri.getPort())); |
367 | 391 | } |
368 | | - proxy = new Proxy(Type.HTTP, new InetSocketAddress(proxyUri.getHost(), proxyUri.getPort())); |
369 | 392 | } |
370 | 393 | uc = this.connectionFactory.getConnection(request.getUri().toURL(), proxy); |
371 | 394 | uc.setDoInput(true); |
|
0 commit comments