Skip to content

Commit

Permalink
Support other connectors
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Bescos Gascon <[email protected]>
  • Loading branch information
jbescos committed Jun 22, 2022
1 parent f850e5e commit 5786071
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -71,13 +53,28 @@
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;
import org.eclipse.jetty.util.HttpCookieStore;
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
Expand Down Expand Up @@ -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, "<<ANY_REALM>>",
String.valueOf(proxyUsername), String.valueOf(proxyPassword)));
String proxyPassword = ClientProperties.getValue(config.getProperties(),
ClientProperties.PROXY_PASSWORD, "http.proxyPassword");
auth.addAuthentication(new BasicAuthentication(proxyUri, "<<ANY_REALM>>",
proxyUsername, proxyPassword));
}
}

Expand All @@ -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}.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -520,4 +520,23 @@ public static <T> T getValue(final Map<String, ?> properties, final String key,
public static <T> T getValue(final Map<String, ?> properties, final String key, final Class<T> type) {
return PropertiesHelper.getValue(properties, key, type, null);
}

/**
* Get the value of the specified property. If null, it will obtain it from System property.
* <p/>
* 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<String, ?> properties, String key, String systemKey) {
String value = PropertiesHelper.getValue(properties, key, String.class, null);
if (value == null) {
value = System.getProperty(systemKey);
}
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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");
Expand All @@ -347,18 +346,20 @@ private URI getProxyUri(ClientRequest request) {
return URI.create(proxyHost + ":" + proxyPort);
}
} else {
return getProxyUri(proxyUri);
return getProxyUriValue(proxyUri);
}
return null;
}

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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ public class ProxySelectorTest {
@Parameterized.Parameters(name = "{index}: {0}")
public static List<Object[]> testData() {
return Arrays.asList(new Object[][]{
// {ApacheConnectorProvider.class},
// {Apache5ConnectorProvider.class},
// {JettyConnectorProvider.class},
{ApacheConnectorProvider.class},
{Apache5ConnectorProvider.class},
{JettyConnectorProvider.class},
{NettyConnectorProvider.class},
{HttpUrlConnectorProvider.class},
});
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 5786071

Please sign in to comment.