This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 5490ea50088 CAMEL-21683: Configure jax-rs consumer via CxfRsEndpoint
bean (#16973)
5490ea50088 is described below
commit 5490ea500882adf45b01d3f498c14a7d3fd98b74
Author: Federico Mariani <[email protected]>
AuthorDate: Fri Jan 31 07:48:14 2025 +0100
CAMEL-21683: Configure jax-rs consumer via CxfRsEndpoint bean (#16973)
* CAMEL-21683: Configure jax-rs consumer via CxfRsEndpoint bean
* Add test case
---
.../camel/component/cxf/jaxrs/CxfRsComponent.java | 60 ++++++++++++----------
.../cxf/jaxrs/CxfRsConsumerWithBeanTest.java | 9 ++++
2 files changed, 42 insertions(+), 27 deletions(-)
diff --git
a/components/camel-cxf/camel-cxf-rest/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsComponent.java
b/components/camel-cxf/camel-cxf-rest/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsComponent.java
index 60c82726b21..4258efea74e 100644
---
a/components/camel-cxf/camel-cxf-rest/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsComponent.java
+++
b/components/camel-cxf/camel-cxf-rest/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsComponent.java
@@ -115,38 +115,44 @@ public class CxfRsComponent extends
HeaderFilterStrategyComponent implements SSL
beanId = beanId.substring(2);
}
- AbstractJAXRSFactoryBean bean =
CamelContextHelper.mandatoryLookup(getCamelContext(), beanId,
+ CxfRsEndpoint answer;
+ AbstractJAXRSFactoryBean bean =
CamelContextHelper.lookup(getCamelContext(), beanId,
AbstractJAXRSFactoryBean.class);
- CxfRsEndpointFactoryBean factory = null;
- if (bean.getClass().getName().contains("blueprint")) {
- // use blueprint
- Class<CxfRsEndpointFactoryBean> clazz =
getCamelContext().getClassResolver().resolveMandatoryClass(
-
"org.apache.camel.component.cxf.jaxrs.blueprint.CxfRsBlueprintEndpointFactoryBean",
- CxfRsEndpointFactoryBean.class);
- factory = getCamelContext().getInjector().newInstance(clazz);
- } else {
- try {
- //try spring first
- Class<CxfRsEndpointFactoryBean> clazz =
getCamelContext().getClassResolver()
-
.resolveMandatoryClass("org.apache.camel.component.cxf.spring.jaxrs.SpringCxfRsEndpointFactoryBean",
- CxfRsEndpointFactoryBean.class);
+ if (bean != null) {
+ CxfRsEndpointFactoryBean factory = null;
+ if (bean.getClass().getName().contains("blueprint")) {
+ // use blueprint
+ Class<CxfRsEndpointFactoryBean> clazz =
getCamelContext().getClassResolver().resolveMandatoryClass(
+
"org.apache.camel.component.cxf.jaxrs.blueprint.CxfRsBlueprintEndpointFactoryBean",
+ CxfRsEndpointFactoryBean.class);
factory = getCamelContext().getInjector().newInstance(clazz);
- } catch (Exception ex) {
- factory = new DefaultCxfRsEndpointFactoryBean();
+ } else {
+ try {
+ //try spring first
+ Class<CxfRsEndpointFactoryBean> clazz =
getCamelContext().getClassResolver()
+
.resolveMandatoryClass("org.apache.camel.component.cxf.spring.jaxrs.SpringCxfRsEndpointFactoryBean",
+ CxfRsEndpointFactoryBean.class);
+ factory =
getCamelContext().getInjector().newInstance(clazz);
+ } catch (Exception ex) {
+ factory = new DefaultCxfRsEndpointFactoryBean();
+ }
}
+ answer = factory.createEndpoint(this, remaining, bean);
+
+ // Apply Spring bean properties (including # notation referenced
bean). Note that the
+ // Spring bean properties values can be overridden by property
defined in URI query.
+ // The super class (DefaultComponent) will invoke "setProperties"
after this method
+ // with to apply properties defined by URI query.
+ if (bean.getProperties() != null) {
+ Map<String, Object> copy = new HashMap<>(bean.getProperties());
+ setProperties(answer, copy);
+ }
+ // setup the skipFaultLogging
+ } else {
+ answer = CamelContextHelper.mandatoryLookup(getCamelContext(),
beanId,
+ CxfRsEndpoint.class);
}
- final CxfRsEndpoint answer = factory.createEndpoint(this, remaining,
bean);
-
- // Apply Spring bean properties (including # notation referenced
bean). Note that the
- // Spring bean properties values can be overridden by property defined
in URI query.
- // The super class (DefaultComponent) will invoke "setProperties"
after this method
- // with to apply properties defined by URI query.
- if (bean.getProperties() != null) {
- Map<String, Object> copy = new HashMap<>(bean.getProperties());
- setProperties(answer, copy);
- }
- // setup the skipFaultLogging
answer.setBeanId(beanId);
return answer;
diff --git
a/components/camel-cxf/camel-cxf-rest/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConsumerWithBeanTest.java
b/components/camel-cxf/camel-cxf-rest/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConsumerWithBeanTest.java
index 8b067475d97..3c1d62d4289 100644
---
a/components/camel-cxf/camel-cxf-rest/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConsumerWithBeanTest.java
+++
b/components/camel-cxf/camel-cxf-rest/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConsumerWithBeanTest.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.cxf.jaxrs;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.cxf.common.CXFTestSupport;
+import org.apache.camel.component.cxf.jaxrs.testbean.CustomerServiceResource;
import org.apache.camel.component.cxf.jaxrs.testbean.ServiceUtil;
import org.apache.camel.spi.Registry;
import org.apache.camel.test.junit5.CamelTestSupport;
@@ -40,10 +41,16 @@ public class CxfRsConsumerWithBeanTest extends
CamelTestSupport {
private static final String CXF_RS_ENDPOINT_URI_2
= "cxfrs://http://localhost:" + CXT
+
"/rest2?resourceClasses=org.apache.camel.component.cxf.jaxrs.testbean.CustomerServiceResource";
+ private static final String CXF_RS_BEAN_ENDPOINT_URI =
"cxfrs:bean:myEndpoint";
@Override
protected void bindToRegistry(Registry registry) throws Exception {
registry.bind("service", new ServiceUtil());
+
+ CxfRsEndpoint cxfRsEndpoint = new CxfRsEndpoint();
+ cxfRsEndpoint.addResourceClass(CustomerServiceResource.class);
+ cxfRsEndpoint.setAddress("http://localhost:" + CXT + "/rest3");
+ registry.bind("myEndpoint", cxfRsEndpoint);
}
@Override
@@ -52,6 +59,7 @@ public class CxfRsConsumerWithBeanTest extends
CamelTestSupport {
public void configure() {
from(CXF_RS_ENDPOINT_URI).to("bean://service?method=invoke(${body[0]},
${body[1]})");
from(CXF_RS_ENDPOINT_URI_2).bean(ServiceUtil.class,
"invoke(${body[0]}, ${body[1]})");
+
from(CXF_RS_BEAN_ENDPOINT_URI).to("bean://service?method=invoke(${body[0]},
${body[1]})");
}
};
}
@@ -60,6 +68,7 @@ public class CxfRsConsumerWithBeanTest extends
CamelTestSupport {
public void testPutConsumer() throws Exception {
sendPutRequest("http://localhost:" + CXT +
"/rest/customerservice/c20");
sendPutRequest("http://localhost:" + CXT +
"/rest2/customerservice/c20");
+ sendPutRequest("http://localhost:" + CXT +
"/rest3/customerservice/c20");
}
private void sendPutRequest(String uri) throws Exception {