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 81e94b4b17f..3820333642f 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 @@ -51,6 +51,7 @@ 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; @@ -279,21 +280,18 @@ class ApacheConnector implements Connector { clientBuilder.setRetryHandler((HttpRequestRetryHandler) retryHandler); } - final Object proxyUri; - proxyUri = config.getProperty(ClientProperties.PROXY_URI); + URI proxyUri = HttpUrlConnector.getProxyUri(config); if (proxyUri != null) { - 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); + HttpHost proxy = new HttpHost(proxyUri.getHost(), proxyUri.getPort(), proxyUri.getScheme()); + String userName = ClientProperties.getValue(config.getProperties(), + ClientProperties.PROXY_USERNAME, "http.proxyUser"); if (userName != null) { - final String password; - password = ClientProperties.getValue(config.getProperties(), ClientProperties.PROXY_PASSWORD, String.class); - + String password = ClientProperties.getValue(config.getProperties(), + ClientProperties.PROXY_PASSWORD, "http.proxyPassword"); if (password != null) { final CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( - new AuthScope(u.getHost(), u.getPort()), + new AuthScope(proxyUri.getHost(), proxyUri.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 9d101055f27..c7522101744 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 @@ -93,6 +93,7 @@ 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; @@ -280,21 +281,19 @@ class Apache5Connector implements Connector { clientBuilder.setRetryStrategy((HttpRequestRetryStrategy) retryHandler); } - final Object proxyUri; - proxyUri = config.getProperty(ClientProperties.PROXY_URI); + URI proxyUri = HttpUrlConnector.getProxyUri(config); if (proxyUri != null) { - 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); + HttpHost proxy = new HttpHost(proxyUri.getScheme(), proxyUri.getHost(), proxyUri.getPort()); + String userName = ClientProperties.getValue(config.getProperties(), + ClientProperties.PROXY_USERNAME, "http.proxyUser"); if (userName != null) { - final String password; - password = ClientProperties.getValue(config.getProperties(), ClientProperties.PROXY_PASSWORD, String.class); + String password = ClientProperties.getValue(config.getProperties(), + ClientProperties.PROXY_PASSWORD, "http.proxyPassword"); if (password != null) { final CredentialsStore credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( - new AuthScope(u.getHost(), u.getPort()), + new AuthScope(proxyUri.getHost(), proxyUri.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 780adc1e8a5..26c962c3c49 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,37 +31,19 @@ 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; @@ -71,6 +53,10 @@ 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; @@ -78,6 +64,17 @@ import org.eclipse.jetty.util.Jetty; import org.eclipse.jetty.util.ssl.SslContextFactory; import org.eclipse.jetty.util.thread.QueuedThreadPool; +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 @@ -187,17 +184,17 @@ class JettyConnector implements Connector { auth.addAuthentication((BasicAuthentication) basicAuthProvider); } - final Object proxyUri = config.getProperties().get(ClientProperties.PROXY_URI); + URI proxyUri = HttpUrlConnector.getProxyUri(config); if (proxyUri != null) { - final URI u = getProxyUri(proxyUri); final ProxyConfiguration proxyConfig = client.getProxyConfiguration(); - proxyConfig.getProxies().add(new HttpProxy(u.getHost(), u.getPort())); - - final Object proxyUsername = config.getProperties().get(ClientProperties.PROXY_USERNAME); + proxyConfig.getProxies().add(new HttpProxy(proxyUri.getHost(), proxyUri.getPort())); + String proxyUsername = ClientProperties.getValue(config.getProperties(), + ClientProperties.PROXY_USERNAME, "http.proxyUser"); if (proxyUsername != null) { - final Object proxyPassword = config.getProperties().get(ClientProperties.PROXY_PASSWORD); - auth.addAuthentication(new BasicAuthentication(u, "<>", - String.valueOf(proxyUsername), String.valueOf(proxyPassword))); + String proxyPassword = ClientProperties.getValue(config.getProperties(), + ClientProperties.PROXY_PASSWORD, "http.proxyPassword"); + auth.addAuthentication(new BasicAuthentication(proxyUri, "<>", + proxyUsername, proxyPassword)); } } @@ -223,17 +220,6 @@ 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 7f1752ec71a..9be3aa5596e 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 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 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 @@ -520,4 +520,23 @@ public static T getValue(final Map properties, final String key, public static T getValue(final Map properties, final String key, final Class type) { return PropertiesHelper.getValue(properties, key, type, null); } + + /** + * Get the value of the specified property. If null, it will obtain it from System property. + *

+ * If the property is not set the method will return {@code null}. + * + * @param properties Map of properties to get the property value from. + * @param key Name of the property. + * @param systemKey Name of the System property. + * @return Value of the property or {@code null}. + * @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; + } } 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 21d2cae3ba9..5f6c425c35b 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 @@ -319,7 +319,7 @@ protected void secureConnection(final JerseyClient client, final HttpURLConnecti } } - private URI getProxyUri(Object proxy) { + private static URI getProxyUriValue(Object proxy) { if (proxy instanceof URI) { return (URI) proxy; } else if (proxy instanceof String) { @@ -329,16 +329,15 @@ private URI getProxyUri(Object proxy) { } } - private String getFromConfigOrSystem(ClientRequest request, String clientProperty, String systemProperty) { - String result = request.resolveProperty(clientProperty, String.class); - if (result == null) { - result = System.getProperty(systemProperty); - } - return result; - } - - private URI getProxyUri(ClientRequest request) { - Configuration config = request.getConfiguration(); + /** + * 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) { Object proxyUri = config.getProperty(ClientProperties.PROXY_URI); if (proxyUri == null) { String proxyHost = System.getProperty("http.proxyHost"); @@ -347,7 +346,7 @@ private URI getProxyUri(ClientRequest request) { return URI.create(proxyHost + ":" + proxyPort); } } else { - return getProxyUri(proxyUri); + return getProxyUriValue(proxyUri); } return null; } @@ -355,10 +354,12 @@ private URI getProxyUri(ClientRequest request) { private ClientResponse _apply(final ClientRequest request) throws IOException { final HttpURLConnection uc; Proxy proxy = null; - URI proxyUri = getProxyUri(request); + URI proxyUri = getProxyUri(request.getConfiguration()); if (proxyUri != null) { - String username = getFromConfigOrSystem(request, ClientProperties.PROXY_USERNAME, "http.proxyUser"); - String password = getFromConfigOrSystem(request, ClientProperties.PROXY_PASSWORD, "http.proxyPassword"); + 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()); 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 facbcf5f58b..5ac8c7609d2 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 @@ -66,9 +66,9 @@ public class ProxySelectorTest { @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}, }); @@ -103,8 +103,8 @@ public void testGet407() { @Test public void testGetSuccess() { - // Next applies for HttpUrlConnectorProvider. It seems Netty is not supporting user/pass in System properties - if (connectorProvider.getClass() == HttpUrlConnectorProvider.class) { + // 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);