Skip to content

Commit

Permalink
Merge pull request #2 from Verdent/revert-1-upstream-rest-client
Browse files Browse the repository at this point in the history
Revert "Improvement to REST client"
  • Loading branch information
Verdent authored May 15, 2019
2 parents dd0b625 + 60ff878 commit 4f9d61f
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 109 deletions.
5 changes: 0 additions & 5 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,6 @@
<artifactId>jersey-rx-client-rxjava2</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-mp-rest-client</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-jaxb</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2010, 2017 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2019 Payara Foundation and/or its affiliates.
* Copyright (c) 2018 Payara Foundation and/or its affiliates.
*
* 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 @@ -26,10 +26,9 @@
* @author Paul Sandoz
* @author Marek Potociar (marek.potociar at oracle.com)
* @author Gaurav Gupta ([email protected])
* @author Patrik Dudits
*
*/
final class PrimitiveValueOfInserter implements ParameterInserter<Object, String> {
final class PrimitiveValueOfInserter implements ParameterInserter<Number, String> {

private final String parameter;
private final String defaultValue;
Expand Down Expand Up @@ -62,7 +61,7 @@ public String getDefaultValueString() {
}

@Override
public String insert(Object value) {
public String insert(Number value) {
if (value != null) {
return value.toString();
} else if (defaultValue != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/*
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019 Payara Foundation 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 @@ -51,7 +50,6 @@
* Model of interface and its annotation.
*
* @author David Kral
* @author Patrik Dudits
*/
class InterfaceModel {

Expand All @@ -68,7 +66,6 @@ class InterfaceModel {
private final Set<ResponseExceptionMapper> responseExceptionMappers;
private final Set<ParamConverterProvider> paramConverterProviders;
private final Set<Annotation> interceptorAnnotations;
private final BeanManager beanManager;

/**
* Creates new model based on interface class. Interface is parsed according to specific annotations.
Expand All @@ -84,14 +81,12 @@ static InterfaceModel from(Class<?> restClientClass,
Set<ResponseExceptionMapper> responseExceptionMappers,
Set<ParamConverterProvider> paramConverterProviders,
List<AsyncInvocationInterceptor> asyncInterceptors,
InjectionManager injectionManager,
BeanManager beanManager) {
InjectionManager injectionManager) {
return new Builder(restClientClass,
responseExceptionMappers,
paramConverterProviders,
asyncInterceptors,
injectionManager,
beanManager)
injectionManager)
.pathValue(restClientClass.getAnnotation(Path.class))
.produces(restClientClass.getAnnotation(Produces.class))
.consumes(restClientClass.getAnnotation(Consumes.class))
Expand All @@ -113,7 +108,6 @@ private InterfaceModel(Builder builder) {
this.interceptorAnnotations = builder.interceptorAnnotations;
this.creationalContext = builder.creationalContext;
this.asyncInterceptors = builder.asyncInterceptors;
this.beanManager = builder.beanManager;
}

/**
Expand Down Expand Up @@ -243,16 +237,11 @@ Object resolveParamValue(Object arg, Parameter parameter) {
return arg;
}

BeanManager getBeanManager() {
return beanManager;
}

private static class Builder {

private final Class<?> restClientClass;

private final InjectionManager injectionManager;
private final BeanManager beanManager;
private String pathValue;
private String[] produces;
private String[] consumes;
Expand All @@ -268,27 +257,30 @@ private Builder(Class<?> restClientClass,
Set<ResponseExceptionMapper> responseExceptionMappers,
Set<ParamConverterProvider> paramConverterProviders,
List<AsyncInvocationInterceptor> asyncInterceptors,
InjectionManager injectionManager,
BeanManager beanManager) {
InjectionManager injectionManager) {
this.injectionManager = injectionManager;
this.restClientClass = restClientClass;
this.responseExceptionMappers = responseExceptionMappers;
this.paramConverterProviders = paramConverterProviders;
this.asyncInterceptors = asyncInterceptors;
this.beanManager = beanManager;
filterAllInterceptorAnnotations();
}

private void filterAllInterceptorAnnotations() {
creationalContext = null;
interceptorAnnotations = new HashSet<>();
if (beanManager != null) {
creationalContext = beanManager.createCreationalContext(null);
for (Annotation annotation : restClientClass.getAnnotations()) {
if (beanManager.isInterceptorBinding(annotation.annotationType())) {
interceptorAnnotations.add(annotation);
try {
if (CDI.current() != null) {
BeanManager beanManager = CDI.current().getBeanManager();
creationalContext = beanManager.createCreationalContext(null);
for (Annotation annotation : restClientClass.getAnnotations()) {
if (beanManager.isInterceptorBinding(annotation.annotationType())) {
interceptorAnnotations.add(annotation);
}
}
}
} catch (IllegalStateException ignored) {
//CDI not present. Ignore.
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/*
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019 Payara Foundation 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 @@ -40,6 +39,7 @@
import java.util.stream.Collectors;

import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.CDI;
import javax.enterprise.inject.spi.InterceptionType;
import javax.enterprise.inject.spi.Interceptor;
import javax.json.JsonValue;
Expand Down Expand Up @@ -74,7 +74,6 @@
* Method model contains all information about method defined in rest client interface.
*
* @author David Kral
* @author Patrik Dudits
*/
class MethodModel {

Expand Down Expand Up @@ -128,8 +127,7 @@ private MethodModel(Builder builder) {
interfaceModel.getResponseExceptionMappers(),
interfaceModel.getParamConverterProviders(),
interfaceModel.getAsyncInterceptors(),
interfaceModel.getInjectionManager(),
interfaceModel.getBeanManager());
interfaceModel.getInjectionManager());
} else {
subResourceModel = null;
}
Expand Down Expand Up @@ -477,31 +475,35 @@ private Builder(InterfaceModel interfaceModel, Method method) {

private void filterAllInterceptorAnnotations() {
invocationInterceptors = new ArrayList<>();
BeanManager beanManager = interfaceModel.getBeanManager();
if (beanManager != null) {
Set<Annotation> interceptorAnnotations = new HashSet<>();
for (Annotation annotation : method.getAnnotations()) {
if (beanManager.isInterceptorBinding(annotation.annotationType())) {
interceptorAnnotations.add(annotation);
try {
if (CDI.current() != null) {
Set<Annotation> interceptorAnnotations = new HashSet<>();
BeanManager beanManager = CDI.current().getBeanManager();
for (Annotation annotation : method.getAnnotations()) {
if (beanManager.isInterceptorBinding(annotation.annotationType())) {
interceptorAnnotations.add(annotation);
}
}
}
interceptorAnnotations.addAll(interfaceModel.getInterceptorAnnotations());
Annotation[] allInterceptorAnnotations = interceptorAnnotations.toArray(new Annotation[0]);
if (allInterceptorAnnotations.length == 0) {
return;
}
List<Interceptor<?>> interceptors = beanManager.resolveInterceptors(InterceptionType.AROUND_INVOKE,
allInterceptorAnnotations);
if (!interceptors.isEmpty()) {
for (Interceptor<?> interceptor : interceptors) {
Object interceptorInstance = beanManager.getReference(interceptor,
interceptor.getBeanClass(),
interfaceModel.getCreationalContext());
invocationInterceptors.add(new InterceptorInvocationContext
.InvocationInterceptor(interceptorInstance,
interceptor));
interceptorAnnotations.addAll(interfaceModel.getInterceptorAnnotations());
Annotation[] allInterceptorAnnotations = interceptorAnnotations.toArray(new Annotation[0]);
if (allInterceptorAnnotations.length == 0) {
return;
}
List<Interceptor<?>> interceptors = beanManager.resolveInterceptors(InterceptionType.AROUND_INVOKE,
allInterceptorAnnotations);
if (!interceptors.isEmpty()) {
for (Interceptor<?> interceptor : interceptors) {
Object interceptorInstance = beanManager.getReference(interceptor,
interceptor.getBeanClass(),
interfaceModel.getCreationalContext());
invocationInterceptors.add(new InterceptorInvocationContext
.InvocationInterceptor(interceptorInstance,
interceptor));
}
}
}
} catch (IllegalStateException ignored) {
//CDI not present. Ignore.
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/*
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019 Payara Foundation 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 @@ -37,11 +36,6 @@
import java.util.stream.Collectors;

import javax.annotation.Priority;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.CDI;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.ws.rs.Priorities;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
Expand All @@ -61,6 +55,7 @@
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.internal.inject.InjectionManager;
import org.glassfish.jersey.internal.inject.InjectionManagerSupplier;
import org.glassfish.jersey.internal.util.ReflectionHelper;
Expand All @@ -69,7 +64,6 @@
* Rest client builder implementation. Creates proxy instance of requested interface.
*
* @author David Kral
* @author Patrik Dudits
*/
public class RestClientBuilderImpl implements RestClientBuilder {

Expand Down Expand Up @@ -189,8 +183,7 @@ public <T> T build(Class<T> interfaceClass) throws IllegalStateException, RestCl
responseExceptionMappers,
paramConverterProviders,
asyncInterceptors,
injectionManagerExposer.injectionManager,
lookupBeanManager());
injectionManagerExposer.injectionManager);


return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(),
Expand All @@ -199,31 +192,6 @@ public <T> T build(Class<T> interfaceClass) throws IllegalStateException, RestCl
);
}

private BeanManager lookupBeanManager() {
Context initialContext = null;
try {
initialContext = new InitialContext();
return (BeanManager) initialContext.lookup("java:comp/BeanManager");
} catch (NamingException e) {
// no bean manager in JNDI
} finally {
if (initialContext != null) {
try {
initialContext.close();
} catch (NamingException e) {
}
}
}
try {
if (CDI.current() != null) {
return CDI.current().getBeanManager();
}
} catch (IllegalStateException e) {
// CDI unavailable
}
return null;
}

private void processConfigProviders(Class<?> restClientInterface, String[] providerArray) {
for (String provider : providerArray) {
Class<?> providerClass = AccessController.doPrivileged(ReflectionHelper.classForNamePA(provider));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,64 @@ public void collectClientRegistrations(@Observes
}
}

/**
* Iterates over all {@link ProcessInjectionPoint} to find only those annotated
* with {@link RestClient} and configures their proper injection.
*
* @param pip processed injection point
*/
public void collectClientProducer(@Observes ProcessInjectionPoint<?, ?> pip) {
RestClient restClient = pip.getInjectionPoint().getAnnotated().getAnnotation(RestClient.class);
if (restClient != null) {
InjectionPoint ip = pip.getInjectionPoint();
Class<?> type = (Class<?>) ip.getType();

RestClientLiteral q = new RestClientLiteral(type);

pip.configureInjectionPoint().addQualifier(q);
}
}

/**
* Creates new producers based on collected interfaces.
*
* @param abd after bean discovery instance
* @param bm bean manager instance
*/
public void restClientRegistration(@Observes AfterBeanDiscovery abd, BeanManager bm) {
interfaces.forEach(type -> abd.addBean(new RestClientProducer(type, bm)));
interfaces.forEach(type -> abd.addBean(new RestClientProducer(new RestClientLiteral(type), type, bm)));
interfaces.forEach(type -> abd.addBean(new RestClientProducer(null, type, bm)));
}

@Qualifier
@Retention(RUNTIME)
@Target({METHOD, FIELD})
@interface MpRestClientQualifier {

Class<?> interfaceType();

}

private static class RestClientLiteral extends AnnotationLiteral<MpRestClientQualifier> implements MpRestClientQualifier {

private final Class<?> interfaceType;

RestClientLiteral(Class<?> interfaceType) {
this.interfaceType = interfaceType;
}

@Override
public Class<?> interfaceType() {
return interfaceType;
}

@Override
public String toString() {
return "RestClientLiteral{"
+ "interfaceType=" + interfaceType
+ '}';
}

}

}
Loading

0 comments on commit 4f9d61f

Please sign in to comment.