[ https://issues.apache.org/jira/browse/CXF-7544?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16266686#comment-16266686 ]
ASF GitHub Bot commented on CXF-7544: ------------------------------------- reta closed pull request #346: CXF-7544: Support @Context-based injection into proxied CDI beans URL: https://github.com/apache/cxf/pull/346 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/CdiClassUnwrapper.java b/integration/cdi/src/main/java/org/apache/cxf/cdi/CdiClassUnwrapper.java index c32e6690981..ac54da90535 100644 --- a/integration/cdi/src/main/java/org/apache/cxf/cdi/CdiClassUnwrapper.java +++ b/integration/cdi/src/main/java/org/apache/cxf/cdi/CdiClassUnwrapper.java @@ -22,8 +22,18 @@ import org.apache.cxf.common.util.ClassUnwrapper; +/** + * Unwraps the CDI proxy classes into real classes. + */ class CdiClassUnwrapper implements ClassUnwrapper { - private static final Pattern PROXY_PATTERN = Pattern.compile(".+\\$\\$.+Proxy"); + /** + * Known proxy patterns for OWB and Weld: + * + * Xxx$$OwbNormalScopeProxy0 + * Xxx$Proxy$_$$_WeldClientProxy + * + */ + private static final Pattern PROXY_PATTERN = Pattern.compile(".+\\$\\$.+Proxy\\d*"); CdiClassUnwrapper() { diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/FilterProviderInfo.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/FilterProviderInfo.java index f57401e635b..0f04b5e94c8 100644 --- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/FilterProviderInfo.java +++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/FilterProviderInfo.java @@ -32,18 +32,23 @@ private Map<Class<?>, Integer> supportedContracts; private boolean dynamic; - public FilterProviderInfo(T provider, + public FilterProviderInfo(Class<?> resourceClass, + Class<?> serviceClass, + T provider, Bus bus, Map<Class<?>, Integer> supportedContracts) { - this(provider, bus, ProviderFactory.DEFAULT_FILTER_NAME_BINDING, false, supportedContracts); + this(resourceClass, serviceClass, provider, bus, ProviderFactory.DEFAULT_FILTER_NAME_BINDING, + false, supportedContracts); } - public FilterProviderInfo(T provider, + public FilterProviderInfo(Class<?> resourceClass, + Class<?> serviceClass, + T provider, Bus bus, String nameBinding, boolean dynamic, Map<Class<?>, Integer> supportedContracts) { - super(provider, bus, true); + super(resourceClass, serviceClass, provider, bus, true); this.nameBinding = Collections.singleton(nameBinding); this.supportedContracts = supportedContracts; this.dynamic = dynamic; diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/ProviderInfo.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/ProviderInfo.java index 234a76091e9..73e7147b482 100644 --- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/ProviderInfo.java +++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/ProviderInfo.java @@ -35,7 +35,16 @@ public ProviderInfo(T provider, Bus bus, boolean custom) { } public ProviderInfo(T provider, Bus bus, boolean checkContexts, boolean custom) { - this(provider, null, bus, checkContexts, custom); + this(provider.getClass(), provider.getClass(), provider, bus, true, custom); + } + + public ProviderInfo(Class<?> resourceClass, Class<?> serviceClass, T provider, Bus bus, boolean custom) { + this(resourceClass, serviceClass, provider, bus, true, custom); + } + + public ProviderInfo(Class<?> resourceClass, Class<?> serviceClass, T provider, Bus bus, + boolean checkContexts, boolean custom) { + this(resourceClass, serviceClass, provider, null, bus, checkContexts, custom); } public ProviderInfo(T provider, @@ -45,14 +54,24 @@ public ProviderInfo(T provider, this(provider, constructorProxies, bus, true, custom); } + public ProviderInfo(Class<?> resourceClass, + Class<?> serviceClass, + T provider, + Map<Class<?>, ThreadLocalProxy<?>> constructorProxies, + Bus bus, + boolean checkContexts, + boolean custom) { + super(resourceClass, serviceClass, true, checkContexts, constructorProxies, bus, provider); + this.provider = provider; + this.custom = custom; + } + public ProviderInfo(T provider, Map<Class<?>, ThreadLocalProxy<?>> constructorProxies, Bus bus, boolean checkContexts, boolean custom) { - super(provider.getClass(), provider.getClass(), true, checkContexts, constructorProxies, bus, provider); - this.provider = provider; - this.custom = custom; + this(provider.getClass(), provider.getClass(), provider, constructorProxies, bus, checkContexts, custom); } @Override diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ServerProviderFactory.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ServerProviderFactory.java index dda7ee290d3..2e859009012 100644 --- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ServerProviderFactory.java +++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ServerProviderFactory.java @@ -211,7 +211,11 @@ protected void setProviders(boolean custom, boolean busGlobal, Object... provide for (Object featureProvider : cfg.getInstances()) { Map<Class<?>, Integer> contracts = cfg.getContracts(featureProvider.getClass()); if (contracts != null && !contracts.isEmpty()) { - allProviders.add(new FilterProviderInfo<Object>(featureProvider, + Class<?> providerCls = ClassHelper.getRealClass(getBus(), featureProvider); + + allProviders.add(new FilterProviderInfo<Object>(featureProvider.getClass(), + providerCls, + featureProvider, getBus(), contracts)); } else { @@ -373,7 +377,10 @@ private void doApplyDynamicFeatures(ClassResourceInfo cri) { for (Object provider : cfg.getInstances()) { Map<Class<?>, Integer> contracts = cfg.getContracts(provider.getClass()); if (contracts != null && !contracts.isEmpty()) { - registerUserProvider(new FilterProviderInfo<Object>(provider, + Class<?> providerCls = ClassHelper.getRealClass(getBus(), provider); + registerUserProvider(new FilterProviderInfo<Object>(provider.getClass(), + providerCls, + provider, getBus(), nameBinding, true, @@ -601,5 +608,4 @@ public int compare(ProviderInfo<?> p1, ProviderInfo<?> p2) { return result; } } - } diff --git a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/spec/ClientImpl.java b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/spec/ClientImpl.java index 78f45f5d34f..61c465a26f2 100644 --- a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/spec/ClientImpl.java +++ b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/spec/ClientImpl.java @@ -39,6 +39,7 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.UriBuilder; +import org.apache.cxf.common.util.ClassHelper; import org.apache.cxf.common.util.SystemPropertyAction; import org.apache.cxf.configuration.jsse.TLSClientParameters; import org.apache.cxf.jaxrs.client.AbstractClient; @@ -274,8 +275,9 @@ public Builder request() { if (contracts == null || contracts.isEmpty()) { providers.add(p); } else { - providers.add( - new FilterProviderInfo<Object>(p, pf.getBus(), contracts)); + final Class<?> providerCls = ClassHelper.getRealClass(pf.getBus(), p); + providers.add(new FilterProviderInfo<Object>(p.getClass(), + providerCls, p, pf.getBus(), contracts)); } } } diff --git a/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/BookStorePreMatchingRequestFilter.java b/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/BookStorePreMatchingRequestFilter.java new file mode 100644 index 00000000000..ec1917837fe --- /dev/null +++ b/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/BookStorePreMatchingRequestFilter.java @@ -0,0 +1,48 @@ +/** + * 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.context.ApplicationScoped; +import javax.ws.rs.container.ContainerRequestContext; +import javax.ws.rs.container.ContainerRequestFilter; +import javax.ws.rs.container.PreMatching; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriInfo; + +@ApplicationScoped @PreMatching +public class BookStorePreMatchingRequestFilter implements ContainerRequestFilter { + private UriInfo uriInfo; + + @Context + public void setUriInfo(UriInfo uriInfo) { + this.uriInfo = uriInfo; + } + + @Override + public void filter(ContainerRequestContext requestContext) throws IOException { + // Contextual instances should be injected independently + if (uriInfo == null || uriInfo.getBaseUri() == null) { + requestContext.abortWith(Response.serverError().entity("uriInfo is not set").build()); + } + } +} diff --git a/systests/cdi/cdi-owb/cdi-producers-owb/pom.xml b/systests/cdi/cdi-owb/cdi-producers-owb/pom.xml index 15f03fde42e..33962487f36 100644 --- a/systests/cdi/cdi-owb/cdi-producers-owb/pom.xml +++ b/systests/cdi/cdi-owb/cdi-producers-owb/pom.xml @@ -45,5 +45,11 @@ <groupId>org.apache.abdera</groupId> <artifactId>abdera-parser</artifactId> </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + <version>2.7.14</version> + <scope>test</scope> + </dependency> </dependencies> </project> \ No newline at end of file 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 309a5ea3a99..9aa26384c81 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 @@ -32,6 +32,7 @@ public boolean configure(FeatureContext context) { context.register(AtomFeedProvider.class); context.register(BookStoreRequestFilter.class); context.register(BookStoreResponseFilter.class); + context.register(SampleNestedFeature.class); return false; } } diff --git a/systests/cdi/cdi-owb/cdi-producers-owb/src/test/java/org/apache/cxf/systest/jaxrs/cdi/SampleNestedFeature.java b/systests/cdi/cdi-owb/cdi-producers-owb/src/test/java/org/apache/cxf/systest/jaxrs/cdi/SampleNestedFeature.java new file mode 100644 index 00000000000..4e1e47ce7f6 --- /dev/null +++ b/systests/cdi/cdi-owb/cdi-producers-owb/src/test/java/org/apache/cxf/systest/jaxrs/cdi/SampleNestedFeature.java @@ -0,0 +1,33 @@ +/** + * 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.systest.jaxrs.cdi; + +import javax.ws.rs.core.Feature; +import javax.ws.rs.core.FeatureContext; + +import org.apache.cxf.systests.cdi.base.BookStorePreMatchingRequestFilter; + +public class SampleNestedFeature implements Feature { + @Override + public boolean configure(FeatureContext context) { + context.register(BookStorePreMatchingRequestFilter.class); + return false; + } +} diff --git a/systests/cdi/cdi-owb/cdi-producers-owb/src/test/java/org/apache/cxf/systest/jaxrs/cdi/unwrapper/ClassUnwrapperTest.java b/systests/cdi/cdi-owb/cdi-producers-owb/src/test/java/org/apache/cxf/systest/jaxrs/cdi/unwrapper/ClassUnwrapperTest.java new file mode 100644 index 00000000000..ae8d5732692 --- /dev/null +++ b/systests/cdi/cdi-owb/cdi-producers-owb/src/test/java/org/apache/cxf/systest/jaxrs/cdi/unwrapper/ClassUnwrapperTest.java @@ -0,0 +1,90 @@ +/** + * 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.systest.jaxrs.cdi.unwrapper; + +import java.util.Set; + +import javax.enterprise.inject.spi.Bean; +import javax.enterprise.inject.spi.BeanManager; +import javax.servlet.ServletContext; +import javax.servlet.ServletContextEvent; + +import org.apache.cxf.Bus; +import org.apache.cxf.common.util.ClassUnwrapper; +import org.apache.cxf.systests.cdi.base.BookStorePreMatchingRequestFilter; +import org.apache.cxf.systests.cdi.base.BookStoreRequestFilter; +import org.apache.webbeans.config.WebBeansContext; +import org.apache.webbeans.spi.ContainerLifecycle; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; + + +public class ClassUnwrapperTest { + private Bus bus; + private ContainerLifecycle lifecycle; + private ServletContextEvent event; + + @Before + public void setUp() { + event = new ServletContextEvent(mock(ServletContext.class)); + lifecycle = WebBeansContext.currentInstance().getService(ContainerLifecycle.class); + lifecycle.startApplication(event); + bus = getBeanReference(Bus.class); + } + + @SuppressWarnings("unchecked") + private<T> T getBeanReference(Class<T> clazz) { + final BeanManager beanManager = lifecycle.getBeanManager(); + final Set<Bean<?>> beans = beanManager.getBeans(clazz); + final Bean<?> bean = beanManager.resolve(beans); + return (T)beanManager.getReference(bean, clazz, beanManager.createCreationalContext(bean)); + } + + @After + public void tearDown() { + lifecycle.stopApplication(event); + } + + @Test + public void testProxyClassIsProperlyUnwrapped() { + final BookStorePreMatchingRequestFilter filter = getBeanReference(BookStorePreMatchingRequestFilter.class); + final ClassUnwrapper unwrapper = (ClassUnwrapper)bus.getProperty(ClassUnwrapper.class.getName()); + + assertThat(unwrapper, notNullValue()); + assertThat(filter.getClass(), not(equalTo(BookStorePreMatchingRequestFilter.class))); + assertThat(unwrapper.getRealClass(filter), equalTo(BookStorePreMatchingRequestFilter.class)); + } + + @Test + public void testRealClassIsProperlyUnwrapped() { + final BookStoreRequestFilter filter = getBeanReference(BookStoreRequestFilter.class); + final ClassUnwrapper unwrapper = (ClassUnwrapper)bus.getProperty(ClassUnwrapper.class.getName()); + + assertThat(unwrapper, notNullValue()); + assertThat(filter.getClass(), equalTo(BookStoreRequestFilter.class)); + assertThat(unwrapper.getRealClass(filter), equalTo(BookStoreRequestFilter.class)); + } +} diff --git a/systests/cdi/cdi-weld/cdi-producers-weld/pom.xml b/systests/cdi/cdi-weld/cdi-producers-weld/pom.xml index 1ed161a73ad..8f73095e01d 100644 --- a/systests/cdi/cdi-weld/cdi-producers-weld/pom.xml +++ b/systests/cdi/cdi-weld/cdi-producers-weld/pom.xml @@ -45,5 +45,11 @@ <groupId>org.apache.abdera</groupId> <artifactId>abdera-parser</artifactId> </dependency> + <dependency> + <groupId>org.jboss.weld.se</groupId> + <artifactId>weld-se-core</artifactId> + <version>${cxf.jboss.weld.version}</version> + <scope>test</scope> + </dependency> </dependencies> </project> 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 309a5ea3a99..9aa26384c81 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 @@ -32,6 +32,7 @@ public boolean configure(FeatureContext context) { context.register(AtomFeedProvider.class); context.register(BookStoreRequestFilter.class); context.register(BookStoreResponseFilter.class); + context.register(SampleNestedFeature.class); return false; } } diff --git a/systests/cdi/cdi-weld/cdi-producers-weld/src/test/java/org/apache/cxf/systest/jaxrs/cdi/SampleNestedFeature.java b/systests/cdi/cdi-weld/cdi-producers-weld/src/test/java/org/apache/cxf/systest/jaxrs/cdi/SampleNestedFeature.java new file mode 100644 index 00000000000..4e1e47ce7f6 --- /dev/null +++ b/systests/cdi/cdi-weld/cdi-producers-weld/src/test/java/org/apache/cxf/systest/jaxrs/cdi/SampleNestedFeature.java @@ -0,0 +1,33 @@ +/** + * 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.systest.jaxrs.cdi; + +import javax.ws.rs.core.Feature; +import javax.ws.rs.core.FeatureContext; + +import org.apache.cxf.systests.cdi.base.BookStorePreMatchingRequestFilter; + +public class SampleNestedFeature implements Feature { + @Override + public boolean configure(FeatureContext context) { + context.register(BookStorePreMatchingRequestFilter.class); + return false; + } +} diff --git a/systests/cdi/cdi-weld/cdi-producers-weld/src/test/java/org/apache/cxf/systest/jaxrs/cdi/unwrapper/ClassUnwrapperTest.java b/systests/cdi/cdi-weld/cdi-producers-weld/src/test/java/org/apache/cxf/systest/jaxrs/cdi/unwrapper/ClassUnwrapperTest.java new file mode 100644 index 00000000000..74056232e64 --- /dev/null +++ b/systests/cdi/cdi-weld/cdi-producers-weld/src/test/java/org/apache/cxf/systest/jaxrs/cdi/unwrapper/ClassUnwrapperTest.java @@ -0,0 +1,75 @@ +/** + * 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.systest.jaxrs.cdi.unwrapper; + +import org.apache.cxf.Bus; +import org.apache.cxf.common.util.ClassUnwrapper; +import org.apache.cxf.systests.cdi.base.BookStorePreMatchingRequestFilter; +import org.apache.cxf.systests.cdi.base.BookStoreRequestFilter; +import org.jboss.weld.environment.se.Weld; +import org.jboss.weld.environment.se.WeldContainer; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.junit.Assert.assertThat; + +public class ClassUnwrapperTest { + private Bus bus; + private WeldContainer container; + + @Before + public void setUp() { + final Weld weld = new Weld(); + container = weld.initialize(); + bus = getBeanReference(Bus.class); + } + + private<T> T getBeanReference(Class<T> clazz) { + return container.select(clazz).get(); + } + + @After + public void tearDown() { + container.close(); + } + + @Test + public void testProxyClassIsProperlyUnwrapped() { + final BookStorePreMatchingRequestFilter filter = getBeanReference(BookStorePreMatchingRequestFilter.class); + final ClassUnwrapper unwrapper = (ClassUnwrapper)bus.getProperty(ClassUnwrapper.class.getName()); + + assertThat(unwrapper, notNullValue()); + assertThat(filter.getClass(), not(equalTo(BookStorePreMatchingRequestFilter.class))); + assertThat(unwrapper.getRealClass(filter), equalTo(BookStorePreMatchingRequestFilter.class)); + } + + @Test + public void testRealClassIsProperlyUnwrapped() { + final BookStoreRequestFilter filter = getBeanReference(BookStoreRequestFilter.class); + final ClassUnwrapper unwrapper = (ClassUnwrapper)bus.getProperty(ClassUnwrapper.class.getName()); + + assertThat(unwrapper, notNullValue()); + assertThat(filter.getClass(), equalTo(BookStoreRequestFilter.class)); + assertThat(unwrapper.getRealClass(filter), equalTo(BookStoreRequestFilter.class)); + } +} ---------------------------------------------------------------- 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 > Support @Context-based injection into proxied CDI beans > ------------------------------------------------------- > > Key: CXF-7544 > URL: https://issues.apache.org/jira/browse/CXF-7544 > Project: CXF > Issue Type: Bug > Affects Versions: 3.1.13, 3.2.0 > Reporter: Andriy Redko > Assignee: Andriy Redko > > The issue pop up as part of https://github.com/apache/cxf/pull/330 > discussion. In case when provider / feature / resource is a proxied CDI bean, > the contextual class members (annotated with @Context) are not injected. > Test case to reproduce: > {code} > @ApplicationScoped > public class BookStoreRequestFilter implements ContainerRequestFilter { > @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()); > } > } > } > {code} > CC [~rmannibucau] > h3. A bit more context > In some circumstances (like using @ApplicationScoped annotation for example) > the CDI runtime will create a proxy class for a particular bean. As the > result, the CXF side is going to bind the particular provider metadata to > this proxy instance. It looks logical and unambiguous. > However, the interesting things are happening when CXF will try to inject > contextual proxies (@Context annotations) into the provider instance. The > injections are successful but the target object for them will be the proxy > instance (not the real instance behind it). Consequently, at runtime, when > the proxy delegates the call to a backing instance, all contextual proxies > are null in there (simply put, not set). > h3. How to solve > Referring to the recent discussions with [~sergeyb], the best solution would > be to delegate the @Context annotation to CDI framework (as such, relieving > the CXF from doing the injection work). This proposal may need a support from > the JAX-RS specification side. > Simpler (interim?) possible solution would be to complement the CDI injection > with @Context injection (delegating this work to the CXF as it works right > now for non-proxy beans and non-CDI deployments). This could be done by > observing ProcessInjectionTarget events and supplying our own InjectionTarget > (have working PoC for this approach). > Regarding constructor injection, it seems like CXF does not support passing > the arguments to provider constructor (in case of CDI, w/o @Context > annotation) so I it would be another (separate) issue to look at. > h3. Update > Addressing the issue requires a significant revamp of the CXF injection > mechanism, it is turned out to be hard to solve even with @AroundConstruct > or, previously, providing custom @Context injectors. The good news is that > the field-based injection could be easily workarounded using setter-based > injection. > {code} > @ApplicationScoped > public class BookStoreRequestFilter implements ContainerRequestFilter { > private ResourceInfo resourceInfo; > > @Context > public void setResourceInfo(ResourceInfo resourceInfo) { > this.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()); > } > } > } > {code} > The work related to CXF injection refactoring is tracked under CXF-7571 -- This message was sent by Atlassian JIRA (v6.4.14#64029)