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

ASF GitHub Bot commented on CAMEL-12541:
----------------------------------------

WillemJiang closed pull request #2350: CAMEL-12541: rsClient does not work 
programmatically, only with XML
URL: https://github.com/apache/camel/pull/2350
 
 
   

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/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpoint.java
 
b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpoint.java
index 954d9842679..46b235a3bd7 100644
--- 
a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpoint.java
+++ 
b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpoint.java
@@ -25,6 +25,7 @@
 import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
 import org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean;
 import org.springframework.context.ApplicationContext;
+import org.springframework.util.ReflectionUtils;
 
 public class CxfRsSpringEndpoint extends CxfRsEndpoint implements BeanIdAware {
     private AbstractJAXRSFactoryBean bean;
@@ -69,7 +70,8 @@ protected JAXRSServerFactoryBean newJAXRSServerFactoryBean() {
 
     @Override
     protected JAXRSClientFactoryBean newJAXRSClientFactoryBean() {
-        return new SpringJAXRSClientFactoryBean();
+        checkBeanType(bean, JAXRSClientFactoryBean.class);
+        return newInstanceWithCommonProperties();
     }
 
     @Override
@@ -88,4 +90,14 @@ public String getBeanId() {
     public void setBeanId(String id) {
         this.beanId = id;
     }
-}
+    
+    private JAXRSClientFactoryBean newInstanceWithCommonProperties() {
+        SpringJAXRSClientFactoryBean cfb = new SpringJAXRSClientFactoryBean();
+        
+        if (bean instanceof SpringJAXRSClientFactoryBean) {
+            ReflectionUtils.shallowCopyFieldState(bean, cfb);
+        }
+
+        return cfb;
+    }    
+}
\ No newline at end of file
diff --git 
a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointTest.java
 
b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointTest.java
index 977d7dca3e3..5e1c10925d1 100644
--- 
a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointTest.java
+++ 
b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointTest.java
@@ -22,13 +22,19 @@
 import org.apache.camel.component.cxf.spring.SpringJAXRSClientFactoryBean;
 import org.apache.camel.component.cxf.spring.SpringJAXRSServerFactoryBean;
 import org.apache.camel.test.spring.CamelSpringTestSupport;
-import org.apache.cxf.version.Version;
 import org.junit.Test;
+import org.springframework.beans.factory.support.BeanDefinitionBuilder;
+import org.springframework.beans.factory.support.DefaultListableBeanFactory;
 import org.springframework.context.support.AbstractXmlApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 
 public class CxfRsSpringEndpointTest extends CamelSpringTestSupport {
     
+    private static final String BEAN_SERVICE_ENDPOINT_NAME = "serviceEndpoint";
+    private static final String BEAN_SERVICE_ADDRESS = 
"http://localhost/programmatically";;
+    private static final String BEAN_SERVICE_USERNAME = 
"BEAN_SERVICE_USERNAME";
+    private static final String BEAN_SERVICE_PASSWORD = 
"BEAN_SERVICE_PASSWORD";
+    
     @Test
     public void testCreateCxfRsServerFactoryBean() {
         CxfRsEndpoint endpoint = 
resolveMandatoryEndpoint("cxfrs://bean://rsServer", CxfRsEndpoint.class);
@@ -62,13 +68,44 @@ public void testCreateCxfRsClientFactoryBean() {
 
     }
     
+    @Test
+    public void testCreateCxfRsClientFactoryBeanProgrammatically() {
+        
+        CxfRsEndpoint endpoint = resolveMandatoryEndpoint("cxfrs://bean://" + 
BEAN_SERVICE_ENDPOINT_NAME, CxfRsEndpoint.class);
+        SpringJAXRSClientFactoryBean cfb = 
(SpringJAXRSClientFactoryBean)endpoint.createJAXRSClientFactoryBean();
+        
+        assertNotSame("Got the same object but must be different", 
super.applicationContext.getBean(BEAN_SERVICE_ENDPOINT_NAME), cfb);
+        assertEquals("Got the wrong address", BEAN_SERVICE_ADDRESS, 
cfb.getAddress());
+        assertNotNull("Service class must not be null", cfb.getServiceClass());
+        assertEquals("Got the wrong ServiceClass", CustomerService.class, 
cfb.getServiceClass());
+        assertEquals("Got the wrong username", BEAN_SERVICE_USERNAME, 
cfb.getUsername());
+        assertEquals("Got the wrong password", BEAN_SERVICE_PASSWORD, 
cfb.getPassword());                
+    }
+
+    public static SpringJAXRSClientFactoryBean serviceEndpoint() {
+
+        SpringJAXRSClientFactoryBean clientFactoryBean = new 
SpringJAXRSClientFactoryBean();
+        clientFactoryBean.setAddress(BEAN_SERVICE_ADDRESS);
+        clientFactoryBean.setServiceClass(CustomerService.class);
+        clientFactoryBean.setUsername(BEAN_SERVICE_USERNAME);
+        clientFactoryBean.setPassword(BEAN_SERVICE_PASSWORD);
+
+        return clientFactoryBean;
+    }    
+    
     @Override
-    protected AbstractXmlApplicationContext createApplicationContext() {
-        String version = Version.getCurrentVersion();
-        if (version.startsWith("2") && (version.contains("2.5") || 
version.contains("2.4"))) {
-            return new ClassPathXmlApplicationContext(new 
String("org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointBeans.xml"));
-        }
-        return new ClassPathXmlApplicationContext(new 
String("org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointBeans-2.6.xml"));
+    protected AbstractXmlApplicationContext createApplicationContext() {      
+        
+        ClassPathXmlApplicationContext applicationContext = new 
ClassPathXmlApplicationContext(new 
String("org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointBeans.xml"));   
     
+        emulateBeanRegistrationProgrammatically(applicationContext);
+        
+        return applicationContext;
     }
 
-}
+    private void 
emulateBeanRegistrationProgrammatically(ClassPathXmlApplicationContext 
applicationContext) {
+        
+        DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) 
applicationContext.getBeanFactory();
+        BeanDefinitionBuilder definitionBuilder = 
BeanDefinitionBuilder.rootBeanDefinition(CxfRsSpringEndpointTest.class.getName()).setFactoryMethod("serviceEndpoint");
+        beanFactory.registerBeanDefinition(BEAN_SERVICE_ENDPOINT_NAME, 
definitionBuilder.getBeanDefinition());
+    }
+}
\ No newline at end of file
diff --git 
a/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointBeans-2.6.xml
 
b/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointBeans-2.6.xml
deleted file mode 100644
index 8b0ca34cfed..00000000000
--- 
a/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointBeans-2.6.xml
+++ /dev/null
@@ -1,57 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
-
-    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.
-
--->
-<beans xmlns="http://www.springframework.org/schema/beans";
-       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
-       xmlns:cxf="http://camel.apache.org/schema/cxf";
-       xsi:schemaLocation="
-       http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
-       http://camel.apache.org/schema/cxf 
http://camel.apache.org/schema/cxf/camel-cxf.xsd
-       http://camel.apache.org/schema/spring 
http://camel.apache.org/schema/spring/camel-spring.xsd";>
-
-
-  <cxf:rsServer id="rsServer" address="http://localhost:9000/router";
-    
serviceClass="org.apache.camel.component.cxf.jaxrs.testbean.CustomerService"
-    loggingFeatureEnabled="true" loggingSizeLimit="200">
-    <cxf:properties>
-        <entry key="aKey" value="aValue"/>
-    </cxf:properties>
-    <cxf:providers>
-       <ref bean="jsonProvider"/>
-    </cxf:providers>
-    <cxf:inInterceptors>
-        <bean class="org.apache.camel.component.cxf.jaxrs.TestInInterceptor"/>
-    </cxf:inInterceptors>
-  </cxf:rsServer>
-
-  <cxf:rsClient id="rsClient" address="http://localhost:9002/helloworld";
-    
serviceClass="org.apache.camel.component.cxf.jaxrs.testbean.CustomerService">
-    <cxf:inInterceptors>
-        <bean class="org.apache.camel.component.cxf.jaxrs.TestInInterceptor"/>
-    </cxf:inInterceptors>
-  </cxf:rsClient>
-
-  <bean id="jsonProvider" 
class="org.apache.cxf.jaxrs.provider.json.JSONProvider"/>
-
-  <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring";>
-  </camelContext>
-  
-  
-
-</beans>
diff --git 
a/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointBeans.xml
 
b/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointBeans.xml
index fcd0e8238fe..8b0ca34cfed 100644
--- 
a/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointBeans.xml
+++ 
b/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointBeans.xml
@@ -23,22 +23,31 @@
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
        http://camel.apache.org/schema/cxf 
http://camel.apache.org/schema/cxf/camel-cxf.xsd
-       http://camel.apache.org/schema/spring 
http://camel.apache.org/schema/spring/camel-spring.xsd
-    ">
+       http://camel.apache.org/schema/spring 
http://camel.apache.org/schema/spring/camel-spring.xsd";>
 
 
   <cxf:rsServer id="rsServer" address="http://localhost:9000/router";
     
serviceClass="org.apache.camel.component.cxf.jaxrs.testbean.CustomerService"
     loggingFeatureEnabled="true" loggingSizeLimit="200">
+    <cxf:properties>
+        <entry key="aKey" value="aValue"/>
+    </cxf:properties>
     <cxf:providers>
        <ref bean="jsonProvider"/>
     </cxf:providers>
+    <cxf:inInterceptors>
+        <bean class="org.apache.camel.component.cxf.jaxrs.TestInInterceptor"/>
+    </cxf:inInterceptors>
   </cxf:rsServer>
 
   <cxf:rsClient id="rsClient" address="http://localhost:9002/helloworld";
-    
serviceClass="org.apache.camel.component.cxf.jaxrs.testbean.CustomerService"/>
+    
serviceClass="org.apache.camel.component.cxf.jaxrs.testbean.CustomerService">
+    <cxf:inInterceptors>
+        <bean class="org.apache.camel.component.cxf.jaxrs.TestInInterceptor"/>
+    </cxf:inInterceptors>
+  </cxf:rsClient>
 
-  <bean id="jsonProvider" class="org.apache.cxf.jaxrs.provider.JSONProvider"/>
+  <bean id="jsonProvider" 
class="org.apache.cxf.jaxrs.provider.json.JSONProvider"/>
 
   <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring";>
   </camelContext>


 

----------------------------------------------------------------
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:
[email protected]


> camel-cxfrs - rsClient does not work programmatically, only with XML
> --------------------------------------------------------------------
>
>                 Key: CAMEL-12541
>                 URL: https://issues.apache.org/jira/browse/CAMEL-12541
>             Project: Camel
>          Issue Type: Bug
>          Components: camel-cxfrs
>    Affects Versions: 2.21.1
>            Reporter: Willian Antunes
>            Priority: Major
>
> As described in the documentation you can use [camel-cxfrs as 
> producer|https://github.com/apache/camel/blob/39c0d63d923bfe9236834ecb1c4470bb7e9e7eaa/components/camel-cxf/src/main/docs/cxfrs-component.adoc#how-to-configure-the-rest-endpoint-in-camel].
>  You have some approaches like using a proxy created from a interface which 
> maps all the services available in the targeted REST web service (sample 
> [here|https://github.com/willianantunes/honesto-sqn/blob/d4bf48257fc64a4725894c7f07c24bb1a516d410/src/main/java/br/com/willianantunes/serenata/JarbasAPI.java#L14]).
>  As I'm using Spring Boot and Apache Camel I may create a bean through XML or 
> programmatically.
> h4. Creating the test sample
> I'm testing with the following:
> [https://gist.github.com/willianantunes/58979bfb91ee30c7ff4e235940e60880]
> You can find a copy of JarbasAPI 
> [here|https://github.com/willianantunes/honesto-sqn/blob/d4bf48257fc64a4725894c7f07c24bb1a516d410/src/main/java/br/com/willianantunes/serenata/JarbasAPI.java#L14].
> h4. When rsClient producer works as expected
> When I do using the first approach using the configuration below everything 
> works fine:
> {code:xml}
> <?xml version="1.0" encoding="UTF-8"?>
> <beans xmlns="http://www.springframework.org/schema/beans";
>        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
>        xmlns:cxf="http://camel.apache.org/schema/cxf";
>        xmlns:jaxrs="http://cxf.apache.org/jaxrs";
>        xmlns:util="http://www.springframework.org/schema/util";
>        xsi:schemaLocation="
>        http://www.springframework.org/schema/beans 
> http://www.springframework.org/schema/beans/spring-beans.xsd
>        http://www.springframework.org/schema/util 
> http://www.springframework.org/schema/util/spring-util.xsd
>        http://camel.apache.org/schema/cxf 
> http://camel.apache.org/schema/cxf/camel-cxf.xsd
>        http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
>        http://camel.apache.org/schema/spring 
> http://camel.apache.org/schema/spring/camel-spring.xsd";>
>     <cxf:rsClient id="serviceEndpointViaXML"
>                   address="https://jarbas.serenata.ai"; 
>                   serviceClass="br.com.willianantunes.serenata.JarbasAPI"/>
> </beans>
> {code}
> h4. When rsClient producer does not work as expected
> XML works fine, but when I do the same thing programmatically it doesn't.
> {code:java}
>     @Bean("serviceEndpoint")
>     public SpringJAXRSClientFactoryBean serviceEndpoint() {
>         SpringJAXRSClientFactoryBean clientFactoryBean = new 
> SpringJAXRSClientFactoryBean();
>         clientFactoryBean.setAddress("https://jarbas.serenata.ai";);
>         clientFactoryBean.setServiceClass(JarbasAPI.class);
>         return clientFactoryBean;
>     }
> {code}
> Although it has no difference compared to XML, it does not work.
> h4. When does it fail?
> Debugging you can see the following:
>  * The bean is correctly got 
> [here|https://github.com/apache/camel/blob/a4cfea6823d31eacf57489ffffe1ffeead9a256b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsComponent.java#L76]
>  is CxfRsComponent.
>  * When a message is sent and there is a _to_ command with the URI 
> *cxfrs:bean:serviceEndpoint,* I see the message arriving 
> [here|https://github.com/apache/camel/blob/a4cfea6823d31eacf57489ffffe1ffeead9a256b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducer.java#L94]
>  in CxfRsProducer.
>  * The method 
> [invokeProxyClient|https://github.com/apache/camel/blob/a4cfea6823d31eacf57489ffffe1ffeead9a256b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducer.java#L103]
>  is called.
>  * The SpringJAXRSClientFactoryBean which I provided as bean is ignored. A 
> new one is created 
> [here|https://github.com/apache/camel/blob/834a59910e4b6b8d089e229b39f6c8673e7c3f9a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsEndpoint.java#L414]
>  and when it arrives to the next line the method invoked is from 
> [CxfRsSpringEndpoint|https://github.com/apache/camel/blob/834a59910e4b6b8d089e229b39f6c8673e7c3f9a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpoint.java]
>  which uses 
> [configurer.ConfigureBean|https://github.com/apache/camel/blob/834a59910e4b6b8d089e229b39f6c8673e7c3f9a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpoint.java#L77]
>  that only works with XML.
> h4. Versions used
>  * Spring Boot: 1.5.12.RELEASE
>  * Apache Camel: 2.21.1
> h4. Proposal to make it works
> The idea obviously is to make both approaches work, maybe merging the 
> configuration or as long as the user is providing a custom 
> SpringJAXRSClientFactoryBean then making it the standard instead of creating 
> a new one for each request as it is available in the CxfRsSpringEndpoint from 
> the start.
> ----
> If you want to understand more, read from 
> [here|https://gitter.im/apache/apache-camel?at=5b09f7a54eaffb692d5adb43] 
> until the [end of 
> conversation|https://gitter.im/apache/apache-camel?at=5b0a13cb54ce23136159a2c0]
>  on Gitter.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to