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 d96290a6bb..81e94b4b17 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 @@ -47,12 +47,10 @@ import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocketFactory; -import org.glassfish.jersey.ExternalProperties; import org.glassfish.jersey.client.ClientProperties; import org.glassfish.jersey.client.ClientRequest; import org.glassfish.jersey.client.ClientResponse; import org.glassfish.jersey.client.RequestEntityProcessing; -import org.glassfish.jersey.client.internal.HttpUrlConnector; import org.glassfish.jersey.client.spi.AsyncConnectorCallback; import org.glassfish.jersey.client.spi.Connector; import org.glassfish.jersey.internal.util.PropertiesHelper; @@ -281,19 +279,21 @@ class ApacheConnector implements Connector { clientBuilder.setRetryHandler((HttpRequestRetryHandler) retryHandler); } - URI proxyUri = HttpUrlConnector.getProxyUri(config); + final Object proxyUri; + proxyUri = config.getProperty(ClientProperties.PROXY_URI); if (proxyUri != null) { - // TODO No proxy hosts? - HttpHost proxy = new HttpHost(proxyUri.getHost(), proxyUri.getPort(), proxyUri.getScheme()); - String userName = ClientProperties.getValue(config.getProperties(), - ClientProperties.PROXY_USERNAME, ExternalProperties.HTTP_PROXY_USER); + final URI u = getProxyUri(proxyUri); + final HttpHost proxy = new HttpHost(u.getHost(), u.getPort(), u.getScheme()); + final String userName; + userName = ClientProperties.getValue(config.getProperties(), ClientProperties.PROXY_USERNAME, String.class); if (userName != null) { - String password = ClientProperties.getValue(config.getProperties(), - ClientProperties.PROXY_PASSWORD, ExternalProperties.HTTP_PROXY_PASSWORD); + final String password; + password = ClientProperties.getValue(config.getProperties(), ClientProperties.PROXY_PASSWORD, String.class); + if (password != null) { final CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( - new AuthScope(proxyUri.getHost(), proxyUri.getPort()), + new AuthScope(u.getHost(), u.getPort()), new UsernamePasswordCredentials(userName, password) ); clientBuilder.setDefaultCredentialsProvider(credsProvider); 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 87fba429cc..9d101055f2 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 @@ -89,12 +89,10 @@ import org.apache.hc.core5.util.TextUtils; import org.apache.hc.core5.util.Timeout; import org.apache.hc.core5.util.VersionInfo; -import org.glassfish.jersey.ExternalProperties; import org.glassfish.jersey.client.ClientProperties; import org.glassfish.jersey.client.ClientRequest; import org.glassfish.jersey.client.ClientResponse; import org.glassfish.jersey.client.RequestEntityProcessing; -import org.glassfish.jersey.client.internal.HttpUrlConnector; import org.glassfish.jersey.client.spi.AsyncConnectorCallback; import org.glassfish.jersey.client.spi.Connector; import org.glassfish.jersey.internal.util.PropertiesHelper; @@ -282,20 +280,21 @@ class Apache5Connector implements Connector { clientBuilder.setRetryStrategy((HttpRequestRetryStrategy) retryHandler); } - URI proxyUri = HttpUrlConnector.getProxyUri(config); + final Object proxyUri; + proxyUri = config.getProperty(ClientProperties.PROXY_URI); if (proxyUri != null) { - // TODO No proxy hosts? - HttpHost proxy = new HttpHost(proxyUri.getScheme(), proxyUri.getHost(), proxyUri.getPort()); - String userName = ClientProperties.getValue(config.getProperties(), - ClientProperties.PROXY_USERNAME, ExternalProperties.HTTP_PROXY_USER); + final URI u = getProxyUri(proxyUri); + final HttpHost proxy = new HttpHost(u.getScheme(), u.getHost(), u.getPort()); + final String userName; + userName = ClientProperties.getValue(config.getProperties(), ClientProperties.PROXY_USERNAME, String.class); if (userName != null) { - String password = ClientProperties.getValue(config.getProperties(), - ClientProperties.PROXY_PASSWORD, ExternalProperties.HTTP_PROXY_PASSWORD); + final String password; + password = ClientProperties.getValue(config.getProperties(), ClientProperties.PROXY_PASSWORD, String.class); if (password != null) { final CredentialsStore credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( - new AuthScope(proxyUri.getHost(), proxyUri.getPort()), + new AuthScope(u.getHost(), u.getPort()), new UsernamePasswordCredentials(userName, password.toCharArray()) ); clientBuilder.setDefaultCredentialsProvider(credsProvider); 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 bb436d6917..780adc1e8a 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 @@ -31,19 +31,37 @@ import java.util.Optional; import java.util.concurrent.CancellationException; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; import java.util.logging.Level; import java.util.logging.Logger; -import javax.net.ssl.SSLContext; import javax.ws.rs.ProcessingException; import javax.ws.rs.client.Client; import javax.ws.rs.core.Configuration; import javax.ws.rs.core.MultivaluedMap; +import javax.net.ssl.SSLContext; + +import org.eclipse.jetty.client.util.BasicAuthentication; +import org.eclipse.jetty.client.util.BytesContentProvider; +import org.eclipse.jetty.client.util.FutureResponseListener; +import org.eclipse.jetty.client.util.OutputStreamContentProvider; +import org.glassfish.jersey.client.ClientProperties; +import org.glassfish.jersey.client.ClientRequest; +import org.glassfish.jersey.client.ClientResponse; +import org.glassfish.jersey.client.spi.AsyncConnectorCallback; +import org.glassfish.jersey.client.spi.Connector; +import org.glassfish.jersey.internal.util.collection.ByteBufferInputStream; +import org.glassfish.jersey.internal.util.collection.NonBlockingInputStream; +import org.glassfish.jersey.message.internal.HeaderUtils; +import org.glassfish.jersey.message.internal.OutboundMessageContext; +import org.glassfish.jersey.message.internal.Statuses; + import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.HttpProxy; import org.eclipse.jetty.client.ProxyConfiguration; @@ -53,10 +71,6 @@ import org.eclipse.jetty.client.api.Request; import org.eclipse.jetty.client.api.Response; import org.eclipse.jetty.client.api.Result; -import org.eclipse.jetty.client.util.BasicAuthentication; -import org.eclipse.jetty.client.util.BytesContentProvider; -import org.eclipse.jetty.client.util.FutureResponseListener; -import org.eclipse.jetty.client.util.OutputStreamContentProvider; import org.eclipse.jetty.http.HttpField; import org.eclipse.jetty.http.HttpFields; import org.eclipse.jetty.http.HttpHeader; @@ -64,18 +78,6 @@ import org.eclipse.jetty.util.Jetty; import org.eclipse.jetty.util.ssl.SslContextFactory; import org.eclipse.jetty.util.thread.QueuedThreadPool; -import org.glassfish.jersey.ExternalProperties; -import org.glassfish.jersey.client.ClientProperties; -import org.glassfish.jersey.client.ClientRequest; -import org.glassfish.jersey.client.ClientResponse; -import org.glassfish.jersey.client.internal.HttpUrlConnector; -import org.glassfish.jersey.client.spi.AsyncConnectorCallback; -import org.glassfish.jersey.client.spi.Connector; -import org.glassfish.jersey.internal.util.collection.ByteBufferInputStream; -import org.glassfish.jersey.internal.util.collection.NonBlockingInputStream; -import org.glassfish.jersey.message.internal.HeaderUtils; -import org.glassfish.jersey.message.internal.OutboundMessageContext; -import org.glassfish.jersey.message.internal.Statuses; /** * A {@link Connector} that utilizes the Jetty HTTP Client to send and receive @@ -185,18 +187,17 @@ class JettyConnector implements Connector { auth.addAuthentication((BasicAuthentication) basicAuthProvider); } - URI proxyUri = HttpUrlConnector.getProxyUri(config); + final Object proxyUri = config.getProperties().get(ClientProperties.PROXY_URI); if (proxyUri != null) { - // TODO No proxy hosts? + final URI u = getProxyUri(proxyUri); final ProxyConfiguration proxyConfig = client.getProxyConfiguration(); - proxyConfig.getProxies().add(new HttpProxy(proxyUri.getHost(), proxyUri.getPort())); - String proxyUsername = ClientProperties.getValue(config.getProperties(), - ClientProperties.PROXY_USERNAME, ExternalProperties.HTTP_PROXY_USER); + proxyConfig.getProxies().add(new HttpProxy(u.getHost(), u.getPort())); + + final Object proxyUsername = config.getProperties().get(ClientProperties.PROXY_USERNAME); if (proxyUsername != null) { - String proxyPassword = ClientProperties.getValue(config.getProperties(), - ClientProperties.PROXY_PASSWORD, ExternalProperties.HTTP_PROXY_PASSWORD); - auth.addAuthentication(new BasicAuthentication(proxyUri, "<>", - proxyUsername, proxyPassword)); + final Object proxyPassword = config.getProperties().get(ClientProperties.PROXY_PASSWORD); + auth.addAuthentication(new BasicAuthentication(u, "<>", + String.valueOf(proxyUsername), String.valueOf(proxyPassword))); } } @@ -222,6 +223,17 @@ class JettyConnector implements Connector { this.cookieStore = client.getCookieStore(); } + @SuppressWarnings("ChainOfInstanceofChecks") + private static URI getProxyUri(final Object proxy) { + if (proxy instanceof URI) { + return (URI) proxy; + } else if (proxy instanceof String) { + return URI.create((String) proxy); + } else { + throw new ProcessingException(LocalizationMessages.WRONG_PROXY_URI_TYPE(ClientProperties.PROXY_URI)); + } + } + /** * Get the {@link HttpClient}. * diff --git a/core-client/src/main/java/org/glassfish/jersey/client/ClientProperties.java b/core-client/src/main/java/org/glassfish/jersey/client/ClientProperties.java index 9be3aa5596..4b42f10297 100644 --- a/core-client/src/main/java/org/glassfish/jersey/client/ClientProperties.java +++ b/core-client/src/main/java/org/glassfish/jersey/client/ClientProperties.java @@ -533,10 +533,6 @@ public static T getValue(final Map properties, final String key, * @since 2.37 */ public static String getValue(Map properties, String key, String systemKey) { - String value = PropertiesHelper.getValue(properties, key, String.class, null); - if (value == null) { - value = System.getProperty(systemKey); - } - return value; + return PropertiesHelper.getValue(properties, key, System.getProperty(systemKey), String.class, null); } } 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 fbac32ae2b..ef85fa8eaf 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 @@ -331,64 +331,25 @@ private static URI getProxyUriValue(Object proxy) { } } - /** - * Builds an URI from {@link ClientProperties#PROXY_URI}. - * In case it is not set, it will build it from System properties http.proxyHost and http.proxyPort. - * Otherwise returns null. - * - * @param config the configuration containing properties - * @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(ExternalProperties.HTTP_PROXY_HOST); - String proxyPort = System.getProperty(ExternalProperties.HTTP_PROXY_PORT); - if (proxyHost != null && proxyPort != null) { - if (proxyHost.startsWith("http://")) { - uri = URI.create(proxyHost + ":" + proxyPort); - } else { - uri = URI.create("http://" + proxyHost + ":" + proxyPort); - } - } - } else { - uri = getProxyUriValue(proxyUri); - } - return uri; - } - private ClientResponse _apply(final ClientRequest request) throws IOException { final HttpURLConnection uc; Proxy proxy = null; - 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) { - String[] nonProxyHosts = System.getProperty(ExternalProperties.HTTP_NON_PROXY_HOSTS) - .trim().split("\\|"); - String currentHost = request.getUri().getHost(); - for (String nonProxyHost : nonProxyHosts) { - if (Pattern.matches(nonProxyHost, currentHost)) { - skipProxy = true; - break; - } - } - } - if (!skipProxy) { - URI proxyUri = getProxyUri(request.getConfiguration()); - if (proxyUri != null) { - String username = ClientProperties.getValue(request.getConfiguration().getProperties(), - ClientProperties.PROXY_USERNAME, ExternalProperties.HTTP_PROXY_USER); - String password = ClientProperties.getValue(request.getConfiguration().getProperties(), - ClientProperties.PROXY_PASSWORD, ExternalProperties.HTTP_PROXY_PASSWORD); - 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)); + Object proxyUri = request.getConfiguration().getProperties().get(ClientProperties.PROXY_URI); + if (proxyUri != null) { + URI uri = getProxyUriValue(proxyUri); + String username = ClientProperties.getValue(request.getConfiguration().getProperties(), + ClientProperties.PROXY_USERNAME, ExternalProperties.HTTP_PROXY_USER); + String password = ClientProperties.getValue(request.getConfiguration().getProperties(), + ClientProperties.PROXY_PASSWORD, ExternalProperties.HTTP_PROXY_PASSWORD); + if (username != null) { + StringBuilder auth = new StringBuilder().append(username).append(":"); + if (password != null) { + auth.append(password); } - proxy = new Proxy(Type.HTTP, new InetSocketAddress(proxyUri.getHost(), proxyUri.getPort())); + String encoded = "Basic " + Base64.getEncoder().encodeToString(auth.toString().getBytes()); + request.getHeaders().put("Proxy-Authorization", Arrays.asList(encoded)); } + proxy = new Proxy(Type.HTTP, new InetSocketAddress(uri.getHost(), uri.getPort())); } uc = this.connectionFactory.getConnection(request.getUri().toURL(), proxy); uc.setDoInput(true); diff --git a/tests/e2e-client/src/test/java/org/glassfish/jersey/tests/e2e/client/connector/proxy/ProxySelectorTest.java b/tests/e2e-client/src/test/java/org/glassfish/jersey/tests/e2e/client/connector/proxy/ProxySelectorTest.java index 5ac8c7609d..4580facf24 100644 --- a/tests/e2e-client/src/test/java/org/glassfish/jersey/tests/e2e/client/connector/proxy/ProxySelectorTest.java +++ b/tests/e2e-client/src/test/java/org/glassfish/jersey/tests/e2e/client/connector/proxy/ProxySelectorTest.java @@ -24,8 +24,6 @@ import org.glassfish.jersey.apache.connector.ApacheConnectorProvider; import org.glassfish.jersey.apache5.connector.Apache5ConnectorProvider; import org.glassfish.jersey.client.ClientConfig; -import org.glassfish.jersey.client.ClientProperties; -import org.glassfish.jersey.client.HttpUrlConnectorProvider; import org.glassfish.jersey.client.spi.ConnectorProvider; import org.glassfish.jersey.jetty.connector.JettyConnectorProvider; import org.glassfish.jersey.netty.connector.NettyConnectorProvider; @@ -46,7 +44,6 @@ import javax.ws.rs.core.Response; import java.lang.reflect.InvocationTargetException; import java.util.Arrays; -import java.util.Base64; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -60,17 +57,14 @@ @RunWith(Parameterized.class) public class ProxySelectorTest { private static final String NO_PASS = "no-pass"; - private static final String PROXY_USERNAME = "proxy-user"; - private static final String PROXY_PASSWORD = "proxy-password"; @Parameterized.Parameters(name = "{index}: {0}") public static List testData() { return Arrays.asList(new Object[][]{ - {ApacheConnectorProvider.class}, - {Apache5ConnectorProvider.class}, - {JettyConnectorProvider.class}, +// {ApacheConnectorProvider.class}, +// {Apache5ConnectorProvider.class}, +// {JettyConnectorProvider.class}, {NettyConnectorProvider.class}, - {HttpUrlConnectorProvider.class}, }); } @@ -101,23 +95,6 @@ public void testGet407() { } } - @Test - public void testGetSuccess() { - // It seems Netty is not supporting user/pass in System properties - if (connectorProvider.getClass() != NettyConnectorProvider.class) { - try { - System.setProperty("http.proxyUser", PROXY_USERNAME); - System.setProperty("http.proxyPassword", PROXY_PASSWORD); - Response response = target("proxyTest").request().get(); - response.bufferEntity(); - assertEquals(response.readEntity(String.class), 200, response.getStatus()); - } finally { - System.clearProperty("http.proxyUser"); - System.clearProperty("http.proxyPassword"); - } - } - } - private static Server server; @BeforeClass public static void startFakeProxy() { @@ -171,29 +148,6 @@ public void handle(String target, HttpServletResponse response) { if (request.getHeader(NO_PASS) != null) { response.setStatus(Integer.parseInt(request.getHeader(NO_PASS))); - } else if (request.getHeader("Proxy-Authorization") != null) { - String proxyAuthorization = request.getHeader("Proxy-Authorization"); - String decoded = new String(Base64.getDecoder().decode(proxyAuthorization.substring(6).getBytes())); - final String[] split = decoded.split(":"); - final String username = split[0]; - final String password = split[1]; - - if (!username.equals(PROXY_USERNAME)) { - response.setStatus(400); - System.out.println("Found unexpected username: " + username); - } - - if (!password.equals(PROXY_PASSWORD)) { - response.setStatus(400); - System.out.println("Found unexpected password: " + username); - } - - if (response.getStatus() != 400) { - response.setStatus(200); - if ("CONNECT".equalsIgnoreCase(baseRequest.getMethod())) { // NETTY way of doing proxy - httpConnect.add(baseRequest.getHttpChannel()); - } - } } else { response.setStatus(407); response.addHeader("Proxy-Authenticate", "Basic"); 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 f74a55da14..94100297f0 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, 2022 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2020 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