From 9feb9f406640d97ff1e73414c6663aefcf581c02 Mon Sep 17 00:00:00 2001 From: Jorge Bescos Gascon Date: Wed, 22 Jun 2022 09:38:06 +0200 Subject: [PATCH] Support other connectors Signed-off-by: Jorge Bescos Gascon --- .../apache/connector/ApacheConnector.java | 18 +++--- .../apache5/connector/Apache5Connector.java | 17 +++-- .../jetty/connector/JettyConnector.java | 62 +++++++------------ .../jersey/client/ClientProperties.java | 21 ++++++- .../client/HttpUrlConnectorProvider.java | 2 +- .../client/internal/HttpUrlConnector.java | 31 +++++----- .../jersey/client/HttpUrlConnectorTest.java | 2 +- .../connector/proxy/ProxySelectorTest.java | 10 +-- .../jersey4003/LostResponseTest.java | 40 ++++++------ 9 files changed, 103 insertions(+), 100 deletions(-) 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 81e94b4b17..3820333642 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 9d101055f2..c752210174 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 780adc1e8a..26c962c3c4 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 7f1752ec71..9be3aa5596 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/HttpUrlConnectorProvider.java b/core-client/src/main/java/org/glassfish/jersey/client/HttpUrlConnectorProvider.java index 0dab495299..2fa7af2e4c 100644 --- a/core-client/src/main/java/org/glassfish/jersey/client/HttpUrlConnectorProvider.java +++ b/core-client/src/main/java/org/glassfish/jersey/client/HttpUrlConnectorProvider.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 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 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 21d2cae3ba..5f6c425c35 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/core-client/src/test/java/org/glassfish/jersey/client/HttpUrlConnectorTest.java b/core-client/src/test/java/org/glassfish/jersey/client/HttpUrlConnectorTest.java index b7a7616b65..e45e038a10 100644 --- a/core-client/src/test/java/org/glassfish/jersey/client/HttpUrlConnectorTest.java +++ b/core-client/src/test/java/org/glassfish/jersey/client/HttpUrlConnectorTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 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 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 facbcf5f58..5ac8c7609d 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); diff --git a/tests/integration/jersey-4003/src/test/java/org/glassfish/jersey/tests/integration/jersey4003/LostResponseTest.java b/tests/integration/jersey-4003/src/test/java/org/glassfish/jersey/tests/integration/jersey4003/LostResponseTest.java index 2ba8042961..1780d05a8d 100644 --- a/tests/integration/jersey-4003/src/test/java/org/glassfish/jersey/tests/integration/jersey4003/LostResponseTest.java +++ b/tests/integration/jersey-4003/src/test/java/org/glassfish/jersey/tests/integration/jersey4003/LostResponseTest.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 @@ -16,28 +16,11 @@ package org.glassfish.jersey.tests.integration.jersey4003; -import org.glassfish.jersey.client.ClientConfig; -import org.glassfish.jersey.client.HttpUrlConnectorProvider; -import org.glassfish.jersey.client.JerseyClientBuilder; -import org.glassfish.jersey.client.JerseyCompletionStageRxInvoker; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import org.mockito.Mockito; - -import javax.ws.rs.client.Client; -import javax.ws.rs.client.Entity; -import javax.ws.rs.client.InvocationCallback; -import javax.ws.rs.client.ResponseProcessingException; -import javax.ws.rs.core.GenericType; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.OutputStream; import java.net.HttpURLConnection; +import java.net.Proxy; import java.net.URL; import java.util.concurrent.CompletionStage; import java.util.concurrent.CountDownLatch; @@ -46,6 +29,23 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.InvocationCallback; +import javax.ws.rs.client.ResponseProcessingException; +import javax.ws.rs.core.GenericType; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import org.glassfish.jersey.client.ClientConfig; +import org.glassfish.jersey.client.HttpUrlConnectorProvider; +import org.glassfish.jersey.client.JerseyClientBuilder; +import org.glassfish.jersey.client.JerseyCompletionStageRxInvoker; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; + public class LostResponseTest { private static final String DUMMY_URL = "http://foo"; @@ -59,7 +59,7 @@ public void setup() throws IOException { HttpUrlConnectorProvider.ConnectionFactory connectionFactory = Mockito.mock(HttpUrlConnectorProvider.ConnectionFactory.class); HttpURLConnection connection = Mockito.mock(HttpURLConnection.class); - Mockito.when(connectionFactory.getConnection(Mockito.any(URL.class))).thenReturn(connection); + Mockito.when(connectionFactory.getConnection(Mockito.any(URL.class), Mockito.any())).thenReturn(connection); OutputStream outputStream = Mockito.mock(OutputStream.class); Mockito.when(connection.getOutputStream()).thenReturn(outputStream);