diff --git a/bom/pom.xml b/bom/pom.xml index 243d2d2f192..e2b5ccbaaff 100644 --- a/bom/pom.xml +++ b/bom/pom.xml @@ -243,11 +243,6 @@ jersey-rx-client-rxjava2 ${project.version} - - org.glassfish.jersey.ext - jersey-mp-rest-client - ${project.version} - org.glassfish.jersey.media jersey-media-jaxb diff --git a/core-client/src/main/java/org/glassfish/jersey/client/internal/inject/PrimitiveValueOfInserter.java b/core-client/src/main/java/org/glassfish/jersey/client/internal/inject/PrimitiveValueOfInserter.java index 9c8b33c753b..9e1ccc879ec 100644 --- a/core-client/src/main/java/org/glassfish/jersey/client/internal/inject/PrimitiveValueOfInserter.java +++ b/core-client/src/main/java/org/glassfish/jersey/client/internal/inject/PrimitiveValueOfInserter.java @@ -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 @@ -26,10 +26,9 @@ * @author Paul Sandoz * @author Marek Potociar (marek.potociar at oracle.com) * @author Gaurav Gupta (gaurav.gupta@payara.fish) - * @author Patrik Dudits * */ -final class PrimitiveValueOfInserter implements ParameterInserter { +final class PrimitiveValueOfInserter implements ParameterInserter { private final String parameter; private final String defaultValue; @@ -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) { diff --git a/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/InterfaceModel.java b/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/InterfaceModel.java index 35a19f502b0..dad9c5e3b34 100644 --- a/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/InterfaceModel.java +++ b/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/InterfaceModel.java @@ -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 @@ -51,7 +50,6 @@ * Model of interface and its annotation. * * @author David Kral - * @author Patrik Dudits */ class InterfaceModel { @@ -68,7 +66,6 @@ class InterfaceModel { private final Set responseExceptionMappers; private final Set paramConverterProviders; private final Set interceptorAnnotations; - private final BeanManager beanManager; /** * Creates new model based on interface class. Interface is parsed according to specific annotations. @@ -84,14 +81,12 @@ static InterfaceModel from(Class restClientClass, Set responseExceptionMappers, Set paramConverterProviders, List 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)) @@ -113,7 +108,6 @@ private InterfaceModel(Builder builder) { this.interceptorAnnotations = builder.interceptorAnnotations; this.creationalContext = builder.creationalContext; this.asyncInterceptors = builder.asyncInterceptors; - this.beanManager = builder.beanManager; } /** @@ -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; @@ -268,27 +257,30 @@ private Builder(Class restClientClass, Set responseExceptionMappers, Set paramConverterProviders, List 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. } } diff --git a/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/MethodModel.java b/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/MethodModel.java index e7a292593c7..52079eb213b 100644 --- a/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/MethodModel.java +++ b/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/MethodModel.java @@ -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 @@ -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; @@ -74,7 +74,6 @@ * Method model contains all information about method defined in rest client interface. * * @author David Kral - * @author Patrik Dudits */ class MethodModel { @@ -128,8 +127,7 @@ private MethodModel(Builder builder) { interfaceModel.getResponseExceptionMappers(), interfaceModel.getParamConverterProviders(), interfaceModel.getAsyncInterceptors(), - interfaceModel.getInjectionManager(), - interfaceModel.getBeanManager()); + interfaceModel.getInjectionManager()); } else { subResourceModel = null; } @@ -477,31 +475,35 @@ private Builder(InterfaceModel interfaceModel, Method method) { private void filterAllInterceptorAnnotations() { invocationInterceptors = new ArrayList<>(); - BeanManager beanManager = interfaceModel.getBeanManager(); - if (beanManager != null) { - Set interceptorAnnotations = new HashSet<>(); - for (Annotation annotation : method.getAnnotations()) { - if (beanManager.isInterceptorBinding(annotation.annotationType())) { - interceptorAnnotations.add(annotation); + try { + if (CDI.current() != null) { + Set 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> 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> 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. } } diff --git a/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/RestClientBuilderImpl.java b/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/RestClientBuilderImpl.java index 1d96c4b777a..fadca0fd315 100644 --- a/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/RestClientBuilderImpl.java +++ b/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/RestClientBuilderImpl.java @@ -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 @@ -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; @@ -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; @@ -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 { @@ -189,8 +183,7 @@ public T build(Class interfaceClass) throws IllegalStateException, RestCl responseExceptionMappers, paramConverterProviders, asyncInterceptors, - injectionManagerExposer.injectionManager, - lookupBeanManager()); + injectionManagerExposer.injectionManager); return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(), @@ -199,31 +192,6 @@ public T build(Class 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)); diff --git a/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/RestClientExtension.java b/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/RestClientExtension.java index 58fca82343b..05743a2eb09 100644 --- a/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/RestClientExtension.java +++ b/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/RestClientExtension.java @@ -71,6 +71,24 @@ 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. * @@ -78,7 +96,39 @@ public void collectClientRegistrations(@Observes * @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 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 + + '}'; + } + } } diff --git a/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/RestClientModel.java b/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/RestClientModel.java index 4e81ca38339..49d353cef3a 100644 --- a/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/RestClientModel.java +++ b/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/RestClientModel.java @@ -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 @@ -24,7 +23,6 @@ import java.util.Map; import java.util.Set; -import javax.enterprise.inject.spi.BeanManager; import javax.ws.rs.client.WebTarget; import javax.ws.rs.ext.ParamConverterProvider; @@ -36,7 +34,6 @@ * Model of the rest client interface. * * @author David Kral - * @author Patrik Dudits */ class RestClientModel { @@ -57,14 +54,12 @@ static RestClientModel from(Class restClientClass, Set responseExceptionMappers, Set paramConverterProviders, List asyncInterceptors, - InjectionManager injectionManager, - BeanManager beanManager) { + InjectionManager injectionManager) { InterfaceModel interfaceModel = InterfaceModel.from(restClientClass, responseExceptionMappers, paramConverterProviders, asyncInterceptors, - injectionManager, - beanManager); + injectionManager); return new Builder() .interfaceModel(interfaceModel) .methodModels(parseMethodModels(interfaceModel)) diff --git a/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/RestClientProducer.java b/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/RestClientProducer.java index eb3acb9d600..7c0c4b3d8bc 100644 --- a/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/RestClientProducer.java +++ b/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/RestClientProducer.java @@ -32,14 +32,12 @@ import javax.enterprise.context.Dependent; import javax.enterprise.context.spi.CreationalContext; -import javax.enterprise.inject.Any; import javax.enterprise.inject.Default; import javax.enterprise.inject.spi.Bean; import javax.enterprise.inject.spi.BeanManager; import javax.enterprise.inject.spi.DeploymentException; import javax.enterprise.inject.spi.InjectionPoint; import javax.enterprise.inject.spi.PassivationCapable; -import javax.enterprise.util.AnnotationLiteral; import org.eclipse.microprofile.config.Config; import org.eclipse.microprofile.config.ConfigProvider; @@ -64,6 +62,7 @@ class RestClientProducer implements Bean, PassivationCapable { private static final String CONFIG_CONNECTION_TIMEOUT = "/mp-rest/connectTimeout"; private static final String CONFIG_READ_TIMEOUT = "/mp-rest/readTimeout"; + private final RestClientExtension.MpRestClientQualifier qualifier; private final BeanManager beanManager; private final Class interfaceType; private final Class scope; @@ -73,11 +72,14 @@ class RestClientProducer implements Bean, PassivationCapable { /** * Creates new instance of RestClientProducer. * + * @param qualifier qualifier which defines rest client interface * @param interfaceType rest client interface * @param beanManager bean manager */ - RestClientProducer(Class interfaceType, + RestClientProducer(RestClientExtension.MpRestClientQualifier qualifier, + Class interfaceType, BeanManager beanManager) { + this.qualifier = qualifier; this.interfaceType = interfaceType; this.beanManager = beanManager; this.config = ConfigProvider.getConfig(); @@ -123,8 +125,7 @@ public Object create(CreationalContext creationalContext) { .ifPresent(aLong -> restClientBuilder.readTimeout(aLong, TimeUnit.MILLISECONDS)); return restClientBuilder.build(interfaceType); } catch (MalformedURLException e) { - throw new IllegalStateException("URL is not in valid format for Rest interface " + interfaceType.getName() - + ": " + baseUrl); + throw new IllegalStateException("URL is not in valid format: " + baseUrl); } } @@ -139,9 +140,11 @@ public Set getTypes() { @Override public Set getQualifiers() { + if (qualifier == null) { + return Collections.singleton(Default.Literal.INSTANCE); + } Set annotations = new HashSet<>(); - annotations.add(new AnnotationLiteral() {}); - annotations.add(new AnnotationLiteral() {}); + annotations.add(qualifier); annotations.add(RestClient.LITERAL); return annotations; } @@ -153,7 +156,10 @@ public Class getScope() { @Override public String getName() { - return interfaceType.getName() + "RestClient"; + if (qualifier == null) { + return interfaceType.getName() + "RestClient"; + } + return interfaceType.getName(); } @Override diff --git a/ext/microprofile/mp-rest-client/src/test/java/org/glassfish/jersey/microprofile/restclient/InterfaceValidationTest.java b/ext/microprofile/mp-rest-client/src/test/java/org/glassfish/jersey/microprofile/restclient/InterfaceValidationTest.java index 2f1b4fdf1ef..a8103994b0c 100644 --- a/ext/microprofile/mp-rest-client/src/test/java/org/glassfish/jersey/microprofile/restclient/InterfaceValidationTest.java +++ b/ext/microprofile/mp-rest-client/src/test/java/org/glassfish/jersey/microprofile/restclient/InterfaceValidationTest.java @@ -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 @@ -30,7 +29,7 @@ public class InterfaceValidationTest { @Test public void testValidInterface() { RestClientModel.from(CorrectInterface.class, new HashSet<>(), new HashSet<>(), new ArrayList<>(), - null, null); + null); } -} \ No newline at end of file +}