camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > CXFRS
Date Sun, 15 Jan 2012 21:31:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CAMEL/CXFRS">CXFRS</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~bibryam">Bilgin
Ibryam</a>
    </h4>
        <br/>
                         <h4>Changes (1)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{snippet:id=HttpExample|lang=java|url=camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducerTest.java}
<br>From Camel 2.1, we also support to specify the query parameters from cxfrs URI for
the CXFRS http centric client. <br></td></tr>
            <tr><td class="diff-changed-lines" >{snippet:id=QueryExample|lang=java|url=camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducerTest.java}To
support the Dynamical routing, you can override the URI&#39;s query parameters by using
the CxfConstants.CAMEL_CXF_RS_QUERY_MAP header to set the parameter map for <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">it.To
support the Dynamical routing, you can override the URI&#39;s query parameters by using
the CxfConstants.CAMEL_CXF_RS_QUERY_MAP header to set the parameter map for</span> it.
<br></td></tr>
            <tr><td class="diff-unchanged" >{snippet:id=QueryMapExample|lang=java|url=camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducerTest.java}
<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="CXFRS-CXFRSComponent"></a>CXFRS Component</h2>

<div class='panelMacro'><table class='noteMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/warning.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td>When using CXF as a consumer,
the <a href="/confluence/display/CAMEL/CXF+Bean+Component" title="CXF Bean Component">CXF
Bean Component</a> allows you to factor out how message payloads are received from their
processing as a RESTful or SOAP web service. This has the potential of using a multitude of
transports to consume web services. The bean component's configuration is also simpler and
provides the fastest method to implement web services using Camel and CXF.</td></tr></table></div>

<p>The <b>cxfrs:</b> component provides integration with <a href="http://incubator.apache.org/cxf/"
class="external-link" rel="nofollow">Apache CXF</a> for connecting to JAX-RS services
hosted in CXF.</p>

<p>Maven users will need to add the following dependency to their pom.xml for this component:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;dependency&gt;</span>
   <span class="code-tag">&lt;groupId&gt;</span>org.apache.camel<span
class="code-tag">&lt;/groupId&gt;</span>
   <span class="code-tag">&lt;artifactId&gt;</span>camel-cxf<span class="code-tag">&lt;/artifactId&gt;</span>
   <span class="code-tag">&lt;version&gt;</span>x.x.x<span class="code-tag">&lt;/version&gt;</span>
 <span class="code-tag"><span class="code-comment">&lt;!-- use the same version
as your Camel core version --&gt;</span></span>
<span class="code-tag">&lt;/dependency&gt;</span>
</pre>
</div></div>

<h3><a name="CXFRS-URIformat"></a>URI format</h3>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
cxfrs:<span class="code-comment">//address?options</span>
</pre>
</div></div>
<p>Where <b>address</b> represents the CXF endpoint's address</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
cxfrs:bean:rsEndpoint
</pre>
</div></div>
<p>Where <b>rsEndpoint</b> represents the spring bean's name which presents
the CXFRS client or server</p>

<p>For either style above, you can append options to the URI as follows:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
cxfrs:bean:cxfEndpoint?resourceClass=org.apache.camel.rs.Example
</pre>
</div></div>

<h3><a name="CXFRS-Options"></a>Options</h3>
<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Description </th>
<th class='confluenceTh'> Example </th>
<th class='confluenceTh'> Required? </th>
<th class='confluenceTh'> default value </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>resourceClasses</tt> </td>
<td class='confluenceTd'> The resource classes which you want to export as REST service
</td>
<td class='confluenceTd'> <tt>resourceClasses =org.apache.camel.rs.Example1,org.apache.camel.rs.Exchange2</tt>
</td>
<td class='confluenceTd'> No </td>
<td class='confluenceTd'> <em>None</em> </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>httpClientAPI</tt> </td>
<td class='confluenceTd'> <b>new to Camel 2.1</b> If it is true, the CxfRsProducer
will use the HttpClientAPI to invoke the service <br class="atl-forced-newline" />
If it is false, the CxfRsProducer will use the ProxyClientAPI to invoke the service </td>
<td class='confluenceTd'> httpClientAPI=true </td>
<td class='confluenceTd'> No </td>
<td class='confluenceTd'> <em>true</em> </td>
</tr>
<tr>
<td class='confluenceTd'> synchronous </td>
<td class='confluenceTd'> New in 2.5, this option will let CxfRsConsumer decide to use
sync or async API to do the underlying work. The default value is false which means it will
try to use async API by default. </td>
<td class='confluenceTd'> synchronous=true </td>
<td class='confluenceTd'> No </td>
<td class='confluenceTd'> false </td>
</tr>
<tr>
<td class='confluenceTd'> throwExceptionOnFailure </td>
<td class='confluenceTd'> New in 2.6, this option tells the CxfRsProducer to inspect
return codes and will generate an Exception if the return code is larger than 207. </td>
<td class='confluenceTd'> throwExceptionOnFailure=true </td>
<td class='confluenceTd'> No </td>
<td class='confluenceTd'> true </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>maxClientCacheSize</tt> </td>
<td class='confluenceTd'> New in 2.6, you can set a IN message header CamelDestinationOverrideUrl
to dynamically override the target destination Web Service or REST Service defined in your
routes.&nbsp; The implementation caches CXF clients or ClientFactoryBean in CxfProvider
and CxfRsProvider.&nbsp; This option allows you to configure the maximum size of the cache.
<br class="atl-forced-newline" /> </td>
<td class='confluenceTd'> maxClientCacheSize=5 </td>
<td class='confluenceTd'> No <br class="atl-forced-newline" /> </td>
<td class='confluenceTd'> 10 </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>setDefaultBus</tt> </td>
<td class='confluenceTd'> New in 2.9.0. Will set the default bus when CXF endpoint create
a bus by itself </td>
<td class='confluenceTd'> <tt>setDefaultBus=true</tt> </td>
<td class='confluenceTd'> No </td>
<td class='confluenceTd'> <tt>false</tt> </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>bus</tt> </td>
<td class='confluenceTd'> New in 2.9.0. A default bus created by CXF Bus Factory. Use
<tt>&#35;</tt> notation to reference a bus object from the registry. The referenced
object must be an instance of <tt>org.apache.cxf.Bus</tt>. </td>
<td class='confluenceTd'> <tt>bus=#busName</tt> </td>
<td class='confluenceTd'> No </td>
<td class='confluenceTd'> <em>None</em> </td>
</tr>
</tbody></table>
</div>
</div>

<p>You can also configure the CXF REST endpoint through the spring configuration. Since
there are lots of difference between the CXF REST client and CXF REST Server, we provides
different configuration for them.<br/>
Please check out the <a href="http://svn.apache.org/repos/asf/camel/trunk/components/camel-cxf/src/main/resources/schema/cxfEndpoint.xsd"
class="external-link" rel="nofollow">schema file</a> and <a href="http://cwiki.apache.org/CXF20DOC/jax-rs.html"
class="external-link" rel="nofollow">CXF REST user guide</a> for more information.</p>

<h3><a name="CXFRS-HowtoconfiguretheRESTendpointinCamel"></a>How to configure
the REST endpoint in Camel</h3>

<p>In <a href="http://svn.apache.org/repos/asf/camel/trunk/components/camel-cxf/src/main/resources/schema/cxfEndpoint.xsd"
class="external-link" rel="nofollow">camel-cxf schema file</a>, there are two elements
for the REST endpoint definition. <b>cxf:rsServer</b> for REST consumer, <b>cxf:rsClient</b>
for REST producer.<br/>
You can find an camel REST service route configuration example here.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">&lt;beans xmlns=<span class="code-quote">"http://www.springframework.org/schema/beans"</span>
       <span class="code-keyword">xmlns:xsi</span>=<span class="code-quote">"http://www.w3.org/2001/XMLSchema-instance"</span>
       <span class="code-keyword">xmlns:cxf</span>=<span class="code-quote">"http://camel.apache.org/schema/cxf"</span>
       <span class="code-keyword">xmlns:jaxrs</span>=<span class="code-quote">"http://cxf.apache.org/jaxrs"</span>
       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://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
    "&gt;

    <span class="code-tag">&lt;bean class=<span class="code-quote">"org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"</span>/&gt;</span>

  <span class="code-tag"><span class="code-comment">&lt;!-- Defined the real
JAXRS back end service  --&gt;</span></span>
  &lt;jaxrs:server id=<span class="code-quote">"restService"</span>
		        address=<span class="code-quote">"http://localhost:${CXFTestSupport.port2}/CxfRsRouterTest/rest"</span>

		        staticSubresourceResolution=<span class="code-quote">"true"</span>&gt;
    <span class="code-tag">&lt;jaxrs:serviceBeans&gt;</span>
      <span class="code-tag">&lt;ref bean=<span class="code-quote">"customerService"</span>/&gt;</span>
    <span class="code-tag">&lt;/jaxrs:serviceBeans&gt;</span>       
  <span class="code-tag">&lt;/jaxrs:server&gt;</span>
  
  <span class="code-tag"><span class="code-comment">&lt;!--  bean id=<span
class="code-quote">"jsonProvider"</span> class=<span class="code-quote">"org.apache.cxf.jaxrs.provider.JSONProvider"</span>/--&gt;</span></span>

  <span class="code-tag">&lt;bean id=<span class="code-quote">"customerService"</span>
class=<span class="code-quote">"org.apache.camel.component.cxf.jaxrs.testbean.CustomerService"</span>
/&gt;</span>
   
  <span class="code-tag"><span class="code-comment">&lt;!-- Defined the server
endpoint to create the cxf-rs consumer --&gt;</span></span> 
  &lt;cxf:rsServer id=<span class="code-quote">"rsServer"</span> address=<span
class="code-quote">"http://localhost:${CXFTestSupport.port1}/CxfRsRouterTest/route"</span>
    serviceClass=<span class="code-quote">"org.apache.camel.component.cxf.jaxrs.testbean.CustomerService"</span>

    loggingFeatureEnabled=<span class="code-quote">"true"</span> loggingSizeLimit=<span
class="code-quote">"20"</span>/&gt;

  <span class="code-tag"><span class="code-comment">&lt;!-- Defined the client
endpoint to create the cxf-rs consumer --&gt;</span></span>
  &lt;cxf:rsClient id=<span class="code-quote">"rsClient"</span> address=<span
class="code-quote">"http://localhost:${CXFTestSupport.port2}/CxfRsRouterTest/rest"</span>
    serviceClass=<span class="code-quote">"org.apache.camel.component.cxf.jaxrs.testbean.CustomerService"</span>
    loggingFeatureEnabled=<span class="code-quote">"true"</span> /&gt;
  
  <span class="code-tag"><span class="code-comment">&lt;!-- The camel route
context --&gt;</span></span>
  <span class="code-tag">&lt;camelContext id=<span class="code-quote">"camel"</span>
xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>
    <span class="code-tag">&lt;route&gt;</span>
       <span class="code-tag">&lt;from uri=<span class="code-quote">"cxfrs://bean://rsServer"</span>/&gt;</span>
       <span class="code-tag"><span class="code-comment">&lt;!-- We can remove
this configure as the CXFRS producer is using the HttpAPI by default --&gt;</span></span>
       <span class="code-tag">&lt;setHeader headerName=<span class="code-quote">"CamelCxfRsUsingHttpAPI"</span>&gt;</span>
         <span class="code-tag">&lt;constant&gt;</span>True<span class="code-tag">&lt;/constant&gt;</span>
       
       <span class="code-tag">&lt;/setHeader&gt;</span>
       <span class="code-tag">&lt;to uri=<span class="code-quote">"cxfrs://bean://rsClient"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>
  <span class="code-tag">&lt;/camelContext&gt;</span>
  
<span class="code-tag">&lt;/beans&gt;</span>
</pre>
</div></div>

<h3><a name="CXFRS-HowtoconsumetheRESTrequestinCamel"></a>How to consume
the REST request in Camel</h3>

<p><a href="http://cwiki.apache.org/CXF20DOC/jax-rs.html" class="external-link" rel="nofollow">CXF
JAXRS front end</a> implements the <a href="https://jsr311.dev.java.net/" class="external-link"
rel="nofollow">JAXRS(JSR311) API</a>, so we can export the resources classes as a
REST service. And we leverage the <a href="http://cwiki.apache.org/confluence/display/CXF20DOC/Invokers"
class="external-link" rel="nofollow">CXF Invoker API</a> to turn a REST request into
a normal Java object method invocation.<br/>
Unlike the <tt>camel-restlet</tt>, you don't need to specify the URI template
within your restlet endpoint, CXF take care of the REST request URI to resource class method
mapping according to the JSR311 specification. All you need to do in Camel is delegate this
method request to a right processor or endpoint.</p>

<p>Here is an example of a CXFRS route...</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">private</span> <span
class="code-keyword">static</span> <span class="code-keyword">final</span>
<span class="code-object">String</span> CXF_RS_ENDPOINT_URI = <span class="code-quote">"cxfrs:<span
class="code-comment">//http://localhost:"</span> + CXT + <span class="code-quote">"/<span
class="code-keyword">rest</span>?resourceClasses=org.apache.camel.component.cxf.jaxrs.testbean.CustomerServiceResource"</span>;
</span>
<span class="code-keyword">protected</span> RouteBuilder createRouteBuilder()
<span class="code-keyword">throws</span> Exception {
    <span class="code-keyword">return</span> <span class="code-keyword">new</span>
RouteBuilder() {
        <span class="code-keyword">public</span> void configure() {
            errorHandler(<span class="code-keyword">new</span> NoErrorHandlerBuilder());
            from(CXF_RS_ENDPOINT_URI).process(<span class="code-keyword">new</span>
Processor() {

                <span class="code-keyword">public</span> void process(Exchange
exchange) <span class="code-keyword">throws</span> Exception {
                    Message inMessage = exchange.getIn();                        
                    <span class="code-comment">// Get the operation name from in message
</span>                    <span class="code-object">String</span> operationName
= inMessage.getHeader(CxfConstants.OPERATION_NAME, <span class="code-object">String</span>.class);
                    <span class="code-keyword">if</span> (<span class="code-quote">"getCustomer"</span>.equals(operationName))
{
                        <span class="code-object">String</span> httpMethod = inMessage.getHeader(Exchange.HTTP_METHOD,
<span class="code-object">String</span>.class);
                        assertEquals(<span class="code-quote">"Get a wrong http method"</span>,
<span class="code-quote">"GET"</span>, httpMethod);
                        <span class="code-object">String</span> path = inMessage.getHeader(Exchange.HTTP_PATH,
<span class="code-object">String</span>.class);
                        <span class="code-comment">// The parameter of the invocation
is stored in the body of in message
</span>                        <span class="code-object">String</span> id
= inMessage.getBody(<span class="code-object">String</span>.class);
                        <span class="code-keyword">if</span> (<span class="code-quote">"/customerservice/customers/126"</span>.equals(path))
{                            
                            Customer customer = <span class="code-keyword">new</span>
Customer();
                            customer.setId(<span class="code-object">Long</span>.parseLong(id));
                            customer.setName(<span class="code-quote">"Willem"</span>);
                            <span class="code-comment">// We just put the response <span
class="code-object">Object</span> into the out message body
</span>                            exchange.getOut().setBody(customer);
                        } <span class="code-keyword">else</span> {
                            <span class="code-keyword">if</span> (<span class="code-quote">"/customerservice/customers/400"</span>.equals(path))
{
                                <span class="code-comment">// We <span class="code-keyword">return</span>
the remote client IP address <span class="code-keyword">this</span> time
</span>                                org.apache.cxf.message.Message cxfMessage = inMessage.getHeader(CxfConstants.CAMEL_CXF_MESSAGE,
org.apache.cxf.message.Message.class);
                                ServletRequest request = (ServletRequest) cxfMessage.get(<span
class="code-quote">"HTTP.REQUEST"</span>);
                                <span class="code-object">String</span> remoteAddress
= request.getRemoteAddr();
                                Response r = Response.status(200).entity(<span class="code-quote">"The
remoteAddress is "</span> + remoteAddress).build();
                                exchange.getOut().setBody(r);
                                <span class="code-keyword">return</span>;
                            }
                            <span class="code-keyword">if</span> (<span class="code-quote">"/customerservice/customers/123"</span>.equals(path))
{
                                <span class="code-comment">// send a customer response
back
</span>                                Response r = Response.status(200).entity(<span
class="code-quote">"customer response back!"</span>).build();
                                exchange.getOut().setBody(r);
                                <span class="code-keyword">return</span>;
                            }
                            <span class="code-keyword">if</span> (<span class="code-quote">"/customerservice/customers/456"</span>.equals(path))
{
                                Response r = Response.status(404).entity(<span class="code-quote">"Can't
found the customer with uri "</span> + path).build();
                                <span class="code-keyword">throw</span> <span
class="code-keyword">new</span> WebApplicationException(r);
                            } <span class="code-keyword">else</span> {
                                <span class="code-keyword">throw</span> <span
class="code-keyword">new</span> RuntimeCamelException(<span class="code-quote">"Can't
found the customer with uri "</span> + path);
                            }
                        }
                    }
                    <span class="code-keyword">if</span> (<span class="code-quote">"updateCustomer"</span>.equals(operationName))
{
                        assertEquals(<span class="code-quote">"Get a wrong customer
message header"</span>, <span class="code-quote">"header1;header2"</span>,
inMessage.getHeader(<span class="code-quote">"test"</span>));
                        <span class="code-object">String</span> httpMethod = inMessage.getHeader(Exchange.HTTP_METHOD,
<span class="code-object">String</span>.class);
                        assertEquals(<span class="code-quote">"Get a wrong http method"</span>,
<span class="code-quote">"PUT"</span>, httpMethod);
                        Customer customer = inMessage.getBody(Customer.class);
                        assertNotNull(<span class="code-quote">"The customer should
not be <span class="code-keyword">null</span>."</span>, customer);
                        <span class="code-comment">// Now you can <span class="code-keyword">do</span>
what you want on the customer object
</span>                        assertEquals(<span class="code-quote">"Get a wrong
customer name."</span>, <span class="code-quote">"Mary"</span>, customer.getName());
                        <span class="code-comment">// set the response back
</span>                        exchange.getOut().setBody(Response.ok().build());
                    }
                    
                }
                
            });
        }
    };
}
</pre>
</div></div>

<p>And the corresponding resource class used to configure the endpoint...</p>

<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td><b>note about
the resource class</b><br />This class is used to configure the JAXRS properties
ONLY.  The methods will NOT be executed during the routing of messages to the endpoint, the
route itself is responsible for ALL processing instead.</td></tr></table></div>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">@Path(<span class="code-quote">"/customerservice/"</span>)
<span class="code-keyword">public</span> class CustomerServiceResource {

    <span class="code-keyword">public</span> CustomerServiceResource() {
    }

    @GET
    @Path(<span class="code-quote">"/customers/{id}/"</span>)
    <span class="code-keyword">public</span> Customer getCustomer(@PathParam(<span
class="code-quote">"id"</span>) <span class="code-object">String</span>
id) {
        <span class="code-keyword">return</span> <span class="code-keyword">null</span>;
    }

    @PUT
    @Path(<span class="code-quote">"/customers/"</span>)
    <span class="code-keyword">public</span> Response updateCustomer(Customer
customer) {
        <span class="code-keyword">return</span> <span class="code-keyword">null</span>;
    }
}
</pre>
</div></div>

<h3><a name="CXFRS-HowtoinvoketheRESTservicethroughcamelcxfrsproducer"></a>How
to invoke the REST service through camel-cxfrs producer</h3>

<p><a href="http://cwiki.apache.org/CXF20DOC/jax-rs.html" class="external-link" rel="nofollow">CXF
JAXRS front end</a> implements <a href="http://cwiki.apache.org/CXF20DOC/jax-rs.html#JAX-RS-ProxybasedAPI"
class="external-link" rel="nofollow">a proxy based client API</a>, with this API
you can invoke the remote REST service through a proxy.<br/>
<tt>camel-cxfrs</tt> producer is based on this <a href="http://cwiki.apache.org/CXF20DOC/jax-rs.html#JAX-RS-ProxybasedAPI"
class="external-link" rel="nofollow">proxy API</a>.<br/>
So, you just need to specify the operation name in the message header and prepare the parameter
in the message body, camel-cxfrs producer will generate right REST request for you.</p>

<p>Here is an example</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">Exchange exchange = template.send(<span class="code-quote">"direct:<span
class="code-comment">//proxy"</span>, <span class="code-keyword">new</span>
Processor() {
</span>    <span class="code-keyword">public</span> void process(Exchange
exchange) <span class="code-keyword">throws</span> Exception {
        exchange.setPattern(ExchangePattern.InOut);
        Message inMessage = exchange.getIn();
        setupDestinationURL(inMessage);
        <span class="code-comment">// set the operation name 
</span>        inMessage.setHeader(CxfConstants.OPERATION_NAME, <span class="code-quote">"getCustomer"</span>);
        <span class="code-comment">// using the proxy client API
</span>        inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API, <span
class="code-object">Boolean</span>.FALSE);
        <span class="code-comment">// set the parameters , <span class="code-keyword">if</span>
you just have one parameter 
</span>        <span class="code-comment">// camel will put <span class="code-keyword">this</span>
object into an <span class="code-object">Object</span>[] itself
</span>        inMessage.setBody(<span class="code-quote">"123"</span>);
    }
});
     
<span class="code-comment">// get the response message 
</span>Customer response = (Customer) exchange.getOut().getBody();

assertNotNull(<span class="code-quote">"The response should not be <span class="code-keyword">null</span>
"</span>, response);
assertEquals(<span class="code-quote">"Get a wrong customer id "</span>, <span
class="code-object">String</span>.valueOf(response.getId()), <span class="code-quote">"123"</span>);
assertEquals(<span class="code-quote">"Get a wrong customer name"</span>, response.getName(),
<span class="code-quote">"John"</span>);
assertEquals(<span class="code-quote">"Get a wrong response code"</span>, 200,
exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE));
</pre>
</div></div>
<p><a href="http://cwiki.apache.org/CXF20DOC/jax-rs.html" class="external-link" rel="nofollow">CXF
JAXRS front end</a> also provides <a href="http://cxf.apache.org/docs/jax-rs.html#JAX-RS-HTTPcentricclients"
class="external-link" rel="nofollow">a http centric client API</a>, You can also
invoke this API from <tt>camel-cxfrs</tt> producer. You need to specify the HTTP_PATH
and Http method and let the the producer know to use the http centric client by using the
URI option <b>httpClientAPI</b> or set the message header with CxfConstants.CAMEL_CXF_RS_USING_HTTP_API.
You can turn the response object to the type class that you specify with CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">Exchange exchange = template.send(<span class="code-quote">"direct:<span
class="code-comment">//http"</span>, <span class="code-keyword">new</span>
Processor() {
</span>    <span class="code-keyword">public</span> void process(Exchange
exchange) <span class="code-keyword">throws</span> Exception {
        exchange.setPattern(ExchangePattern.InOut);
        Message inMessage = exchange.getIn();
        setupDestinationURL(inMessage);
        <span class="code-comment">// using the http central client API
</span>        inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API, <span
class="code-object">Boolean</span>.TRUE);
        <span class="code-comment">// set the Http method
</span>        inMessage.setHeader(Exchange.HTTP_METHOD, <span class="code-quote">"GET"</span>);
        <span class="code-comment">// set the relative path
</span>        inMessage.setHeader(Exchange.HTTP_PATH, <span class="code-quote">"/customerservice/customers/123"</span>);
               
        <span class="code-comment">// Specify the response class , cxfrs will use InputStream
as the response object type 
</span>        inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS, Customer.class);
        <span class="code-comment">// since we use the Get method, so we don't need
to set the message body
</span>        inMessage.setBody(<span class="code-keyword">null</span>);
               
    }
});
     
<span class="code-comment">// get the response message 
</span>Customer response = (Customer) exchange.getOut().getBody();

assertNotNull(<span class="code-quote">"The response should not be <span class="code-keyword">null</span>
"</span>, response);
assertEquals(<span class="code-quote">"Get a wrong customer id "</span>, <span
class="code-object">String</span>.valueOf(response.getId()), <span class="code-quote">"123"</span>);
assertEquals(<span class="code-quote">"Get a wrong customer name"</span>, response.getName(),
<span class="code-quote">"John"</span>);
assertEquals(<span class="code-quote">"Get a wrong response code"</span>, 200,
exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE));
</pre>
</div></div>
<p>From Camel 2.1, we also support to specify the query parameters from cxfrs URI for
the CXFRS http centric client.<br/>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">Exchange exchange = template.send(<span class="code-quote">"cxfrs:<span
class="code-comment">//http://localhost:"</span> + getPort2() + <span class="code-quote">"/"</span>
+ getClass().getSimpleName() + <span class="code-quote">"/testQuery?httpClientAPI=<span
class="code-keyword">true</span>&amp;q1=12&amp;q2=13"</span></span>
</pre>
</div></div>To support the Dynamical routing, you can override the URI's query
parameters by using the CxfConstants.CAMEL_CXF_RS_QUERY_MAP header to set the parameter map
for it.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">Map&lt;<span class="code-object">String</span>,
<span class="code-object">String</span>&gt; queryMap = <span class="code-keyword">new</span>
LinkedHashMap&lt;<span class="code-object">String</span>, <span class="code-object">String</span>&gt;();
                   
queryMap.put(<span class="code-quote">"q1"</span>, <span class="code-quote">"<span
class="code-keyword">new</span>"</span>);
queryMap.put(<span class="code-quote">"q2"</span>, <span class="code-quote">"world"</span>);
                   
inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_QUERY_MAP, queryMap);
</pre>
</div></div>
    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/CXFRS">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=119513&revisedVersion=19&originalVersion=18">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/CXFRS?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message