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

ConnectorProvider support added to mp rest client #4347

Merged
merged 3 commits into from
Dec 13, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -59,7 +59,9 @@
import org.eclipse.microprofile.rest.client.ext.AsyncInvocationInterceptorFactory;
import org.eclipse.microprofile.rest.client.ext.ResponseExceptionMapper;
import org.eclipse.microprofile.rest.client.spi.RestClientListener;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.Initializable;
import org.glassfish.jersey.client.spi.ConnectorProvider;
import org.glassfish.jersey.ext.cdi1x.internal.CdiUtil;
import org.glassfish.jersey.internal.inject.InjectionManager;
import org.glassfish.jersey.internal.inject.InjectionManagerSupplier;
Expand Down Expand Up @@ -92,6 +94,7 @@ class RestClientBuilderImpl implements RestClientBuilder {
private KeyStore sslTrustStore;
private KeyStore sslKeyStore;
private char[] sslKeyStorePassword;
private ConnectorProvider connector;

RestClientBuilderImpl() {
clientBuilder = ClientBuilder.newBuilder();
Expand Down Expand Up @@ -142,15 +145,15 @@ public <T> T build(Class<T> interfaceClass) throws IllegalStateException, RestCl
throw new IllegalStateException("Base uri/url cannot be null!");
}

for (RestClientListener restClientListener : ServiceLoader.load(RestClientListener.class)) {
Verdent marked this conversation as resolved.
Show resolved Hide resolved
restClientListener.onNewClient(interfaceClass, this);
}

//Provider registration part
processProviders(interfaceClass);
InjectionManagerExposer injectionManagerExposer = new InjectionManagerExposer();
register(injectionManagerExposer);

for (RestClientListener restClientListener : ServiceLoader.load(RestClientListener.class)) {
restClientListener.onNewClient(interfaceClass, this);
}

//We need to check first if default exception mapper was not disabled by property on builder.
registerExceptionMapper();
//sort all AsyncInvocationInterceptorFactory by priority
Expand All @@ -174,7 +177,16 @@ public <T> T build(Class<T> interfaceClass) throws IllegalStateException, RestCl
clientBuilder.keyStore(sslKeyStore, sslKeyStorePassword);
}

Client client = clientBuilder.build();
Client client;
if (connector == null) {
client = clientBuilder.build();
} else {
ClientConfig config = new ClientConfig();
config.loadFrom(getConfiguration());
config.connectorProvider(connector);
client = ClientBuilder.newClient(config);
}

if (client instanceof Initializable) {
((Initializable) client).preInitialize();
}
Expand Down Expand Up @@ -377,7 +389,8 @@ public RestClientBuilder register(Object component, Map<Class<?>, Integer> contr
private boolean isSupportedCustomProvider(Class<?> providerClass) {
return ResponseExceptionMapper.class.isAssignableFrom(providerClass)
|| ParamConverterProvider.class.isAssignableFrom(providerClass)
|| AsyncInvocationInterceptorFactory.class.isAssignableFrom(providerClass);
|| AsyncInvocationInterceptorFactory.class.isAssignableFrom(providerClass)
|| ConnectorProvider.class.isAssignableFrom(providerClass);
}

private void registerCustomProvider(Object instance, Integer priority) {
Expand All @@ -399,6 +412,9 @@ private void registerCustomProvider(Object instance, Integer priority) {
.add(new AsyncInvocationInterceptorFactoryPriorityWrapper((AsyncInvocationInterceptorFactory) instance,
priority));
}
if (instance instanceof ConnectorProvider) {
connector = (ConnectorProvider) instance;
}
}

private static class InjectionManagerExposer implements Feature {
Expand Down
5 changes: 5 additions & 0 deletions tests/integration/microprofile/rest-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@
<type>pom</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.connectors</groupId>
<artifactId>jersey-apache-connector</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.glassfish.jersey.restclient;
Verdent marked this conversation as resolved.
Show resolved Hide resolved

import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.CountDownLatch;

import org.apache.http.conn.ConnectionRequest;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.eclipse.microprofile.rest.client.RestClientBuilder;
import org.glassfish.jersey.apache.connector.ApacheClientProperties;
import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.glassfish.jersey.test.TestProperties;
import org.junit.Test;

import static org.testng.Assert.assertEquals;

/**
* Created by David Kral.
*/
public class ConnectorTest extends JerseyTest {

@Override
protected ResourceConfig configure() {
enable(TestProperties.LOG_TRAFFIC);
return new ResourceConfig(ApplicationResourceImpl.class);
}

@Test
public void testConnector() throws URISyntaxException {
CountDownLatch countDownLatch = new CountDownLatch(1);
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager() {

@Override
public ConnectionRequest requestConnection(HttpRoute route, Object state) {
countDownLatch.countDown();
return super.requestConnection(route, state);
}

};

ApplicationResource app = RestClientBuilder.newBuilder()
.baseUri(new URI("http://localhost:9998"))
.property(ApacheClientProperties.CONNECTION_MANAGER, connectionManager)
.register(ApacheConnectorProvider.class)
.build(ApplicationResource.class);

app.getTestMap();
assertEquals(countDownLatch.getCount(), 0);
}

}