How do I change out request uri in my camel cxfrs route

Solution Verified - Updated -

Environment

  • Red Hat JBoss Fuse
    • 6.x

Issue

I have a similar camel cxfrs route as the sample on camel-cxfrs page:
http://camel.apache.org/cxfrs.html

  <cxf:rsServer id="rsServer" address="http://localhost:9080/CxfRsRouterTest/route"
    serviceClass="org.apache.camel.component.cxf.jaxrs.testbean.CustomerService" 
    loggingFeatureEnabled="true" loggingSizeLimit="20" skipFaultLogging="true"/>

  <cxf:rsClient id="rsClient" address="http://localhost:9081/CxfRsRouterTest/rest"
    serviceClass="org.apache.camel.component.cxf.jaxrs.testbean.CustomerService"
    loggingFeatureEnabled="true" skipFaultLogging="true"/>

  <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
    <route>
       <from uri="cxfrs://bean://rsServer"/>
       <to uri="cxfrs://bean://rsClient"/>
    </route>
  </camelContext>

However, the uri from my request is being directly translated to the address request out to my external service. For instance, a HTTP GET request:
http://localhost:9080/CxfRsRouterTest/route/customerservice/customers/123
will be sent out via cxfrs rsClient endpoint as:
http://localhost:9081/CxfRsRouterTest/rest/customerservice/customers/123

Is there a way to change it? If yes, can it be changed dynamically?

Resolution

You can change it by setting Camel Exchange header "CamelHttpPath":

   <route>
       <from uri="cxfrs://bean://rsServer"/>
       <setHeader headerName="CamelHttpPath">
           <constant>/customerservice/customers/124</constant>
       </setHeader>
       <to uri="cxfrs://bean://rsClient"/>
    </route>

If you would like to dynamically change the out request uri, then you could use camel expression language or supported scripting language. For instance, if you have an ID in the payload and you would like to change uri based on the ID, then you could use camel simple expression language:

    <route>
       <from uri="cxfrs://bean://rsServer"/>
       <setHeader headerName="CamelHttpPath">
           <simple>/customerservice/customers/${body.id}</simple>
       </setHeader>
       <to uri="cxfrs://bean://rsClient"/>
    </route>

giving that the IN message body contains a POJO which has a getId() method.

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.