Skip to content

Commit bc67ee3

Browse files
authored
ConnectorProvider support added to mp rest client (#4347)
* ConnectorProvider support added to mp rest client Signed-off-by: David Kral <david.k.kral@oracle.com>
1 parent 2c5d5b6 commit bc67ee3

File tree

3 files changed

+98
-7
lines changed

3 files changed

+98
-7
lines changed

ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/RestClientBuilderImpl.java

+23-7
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import java.util.List;
3232
import java.util.Map;
3333
import java.util.Optional;
34-
import java.util.ServiceLoader;
3534
import java.util.Set;
3635
import java.util.concurrent.ExecutorService;
3736
import java.util.concurrent.Executors;
@@ -59,8 +58,11 @@
5958
import org.eclipse.microprofile.rest.client.ext.AsyncInvocationInterceptorFactory;
6059
import org.eclipse.microprofile.rest.client.ext.ResponseExceptionMapper;
6160
import org.eclipse.microprofile.rest.client.spi.RestClientListener;
61+
import org.glassfish.jersey.client.ClientConfig;
6262
import org.glassfish.jersey.client.Initializable;
63+
import org.glassfish.jersey.client.spi.ConnectorProvider;
6364
import org.glassfish.jersey.ext.cdi1x.internal.CdiUtil;
65+
import org.glassfish.jersey.internal.ServiceFinder;
6466
import org.glassfish.jersey.internal.inject.InjectionManager;
6567
import org.glassfish.jersey.internal.inject.InjectionManagerSupplier;
6668
import org.glassfish.jersey.internal.util.ReflectionHelper;
@@ -92,6 +94,7 @@ class RestClientBuilderImpl implements RestClientBuilder {
9294
private KeyStore sslTrustStore;
9395
private KeyStore sslKeyStore;
9496
private char[] sslKeyStorePassword;
97+
private ConnectorProvider connector;
9598

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

148+
for (RestClientListener restClientListener : ServiceFinder.find(RestClientListener.class)) {
149+
restClientListener.onNewClient(interfaceClass, this);
150+
}
151+
145152
//Provider registration part
146153
processProviders(interfaceClass);
147154
InjectionManagerExposer injectionManagerExposer = new InjectionManagerExposer();
148155
register(injectionManagerExposer);
149156

150-
for (RestClientListener restClientListener : ServiceLoader.load(RestClientListener.class)) {
151-
restClientListener.onNewClient(interfaceClass, this);
152-
}
153-
154157
//We need to check first if default exception mapper was not disabled by property on builder.
155158
registerExceptionMapper();
156159
//sort all AsyncInvocationInterceptorFactory by priority
@@ -174,7 +177,16 @@ public <T> T build(Class<T> interfaceClass) throws IllegalStateException, RestCl
174177
clientBuilder.keyStore(sslKeyStore, sslKeyStorePassword);
175178
}
176179

177-
Client client = clientBuilder.build();
180+
Client client;
181+
if (connector == null) {
182+
client = clientBuilder.build();
183+
} else {
184+
ClientConfig config = new ClientConfig();
185+
config.loadFrom(getConfiguration());
186+
config.connectorProvider(connector);
187+
client = ClientBuilder.newClient(config);
188+
}
189+
178190
if (client instanceof Initializable) {
179191
((Initializable) client).preInitialize();
180192
}
@@ -377,7 +389,8 @@ public RestClientBuilder register(Object component, Map<Class<?>, Integer> contr
377389
private boolean isSupportedCustomProvider(Class<?> providerClass) {
378390
return ResponseExceptionMapper.class.isAssignableFrom(providerClass)
379391
|| ParamConverterProvider.class.isAssignableFrom(providerClass)
380-
|| AsyncInvocationInterceptorFactory.class.isAssignableFrom(providerClass);
392+
|| AsyncInvocationInterceptorFactory.class.isAssignableFrom(providerClass)
393+
|| ConnectorProvider.class.isAssignableFrom(providerClass);
381394
}
382395

383396
private void registerCustomProvider(Object instance, Integer priority) {
@@ -399,6 +412,9 @@ private void registerCustomProvider(Object instance, Integer priority) {
399412
.add(new AsyncInvocationInterceptorFactoryPriorityWrapper((AsyncInvocationInterceptorFactory) instance,
400413
priority));
401414
}
415+
if (instance instanceof ConnectorProvider) {
416+
connector = (ConnectorProvider) instance;
417+
}
402418
}
403419

404420
private static class InjectionManagerExposer implements Feature {

tests/integration/microprofile/rest-client/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@
8787
<type>pom</type>
8888
<scope>test</scope>
8989
</dependency>
90+
<dependency>
91+
<groupId>org.glassfish.jersey.connectors</groupId>
92+
<artifactId>jersey-apache-connector</artifactId>
93+
<scope>test</scope>
94+
</dependency>
9095
</dependencies>
9196

9297
<profiles>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* This Source Code may also be made available under the following Secondary
9+
* Licenses when the conditions for such availability set forth in the
10+
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11+
* version 2 with the GNU Classpath Exception, which is available at
12+
* https://www.gnu.org/software/classpath/license.html.
13+
*
14+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+
*/
16+
17+
package org.glassfish.jersey.restclient;
18+
19+
import java.net.URI;
20+
import java.net.URISyntaxException;
21+
import java.util.concurrent.CountDownLatch;
22+
23+
import org.apache.http.conn.ConnectionRequest;
24+
import org.apache.http.conn.routing.HttpRoute;
25+
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
26+
import org.eclipse.microprofile.rest.client.RestClientBuilder;
27+
import org.glassfish.jersey.apache.connector.ApacheClientProperties;
28+
import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
29+
import org.glassfish.jersey.server.ResourceConfig;
30+
import org.glassfish.jersey.test.JerseyTest;
31+
import org.glassfish.jersey.test.TestProperties;
32+
import org.junit.Test;
33+
34+
import static org.testng.Assert.assertEquals;
35+
36+
/**
37+
* Created by David Kral.
38+
*/
39+
public class ConnectorTest extends JerseyTest {
40+
41+
@Override
42+
protected ResourceConfig configure() {
43+
enable(TestProperties.LOG_TRAFFIC);
44+
return new ResourceConfig(ApplicationResourceImpl.class);
45+
}
46+
47+
@Test
48+
public void testConnector() throws URISyntaxException {
49+
CountDownLatch countDownLatch = new CountDownLatch(1);
50+
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager() {
51+
52+
@Override
53+
public ConnectionRequest requestConnection(HttpRoute route, Object state) {
54+
countDownLatch.countDown();
55+
return super.requestConnection(route, state);
56+
}
57+
58+
};
59+
60+
ApplicationResource app = RestClientBuilder.newBuilder()
61+
.baseUri(new URI("http://localhost:9998"))
62+
.property(ApacheClientProperties.CONNECTION_MANAGER, connectionManager)
63+
.register(ApacheConnectorProvider.class)
64+
.build(ApplicationResource.class);
65+
66+
app.getTestMap();
67+
assertEquals(countDownLatch.getCount(), 0);
68+
}
69+
70+
}

0 commit comments

Comments
 (0)