Skip to content

Commit

Permalink
Implemented JERSEY-2530.
Browse files Browse the repository at this point in the history
- Added support for proxy in Grizzly async client connector.

Change-Id: I946e16c4393040359417e3e64602c24080762e0d
Signed-off-by: Marek Potociar <[email protected]>
  • Loading branch information
Marek Potociar committed Jun 3, 2014
1 parent f076822 commit c481b53
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -78,10 +79,12 @@
import com.ning.http.client.HttpResponseBodyPart;
import com.ning.http.client.HttpResponseHeaders;
import com.ning.http.client.HttpResponseStatus;
import com.ning.http.client.ProxyServerSelector;
import com.ning.http.client.Request;
import com.ning.http.client.RequestBuilder;
import com.ning.http.client.providers.grizzly.FeedableBodyGenerator;
import com.ning.http.client.providers.grizzly.GrizzlyAsyncHttpProvider;
import com.ning.http.util.ProxyUtils;

import jersey.repackaged.com.google.common.util.concurrent.SettableFuture;

Expand Down Expand Up @@ -121,6 +124,30 @@ class GrizzlyConnector implements Connector {

builder.setRequestTimeoutInMs(ClientProperties.getValue(config.getProperties(),
ClientProperties.READ_TIMEOUT, 0));

Object proxyUri;
proxyUri = config.getProperty(ClientProperties.PROXY_URI);
if (proxyUri != null) {
final URI u = getProxyUri(proxyUri);
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);
}
}
ProxyServerSelector proxyServerSelector = ProxyUtils.createProxyServerSelector(proxyProperties);
builder.setProxyServerSelector(proxyServerSelector);
}
} else {
executorService = Executors.newCachedThreadPool();
builder.setExecutorService(executorService);
Expand All @@ -133,11 +160,23 @@ class GrizzlyConnector implements Connector {
if (client.getHostnameVerifier() != null) {
builder.setHostnameVerifier(client.getHostnameVerifier());
}

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 Expand Up @@ -339,7 +378,7 @@ private com.ning.http.client.Request translate(final ClientRequest requestContex
builder = builder.setBody(entityBytes);
} else if (entityProcessing == RequestEntityProcessing.CHUNKED) {
final FeedableBodyGenerator bodyGenerator = new FeedableBodyGenerator();
final Integer chunkSize = requestContext.resolveProperty(ClientProperties.CHUNKED_ENCODING_SIZE, Integer.valueOf(0));
final Integer chunkSize = requestContext.resolveProperty(ClientProperties.CHUNKED_ENCODING_SIZE, 0);
if (chunkSize > 0) {
bodyGenerator.setMaxPendingBytes(chunkSize);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,37 @@
import com.ning.http.client.AsyncHttpClient;

/**
* Connector provider for Grizzly asynchronous HTTP client-based connectors.
* Connector provider for Jersey {@link Connector connectors} that utilize
* Grizzly Asynchronous HTTP Client to send and receive HTTP request and responses.
* <p>
* Connectors created by this provider use {@link org.glassfish.jersey.client.RequestEntityProcessing#CHUNKED
* chunked encoding} as a default setting. This can be overridden by setting the
* {@link org.glassfish.jersey.client.ClientProperties#REQUEST_ENTITY_PROCESSING} property.
* The following connector configuration properties are supported:
* <ul>
* <li>{@link org.glassfish.jersey.client.ClientProperties#CONNECT_TIMEOUT}</li>
* <li>{@link org.glassfish.jersey.client.ClientProperties#READ_TIMEOUT}</li>
* <li>{@link org.glassfish.jersey.client.ClientProperties#REQUEST_ENTITY_PROCESSING}
* - default value is {@link org.glassfish.jersey.client.RequestEntityProcessing#CHUNKED}</li>
* <li>{@link org.glassfish.jersey.client.ClientProperties#PROXY_URI}</li>
* <li>{@link org.glassfish.jersey.client.ClientProperties#PROXY_USERNAME}</li>
* <li>{@link org.glassfish.jersey.client.ClientProperties#PROXY_PASSWORD}</li>
* </ul>
* </p>
* <p>
* Connector instances created via this connector provider use
* {@link org.glassfish.jersey.client.RequestEntityProcessing#CHUNKED chunked encoding} as a default setting.
* This can be overridden by the {@link org.glassfish.jersey.client.ClientProperties#REQUEST_ENTITY_PROCESSING}.
* </p>
* <p>
* If a {@link org.glassfish.jersey.client.ClientResponse} is obtained and an entity is not read from the response then
* {@link org.glassfish.jersey.client.ClientResponse#close()} MUST be called after processing the response to release
* connection-based resources.
* </p>
* <p>
* If a response entity is obtained that is an instance of {@link java.io.Closeable} then the instance MUST
* be closed after processing the entity to release connection-based resources.
* <p/>
* <p>
* The following methods are currently supported: HEAD, GET, POST, PUT, DELETE, OPTIONS, PATCH and TRACE.
* <p/>
*
* @author Marek Potociar (marek.potociar at oracle.com)
* @since 2.5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@
#

error.buffering.entity=Error buffering the entity.
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 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.

0 comments on commit c481b53

Please sign in to comment.