Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Proxy parsing logic from connectors to a common class #5095

Merged
merged 1 commit into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicLong;
Expand All @@ -51,6 +52,7 @@
import org.glassfish.jersey.client.ClientRequest;
import org.glassfish.jersey.client.ClientResponse;
import org.glassfish.jersey.client.RequestEntityProcessing;
import org.glassfish.jersey.client.innate.ClientProxy;
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,28 +281,20 @@ class ApacheConnector implements Connector {
clientBuilder.setRetryHandler((HttpRequestRetryHandler) retryHandler);
}

final Object proxyUri;
proxyUri = config.getProperty(ClientProperties.PROXY_URI);
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);
if (userName != null) {
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(u.getHost(), u.getPort()),
new UsernamePasswordCredentials(userName, password)
);
clientBuilder.setDefaultCredentialsProvider(credsProvider);
}
final Optional<ClientProxy> proxy = ClientProxy.proxyFromConfiguration(config);
proxy.ifPresent(clientProxy -> {
final URI u = clientProxy.uri();
final HttpHost proxyHost = new HttpHost(u.getHost(), u.getPort(), u.getScheme());
if (clientProxy.userName() != null && clientProxy.password() != null) {
final CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(u.getHost(), u.getPort()),
new UsernamePasswordCredentials(clientProxy.userName(), clientProxy.password())
);
clientBuilder.setDefaultCredentialsProvider(credsProvider);
}
clientBuilder.setProxy(proxy);
}
clientBuilder.setProxy(proxyHost);
});

final Boolean preemptiveBasicAuthProperty = (Boolean) config.getProperties()
.get(ApacheClientProperties.PREEMPTIVE_BASIC_AUTHENTICATION);
Expand Down Expand Up @@ -452,16 +446,6 @@ public CookieStore getCookieStore() {
return cookieStore;
}

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));
}
}

@Override
public ClientResponse apply(final ClientRequest clientRequest) throws ProcessingException {
final HttpUriRequest request = getUriHttpRequest(clientRequest);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2013, 2020 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
Expand All @@ -19,7 +19,5 @@ error.reading.httpentity.stream=Error reading InputStream from HttpEntity: "{0}"
failed.to.stop.client=Failed to stop the client.
# {0} - property name, e.g. jersey.config.client.httpclient.connectionManager; {1}, {2} - full class name
ignoring.value.of.property=Ignoring value of property "{0}" ("{1}") - not instance of "{2}".
# {0} - property name - jersey.config.client.httpclient.proxyUri
wrong.proxy.uri.type=The proxy URI ("{0}") property MUST be an instance of String or URI.
invalid.configurable.component.type=The supplied component "{0}" is not assignable from JerseyClient or JerseyWebTarget.
expected.connector.provider.not.used=The supplied component is not configured to use a ApacheConnectorProvider.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.logging.Level;
Expand Down Expand Up @@ -93,6 +94,7 @@
import org.glassfish.jersey.client.ClientRequest;
import org.glassfish.jersey.client.ClientResponse;
import org.glassfish.jersey.client.RequestEntityProcessing;
import org.glassfish.jersey.client.innate.ClientProxy;
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,28 +282,20 @@ class Apache5Connector implements Connector {
clientBuilder.setRetryStrategy((HttpRequestRetryStrategy) retryHandler);
}

final Object proxyUri;
proxyUri = config.getProperty(ClientProperties.PROXY_URI);
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);
if (userName != null) {
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(u.getHost(), u.getPort()),
new UsernamePasswordCredentials(userName, password.toCharArray())
);
clientBuilder.setDefaultCredentialsProvider(credsProvider);
}
final Optional<ClientProxy> proxy = ClientProxy.proxyFromConfiguration(config);
proxy.ifPresent(clientProxy -> {
final URI u = clientProxy.uri();
final HttpHost proxyHost = new HttpHost(u.getScheme(), u.getHost(), u.getPort());
if (clientProxy.userName() != null && clientProxy.password() != null) {
final CredentialsStore credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(u.getHost(), u.getPort()),
new UsernamePasswordCredentials(clientProxy.userName(), clientProxy.password().toCharArray())
);
clientBuilder.setDefaultCredentialsProvider(credsProvider);
}
clientBuilder.setProxy(proxy);
}
clientBuilder.setProxy(proxyHost);
});

final Boolean preemptiveBasicAuthProperty = (Boolean) config.getProperties()
.get(Apache5ClientProperties.PREEMPTIVE_BASIC_AUTHENTICATION);
Expand Down Expand Up @@ -456,16 +450,6 @@ public CookieStore getCookieStore() {
return cookieStore;
}

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));
}
}

@Override
public ClientResponse apply(final ClientRequest clientRequest) throws ProcessingException {
final HttpUriRequest request = getUriHttpRequest(clientRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,5 @@ error.reading.httpentity.stream=Error reading InputStream from HttpEntity: "{0}"
failed.to.stop.client=Failed to stop the client.
# {0} - property name, e.g. jersey.config.client.httpclient.connectionManager; {1}, {2} - full class name
ignoring.value.of.property=Ignoring value of property "{0}" ("{1}") - not instance of "{2}".
# {0} - property name - jersey.config.client.httpclient.proxyUri
wrong.proxy.uri.type=The proxy URI ("{0}") property MUST be an instance of String or URI.
invalid.configurable.component.type=The supplied component "{0}" is not assignable from JerseyClient or JerseyWebTarget.
expected.connector.provider.not.used=The supplied component is not configured to use a ApacheConnectorProvider.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 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 All @@ -22,6 +22,7 @@
import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
Expand All @@ -39,6 +40,7 @@
import org.glassfish.jersey.client.ClientRequest;
import org.glassfish.jersey.client.ClientResponse;
import org.glassfish.jersey.client.RequestEntityProcessing;
import org.glassfish.jersey.client.innate.ClientProxy;
import org.glassfish.jersey.client.spi.AsyncConnectorCallback;
import org.glassfish.jersey.client.spi.Connector;
import org.glassfish.jersey.internal.Version;
Expand Down Expand Up @@ -83,7 +85,7 @@ class GrizzlyConnector implements Connector {
GrizzlyConnector(final Client client,
final Configuration config,
final GrizzlyConnectorProvider.AsyncClientCustomizer asyncClientCustomizer) {
AsyncHttpClientConfig.Builder builder = new AsyncHttpClientConfig.Builder();
final AsyncHttpClientConfig.Builder builder = new AsyncHttpClientConfig.Builder();

ExecutorService executorService;
if (config != null) {
Expand All @@ -95,37 +97,31 @@ class GrizzlyConnector implements Connector {
executorService = Executors.newCachedThreadPool();
}

builder = builder.setExecutorService(executorService);
builder.setExecutorService(executorService);

builder.setConnectTimeout(ClientProperties.getValue(config.getProperties(),
ClientProperties.CONNECT_TIMEOUT, 10000));

builder.setRequestTimeout(ClientProperties.getValue(config.getProperties(),
ClientProperties.READ_TIMEOUT, 10000));

Object proxyUri;
proxyUri = config.getProperty(ClientProperties.PROXY_URI);
if (proxyUri != null) {
final URI u = getProxyUri(proxyUri);
final Optional<ClientProxy> proxy = ClientProxy.proxyFromConfiguration(config);
proxy.ifPresent(clientProxy -> {
final URI u = clientProxy.uri();
final Properties proxyProperties = new Properties();
proxyProperties.setProperty(ProxyUtils.PROXY_PROTOCOL, u.getScheme());
proxyProperties.setProperty(ProxyUtils.PROXY_HOST, u.getHost());
proxyProperties.setProperty(ProxyUtils.PROXY_PORT, String.valueOf(u.getPort()));

final String userName = ClientProperties.getValue(
config.getProperties(), ClientProperties.PROXY_USERNAME, String.class);
if (userName != null) {
proxyProperties.setProperty(ProxyUtils.PROXY_USER, userName);

final String password = ClientProperties.getValue(
config.getProperties(), ClientProperties.PROXY_PASSWORD, String.class);
if (password != null) {
proxyProperties.setProperty(ProxyUtils.PROXY_PASSWORD, password);
if (clientProxy.userName() != null) {
proxyProperties.setProperty(ProxyUtils.PROXY_USER, clientProxy.userName());
if (clientProxy.password() != null) {
proxyProperties.setProperty(ProxyUtils.PROXY_PASSWORD, clientProxy.password());
}
}
ProxyServerSelector proxyServerSelector = ProxyUtils.createProxyServerSelector(proxyProperties);
builder.setProxyServerSelector(proxyServerSelector);
}
});
} else {
executorService = Executors.newCachedThreadPool();
builder.setExecutorService(executorService);
Expand All @@ -140,25 +136,14 @@ class GrizzlyConnector implements Connector {
}

if (asyncClientCustomizer != null) {
builder = asyncClientCustomizer.customize(client, config, builder);
asyncClientCustomizer.customize(client, config, builder);
}

AsyncHttpClientConfig asyncClientConfig = builder.build();

this.grizzlyClient = new AsyncHttpClient(new GrizzlyAsyncHttpProvider(asyncClientConfig), asyncClientConfig);
}

@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 underlying Grizzly {@link com.ning.http.client.AsyncHttpClient} instance.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,5 +16,4 @@

error.buffering.entity=Error buffering the entity.
expected.connector.provider.not.used=The supplied component is not configured to use a GrizzlyConnectorProvider.
invalid.configurable.component.type=The supplied component "{0}" is not assignable from JerseyClient or JerseyWebTarget.
wrong.proxy.uri.type=The proxy URI ("{0}") property MUST be an instance of String or URI.
invalid.configurable.component.type=The supplied component "{0}" is not assignable from JerseyClient or JerseyWebTarget.
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@
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;
Expand All @@ -54,6 +52,7 @@
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.ClientRequest;
import org.glassfish.jersey.client.ClientResponse;
import org.glassfish.jersey.client.innate.ClientProxy;
import org.glassfish.jersey.client.spi.AsyncConnectorCallback;
import org.glassfish.jersey.client.spi.Connector;
import org.glassfish.jersey.internal.util.collection.ByteBufferInputStream;
Expand Down Expand Up @@ -187,19 +186,17 @@ class JettyConnector implements Connector {
auth.addAuthentication((BasicAuthentication) basicAuthProvider);
}

final Object proxyUri = config.getProperties().get(ClientProperties.PROXY_URI);
if (proxyUri != null) {
final URI u = getProxyUri(proxyUri);
final Optional<ClientProxy> proxy = ClientProxy.proxyFromConfiguration(config);
proxy.ifPresent(clientProxy -> {
final ProxyConfiguration proxyConfig = client.getProxyConfiguration();
final URI u = clientProxy.uri();
proxyConfig.getProxies().add(new HttpProxy(u.getHost(), u.getPort()));

final Object proxyUsername = config.getProperties().get(ClientProperties.PROXY_USERNAME);
if (proxyUsername != null) {
final Object proxyPassword = config.getProperties().get(ClientProperties.PROXY_PASSWORD);
if (clientProxy.userName() != null) {
auth.addAuthentication(new BasicAuthentication(u, "<<ANY_REALM>>",
String.valueOf(proxyUsername), String.valueOf(proxyPassword)));
clientProxy.userName(), clientProxy.password()));
}
}
});

if (disableCookies) {
client.setCookieStore(new HttpCookieStore.Empty());
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) 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
Expand All @@ -16,7 +16,5 @@

# {0} - HTTP method, e.g. GET, DELETE
method.not.supported=Method {0} not supported.
# {0} - property name - jersey.config.client.proxyUri
wrong.proxy.uri.type=The proxy URI ("{0}") property MUST be an instance of String or URI.
invalid.configurable.component.type=The supplied component "{0}" is not assignable from JerseyClient or JerseyWebTarget.
expected.connector.provider.not.used=The supplied component is not configured to use a JettyConnectorProvider.
Loading