diff --git a/connectors/apache-connector/src/main/java/org/glassfish/jersey/apache/connector/ApacheConnector.java b/connectors/apache-connector/src/main/java/org/glassfish/jersey/apache/connector/ApacheConnector.java index 3820333642f..2b48621be37 100644 --- a/connectors/apache-connector/src/main/java/org/glassfish/jersey/apache/connector/ApacheConnector.java +++ b/connectors/apache-connector/src/main/java/org/glassfish/jersey/apache/connector/ApacheConnector.java @@ -282,6 +282,7 @@ class ApacheConnector implements Connector { URI proxyUri = HttpUrlConnector.getProxyUri(config); if (proxyUri != null) { + // FIXME No proxy hosts? HttpHost proxy = new HttpHost(proxyUri.getHost(), proxyUri.getPort(), proxyUri.getScheme()); String userName = ClientProperties.getValue(config.getProperties(), ClientProperties.PROXY_USERNAME, "http.proxyUser"); diff --git a/connectors/apache5-connector/src/main/java/org/glassfish/jersey/apache5/connector/Apache5Connector.java b/connectors/apache5-connector/src/main/java/org/glassfish/jersey/apache5/connector/Apache5Connector.java index c7522101744..287ec517c3b 100644 --- a/connectors/apache5-connector/src/main/java/org/glassfish/jersey/apache5/connector/Apache5Connector.java +++ b/connectors/apache5-connector/src/main/java/org/glassfish/jersey/apache5/connector/Apache5Connector.java @@ -283,6 +283,7 @@ class Apache5Connector implements Connector { URI proxyUri = HttpUrlConnector.getProxyUri(config); if (proxyUri != null) { + // FIXME No proxy hosts? HttpHost proxy = new HttpHost(proxyUri.getScheme(), proxyUri.getHost(), proxyUri.getPort()); String userName = ClientProperties.getValue(config.getProperties(), ClientProperties.PROXY_USERNAME, "http.proxyUser"); diff --git a/connectors/jetty-connector/src/main/java/org/glassfish/jersey/jetty/connector/JettyConnector.java b/connectors/jetty-connector/src/main/java/org/glassfish/jersey/jetty/connector/JettyConnector.java index 26c962c3c49..c5e0451456e 100644 --- a/connectors/jetty-connector/src/main/java/org/glassfish/jersey/jetty/connector/JettyConnector.java +++ b/connectors/jetty-connector/src/main/java/org/glassfish/jersey/jetty/connector/JettyConnector.java @@ -186,6 +186,7 @@ class JettyConnector implements Connector { URI proxyUri = HttpUrlConnector.getProxyUri(config); if (proxyUri != null) { + // FIXME No proxy hosts? final ProxyConfiguration proxyConfig = client.getProxyConfiguration(); proxyConfig.getProxies().add(new HttpProxy(proxyUri.getHost(), proxyUri.getPort())); String proxyUsername = ClientProperties.getValue(config.getProperties(), diff --git a/core-client/src/main/java/org/glassfish/jersey/client/internal/HttpUrlConnector.java b/core-client/src/main/java/org/glassfish/jersey/client/internal/HttpUrlConnector.java index 5f6c425c35b..468ca4482b9 100644 --- a/core-client/src/main/java/org/glassfish/jersey/client/internal/HttpUrlConnector.java +++ b/core-client/src/main/java/org/glassfish/jersey/client/internal/HttpUrlConnector.java @@ -53,6 +53,7 @@ import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; +import org.glassfish.jersey.ExternalProperties; import org.glassfish.jersey.client.ClientProperties; import org.glassfish.jersey.client.ClientRequest; import org.glassfish.jersey.client.ClientResponse; @@ -338,34 +339,49 @@ private static URI getProxyUriValue(Object proxy) { * @return the URI or null */ public static URI getProxyUri(Configuration config) { + URI uri = null; Object proxyUri = config.getProperty(ClientProperties.PROXY_URI); if (proxyUri == null) { - String proxyHost = System.getProperty("http.proxyHost"); - String proxyPort = System.getProperty("http.proxyPort"); + String proxyHost = System.getProperty(ExternalProperties.HTTP_PROXY_HOST); + String proxyPort = System.getProperty(ExternalProperties.HTTP_PROXY_PORT); if (proxyHost != null && proxyPort != null) { - return URI.create(proxyHost + ":" + proxyPort); + if (proxyHost.startsWith("http://")) { + uri = URI.create(proxyHost + ":" + proxyPort); + } else { + uri = URI.create("http://" + proxyHost + ":" + proxyPort); + } } } else { - return getProxyUriValue(proxyUri); + uri = getProxyUriValue(proxyUri); } - return null; + return uri; } private ClientResponse _apply(final ClientRequest request) throws IOException { final HttpURLConnection uc; Proxy proxy = null; - URI proxyUri = getProxyUri(request.getConfiguration()); - if (proxyUri != null) { - String username = ClientProperties.getValue(request.getConfiguration().getProperties(), - ClientProperties.PROXY_USERNAME, "http.proxyUser"); - String password = ClientProperties.getValue(request.getConfiguration().getProperties(), - ClientProperties.PROXY_PASSWORD, "http.proxyPassword"); - if (username != null && password != null) { - StringBuilder auth = new StringBuilder().append(username).append(":").append(password); - String encoded = "Basic " + Base64.getEncoder().encodeToString(auth.toString().getBytes()); - request.getHeaders().put("Proxy-Authorization", Arrays.asList(encoded)); + boolean skipProxy = false; + // Evaluate HTTP_NON_PROXY_HOSTS if HTTP_PROXY_HOST is also set + if (System.getProperty(ExternalProperties.HTTP_PROXY_HOST) != null + && System.getProperty(ExternalProperties.HTTP_NON_PROXY_HOSTS) != null) { + List nonProxyHosts = Arrays.asList(System.getProperty(ExternalProperties.HTTP_NON_PROXY_HOSTS) + .trim().split(",")); + skipProxy = nonProxyHosts.contains(request.getUri().getHost()); + } + if (!skipProxy) { + URI proxyUri = getProxyUri(request.getConfiguration()); + if (proxyUri != null) { + String username = ClientProperties.getValue(request.getConfiguration().getProperties(), + ClientProperties.PROXY_USERNAME, "http.proxyUser"); + String password = ClientProperties.getValue(request.getConfiguration().getProperties(), + ClientProperties.PROXY_PASSWORD, "http.proxyPassword"); + if (username != null && password != null) { + StringBuilder auth = new StringBuilder().append(username).append(":").append(password); + String encoded = "Basic " + Base64.getEncoder().encodeToString(auth.toString().getBytes()); + request.getHeaders().put("Proxy-Authorization", Arrays.asList(encoded)); + } + proxy = new Proxy(Type.HTTP, new InetSocketAddress(proxyUri.getHost(), proxyUri.getPort())); } - proxy = new Proxy(Type.HTTP, new InetSocketAddress(proxyUri.getHost(), proxyUri.getPort())); } uc = this.connectionFactory.getConnection(request.getUri().toURL(), proxy); uc.setDoInput(true); diff --git a/tests/integration/externalproperties/src/test/java/org/glassfish/jersey/tests/externalproperties/HttpProxyTest.java b/tests/integration/externalproperties/src/test/java/org/glassfish/jersey/tests/externalproperties/HttpProxyTest.java index 94100297f0a..f74a55da144 100644 --- a/tests/integration/externalproperties/src/test/java/org/glassfish/jersey/tests/externalproperties/HttpProxyTest.java +++ b/tests/integration/externalproperties/src/test/java/org/glassfish/jersey/tests/externalproperties/HttpProxyTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2020, 2022 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at