cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r...@apache.org
Subject [cxf] branch master updated: CXF-7501: Cannot inject field in ContainerRequestFilter (and generally, into any providers registered using FeatureContext) (#330)
Date Sun, 29 Oct 2017 18:03:22 GMT
This is an automated email from the ASF dual-hosted git repository.

reta pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/master by this push:
     new 9850612  CXF-7501: Cannot inject field in ContainerRequestFilter (and generally,
into any providers registered using FeatureContext) (#330)
9850612 is described below

commit 98506127c35cbcf5add931d16a705b616fabfc5d
Author: Andriy Redko <drreta@gmail.com>
AuthorDate: Sun Oct 29 14:03:20 2017 -0400

    CXF-7501: Cannot inject field in ContainerRequestFilter (and generally, into any providers
registered using FeatureContext) (#330)
    
    * CXF-7501: Cannot inject field in ContainerRequestFilter (and generally, into any providers
registered using FeatureContext). Using proper CDI bean resolution with fallback to default
strategy.
    
    * CXF-7501: Cannot inject field in ContainerRequestFilter (and generally, into any providers
registered using FeatureContext). Adding test case to verify proper @Context injection
---
 .../cxf/cdi/CdiServerConfigurableFactory.java      | 34 ++++++++++++----------
 .../systests/cdi/base/BookStoreRequestFilter.java  |  8 +++++
 ...estFilter.java => BookStoreResponseFilter.java} | 17 +++++++----
 .../cxf/systest/jaxrs/cdi/SampleFeature.java       |  2 ++
 .../cxf/systest/jaxrs/cdi/SampleFeature.java       |  2 ++
 5 files changed, 42 insertions(+), 21 deletions(-)

diff --git a/integration/cdi/src/main/java/org/apache/cxf/cdi/CdiServerConfigurableFactory.java
b/integration/cdi/src/main/java/org/apache/cxf/cdi/CdiServerConfigurableFactory.java
index 15d4643..9090bbf 100644
--- a/integration/cdi/src/main/java/org/apache/cxf/cdi/CdiServerConfigurableFactory.java
+++ b/integration/cdi/src/main/java/org/apache/cxf/cdi/CdiServerConfigurableFactory.java
@@ -18,12 +18,11 @@
  */
 package org.apache.cxf.cdi;
 
+import java.util.Set;
+
 import javax.enterprise.context.spi.CreationalContext;
-import javax.enterprise.inject.spi.AnnotatedType;
 import javax.enterprise.inject.spi.Bean;
-import javax.enterprise.inject.spi.BeanAttributes;
 import javax.enterprise.inject.spi.BeanManager;
-import javax.enterprise.inject.spi.InjectionTargetFactory;
 import javax.ws.rs.RuntimeType;
 import javax.ws.rs.core.Configurable;
 import javax.ws.rs.core.FeatureContext;
@@ -31,6 +30,7 @@ import javax.ws.rs.core.FeatureContext;
 import org.apache.cxf.cdi.event.DisposableCreationalContext;
 import org.apache.cxf.jaxrs.impl.ConfigurableImpl;
 import org.apache.cxf.jaxrs.impl.ConfigurableImpl.Instantiator;
+import org.apache.cxf.jaxrs.impl.ConfigurationImpl;
 import org.apache.cxf.jaxrs.provider.ServerConfigurableFactory;
 
 /** 
@@ -49,7 +49,8 @@ public class CdiServerConfigurableFactory implements ServerConfigurableFactory
{
     }
     
     /** 
-     * Instantiates the instance of the provider using CDI/BeanManager 
+     * Instantiates the instance of the provider using CDI/BeanManager (or fall back
+     * to default strategy of CDI bean is not available).
      */
     private static class CdiInstantiator implements Instantiator {
         private final BeanManager beanManager;
@@ -60,18 +61,21 @@ public class CdiServerConfigurableFactory implements ServerConfigurableFactory
{
         
         @Override
         public <T> Object create(Class<T> cls) {
-            final AnnotatedType<T> annotatedType = beanManager.createAnnotatedType(cls);
-            final InjectionTargetFactory<T> injectionTargetFactory = 
-                beanManager.getInjectionTargetFactory(annotatedType);
-            final BeanAttributes<T> attributes = beanManager.createBeanAttributes(annotatedType);
-            final Bean<T> bean = beanManager.createBean(attributes, cls, injectionTargetFactory);
-            final CreationalContext<?> context = beanManager.createCreationalContext(bean);
-            
-            if (!beanManager.isNormalScope(bean.getScope())) {
-                beanManager.fireEvent(new DisposableCreationalContext(context));
+            final Set<Bean<?>> candidates = beanManager.getBeans(cls);
+            final Bean<?> bean = beanManager.resolve(candidates);
+
+            if (bean != null) {
+                final CreationalContext<?> context = beanManager.createCreationalContext(bean);
+                
+                if (!beanManager.isNormalScope(bean.getScope())) {
+                    beanManager.fireEvent(new DisposableCreationalContext(context));
+                }
+
+                return beanManager.getReference(bean, cls, context);
+            } else {
+                // No CDI bean available, falling back to default instantiation strategy
+                return ConfigurationImpl.createProvider(cls);
             }
-            
-            return beanManager.getReference(bean, cls, context);
         }
     }
     
diff --git a/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/BookStoreRequestFilter.java
b/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/BookStoreRequestFilter.java
index cbd8811..4fb8f84 100644
--- a/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/BookStoreRequestFilter.java
+++ b/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/BookStoreRequestFilter.java
@@ -24,14 +24,22 @@ import java.io.IOException;
 import javax.inject.Inject;
 import javax.ws.rs.container.ContainerRequestContext;
 import javax.ws.rs.container.ContainerRequestFilter;
+import javax.ws.rs.container.ResourceInfo;
+import javax.ws.rs.core.Context;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.Status;
 
 public class BookStoreRequestFilter implements ContainerRequestFilter {
     @Inject private BookStoreAuthenticator authenticator;
+    @Context private ResourceInfo resourceInfo;
     
     @Override
     public void filter(ContainerRequestContext requestContext) throws IOException {
+        // Contextual instances should be injected independently
+        if (resourceInfo == null || resourceInfo.getResourceMethod() == null) {
+            requestContext.abortWith(Response.serverError().build());
+        }
+        
         if (!authenticator.authenticated()) {
             requestContext.abortWith(Response.status(Status.UNAUTHORIZED).build());
         }
diff --git a/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/BookStoreRequestFilter.java
b/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/BookStoreResponseFilter.java
similarity index 62%
copy from systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/BookStoreRequestFilter.java
copy to systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/BookStoreResponseFilter.java
index cbd8811..2f5f40f 100644
--- a/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/BookStoreRequestFilter.java
+++ b/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/BookStoreResponseFilter.java
@@ -21,19 +21,24 @@ package org.apache.cxf.systests.cdi.base;
 
 import java.io.IOException;
 
+import javax.enterprise.inject.Vetoed;
 import javax.inject.Inject;
 import javax.ws.rs.container.ContainerRequestContext;
-import javax.ws.rs.container.ContainerRequestFilter;
+import javax.ws.rs.container.ContainerResponseContext;
+import javax.ws.rs.container.ContainerResponseFilter;
 import javax.ws.rs.core.Response;
-import javax.ws.rs.core.Response.Status;
 
-public class BookStoreRequestFilter implements ContainerRequestFilter {
+@Vetoed
+public class BookStoreResponseFilter implements ContainerResponseFilter {
     @Inject private BookStoreAuthenticator authenticator;
     
     @Override
-    public void filter(ContainerRequestContext requestContext) throws IOException {
-        if (!authenticator.authenticated()) {
-            requestContext.abortWith(Response.status(Status.UNAUTHORIZED).build());
+    public void filter(ContainerRequestContext requestContext,
+            ContainerResponseContext responseContext) throws IOException {
+        if (authenticator != null) {
+            // This filter should not be created using CDI runtime (it is vetoed) as 
+            // such the injection should not be performed.
+            responseContext.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
         }
     }
 }
diff --git a/systests/cdi/cdi-owb/cdi-producers-owb/src/test/java/org/apache/cxf/systest/jaxrs/cdi/SampleFeature.java
b/systests/cdi/cdi-owb/cdi-producers-owb/src/test/java/org/apache/cxf/systest/jaxrs/cdi/SampleFeature.java
index ba34082..309a5ea 100644
--- a/systests/cdi/cdi-owb/cdi-producers-owb/src/test/java/org/apache/cxf/systest/jaxrs/cdi/SampleFeature.java
+++ b/systests/cdi/cdi-owb/cdi-producers-owb/src/test/java/org/apache/cxf/systest/jaxrs/cdi/SampleFeature.java
@@ -24,12 +24,14 @@ import javax.ws.rs.core.FeatureContext;
 
 import org.apache.cxf.jaxrs.provider.atom.AtomFeedProvider;
 import org.apache.cxf.systests.cdi.base.BookStoreRequestFilter;
+import org.apache.cxf.systests.cdi.base.BookStoreResponseFilter;
 
 public class SampleFeature implements Feature {
     @Override
     public boolean configure(FeatureContext context) {
         context.register(AtomFeedProvider.class);
         context.register(BookStoreRequestFilter.class);
+        context.register(BookStoreResponseFilter.class);
         return false;
     }
 }
diff --git a/systests/cdi/cdi-weld/cdi-producers-weld/src/test/java/org/apache/cxf/systest/jaxrs/cdi/SampleFeature.java
b/systests/cdi/cdi-weld/cdi-producers-weld/src/test/java/org/apache/cxf/systest/jaxrs/cdi/SampleFeature.java
index ba34082..309a5ea 100644
--- a/systests/cdi/cdi-weld/cdi-producers-weld/src/test/java/org/apache/cxf/systest/jaxrs/cdi/SampleFeature.java
+++ b/systests/cdi/cdi-weld/cdi-producers-weld/src/test/java/org/apache/cxf/systest/jaxrs/cdi/SampleFeature.java
@@ -24,12 +24,14 @@ import javax.ws.rs.core.FeatureContext;
 
 import org.apache.cxf.jaxrs.provider.atom.AtomFeedProvider;
 import org.apache.cxf.systests.cdi.base.BookStoreRequestFilter;
+import org.apache.cxf.systests.cdi.base.BookStoreResponseFilter;
 
 public class SampleFeature implements Feature {
     @Override
     public boolean configure(FeatureContext context) {
         context.register(AtomFeedProvider.class);
         context.register(BookStoreRequestFilter.class);
+        context.register(BookStoreResponseFilter.class);
         return false;
     }
 }

-- 
To stop receiving notification emails like this one, please contact
['"commits@cxf.apache.org" <commits@cxf.apache.org>'].

Mime
View raw message