Skip to content

Commit

Permalink
Suggestions implemented
Browse files Browse the repository at this point in the history
Signed-off-by: David Kral <david.k.kral@oracle.com>
  • Loading branch information
Verdent committed May 14, 2019
1 parent 9f03c5f commit aee1a66
Show file tree
Hide file tree
Showing 38 changed files with 83 additions and 4,091 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2019 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 @@ -355,15 +355,6 @@ public JerseyWebTarget preInitialize() {
return this;
}

/**
* Returns injection manager.
*
* @return injection manager
*/
public InjectionManager getInjectionManager() {
return config.getRuntime().getInjectionManager();
}

@Override
public String toString() {
return "JerseyWebTarget { " + targetUri.toTemplate() + " }";
Expand Down
2 changes: 1 addition & 1 deletion ext/microprofile/mp-rest-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
<dependency>
<groupId>io.smallrye</groupId>
<artifactId>smallrye-config</artifactId>
<version>1.3.5</version>
<version>1.3.6</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -206,15 +208,20 @@ Form resolveForm(Form form,
}

private Object resolveValueFromField(Field field, Object instance) {
try {
Object toReturn;
field.setAccessible(true);
toReturn = field.get(instance);
field.setAccessible(false);
return toReturn;
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
return AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
try {
Object toReturn;
if (field.isAccessible()) {
return field.get(instance);
}
field.setAccessible(true);
toReturn = field.get(instance);
field.setAccessible(false);
return toReturn;
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
});
}

private static class Builder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,29 @@
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Constructor;
import java.lang.reflect.Proxy;
import java.security.AccessController;
import java.security.PrivilegedAction;

import org.glassfish.jersey.internal.util.ReflectionHelper;

/**
* Created by David Kral.
*/
class ReflectionUtil {

static <T> T createInstance(Class<T> tClass) {
try {
return tClass.newInstance();
} catch (Throwable t) {
throw new RuntimeException("No default constructor in class " + tClass + " present. Class cannot be created!", t);
}
return AccessController.doPrivileged((PrivilegedAction<T>) () -> {
try {
return tClass.newInstance();
} catch (Throwable t) {
throw new RuntimeException("No default constructor in class " + tClass + " present. Class cannot be created!", t);
}
});
}

@SuppressWarnings("unchecked")
static <T> T createProxyInstance(Class<T> restClientClass) {
return (T) Proxy.newProxyInstance(
return AccessController.doPrivileged((PrivilegedAction<T>) () -> (T) Proxy.newProxyInstance(
Thread.currentThread().getContextClassLoader(),
new Class[] {restClientClass},
(proxy, m, args) -> {
Expand All @@ -47,7 +53,7 @@ static <T> T createProxyInstance(Class<T> restClientClass) {
.unreflectSpecial(m, restClientClass)
.bindTo(proxy)
.invokeWithArguments(args);
});
}));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@

import javax.annotation.Priority;
import javax.ws.rs.Priorities;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Configuration;
import javax.ws.rs.core.Feature;
import javax.ws.rs.core.FeatureContext;
import javax.ws.rs.ext.ParamConverterProvider;

import org.eclipse.microprofile.config.Config;
Expand All @@ -49,9 +54,10 @@
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.Initializable;
import org.glassfish.jersey.client.JerseyClient;
import org.glassfish.jersey.client.JerseyClientBuilder;
import org.glassfish.jersey.client.JerseyWebTarget;
import org.glassfish.jersey.internal.inject.InjectionManager;
import org.glassfish.jersey.internal.inject.InjectionManagerSupplier;
import org.glassfish.jersey.internal.util.ReflectionHelper;

/**
Expand All @@ -72,16 +78,16 @@ public class RestClientBuilderImpl implements RestClientBuilder {
private final Config config;
private final ConfigWrapper configWrapper;
private URI uri;
private JerseyClientBuilder jerseyClientBuilder;
private ClientBuilder clientBuilder;
private Supplier<ExecutorService> executorService;

RestClientBuilderImpl() {
jerseyClientBuilder = new JerseyClientBuilder();
clientBuilder = ClientBuilder.newBuilder();
responseExceptionMappers = new HashSet<>();
paramConverterProviders = new HashSet<>();
asyncInterceptorFactories = new ArrayList<>();
config = ConfigProvider.getConfig();
configWrapper = new ConfigWrapper(jerseyClientBuilder.getConfiguration());
configWrapper = new ConfigWrapper(clientBuilder.getConfiguration());
executorService = Executors::newCachedThreadPool;
}

Expand All @@ -97,13 +103,13 @@ public RestClientBuilder baseUrl(URL url) {

@Override
public RestClientBuilder connectTimeout(long timeout, TimeUnit unit) {
jerseyClientBuilder.connectTimeout(timeout, unit);
clientBuilder.connectTimeout(timeout, unit);
return this;
}

@Override
public RestClientBuilder readTimeout(long timeout, TimeUnit unit) {
jerseyClientBuilder.readTimeout(timeout, unit);
clientBuilder.readTimeout(timeout, unit);
return this;
}

Expand All @@ -125,7 +131,7 @@ public <T> T build(Class<T> interfaceClass) throws IllegalStateException, RestCl
}

//Provider registration part
Object providersFromJerseyConfig = jerseyClientBuilder.getConfiguration()
Object providersFromJerseyConfig = clientBuilder.getConfiguration()
.getProperty(interfaceClass.getName() + CONFIG_PROVIDERS);
if (providersFromJerseyConfig instanceof String && !((String) providersFromJerseyConfig).isEmpty()) {
String[] providerArray = ((String) providersFromJerseyConfig).split(PROVIDER_SEPARATOR);
Expand All @@ -140,13 +146,15 @@ public <T> T build(Class<T> interfaceClass) throws IllegalStateException, RestCl
for (RegisterProvider registerProvider : registerProviders) {
register(registerProvider.value(), registerProvider.priority() < 0 ? Priorities.USER : registerProvider.priority());
}
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.
Object disableDefaultMapperJersey = jerseyClientBuilder.getConfiguration().getProperty(CONFIG_DISABLE_DEFAULT_MAPPER);
Object disableDefaultMapperJersey = clientBuilder.getConfiguration().getProperty(CONFIG_DISABLE_DEFAULT_MAPPER);
if (disableDefaultMapperJersey != null && disableDefaultMapperJersey.equals(Boolean.FALSE)) {
register(new DefaultResponseExceptionMapper());
} else if (disableDefaultMapperJersey == null) {
Expand All @@ -163,17 +171,19 @@ public <T> T build(Class<T> interfaceClass) throws IllegalStateException, RestCl
.collect(Collectors.toList());
asyncInterceptors.forEach(AsyncInvocationInterceptor::prepareContext);

jerseyClientBuilder.executorService(new ExecutorServiceWrapper(executorService.get(), asyncInterceptors));
clientBuilder.executorService(new ExecutorServiceWrapper(executorService.get(), asyncInterceptors));

JerseyClient client = jerseyClientBuilder.build();
client.preInitialize();
JerseyWebTarget webTarget = client.target(this.uri);
Client client = clientBuilder.build();
if (client instanceof Initializable) {
((Initializable) client).preInitialize();
}
WebTarget webTarget = client.target(this.uri);

RestClientModel restClientModel = RestClientModel.from(interfaceClass,
responseExceptionMappers,
paramConverterProviders,
asyncInterceptors,
webTarget.getInjectionManager());
injectionManagerExposer.injectionManager);


return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(),
Expand All @@ -196,7 +206,7 @@ private void processConfigProviders(Class<?> restClientInterface, String[] provi
private int getProviderPriority(Class<?> restClientInterface, Class<?> providerClass) {
String property = restClientInterface.getName() + CONFIG_PROVIDERS + "/"
+ providerClass.getName() + CONFIG_PROVIDER_PRIORITY;
Object providerPriorityJersey = jerseyClientBuilder.getConfiguration().getProperty(property);
Object providerPriorityJersey = clientBuilder.getConfiguration().getProperty(property);
if (providerPriorityJersey == null) {
//If property was not set on Jersey ClientBuilder, we need to check MP config.
Optional<Integer> providerPriorityMP = config.getOptionalValue(property, int.class);
Expand All @@ -217,7 +227,7 @@ public Configuration getConfiguration() {

@Override
public RestClientBuilder property(String name, Object value) {
jerseyClientBuilder.property(name, value);
clientBuilder.property(name, value);
return this;
}

Expand All @@ -226,7 +236,7 @@ public RestClientBuilder register(Class<?> aClass) {
if (isSupportedCustomProvider(aClass)) {
register(ReflectionUtil.createInstance(aClass));
} else {
jerseyClientBuilder.register(aClass);
clientBuilder.register(aClass);
}
return this;
}
Expand All @@ -236,7 +246,7 @@ public RestClientBuilder register(Class<?> aClass, int i) {
if (isSupportedCustomProvider(aClass)) {
register(ReflectionUtil.createInstance(aClass), i);
} else {
jerseyClientBuilder.register(aClass, i);
clientBuilder.register(aClass, i);
}
return this;
}
Expand All @@ -246,7 +256,7 @@ public RestClientBuilder register(Class<?> aClass, Class<?>... classes) {
if (isSupportedCustomProvider(aClass)) {
register(ReflectionUtil.createInstance(aClass), classes);
} else {
jerseyClientBuilder.register(aClass, classes);
clientBuilder.register(aClass, classes);
}
return this;
}
Expand All @@ -256,7 +266,7 @@ public RestClientBuilder register(Class<?> aClass, Map<Class<?>, Integer> map) {
if (isSupportedCustomProvider(aClass)) {
register(ReflectionUtil.createInstance(aClass), map);
} else {
jerseyClientBuilder.register(aClass, map);
clientBuilder.register(aClass, map);
}
return this;
}
Expand All @@ -266,17 +276,17 @@ public RestClientBuilder register(Object o) {
if (o instanceof ResponseExceptionMapper) {
ResponseExceptionMapper mapper = (ResponseExceptionMapper) o;
registerCustomProvider(o, -1);
jerseyClientBuilder.register(mapper, mapper.getPriority());
clientBuilder.register(mapper, mapper.getPriority());
} else {
jerseyClientBuilder.register(o);
clientBuilder.register(o);
registerCustomProvider(o, -1);
}
return this;
}

@Override
public RestClientBuilder register(Object o, int i) {
jerseyClientBuilder.register(o, i);
clientBuilder.register(o, i);
registerCustomProvider(o, i);
return this;
}
Expand All @@ -288,7 +298,7 @@ public RestClientBuilder register(Object o, Class<?>... classes) {
register(o);
}
}
jerseyClientBuilder.register(o, classes);
clientBuilder.register(o, classes);
return this;
}

Expand All @@ -301,7 +311,7 @@ public RestClientBuilder register(Object o, Map<Class<?>, Integer> map) {
registerCustomProvider(o, map.get(ParamConverterProvider.class));
}
}
jerseyClientBuilder.register(o, map);
clientBuilder.register(o, map);
return this;
}

Expand Down Expand Up @@ -330,4 +340,18 @@ private void registerCustomProvider(Object instance, int priority) {
}
}

private static class InjectionManagerExposer implements Feature {
InjectionManager injectionManager;

@Override
public boolean configure(FeatureContext context) {
if (context instanceof InjectionManagerSupplier) {
this.injectionManager = ((InjectionManagerSupplier) context).getInjectionManager();
return true;
} else {
throw new IllegalArgumentException("The client needs Jersey runtime to work properly");
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.glassfish.jersey.test.TestProperties;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
* Created by David Kral.
Expand All @@ -41,7 +42,7 @@ public void testGetIt() throws URISyntaxException {
ApplicationResource app = RestClientBuilder.newBuilder()
.baseUri(new URI("http://localhost:9998"))
.build(ApplicationResource.class);
System.out.println(app.getValue());
System.out.println(app.sayHi());
assertEquals("This is default value!", app.getValue());
assertEquals("Hi", app.sayHi());
}
}
Loading

0 comments on commit aee1a66

Please sign in to comment.