[ 
https://issues.apache.org/jira/browse/CXF-7501?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16224117#comment-16224117
 ] 

ASF GitHub Bot commented on CXF-7501:
-------------------------------------

reta closed pull request #330: CXF-7501: Cannot inject field in 
ContainerRequestFilter (and generally, into any providers registered using 
FeatureContext)
URL: https://github.com/apache/cxf/pull/330
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

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 15d4643d847..9090bbfa664 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 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 @@
     }
     
     /** 
-     * 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 @@
         
         @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 cbd88115afe..4fb8f846c4a 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 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/BookStoreResponseFilter.java
 
b/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/BookStoreResponseFilter.java
new file mode 100644
index 00000000000..2f5f40fd61d
--- /dev/null
+++ 
b/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/BookStoreResponseFilter.java
@@ -0,0 +1,44 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+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.ContainerResponseContext;
+import javax.ws.rs.container.ContainerResponseFilter;
+import javax.ws.rs.core.Response;
+
+@Vetoed
+public class BookStoreResponseFilter implements ContainerResponseFilter {
+    @Inject private BookStoreAuthenticator authenticator;
+    
+    @Override
+    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 ba340825828..309a5ea3a99 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 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 ba340825828..309a5ea3a99 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 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;
     }
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> Cannot inject field in ContainerRequestFilter
> ---------------------------------------------
>
>                 Key: CXF-7501
>                 URL: https://issues.apache.org/jira/browse/CXF-7501
>             Project: CXF
>          Issue Type: Bug
>          Components: JAX-RS
>    Affects Versions: 3.1.10, 3.2.0
>         Environment: Linux Mint 64 bit, TomEE Plus 7.0.3, JavaEE 7 
> application using MVC specification and reference implementation(Libs 
> Attached)
>            Reporter: Jeyvison Nascimento
>            Assignee: Andriy Redko
>              Labels: cdi
>             Fix For: 3.1.14, 3.2.1
>
>         Attachments: javax-mvc.jar, ozark.jar
>
>
> Hey folks.
> We found a weird behavior while running MVC specification(JSR 371) on TomEE 
> witch CXF. We have a *ContainerRequestFilter* defined called 
> *JaxRsContextFilter* 
> {code:java}
> @PreMatching
> @Priority(0)
> public class JaxRsContextFilter implements ContainerRequestFilter {
>     @Inject
>     private JaxRsContextProducer jaxRsContextProducer;
>     @Context
>     private Configuration configuration;
>     @Context
>     private HttpServletRequest request;
>     @Context
>     private HttpServletResponse response;
>     public JaxRsContextFilter() {
>     }
>     public void filter(ContainerRequestContext requestContext) throws 
> IOException {
>         this.jaxRsContextProducer.populate(this.configuration, this.request, 
> this.response);
>     }
> }
> {code}
> You can see that we have a JaxRsContextProducer annotated to be injected as a 
> field in our object but when JAXRSUtils is called to run the the container 
> filters it injects the fields annotated as *@Context* , not the fields 
> annotated with *@Inject*.
> {code:java}
>  for (ProviderInfo<ContainerRequestFilter> filter : containerFilters) {
>                 try {
>                     InjectionUtils.injectContexts(filter.getProvider(), 
> filter, m);
>                     filter.getProvider().filter(context);
>                 } catch (IOException ex) {
>                     throw ExceptionUtils.toInternalServerErrorException(ex, 
> null); 
>                 }
> {code}
> It causes our filter(*JaxRsContextFilter*) to throw a NullPointerException 
> when filtering the request because it uses the producer to perform some 
> actions in  this operation.
> I believe this field should be injected as well, not only the *@Context* 
> fields.



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to