camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > CXF
Date Sun, 08 Aug 2010 11:25:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1810/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/CXF">CXF</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~christian%2Bschneider">Christian Schneider</a>
    </h4>
        <div id="versionComment">
        <b>Comment:</b>
        Moved CXF Bean Component to a separate page<br />
    </div>
        <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" >*Message Mode:* Attachments are not supported as it does not process the message at all. <br> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;"> <br>h2. CXF Bean Component (*2.0 or later*) <br> <br>The *cxfbean:* component allows other Camel endpoints to send exchange and invoke Web service bean objects.  (*Currently, it only supports JAXRS, JAXWS(new to camel2.1) annotated service bean.*) <br> <br>*Note*: {{CxfBeanEndpoint}} is a {{ProcessorEndpoint}} so it has no consumers.  It works similarly to a Bean component. <br> <br>h3. URI format <br> <br>{code} <br>cxfbean:serviceBeanRef <br>{code} <br>Where *serviceBeanRef* is a registry key to look up the service bean object. If {{serviceBeanRef}} references a {{List}} object, elements of the {{List}} are the service bean objects accepted by the endpoint. <br> <br>h3. Options <br>{div:class=confluenceTableSmall} <br>|| Name || Description || Example || Required? || Default Value || <br>| {{cxfBeanBinding}} | CXF bean binding specified by the {{\#}} notation.  The referenced object must be an instance of {{org.apache.camel.component.cxf.cxfbean.CxfBeanBinding}}. | {{cxfBinding=#bindingName}} | No | An instance of {{org.apache.camel.component.cxf.cxfbean.DefaultCxfBeanBinding}} | <br>| {{bus}} | CXF bus reference specified by the {{\#}} notation. The referenced object must be an instance of {{org.apache.cxf.Bus}}. | {{bus=#busName}} | No | Default bus created by CXF Bus Factory | <br>| {{headerFilterStrategy}} | Header filter strategy specified by the {{\#}} notation.  The referenced object must be an instance of {{org.apache.camel.spi.HeaderFilterStrategy}}. | {{headerFilterStrategy=#strategyName}} | No | An instance of {{org.apache.camel.component.cxf.CxfHeaderFilterStrategy}} | <br>| {{setDefaultBus}} | Will set the default bus when CXF endpoint create a bus by itself. | {{true}}, {{false}} | No | {{false}} | <br>| {{populateFromClass}}\\ | Since 2.3, the wsdlLocation annotated in the POJO is ignored (by default) unless this option is set to&amp;nbsp; {{false.}} Prior to 2.3, the wsdlLocation annotated in the POJO is always honored and it is not possible to ignore.\\ | {{true}}, {{false}} | No | {{true}} | <br>{div} <br> <br>h3. Headers <br>{div:class=confluenceTableSmall} <br>|| Name || Description || Type || Required? || Default Value || In/Out || Examples || <br>| {{CamelHttpCharacterEncoding}} (before 2.0-m2: {{CamelCxfBeanCharacterEncoding}}) | Character encoding | {{String}} | No | None | In | ISO-8859-1 | <br>| {{CamelContentType}} (before 2.0-m2: {{CamelCxfBeanContentType}}) | Content type | {{String}} | No | \**/*\* | In | {{text/xml}} | <br>| CamelHttpBaseUri \\ <br>(2.0-m3 and before: {{CamelCxfBeanRequestBasePath}}) | The value of this header will be set in the CXF message as the {{Message.BASE_PATH}} property.  It is needed by CXF JAX-RS processing.  Basically, it is the scheme, host and port portion of the request URI. | {{String}} | Yes | The Endpoint URI of the source endpoint in the Camel exchange | In | [http://localhost:9000] | <br>| {{CamelHttpPath}} (before 2.0-m2: {{CamelCxfBeanRequestPat{}}}h) | Request URI&#39;s path | {{String}} | Yes | None | In | {{consumer/123}} | <br>| {{CamelHttpMethod}} (before 2.0-m2: {{CamelCxfBeanVerb}}) | RESTful request verb | {{String}} | Yes | None | In | {{GET}}, {{PUT}}, {{POST}}, {{DELETE}} | <br>| {{CamelHttpResponseCode}} | HTTP response code | {{Integer}} | No \\ | None | Out \\ | 200 \\ | <br>{div} <br>*Note: Currently, CXF Bean component has (only) been tested with Jetty HTTP component it can understand headers from Jetty HTTP component without requiring conversion.* <br> <br>h3. A Working Sample <br> <br>This sample shows how to create a route that starts a Jetty HTTP server.  The route sends requests to a CXF Bean and invokes a JAXRS annotated service. <br> <br>First, create a route as follows.  The {{from}} endpoint is a Jetty HTTP endpoint that is listening on port 9000.  Notice that the {{matchOnUriPrefix}} option must be set to {{true}} because RESTful request URI will not match the endpoint&#39;s URI http:­//localhost:9000 exactly. <br>{snippet:id=routeDefinition|lang=xml|url=camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/cxfbean/CxfBeanTest-context.xml} <br>The {{to}} endpoint is a CXF Bean with bean name {{customerServiceBean}}. The name will be looked up from the registry.  Next, we make sure our service bean is available in Spring registry.  We create a bean definition in the Spring configuration.  In this example, we create a List of service beans (of one element).  We could have created just a single bean without a List. <br>{snippet:id=beanDefinition|lang=xml|url=camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/cxfbean/CxfBeanTest-context.xml} <br>That&#39;s it.  Once the route is started, the web service is ready for business.  A HTTP client can make a request and receive response. <br>{snippet:id=clientInvocation|lang=java|url=camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/cxfbean/CxfBeanTest.java} <br> <br> <br></td></tr>
            <tr><td class="diff-unchanged" >{include:Endpoint See Also} <br></td></tr>
        </table>
</div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="CXF-CXFComponent"></a>CXF Component</h2>

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

<div>
<ul>
    <li><a href='#CXF-CXFComponent'>CXF Component</a></li>
<ul>
    <li><a href='#CXF-URIformat'>URI format</a></li>
    <li><a href='#CXF-Options'>Options</a></li>
<ul>
    <li><a href='#CXF-Thedescriptionsofthedataformats'>The descriptions of the dataformats</a></li>
<ul>
    <li><a href='#CXF-HowtoenableCXF%27sLoggingOutInterceptorinMESSAGEmode'>How to enable CXF's LoggingOutInterceptor in MESSAGE mode</a></li>
</ul>
    <li><a href='#CXF-DescriptionofrelayHeadersoption'>Description of relayHeaders option</a></li>
<ul>
    <li><a href='#CXF-AvailableinRelease1.6.1andafter%28onlyinPOJOmode%29'>Available in Release 1.6.1 and after (only in POJO mode)</a></li>
    <li><a href='#CXF-ChangessinceRelease2.0'>Changes since Release 2.0</a></li>
</ul>
</ul>
    <li><a href='#CXF-ConfiguretheCXFendpointswithSpring'>Configure the CXF endpoints with Spring</a></li>
    <li><a href='#CXF-Howtomakethecamelcxfcomponentuselog4jinsteadofjava.util.logging'>How to make the camel-cxf component use log4j instead of java.util.logging</a></li>
    <li><a href='#CXF-Howtoletcamelcxfresponsemessagewithxmlstartdocument'>How to let camel-cxf response message with xml start document</a></li>
    <li><a href='#CXF-HowtoconsumeamessagefromacamelcxfendpointinPOJOdataformat'>How to consume a message from a camel-cxf endpoint in POJO data format</a></li>
    <li><a href='#CXF-HowtopreparethemessageforthecamelcxfendpointinPOJOdataformat'>How to prepare the message for the camel-cxf endpoint in POJO data format</a></li>
    <li><a href='#CXF-HowtodealwiththemessageforacamelcxfendpointinPAYLOADdataformat'>How to deal with the message for a camel-cxf endpoint in PAYLOAD data format</a></li>
    <li><a href='#CXF-HowtogetandsetSOAPheadersinPOJOmode'>How to get and set SOAP headers in POJO mode</a></li>
    <li><a href='#CXF-HowtogetandsetSOAPheadersinPAYLOADmode'>How to get and set SOAP headers in PAYLOAD mode</a></li>
    <li><a href='#CXF-SOAPheadersarenotavailableinMESSAGEmode'>SOAP headers are not available in MESSAGE mode</a></li>
    <li><a href='#CXF-HowtothrowaSOAPFaultfromCamel'>How to throw a SOAP Fault from Camel</a></li>
    <li><a href='#CXF-Howtopropagateacamelcxfendpoint%27srequestandresponsecontext'>How to propagate a camel-cxf endpoint's request and response context</a></li>
    <li><a href='#CXF-AttachmentSupport'>Attachment Support</a></li>
    <li><a href='#CXF-SeeAlso'>See Also</a></li>
</ul>
</ul></div>

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

<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="/confluence/images/icons/emoticons/check.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td><b>CXF dependencies</b><br />If you want to learn about CXF dependencies you can checkout the <tt><a href="http://svn.apache.org/repos/asf/cxf/trunk/distribution/src/main/release/lib/WHICH_JARS" class="external-link" rel="nofollow">WHICH-JARS</a></tt> text file.</td></tr></table></div>

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

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
cxf:bean:cxfEndpoint[?options]
</pre>
</div></div>
<p>Where <b>cxfEndpoint</b> represents a bean ID that references a bean in the Spring bean registry. With this URI format, most of the endpoint details are specified in the bean definition.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
cxf:<span class="code-comment">//someAddress[?options]</span>
</pre>
</div></div>
<p>Where <b>someAddress</b> specifies the CXF endpoint's address. With this URI format, most of the endpoint details are specified using options.</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">
cxf:bean:cxfEndpoint?wsdlURL=wsdl/hello_world.wsdl&amp;dataFormat=PAYLOAD
</pre>
</div></div>

<h3><a name="CXF-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>wsdlURL</tt> </td>
<td class='confluenceTd'> The location of the WSDL. </td>
<td class='confluenceTd'> <a href="file://local/wsdl/hello.wsdl" class="external-link" rel="nofollow">file://local/wsdl/hello.wsdl</a> or <tt>wsdl/hello.wsdl</tt> </td>
<td class='confluenceTd'> No </td>
<td class='confluenceTd'> WSDL is obtained from endpoint address by default. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>serviceClass</tt> </td>
<td class='confluenceTd'> The name of the SEI (Service Endpoint Interface) class.  This class can have, but does not require, JSR181 annotations.&nbsp; <br class="atl-forced-newline" />
<b>Since 2.0,</b> this option is only required by POJO mode.&nbsp;&nbsp; If the wsdlURL option is provided, serviceClass is not required for PAYLOAD and MESSAGE mode.&nbsp; When wsdlURL option is used without serviceClass, the serviceName and portName (endpointName for Spring configuration) options <b>MUST</b> be provided. <br class="atl-forced-newline" />
<br class="atl-forced-newline" />
<b>Since 2.0</b>, it is possible to use <tt>&#35;</tt> notation to reference a <tt>serviceClass</tt> object instance from the registry.&nbsp; E.g. <tt>serviceClass=#beanName</tt>. <br class="atl-forced-newline" />
<br class="atl-forced-newline" />
<b>Please be advised that the referenced object cannot be a Proxy (Spring AOP Proxy is OK)</b> as it relies on <tt>Object.getClass().getName()</tt> method for non Spring AOP Proxy. <br class="atl-forced-newline" /> </td>
<td class='confluenceTd'> <tt>org.apache.camel.Hello</tt> </td>
<td class='confluenceTd'> Yes </td>
<td class='confluenceTd'>&nbsp;</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>serviceClassInstance</tt> </td>
<td class='confluenceTd'> <b>In 1.6 or later (will be deprecated in 2.0)</b>, <tt>serviceClassInstance</tt> works like <tt>serviceClass=#beanName</tt>, which looks up a <tt>serviceObject</tt> instance from the registry. </td>
<td class='confluenceTd'> <tt>serviceClassInstance=beanName</tt> </td>
<td class='confluenceTd'> No (use either <tt>serviceClass</tt> or <tt>serviceClassInstance</tt>) </td>
<td class='confluenceTd'>&nbsp;</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>serviceName</tt> </td>
<td class='confluenceTd'> The service name this service is implementing, it maps to the <tt>wsdl:service@name</tt>. </td>
<td class='confluenceTd'> {http:­//org.apache.camel}ServiceName </td>
<td class='confluenceTd'> Only if more than one <tt>serviceName</tt> in WSDL present, and it is required for camel-cxf consumer since camel 2.2 </td>
<td class='confluenceTd'>&nbsp;</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>portName</tt> </td>
<td class='confluenceTd'> The port name this service is implementing, it maps to the <tt>wsdl:port@name</tt>. </td>
<td class='confluenceTd'> {http:­//org.apache.camel}PortName </td>
<td class='confluenceTd'> Only if more than one <tt>portName</tt> under the <tt>serviceName</tt> is present, and it is required for camel-cxf consumer since camel 2.2 </td>
<td class='confluenceTd'>&nbsp;</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>dataFormat</tt> </td>
<td class='confluenceTd'> Which data type messages the CXF endpoint supports </td>
<td class='confluenceTd'> <tt>POJO</tt>, <tt>PAYLOAD</tt>, <tt>MESSAGE</tt> </td>
<td class='confluenceTd'> No </td>
<td class='confluenceTd'> <tt>POJO</tt> </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>relayHeaders</tt> </td>
<td class='confluenceTd'> Available since 1.6.1. Please see the <b>Description of</b> <tt><b>relayHeaders</b></tt> <b>option</b> section for this option in 2.0.  Should a CXF endpoint relay headers along the route. Currently only available when <tt>dataFormat=POJO</tt> </td>
<td class='confluenceTd'> <tt>true</tt>, <tt>false</tt> </td>
<td class='confluenceTd'> No </td>
<td class='confluenceTd'> <tt>true</tt> </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>wrapped</tt> </td>
<td class='confluenceTd'> Which kind of operation that CXF  endpoint producer will invoke </td>
<td class='confluenceTd'> <tt>true</tt>, <tt>false</tt> </td>
<td class='confluenceTd'> No </td>
<td class='confluenceTd'> <tt>false</tt> </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>wrappedStyle</tt> </td>
<td class='confluenceTd'> <b>New in 2.5.0</b> The WSDL style that describes how parameters are represented in the SOAP body. If the value is false, CXF will chose the document-literal unwrapped style, If the value is true, CXF will chose the document-literal wrapped style </td>
<td class='confluenceTd'> <tt>true</tt>, <tt>false</tt> </td>
<td class='confluenceTd'> No </td>
<td class='confluenceTd'> <tt>Null</tt> </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>setDefaultBus</tt> </td>
<td class='confluenceTd'> Will set the default bus when CXF endpoint create a bus by itself </td>
<td class='confluenceTd'> <tt>true</tt>, <tt>false</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.0, 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>. <br class="atl-forced-newline" /> </td>
<td class='confluenceTd'> <tt>bus=#busName</tt> </td>
<td class='confluenceTd'> No </td>
<td class='confluenceTd'> Default bus created by CXF Bus Factory <br class="atl-forced-newline" /> </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>cxfBinding</tt> </td>
<td class='confluenceTd'> New in 2.0, use <tt>&#35;</tt> notation to reference a CXF binding object from the registry. The referenced object must be an instance of <tt>org.apache.camel.component.cxf.CxfBinding</tt>. </td>
<td class='confluenceTd'> <tt>cxfBinding=#bindingName</tt> <br class="atl-forced-newline" /> </td>
<td class='confluenceTd'> No <br class="atl-forced-newline" /> </td>
<td class='confluenceTd'> An instance of <tt>org.apache.camel.component.cxf.DefaultCxfBinding</tt> <br class="atl-forced-newline" /> </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>headerFilterStrategy</tt> </td>
<td class='confluenceTd'> New in 2.0, use <tt>&#35;</tt> notation to reference a header filter strategy object from the registry. The referenced object must be an instance of <tt>org.apache.camel.spi.HeaderFilterStrategy</tt>. </td>
<td class='confluenceTd'> <tt>headerFilterStrategy=#strategyName</tt> </td>
<td class='confluenceTd'> No <br class="atl-forced-newline" /> </td>
<td class='confluenceTd'> An instance of <tt>org.apache.camel.component.cxf.CxfHeaderFilterStrategy</tt> <br class="atl-forced-newline" /> </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>loggingFeatureEnabled</tt> </td>
<td class='confluenceTd'> New in 2.3, this option enables CXF Logging Feature which writes inbound and outbound SOAP messages to log. </td>
<td class='confluenceTd'> <tt>loggingFeatureEnabled</tt><tt>=true</tt> </td>
<td class='confluenceTd'> No <br class="atl-forced-newline" /> </td>
<td class='confluenceTd'> false <br class="atl-forced-newline" /> </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>defaultOperationName</tt> </td>
<td class='confluenceTd'> New in 2.4, this option will set the default operationName that will be used by the CxfProducer which invokes the remote service. </td>
<td class='confluenceTd'> <tt>defaultOperationName</tt><tt>=greetMe</tt> </td>
<td class='confluenceTd'> No <br class="atl-forced-newline" /> </td>
<td class='confluenceTd'> null </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>defaultOperationNameSpace</tt> </td>
<td class='confluenceTd'> New in 2.4, this option will set the default operationNamespace that will be used by the CxfProducer which invokes the remote service. </td>
<td class='confluenceTd'> <tt>defaultOperationNamespace</tt>{{=
<a href="http://apache.org/hello_world_soap_http" class="external-link" rel="nofollow">http://apache.org/hello_world_soap_http</a>
<p>}}</p></td>
<th class='confluenceTh'> No <br class="atl-forced-newline" /> </th>
<td class='confluenceTd'> null </td>
</tr>
</tbody></table>
</div>
</div>

<p>The <tt>serviceName</tt> and <tt>portName</tt> are <a href="http://en.wikipedia.org/wiki/QName" class="external-link" rel="nofollow">QNames</a>, so if you provide them be sure to prefix them with their {namespace} as shown in the examples above.</p>

<p><b>NOTE</b> From CAMEL 1.5.1 , the <tt>serviceClass</tt> for a CXF producer (that is, the <tt>to</tt> endpoint) should be a Java interface.</p>

<h4><a name="CXF-Thedescriptionsofthedataformats"></a>The descriptions of the dataformats</h4>

<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> DataFormat </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>POJO</tt> </td>
<td class='confluenceTd'> POJOs (Plain old Java objects) are the Java parameters to the method being invoked on the target server.  Both Protocol and Logical JAX-WS handlers are supported. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>PAYLOAD</tt> </td>
<td class='confluenceTd'> <tt>PAYLOAD</tt> is the message payload (the contents of the <tt>soap:body</tt>) after message configuration in the CXF endpoint is applied.  Only Protocol JAX-WS handler is supported.  Logical JAX-WS handler is not supported. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>MESSAGE</tt> </td>
<td class='confluenceTd'> <tt>MESSAGE</tt> is the raw message that is received from the transport layer.  JAX-WS handler is not supported. </td>
</tr>
</tbody></table>
</div>

<p>You can determine the data format mode of an exchange by retrieving the exchange property, <tt>CamelCXFDataFormat</tt>.  The exchange key constant is defined in <tt>org.apache.camel.component.cxf.CxfConstants.DATA_FORMAT_PROPERTY</tt>.</p></div>

<h5><a name="CXF-HowtoenableCXF%27sLoggingOutInterceptorinMESSAGEmode"></a>How to enable CXF's LoggingOutInterceptor in MESSAGE mode</h5>

<p>CXF's <tt>LoggingOutInterceptor</tt> outputs outbound message that goes on the wire to logging system (Java Util Logging).  Since the <tt>LoggingOutInterceptor</tt> is in <tt>PRE_STREAM</tt> phase (but <tt>PRE_STREAM</tt> phase is removed in <tt>MESSAGE</tt> mode), you have to configure <tt>LoggingOutInterceptor</tt> to be run during the <tt>WRITE</tt> phase.  The following is an example.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">   <span class="code-tag">&lt;bean id=<span class="code-quote">"loggingOutInterceptor"</span> class=<span class="code-quote">"org.apache.cxf.interceptor.LoggingOutInterceptor"</span>&gt;</span>
        <span class="code-tag"><span class="code-comment">&lt;!--  it really should have been user-prestream but CXF does have such phase! --&gt;</span></span>
        <span class="code-tag">&lt;constructor-arg value=<span class="code-quote">"write"</span>/&gt;</span> 
   <span class="code-tag">&lt;/bean&gt;</span>
   		
&lt;cxf:cxfEndpoint id=<span class="code-quote">"serviceEndpoint"</span> address=<span class="code-quote">"http://localhost:9002/helloworld"</span>
	serviceClass=<span class="code-quote">"org.apache.camel.component.cxf.HelloService"</span>&gt;
	<span class="code-tag">&lt;cxf:outInterceptors&gt;</span>
	    <span class="code-tag">&lt;ref bean=<span class="code-quote">"loggingOutInterceptor"</span>/&gt;</span>
	<span class="code-tag">&lt;/cxf:outInterceptors&gt;</span>
	<span class="code-tag">&lt;cxf:properties&gt;</span>
		<span class="code-tag">&lt;entry key=<span class="code-quote">"dataFormat"</span> value=<span class="code-quote">"MESSAGE"</span>/&gt;</span>
	<span class="code-tag">&lt;/cxf:properties&gt;</span>
<span class="code-tag">&lt;/cxf:cxfEndpoint&gt;</span>
</pre>
</div></div>

<h4><a name="CXF-DescriptionofrelayHeadersoption"></a>Description of relayHeaders option</h4>

<p>There are <em>in-band</em> and <em>out-of-band</em> on-the-wire headers from the perspective of a JAXWS WSDL-first developer.</p>

<p>The <em>in-band</em> headers are headers that are explicitly defined as part of the WSDL binding contract for an endpoint such as SOAP headers.</p>

<p>The <em>out-of-band</em> headers are headers that are serialized over the wire, but are not explicitly part of the WSDL binding contract.</p>

<p>Headers relaying/filtering is bi-directional.</p>

<p>When a route has a CXF endpoint and the developer needs to have on-the-wire headers, such as SOAP headers, be relayed along the route to be consumed say by another JAXWS endpoint, then <tt>relayHeaders</tt> should be set to <tt>true</tt>, which is the default value.</p>

<h5><a name="CXF-AvailableinRelease1.6.1andafter%28onlyinPOJOmode%29"></a>Available in Release 1.6.1 and after (only in POJO mode)</h5>

<p>The <tt>relayHeaders=true</tt> express an intent to relay the headers. The actual decision on whether a given header is relayed is delegated to a pluggable instance that implements the <tt>MessageHeadersRelay</tt> interface. A concrete implementation of <tt>MessageHeadersRelay</tt> will be consulted to decide if a header needs to be relayed or not. There is already an implementation of <tt>SoapMessageHeadersRelay</tt> which binds itself to well-known SOAP name spaces. Currently only out-of-band headers are filtered, and in-band headers will always be relayed when <tt>relayHeaders=true</tt>. If there is a header on the wire, whose name space is unknown to the runtime, then a fall back <tt>DefaultMessageHeadersRelay</tt> will be used, which simply allows all headers to be relayed.</p>

<p>The <tt>relayHeaders=false</tt> setting asserts that all headers in-band and out-of-band will be dropped.</p>

<p>You can plugin your own <tt>MessageHeadersRelay</tt> implementations overriding or adding additional ones to the list of relays. In order to override a preloaded relay instance just make sure that your <tt>MessageHeadersRelay</tt> implementation services the same name spaces as the one you looking to override. Also note, that the overriding relay has to service all of the name spaces as the one you looking to override, or else a runtime exception on route start up will be thrown as this would introduce an ambiguity in name spaces to relay instance mappings.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;cxf:cxfEndpoint ...&gt;</span>
   <span class="code-tag">&lt;cxf:properties&gt;</span>
     <span class="code-tag">&lt;entry key=<span class="code-quote">"org.apache.camel.cxf.message.headers.relays"</span>&gt;</span>
       <span class="code-tag">&lt;list&gt;</span>
         <span class="code-tag">&lt;ref bean=<span class="code-quote">"customHeadersRelay"</span>/&gt;</span>
       <span class="code-tag">&lt;/list&gt;</span>
     <span class="code-tag">&lt;/entry&gt;</span>
   <span class="code-tag">&lt;/cxf:properties&gt;</span>
 <span class="code-tag">&lt;/cxf:cxfEndpoint&gt;</span>
 <span class="code-tag">&lt;bean id=<span class="code-quote">"customHeadersRelay"</span> class=<span class="code-quote">"org.apache.camel.component.cxf.soap.headers.CustomHeadersRelay"</span>/&gt;</span>
</pre>
</div></div>
<p>Take a look at the tests that show how you'd be able to relay/drop headers here:</p>

<p><a href="https://svn.apache.org/repos/asf/camel/branches/camel-1.x/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/soap/headers/CxfMessageHeadersRelayTest.java" class="external-link" rel="nofollow">https://svn.apache.org/repos/asf/camel/branches/camel-1.x/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/soap/headers/CxfMessageHeadersRelayTest.java</a></p>




<h5><a name="CXF-ChangessinceRelease2.0"></a>Changes since Release 2.0</h5>

<ul>
	<li><tt>POJO</tt> and <tt>PAYLOAD</tt> modes are supported.  In <tt>POJO</tt> mode, only out-of-band message headers are available for filtering as the in-band headers have been processed and removed from header list by CXF.  The in-band headers are incorporated into the <tt>MessageContentList</tt> in POJO mode.  The <tt>camel-cxf</tt> component does make any attempt to remove the in-band headers from the <tt>MessageContentList</tt> as it does in 1.6.1.  If filtering of in-band headers is required, please use <tt>PAYLOAD</tt> mode or plug in a (pretty straightforward) CXF interceptor/JAXWS Handler to the CXF endpoint.</li>
	<li>The Message Header Relay mechanism has been merged into <tt>CxfHeaderFilterStrategy</tt>.  The <tt>relayHeaders</tt> option, its semantics, and default value remain the same, but it is a property of <tt>CxfHeaderFilterStrategy</tt>.<br/>
Here is an example of configuring it.
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;bean id=<span class="code-quote">"dropAllMessageHeadersStrategy"</span> class=<span class="code-quote">"org.apache.camel.component.cxf.CxfHeaderFilterStrategy"</span>&gt;</span>

    <span class="code-tag"><span class="code-comment">&lt;!--  Set relayHeaders to false to drop all SOAP headers --&gt;</span></span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"relayHeaders"</span> value=<span class="code-quote">"false"</span>/&gt;</span>
    
<span class="code-tag">&lt;/bean&gt;</span>
</pre>
</div></div>
<p>Then, your endpoint can reference the <tt>CxfHeaderFilterStrategy</tt>.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;route&gt;</span>
    <span class="code-tag">&lt;from uri=<span class="code-quote">"cxf:bean:routerNoRelayEndpoint?headerFilterStrategy=#dropAllMessageHeadersStrategy"</span>/&gt;</span>          
    <span class="code-tag">&lt;to uri=<span class="code-quote">"cxf:bean:serviceNoRelayEndpoint?headerFilterStrategy=#dropAllMessageHeadersStrategy"</span>/&gt;</span>
<span class="code-tag">&lt;/route&gt;</span>
</pre>
</div></div></li>
	<li>The <tt>MessageHeadersRelay</tt> interface has changed slightly and has been renamed to <tt>MessageHeaderFilter</tt>.  It is a property of <tt>CxfHeaderFilterStrategy</tt>.  Here is an example of configuring user defined Message Header Filters:
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;bean id=<span class="code-quote">"customMessageFilterStrategy"</span> class=<span class="code-quote">"org.apache.camel.component.cxf.CxfHeaderFilterStrategy"</span>&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"messageHeaderFilters"</span>&gt;</span>
        <span class="code-tag">&lt;list&gt;</span>
            <span class="code-tag"><span class="code-comment">&lt;!--  SoapMessageHeaderFilter is the built in filter.  It can be removed by omitting it. --&gt;</span></span>
            <span class="code-tag">&lt;bean class=<span class="code-quote">"org.apache.camel.component.cxf.SoapMessageHeaderFilter"</span>/&gt;</span>          
            
            <span class="code-tag"><span class="code-comment">&lt;!--  Add custom filter here --&gt;</span></span>    
            <span class="code-tag">&lt;bean class=<span class="code-quote">"org.apache.camel.component.cxf.soap.headers.CustomHeaderFilter"</span>/&gt;</span>
        <span class="code-tag">&lt;/list&gt;</span>
    <span class="code-tag">&lt;/property&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>
</pre>
</div></div></li>
	<li>Other than <tt>relayHeaders</tt>, there are new properties that can be configured in <tt>CxfHeaderFilterStrategy</tt>.
<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'> type </th>
<th class='confluenceTh'> Required? </th>
<th class='confluenceTh'> Default value </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>relayHeaders</tt> </td>
<td class='confluenceTd'> All message headers will be processed by Message Header Filters </td>
<td class='confluenceTd'> <tt>boolean</tt> </td>
<td class='confluenceTd'> No </td>
<td class='confluenceTd'> <tt>true</tt> (1.6.1 behavior) </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>relayAllMessageHeaders</tt> </td>
<td class='confluenceTd'> All message headers will be propagated (without processing by Message Header Filters) </td>
<td class='confluenceTd'> <tt>boolean</tt> </td>
<td class='confluenceTd'> No </td>
<td class='confluenceTd'> <tt>false</tt> (1.6.1 behavior) </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>allowFilterNamespaceClash</tt> </td>
<td class='confluenceTd'> If two filters overlap in activation namespace, the property control how it should be handled.  If the value is <tt>true</tt>, last one wins.  If the value is <tt>false</tt>, it will throw an exception </td>
<td class='confluenceTd'> <tt>boolean</tt> </td>
<td class='confluenceTd'> No </td>
<td class='confluenceTd'> <tt>false</tt> (1.6.1 behavior) </td>
</tr>
</tbody></table>
</div>
</div></li>
</ul>


<h3><a name="CXF-ConfiguretheCXFendpointswithSpring"></a>Configure the CXF endpoints with Spring</h3>

<p>You can configure the CXF endpoint with the Spring configuration file shown below, and you can also embed the endpoint into the <tt>camelContext</tt> tags.  When you are invoking the service endpoint, you can set the <tt>operationName</tt> and <tt>operationNameSpace</tt> headers to explicitly state which operation you are calling.</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://activemq.apache.org/camel/schema/cxfEndpoint"</span>
        xsi:schemaLocation="
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                http://activemq.apache.org/camel/schema/cxfEndpoint http://activemq.apache.org/camel/schema/cxf/camel-cxf-1.6.0.xsd
                http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring.xsd     "&gt;
     &lt;cxf:cxfEndpoint id=<span class="code-quote">"routerEndpoint"</span> address=<span class="code-quote">"http://localhost:9003/CamelContext/RouterPort"</span>
     		serviceClass=<span class="code-quote">"org.apache.hello_world_soap_http.GreeterImpl"</span>/&gt;
     &lt;cxf:cxfEndpoint id=<span class="code-quote">"serviceEndpoint"</span> address=<span class="code-quote">"http://localhost:9000/SoapContext/SoapPort"</span>
     		wsdlURL=<span class="code-quote">"testutils/hello_world.wsdl"</span>
     		serviceClass=<span class="code-quote">"org.apache.hello_world_soap_http.Greeter"</span>
     		endpointName=<span class="code-quote">"s:SoapPort"</span>
     		serviceName=<span class="code-quote">"s:SOAPService"</span>
     	<span class="code-keyword">xmlns:s</span>=<span class="code-quote">"http://apache.org/hello_world_soap_http"</span> /&gt;
     <span class="code-tag">&lt;camelContext id=<span class="code-quote">"camel"</span> xmlns=<span class="code-quote">"http://activemq.apache.org/camel/schema/spring"</span>&gt;</span>
       <span class="code-tag">&lt;route&gt;</span>
         <span class="code-tag">&lt;from uri=<span class="code-quote">"cxf:bean:routerEndpoint"</span> /&gt;</span>
         <span class="code-tag">&lt;to uri=<span class="code-quote">"cxf:bean:serviceEndpoint"</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>
<p><b>NOTE</b> In Camel 2.x we change to use {{http:­//camel.apache.org/schema/cxf}} as the CXF endpoint's target namespace.</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>
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.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     "&gt;
 ...
</pre>
</div></div>
<p>Be sure to include the JAX-WS <tt>schemaLocation</tt> attribute specified on the root beans element. This allows CXF to validate the file and is required.  Also note the namespace declarations at the end of the <tt>&lt;cxf:cxfEndpoint/&gt;</tt> tag--these are required because the combined {<tt>namespace}localName</tt> syntax is presently not supported for this tag's attribute values.</p>

<p>The <tt>cxf:cxfEndpoint</tt> element supports many additional attributes:</p>
<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Value </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>PortName</tt> </td>
<td class='confluenceTd'> The endpoint name this service is implementing, it maps to the <tt>wsdl:port@name</tt>. In the format of <tt>ns:PORT_NAME</tt> where <tt>ns</tt> is a namespace prefix valid at this scope. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>serviceName</tt> </td>
<td class='confluenceTd'> The service name this service is implementing, it maps to the <tt>wsdl:service@name</tt>. In the format of <tt>ns:SERVICE_NAME</tt> where <tt>ns</tt> is a namespace prefix valid at this scope. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>wsdlURL</tt> </td>
<td class='confluenceTd'> The location of the WSDL. Can be on the classpath, file system, or be hosted remotely. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>bindingId</tt> </td>
<td class='confluenceTd'> The <tt>bindingId</tt> for the service model to use. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>address</tt> </td>
<td class='confluenceTd'> The service publish address. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>bus</tt> </td>
<td class='confluenceTd'> The bus name that will be used in the JAX-WS endpoint. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>serviceClass</tt> </td>
<td class='confluenceTd'> The class name of the SEI (Service Endpoint Interface) class which could have JSR181 annotation or not. </td>
</tr>
</tbody></table>
</div>

<p>It also supports many child elements:</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Value </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>cxf:inInterceptors</tt> </td>
<td class='confluenceTd'> The incoming interceptors for this endpoint. A list of <tt>&lt;bean&gt;</tt> or <tt>&lt;ref&gt;</tt>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>cxf:inFaultInterceptors</tt> </td>
<td class='confluenceTd'> The incoming fault interceptors for this endpoint.  A list of <tt>&lt;bean&gt;</tt> or <tt>&lt;ref&gt;</tt>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>cxf:outInterceptors</tt> </td>
<td class='confluenceTd'> The outgoing interceptors for this endpoint. A list of <tt>&lt;bean&gt;</tt> or <tt>&lt;ref&gt;</tt>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>cxf:outFaultInterceptors</tt> </td>
<td class='confluenceTd'> The outgoing fault interceptors for this endpoint. A list of <tt>&lt;bean&gt;</tt> or <tt>&lt;ref&gt;</tt>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>cxf:properties</tt> </td>
<td class='confluenceTd'> A properties map which should be supplied to the JAX-WS endpoint. See below. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>cxf:handlers</tt> </td>
<td class='confluenceTd'> A JAX-WS handler list which should be supplied to the JAX-WS endpoint. See below. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>cxf:dataBinding</tt> </td>
<td class='confluenceTd'> You can specify the which <tt>DataBinding</tt> will be use in the endpoint. This can be supplied using the Spring <tt>&lt;bean class="MyDataBinding"/&gt;</tt> syntax. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>cxf:binding</tt> </td>
<td class='confluenceTd'> You can specify the <tt>BindingFactory</tt> for this endpoint to use.  This can be supplied using the Spring <tt>&lt;bean class="MyBindingFactory"/&gt;</tt> syntax. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>cxf:features</tt> </td>
<td class='confluenceTd'> The features that hold the interceptors for this endpoint. A list of {{&lt;bean&gt;}}s or {{&lt;ref&gt;}}s </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>cxf:schemaLocations</tt> </td>
<td class='confluenceTd'> The schema locations for endpoint to use. A list of {{&lt;schemaLocation&gt;}}s </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>cxf:serviceFactory</tt> </td>
<td class='confluenceTd'> The service factory for this endpoint to use. This can be supplied using the Spring <tt>&lt;bean class="MyServiceFactory"/&gt;</tt> syntax </td>
</tr>
</tbody></table>
</div>
</div>

<p>You can find more advanced examples which show how to provide interceptors , properties and handlers here:<br/>
<a href="http://cwiki.apache.org/CXF20DOC/jax-ws-configuration.html" class="external-link" rel="nofollow">http://cwiki.apache.org/CXF20DOC/jax-ws-configuration.html</a></p>

<p><b>NOTE</b><br/>
You can use cxf:properties to set the camel-cxf endpoint's dataFormat and setDefaultBus properties from spring configuration file.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
&lt;cxf:cxfEndpoint id=<span class="code-quote">"testEndpoint"</span> address=<span class="code-quote">"http://localhost:9000/router"</span>
     serviceClass=<span class="code-quote">"org.apache.camel.component.cxf.HelloService"</span>
     endpointName=<span class="code-quote">"s:PortName"</span>
     serviceName=<span class="code-quote">"s:ServiceName"</span>
     <span class="code-keyword">xmlns:s</span>=<span class="code-quote">"http://www.example.com/test"</span>&gt;
     <span class="code-tag">&lt;cxf:properties&gt;</span>
       <span class="code-tag">&lt;entry key=<span class="code-quote">"dataFormat"</span> value=<span class="code-quote">"MESSAGE"</span>/&gt;</span>
       <span class="code-tag">&lt;entry key=<span class="code-quote">"setDefaultBus"</span> value=<span class="code-quote">"true"</span>/&gt;</span>
     <span class="code-tag">&lt;/cxf:properties&gt;</span>
   <span class="code-tag">&lt;/cxf:cxfEndpoint&gt;</span>
</pre>
</div></div>

<h3><a name="CXF-Howtomakethecamelcxfcomponentuselog4jinsteadofjava.util.logging"></a>How to make the camel-cxf component use log4j instead of java.util.logging</h3>

<p>CXF's default logger is <tt>java.util.logging</tt>. If you want to change it to log4j, proceed as follows. Create a file, in the classpath, named <tt>META-INF/cxf/org.apache.cxf.logger</tt>. This file should contain the fully-qualified name of the class, <tt>org.apache.cxf.common.logging.Log4jLogger</tt>, with no comments, on a single line.</p>

<h3><a name="CXF-Howtoletcamelcxfresponsemessagewithxmlstartdocument"></a>How to let camel-cxf response message with xml start document</h3>

<p>If you are using some soap client such as PHP, you will get this kind of error, because CXF doesn't add the XML start document "&lt;?xml version="1.0" encoding="utf-8"?&gt;"</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
Error:sendSms: SoapFault exception: [Client] looks like we got no XML document in [...] 
</pre>
</div></div>
<p>To resolved this issue, you just need to tell StaxOutInterceptor to write the XML start document for you.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">public class WriteXmlDeclarationInterceptor extends AbstractPhaseInterceptor<span class="code-tag">&lt;SoapMessage&gt;</span> {
    public WriteXmlDeclarationInterceptor() {
        super(Phase.PRE_STREAM);
        addBefore(StaxOutInterceptor.class.getName());
    }

    public void handleMessage(SoapMessage message) throws Fault {
        message.put(<span class="code-quote">"org.apache.cxf.stax.force-start-document"</span>, Boolean.TRUE);        
    }

}
</pre>
</div></div>
<p>You can add a customer interceptor like this and configure it into you camel-cxf endpont</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">&lt;cxf:cxfEndpoint id=<span class="code-quote">"routerEndpoint"</span> address=<span class="code-quote">"http://localhost:9003/CamelContext/RouterPort"</span>
 		serviceClass=<span class="code-quote">"org.apache.hello_world_soap_http.GreeterImpl"</span>&gt;
     <span class="code-tag">&lt;cxf:outInterceptors&gt;</span>
         <span class="code-tag"><span class="code-comment">&lt;!-- This interceptor will force the CXF server send the XML start document to client --&gt;</span></span>
         <span class="code-tag">&lt;bean class=<span class="code-quote">"org.apache.camel.component.cxf.WriteXmlDeclarationInterceptor"</span>/&gt;</span>
     <span class="code-tag">&lt;/cxf:outInterceptors&gt;</span>
<span class="code-tag">&lt;/cxf:cxfEndpoint&gt;</span>
</pre>
</div></div>
<p>Or adding a message header for it like this if you are using <b>Camel 2.4</b>.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
 <span class="code-comment">// set up the response context which force start document
</span> Map&lt;<span class="code-object">String</span>, <span class="code-object">Object</span>&gt; map = <span class="code-keyword">new</span> HashMap&lt;<span class="code-object">String</span>, <span class="code-object">Object</span>&gt;();
 map.put(<span class="code-quote">"org.apache.cxf.stax.force-start-document"</span>, <span class="code-object">Boolean</span>.TRUE);
 exchange.getOut().setHeader(Client.RESPONSE_CONTEXT, map);
</pre>
</div></div>


<h3><a name="CXF-HowtoconsumeamessagefromacamelcxfendpointinPOJOdataformat"></a>How to consume a message from a camel-cxf endpoint in POJO data format</h3>

<p>The <tt>camel-cxf</tt> endpoint consumer POJO data format is based on the <a href="http://cwiki.apache.org/CXF20DOC/invokers.html" class="external-link" rel="nofollow">cxf invoker</a>, so the message header has a property with the name of <tt>CxfConstants.OPERATION_NAME</tt> and the message body is a list of the SEI method parameters.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> class PersonProcessor <span class="code-keyword">implements</span> Processor {

    <span class="code-keyword">private</span> <span class="code-keyword">static</span> <span class="code-keyword">final</span> <span class="code-keyword">transient</span> Log LOG = LogFactory.getLog(PersonProcessor.class);

    @SuppressWarnings(<span class="code-quote">"unchecked"</span>)
    <span class="code-keyword">public</span> void process(Exchange exchange) <span class="code-keyword">throws</span> Exception {
        LOG.info(<span class="code-quote">"processing exchange in camel"</span>);

        BindingOperationInfo boi = (BindingOperationInfo)exchange.getProperty(BindingOperationInfo.class.toString());
        <span class="code-keyword">if</span> (boi != <span class="code-keyword">null</span>) {
            LOG.info(<span class="code-quote">"boi.isUnwrapped"</span> + boi.isUnwrapped());
        }
        <span class="code-comment">// Get the parameters list which element is the holder.
</span>        MessageContentsList msgList = (MessageContentsList)exchange.getIn().getBody();
        Holder&lt;<span class="code-object">String</span>&gt; personId = (Holder&lt;<span class="code-object">String</span>&gt;)msgList.get(0);
        Holder&lt;<span class="code-object">String</span>&gt; ssn = (Holder&lt;<span class="code-object">String</span>&gt;)msgList.get(1);
        Holder&lt;<span class="code-object">String</span>&gt; name = (Holder&lt;<span class="code-object">String</span>&gt;)msgList.get(2);

        <span class="code-keyword">if</span> (personId.value == <span class="code-keyword">null</span> || personId.value.length() == 0) {
            LOG.info(<span class="code-quote">"person id 123, so throwing exception"</span>);
            <span class="code-comment">// Try to <span class="code-keyword">throw</span> out the soap fault message
</span>            org.apache.camel.wsdl_first.types.UnknownPersonFault personFault =
                <span class="code-keyword">new</span> org.apache.camel.wsdl_first.types.UnknownPersonFault();
            personFault.setPersonId("");
            org.apache.camel.wsdl_first.UnknownPersonFault fault =
                <span class="code-keyword">new</span> org.apache.camel.wsdl_first.UnknownPersonFault(<span class="code-quote">"Get the <span class="code-keyword">null</span> value of person name"</span>, personFault);
            <span class="code-comment">// Since camel has its own exception handler framework, we can't <span class="code-keyword">throw</span> the exception to trigger it
</span>            <span class="code-comment">// We just set the fault message in the exchange <span class="code-keyword">for</span> camel-cxf component handling and <span class="code-keyword">return</span>
</span>            exchange.getOut().setFault(<span class="code-keyword">true</span>);
            exchange.getOut().setBody(fault);
            <span class="code-keyword">return</span>;
        }

        name.value = <span class="code-quote">"Bonjour"</span>;
        ssn.value = <span class="code-quote">"123"</span>;
        LOG.info(<span class="code-quote">"setting Bonjour as the response"</span>);
        <span class="code-comment">// Set the response message, first element is the <span class="code-keyword">return</span> value of the operation,
</span>        <span class="code-comment">// the others are the holders of method parameters
</span>        exchange.getOut().setBody(<span class="code-keyword">new</span> <span class="code-object">Object</span>[] {<span class="code-keyword">null</span>, personId, ssn, name});
    }

}
</pre>
</div></div>

<h3><a name="CXF-HowtopreparethemessageforthecamelcxfendpointinPOJOdataformat"></a>How to prepare the message for the camel-cxf endpoint in POJO data format</h3>

<p>The <tt>camel-cxf</tt> endpoint producer is based on the <a href="https://svn.apache.org/repos/asf/cxf/trunk/api/src/main/java/org/apache/cxf/endpoint/Client.java" class="external-link" rel="nofollow">cxf client API</a>. First you need to specify the operation name in the message header, then add the method parameters to a list, and initialize the message with this parameter list. The response message's body is a messageContentsList, you can get the result from that list.</p>

<p><b>NOTE</b> After Camel 1.5 , we change the message body from object array to message content list. If you still want to get the object array from the message body, you can get the body using <tt>message.getbody(Object[].class)</tt>, as follows:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">Exchange senderExchange = <span class="code-keyword">new</span> DefaultExchange(context, ExchangePattern.InOut);
<span class="code-keyword">final</span> List&lt;<span class="code-object">String</span>&gt; params = <span class="code-keyword">new</span> ArrayList&lt;<span class="code-object">String</span>&gt;();
<span class="code-comment">// Prepare the request message <span class="code-keyword">for</span> the camel-cxf procedure
</span>params.add(TEST_MESSAGE);
senderExchange.getIn().setBody(params);
senderExchange.getIn().setHeader(CxfConstants.OPERATION_NAME, ECHO_OPERATION);

Exchange exchange = template.send(<span class="code-quote">"direct:EndpointA"</span>, senderExchange);

org.apache.camel.Message out = exchange.getOut();
<span class="code-comment">// The response message's body is an MessageContentsList which first element is the <span class="code-keyword">return</span> value of the operation,
</span><span class="code-comment">// If there are some holder parameters, the holder parameter will be filled in the reset of List.
</span><span class="code-comment">// The result will be extract from the MessageContentsList with the <span class="code-object">String</span> class type
</span>MessageContentsList result = (MessageContentsList)out.getBody();
LOG.info(<span class="code-quote">"Received output text: "</span> + result.get(0));
Map&lt;<span class="code-object">String</span>, <span class="code-object">Object</span>&gt; responseContext = CastUtils.<span class="code-keyword">cast</span>((Map)out.getHeader(Client.RESPONSE_CONTEXT));
assertNotNull(responseContext);
assertEquals(<span class="code-quote">"We should get the response context here"</span>, <span class="code-quote">"UTF-8"</span>, responseContext.get(org.apache.cxf.message.Message.ENCODING));
assertEquals(<span class="code-quote">"Reply body on Camel is wrong"</span>, <span class="code-quote">"echo "</span> + TEST_MESSAGE, result.get(0));
</pre>
</div></div>

<h3><a name="CXF-HowtodealwiththemessageforacamelcxfendpointinPAYLOADdataformat"></a>How to deal with the message for a camel-cxf endpoint in PAYLOAD data format</h3>

<p><tt>PAYLOAD</tt> means that you process the payload message from the SOAP envelope. You can use the <tt>Header.HEADER_LIST</tt> as the key to set or get the SOAP headers and use the <tt>List&lt;Element&gt;</tt> to set or get SOAP body elements.<br/>
Camel 1.x branch, you can get the <tt>List&lt;Element&gt;</tt> and header from the CXF Message, but if you want to set the response message, you need to create the CXF message using the CXF API.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">protected</span> RouteBuilder createRouteBuilder() {
    <span class="code-keyword">return</span> <span class="code-keyword">new</span> RouteBuilder() {
        <span class="code-keyword">public</span> void configure() {
            from(SIMPLE_ENDPOINT_URI + <span class="code-quote">"&amp;dataFormat=PAYLOAD"</span>).to(<span class="code-quote">"log:info"</span>).process(<span class="code-keyword">new</span> Processor() {
                <span class="code-keyword">public</span> void process(<span class="code-keyword">final</span> Exchange exchange) <span class="code-keyword">throws</span> Exception {
                    Message inMessage = exchange.getIn();
                    <span class="code-keyword">if</span> (inMessage <span class="code-keyword">instanceof</span> CxfMessage) {
                        CxfMessage cxfInMessage = (CxfMessage) inMessage;
                        CxfMessage cxfOutMessage = (CxfMessage) exchange.getOut();
                        List&lt;Element&gt; inElements = cxfInMessage.getMessage().get(List.class);
                        List&lt;Element&gt; outElements = <span class="code-keyword">new</span> ArrayList&lt;Element&gt;();
                        XmlConverter converter = <span class="code-keyword">new</span> XmlConverter();
                        <span class="code-object">String</span> documentString = ECHO_RESPONSE;
                        <span class="code-keyword">if</span> (inElements.get(0).getLocalName().equals(<span class="code-quote">"echoBoolean"</span>)) {
                            documentString = ECHO_BOOLEAN_RESPONSE;
                        }
                        org.apache.cxf.message.Exchange ex = ((CxfExchange)exchange).getExchange();
                        Endpoint ep = ex.get(Endpoint.class);
                        org.apache.cxf.message.Message response = ep.getBinding().createMessage();
                        Document outDocument = converter.toDOMDocument(documentString);
                        outElements.add(outDocument.getDocumentElement());
                        response.put(List.class, outElements);
                        cxfOutMessage.setMessage(response);                            
                    }
                }
            });
        }
    };
}
</pre>
</div></div>
<p><b>Change in 2.0</b>, There is no more <tt>CxfMessage</tt>, we just use the common Camel <tt>DefaultMessageImpl</tt> under layer. <tt>Message.getBody()</tt> will return an <tt>org.apache.camel.component.cxf.CxfPayload</tt> object, which has getters for SOAP message headers and Body elements. This change enables decoupling the native CXF message from the Camel message.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">protected</span> RouteBuilder createRouteBuilder() {
    <span class="code-keyword">return</span> <span class="code-keyword">new</span> RouteBuilder() {
        <span class="code-keyword">public</span> void configure() {
            from(SIMPLE_ENDPOINT_URI + <span class="code-quote">"&amp;dataFormat=PAYLOAD"</span>).to(<span class="code-quote">"log:info"</span>).process(<span class="code-keyword">new</span> Processor() {
                @SuppressWarnings(<span class="code-quote">"unchecked"</span>)
                <span class="code-keyword">public</span> void process(<span class="code-keyword">final</span> Exchange exchange) <span class="code-keyword">throws</span> Exception {
                    CxfPayload&lt;SoapHeader&gt; requestPayload = exchange.getIn().getBody(CxfPayload.class);
                    List&lt;Element&gt; inElements = requestPayload.getBody();
                    List&lt;Element&gt; outElements = <span class="code-keyword">new</span> ArrayList&lt;Element&gt;();
                    <span class="code-comment">// You can use a customer toStringConverter to turn a CxfPayLoad message into <span class="code-object">String</span> as you want                        
</span>                    <span class="code-object">String</span> request = exchange.getIn().getBody(<span class="code-object">String</span>.class);
                    XmlConverter converter = <span class="code-keyword">new</span> XmlConverter();
                    <span class="code-object">String</span> documentString = ECHO_RESPONSE;
                    <span class="code-keyword">if</span> (inElements.get(0).getLocalName().equals(<span class="code-quote">"echoBoolean"</span>)) {
                        documentString = ECHO_BOOLEAN_RESPONSE;
                        assertEquals(<span class="code-quote">"Get a wrong request"</span>, ECHO_BOOLEAN_REQUEST, request);
                    } <span class="code-keyword">else</span> {
                        assertEquals(<span class="code-quote">"Get a wrong request"</span>, ECHO_REQUEST, request);
                    }
                    Document outDocument = converter.toDOMDocument(documentString);
                    outElements.add(outDocument.getDocumentElement());
                    <span class="code-comment">// set the payload header with <span class="code-keyword">null</span>
</span>                    CxfPayload&lt;SoapHeader&gt; responsePayload = <span class="code-keyword">new</span> CxfPayload&lt;SoapHeader&gt;(<span class="code-keyword">null</span>, outElements);
                    exchange.getOut().setBody(responsePayload); 
                }
            });
        }
    };
}
</pre>
</div></div>

<h3><a name="CXF-HowtogetandsetSOAPheadersinPOJOmode"></a>How to get and set SOAP headers in POJO mode</h3>

<p><tt>POJO</tt> means that the data format is a "list of Java objects" when the Camel-cxf endpoint produces or consumes Camel exchanges.  Even though Camel expose message body as POJOs in this mode, Camel-cxf still provides access to read and write SOAP headers.  However, since CXF interceptors remove in-band SOAP headers from Header list after they have been processed, only out-of-band SOAP headers are available to Camel-cxf in POJO mode.</p>

<p>The following example illustrate how to get/set SOAP headers.  Suppose we have a route that forwards from one Camel-cxf endpoint to another.  That is, SOAP Client &#45;&gt; Camel &#45;&gt; CXF service.   We can attach two processors to obtain/insert SOAP headers at (1) before request goes out to the CXF service and (2) before response comes back to the SOAP Client.  Processor (1) and (2) in this example are InsertRequestOutHeaderProcessor and InsertResponseOutHeaderProcessor.  Our route looks like this:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;route&gt;</span>
    <span class="code-tag">&lt;from uri=<span class="code-quote">"cxf:bean:routerRelayEndpointWithInsertion"</span>/&gt;</span>
    <span class="code-tag">&lt;process ref=<span class="code-quote">"InsertRequestOutHeaderProcessor"</span> /&gt;</span>
    <span class="code-tag">&lt;to uri=<span class="code-quote">"cxf:bean:serviceRelayEndpointWithInsertion"</span>/&gt;</span>
    <span class="code-tag">&lt;process ref=<span class="code-quote">"InsertResponseOutHeaderProcessor"</span> /&gt;</span>
<span class="code-tag">&lt;/route&gt;</span>     
</pre>
</div></div>

<p><b>In 2.x</b> SOAP headers are propagated to and from Camel Message headers.  The Camel message header name is "org.apache.cxf.headers.Header.list" which is a constant defined in CXF (org.apache.cxf.headers.Header.HEADER_LIST).  The header value is a List of CXF SoapHeader objects (org.apache.cxf.binding.soap.SoapHeader).  The following snippet is the InsertResponseOutHeaderProcessor (that insert a new SOAP header in the response message).  The way to access SOAP headers in both InsertResponseOutHeaderProcessor and InsertRequestOutHeaderProcessor are actually the same.  The only difference between the two processors is setting the direction of the inserted SOAP header.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">public</span> <span class="code-keyword">static</span> class InsertResponseOutHeaderProcessor <span class="code-keyword">implements</span> Processor {

    @SuppressWarnings(<span class="code-quote">"unchecked"</span>)
    <span class="code-keyword">public</span> void process(Exchange exchange) <span class="code-keyword">throws</span> Exception {
        List&lt;SoapHeader&gt; soapHeaders = (List)exchange.getIn().getHeader(Header.HEADER_LIST);

        <span class="code-comment">// Insert a <span class="code-keyword">new</span> header
</span>        <span class="code-object">String</span> xml = <span class="code-quote">"&lt;?xml version=\"</span>1.0\<span class="code-quote">" encoding=\"</span>utf-8\<span class="code-quote">"?&gt;&lt;outofbandHeader "</span>
            + <span class="code-quote">"xmlns=\"</span>http:<span class="code-comment">//cxf.apache.org/outofband/Header\<span class="code-quote">" hdrAttribute=\"</span>testHdrAttribute\<span class="code-quote">" "</span>
</span>            + <span class="code-quote">"xmlns:soap=\"</span>http:<span class="code-comment">//schemas.xmlsoap.org/soap/envelope/\<span class="code-quote">" soap:mustUnderstand=\"</span>1\<span class="code-quote">"&gt;"</span>
</span>            + <span class="code-quote">"&lt;name&gt;New_testOobHeader&lt;/name&gt;&lt;value&gt;New_testOobHeaderValue&lt;/value&gt;&lt;/outofbandHeader&gt;"</span>;
        SoapHeader newHeader = <span class="code-keyword">new</span> SoapHeader(soapHeaders.get(0).getName(),
                       DOMUtils.readXml(<span class="code-keyword">new</span> StringReader(xml)).getDocumentElement());
        <span class="code-comment">// make sure direction is OUT since it is a response message.
</span>        newHeader.setDirection(Direction.DIRECTION_OUT);
        <span class="code-comment">//newHeader.setMustUnderstand(<span class="code-keyword">false</span>);
</span>        soapHeaders.add(newHeader);
        
    }
    
}

</pre>
</div></div>

<p><b>In 1.x</b> SOAP headers are not propagated to and from Camel Message headers. Users have to go deeper into CXF APIs to access SOAP headers.  Also, accessing the SOAP headers in a request message is slight different than in a response message.  The InsertRequestOutHeaderProcessor and InsertResponseOutHeaderProcessor are as follow.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">public</span> <span class="code-keyword">static</span> class InsertRequestOutHeaderProcessor <span class="code-keyword">implements</span> Processor {
    <span class="code-keyword">public</span> void process(Exchange exchange) <span class="code-keyword">throws</span> Exception {
        CxfMessage message = exchange.getIn().getBody(CxfMessage.class);
        Message cxf = message.getMessage();
        List&lt;SoapHeader&gt; soapHeaders = (List)cxf.get(Header.HEADER_LIST);

        <span class="code-comment">// Insert a <span class="code-keyword">new</span> header
</span>        <span class="code-object">String</span> xml = <span class="code-quote">"&lt;?xml version=\"</span>1.0\<span class="code-quote">" encoding=\"</span>utf-8\<span class="code-quote">"?&gt;&lt;outofbandHeader "</span>
            + <span class="code-quote">"xmlns=\"</span>http:<span class="code-comment">//cxf.apache.org/outofband/Header\<span class="code-quote">" hdrAttribute=\"</span>testHdrAttribute\<span class="code-quote">" "</span>
</span>            + <span class="code-quote">"xmlns:soap=\"</span>http:<span class="code-comment">//schemas.xmlsoap.org/soap/envelope/\<span class="code-quote">" soap:mustUnderstand=\"</span>1\<span class="code-quote">"&gt;"</span>
</span>            + <span class="code-quote">"&lt;name&gt;New_testOobHeader&lt;/name&gt;&lt;value&gt;New_testOobHeaderValue&lt;/value&gt;&lt;/outofbandHeader&gt;"</span>;
        
        SoapHeader newHeader = <span class="code-keyword">new</span> SoapHeader(soapHeaders.get(0).getName(),
                                              DOMUtils.readXml(<span class="code-keyword">new</span> StringReader(xml)).getDocumentElement());
        <span class="code-comment">// make sure direction is IN since it is a request message.
</span>        newHeader.setDirection(Direction.DIRECTION_IN);
        <span class="code-comment">//newHeader.setMustUnderstand(<span class="code-keyword">false</span>);
</span>        soapHeaders.add(newHeader);
        
    }
}

<span class="code-keyword">public</span> <span class="code-keyword">static</span> class InsertResponseOutHeaderProcessor <span class="code-keyword">implements</span> Processor {
    <span class="code-keyword">public</span> void process(Exchange exchange) <span class="code-keyword">throws</span> Exception {
        CxfMessage message = exchange.getIn().getBody(CxfMessage.class);
        Map responseContext = (Map)message.getMessage().get(Client.RESPONSE_CONTEXT);
        List&lt;SoapHeader&gt; soapHeaders = (List)responseContext.get(Header.HEADER_LIST);
        
        <span class="code-comment">// Insert a <span class="code-keyword">new</span> header
</span>        <span class="code-object">String</span> xml = <span class="code-quote">"&lt;?xml version=\"</span>1.0\<span class="code-quote">" encoding=\"</span>utf-8\<span class="code-quote">"?&gt;&lt;outofbandHeader "</span>
            + <span class="code-quote">"xmlns=\"</span>http:<span class="code-comment">//cxf.apache.org/outofband/Header\<span class="code-quote">" hdrAttribute=\"</span>testHdrAttribute\<span class="code-quote">" "</span>
</span>            + <span class="code-quote">"xmlns:soap=\"</span>http:<span class="code-comment">//schemas.xmlsoap.org/soap/envelope/\<span class="code-quote">" soap:mustUnderstand=\"</span>1\<span class="code-quote">"&gt;"</span>
</span>            + <span class="code-quote">"&lt;name&gt;New_testOobHeader&lt;/name&gt;&lt;value&gt;New_testOobHeaderValue&lt;/value&gt;&lt;/outofbandHeader&gt;"</span>;
        SoapHeader newHeader = <span class="code-keyword">new</span> SoapHeader(soapHeaders.get(0).getName(),
                       DOMUtils.readXml(<span class="code-keyword">new</span> StringReader(xml)).getDocumentElement());
        <span class="code-comment">// make sure direction is OUT since it is a response message.
</span>        newHeader.setDirection(Direction.DIRECTION_OUT);
        <span class="code-comment">//newHeader.setMustUnderstand(<span class="code-keyword">false</span>);
</span>        soapHeaders.add(newHeader);
                                       
    }
}
</pre>
</div></div>

<h3><a name="CXF-HowtogetandsetSOAPheadersinPAYLOADmode"></a>How to get and set SOAP headers in PAYLOAD mode</h3>

<p>We've already shown how to access SOAP message (CxfPayload object) in PAYLOAD mode (See "How to deal with the message for a camel-cxf endpoint in PAYLOAD data format").</p>

<p><b>In 2.x</b> Once you obtain a CxfPayload object, you can invoke the CxfPayload.getHeaders() method that returns a List of DOM Elements (SOAP headers).</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(routerEndpointURI).process(<span class="code-keyword">new</span> Processor() {
    @SuppressWarnings(<span class="code-quote">"unchecked"</span>)
    <span class="code-keyword">public</span> void process(Exchange exchange) <span class="code-keyword">throws</span> Exception {
        CxfPayload&lt;SoapHeader&gt; payload = exchange.getIn().getBody(CxfPayload.class);
        List&lt;Element&gt; elements = payload.getBody();
        assertNotNull(<span class="code-quote">"We should get the elements here"</span>, elements);
        assertEquals(<span class="code-quote">"Get the wrong elements size"</span>, 1, elements.size());
        assertEquals(<span class="code-quote">"Get the wrong namespace URI"</span>, <span class="code-quote">"http:<span class="code-comment">//camel.apache.org/pizza/types"</span>, 
</span>                elements.get(0).getNamespaceURI());
            
        List&lt;SoapHeader&gt; headers = payload.getHeaders();
        assertNotNull(<span class="code-quote">"We should get the headers here"</span>, headers);
        assertEquals(<span class="code-quote">"Get the wrong headers size"</span>, headers.size(), 1);
        assertEquals(<span class="code-quote">"Get the wrong namespace URI"</span>, 
                ((Element)(headers.get(0).getObject())).getNamespaceURI(), 
                <span class="code-quote">"http:<span class="code-comment">//camel.apache.org/pizza/types"</span>);         
</span>    }
    
})
.to(serviceEndpointURI);
</pre>
</div></div>

<p>&#42;In 1.x" You can get/set to the CXF Message by the key "org.apache.cxf.headers.Header.list" which is a constant defined in CXF (org.apache.cxf.headers.Header.HEADER_LIST).</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(routerEndpointURI).process(<span class="code-keyword">new</span> Processor() {
    @SuppressWarnings(<span class="code-quote">"unchecked"</span>)
    <span class="code-keyword">public</span> void process(Exchange exchange) <span class="code-keyword">throws</span> Exception {
        Message inMessage = exchange.getIn();
        CxfMessage message = (CxfMessage) inMessage;
        List&lt;Element&gt; elements = message.getMessage().get(List.class);
        assertNotNull(<span class="code-quote">"We should get the payload elements here"</span> , elements);
        assertEquals(<span class="code-quote">"Get the wrong elements size"</span> , elements.size(), 1);
        assertEquals(<span class="code-quote">"Get the wrong namespace URI"</span> , elements.get(0).getNamespaceURI(), <span class="code-quote">"http:<span class="code-comment">//camel.apache.org/pizza/types"</span>);
</span>            
        List&lt;SoapHeader&gt; headers = CastUtils.<span class="code-keyword">cast</span>((List&lt;?&gt;)message.getMessage().get(Header.HEADER_LIST));
        assertNotNull(<span class="code-quote">"We should get the headers here"</span>, headers);
        assertEquals(<span class="code-quote">"Get the wrong headers size"</span>, headers.size(), 1);
        assertEquals(<span class="code-quote">"Get the wrong namespace URI"</span> , ((Element)(headers.get(0).getObject())).getNamespaceURI(), <span class="code-quote">"http:<span class="code-comment">//camel.apache.org/pizza/types"</span>);
</span>        
    }
    
})
.to(serviceEndpointURI);
</pre>
</div></div>

<h3><a name="CXF-SOAPheadersarenotavailableinMESSAGEmode"></a>SOAP headers are not available in MESSAGE mode</h3>

<p>SOAP headers are not available in MESSAGE mode as SOAP processing is skipped.</p>

<h3><a name="CXF-HowtothrowaSOAPFaultfromCamel"></a>How to throw a SOAP Fault from Camel</h3>

<p>If you are using a <tt>camel-cxf</tt> endpoint to consume the SOAP request, you may need to throw the SOAP Fault from the camel context.<br/>
Basically, you can use the <tt>throwFault</tt> DSL to do that; it works for <tt>POJO</tt>, <tt>PAYLOAD</tt> and <tt>MESSAGE</tt> data format.<br/>
You can define the soap fault like this</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">SOAP_FAULT = <span class="code-keyword">new</span> SoapFault(EXCEPTION_MESSAGE, SoapFault.FAULT_CODE_CLIENT);
Element detail = SOAP_FAULT.getOrCreateDetail();
Document doc = detail.getOwnerDocument();
Text tn = doc.createTextNode(DETAIL_TEXT);
detail.appendChild(tn);
</pre>
</div></div>
<p>Then throw it as you like</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(routerEndpointURI).setFaultBody(constant(SOAP_FAULT));
</pre>
</div></div>
<p>If your CXF endpoint is working in the <tt>MESSAGE</tt> data format, you could set the the SOAP Fault message in the message body and set the response code in the message header.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(routerEndpointURI).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 out = exchange.getOut();
        <span class="code-comment">// Set the message body with the 
</span>        out.setBody(<span class="code-keyword">this</span>.getClass().getResourceAsStream(<span class="code-quote">"SoapFaultMessage.xml"</span>));
        <span class="code-comment">// Set the response code here
</span>        out.setHeader(org.apache.cxf.message.Message.RESPONSE_CODE, <span class="code-keyword">new</span> <span class="code-object">Integer</span>(500));
    }

});
</pre>
</div></div>
<p><b>NOTE</b> the response code setting only works in Camel's version &gt;= 1.5.1</p>

<h3><a name="CXF-Howtopropagateacamelcxfendpoint%27srequestandresponsecontext"></a>How to propagate a camel-cxf endpoint's request and response context</h3>

<p><a href="https://svn.apache.org/repos/asf/cxf/trunk/api/src/main/java/org/apache/cxf/endpoint/Client.java" class="external-link" rel="nofollow">cxf client API</a> provides a way to invoke the operation with request and response context. If you are using a <tt>camel-cxf</tt> endpoint producer to invoke the outside web service, you can set the request context and get response context with the following code:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
        CxfExchange exchange = (CxfExchange)template.send(getJaxwsEndpointUri(), <span class="code-keyword">new</span> Processor() {
             <span class="code-keyword">public</span> void process(<span class="code-keyword">final</span> Exchange exchange) {
                 <span class="code-keyword">final</span> List&lt;<span class="code-object">String</span>&gt; params = <span class="code-keyword">new</span> ArrayList&lt;<span class="code-object">String</span>&gt;();
                 params.add(TEST_MESSAGE);
                 <span class="code-comment">// Set the request context to the inMessage
</span>                 Map&lt;<span class="code-object">String</span>, <span class="code-object">Object</span>&gt; requestContext = <span class="code-keyword">new</span> HashMap&lt;<span class="code-object">String</span>, <span class="code-object">Object</span>&gt;();
                 requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, JAXWS_SERVER_ADDRESS);
                 exchange.getIn().setBody(params);
                 exchange.getIn().setHeader(Client.REQUEST_CONTEXT , requestContext);
                 exchange.getIn().setHeader(CxfConstants.OPERATION_NAME, GREET_ME_OPERATION);
             }
         });
         org.apache.camel.Message out = exchange.getOut();
         <span class="code-comment">// The output is an object array, the first element of the array is the <span class="code-keyword">return</span> value
</span>         <span class="code-object">Object</span>\[\] output = out.getBody(<span class="code-object">Object</span>\[\].class);
         LOG.info(<span class="code-quote">"Received output text: "</span> + output\[0\]);
         <span class="code-comment">// Get the response context form outMessage
</span>         Map&lt;<span class="code-object">String</span>, <span class="code-object">Object</span>&gt; responseContext = CastUtils.<span class="code-keyword">cast</span>((Map)out.getHeader(Client.RESPONSE_CONTEXT));
         assertNotNull(responseContext);
         assertEquals(<span class="code-quote">"Get the wrong wsdl opertion name"</span>, <span class="code-quote">"{http:<span class="code-comment">//apache.org/hello_world_soap_http}greetMe"</span>,
</span>                      responseContext.get(<span class="code-quote">"javax.xml.ws.wsdl.operation"</span>).toString());
</pre>
</div></div>

<h3><a name="CXF-AttachmentSupport"></a>Attachment Support</h3>

<p><b>POJO Mode:</b> Both SOAP with Attachment and MTOM are supported (see example in Payload Mode for enabling MTOM).&nbsp; However, SOAP with Attachment is not tested.&nbsp; Since attachments are marshalled and unmarshalled into POJOs, users typically do not need to deal with the attachment themself.&nbsp; Attachments are propagated to Camel message's attachments since 2.1.&nbsp; So, it is possible to retreive attachments by Camel Message API </p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">DataHandler Message.getAttachment(<span class="code-object">String</span> id)</pre>
</div></div>
<p>.</p>

<p><b>Payload Mode:</b> MTOM is supported since 2.1.  Attachments can be retrieve by Camel Message APIs mentioned above.  SOAP with Attachment is not supported as there is no SOAP processing in this mode.</p>

<p>To enable MTOM, set the CXF endpoint property "mtom_enabled" to <em>true</em>.  (I believe you can only do it with Spring.)</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
&lt;cxf:cxfEndpoint id=<span class="code-quote">"routerEndpoint"</span> address=<span class="code-quote">"http://localhost:9091/jaxws-mtom/hello"</span>
         wsdlURL=<span class="code-quote">"mtom.wsdl"</span>
         serviceName=<span class="code-quote">"ns:HelloService"</span>
         endpointName=<span class="code-quote">"ns:HelloPort"</span>
         <span class="code-keyword">xmlns:ns</span>=<span class="code-quote">"http://apache.org/camel/cxf/mtom_feature"</span>&gt;

     <span class="code-tag">&lt;cxf:properties&gt;</span>
         <span class="code-tag"><span class="code-comment">&lt;!--  enable mtom by setting this property to true --&gt;</span></span>
         <span class="code-tag">&lt;entry key=<span class="code-quote">"mtom-enabled"</span> value=<span class="code-quote">"true"</span>/&gt;</span>
         
         <span class="code-tag"><span class="code-comment">&lt;!--  set the camel-cxf endpoint data fromat to PAYLOAD mode --&gt;</span></span>
         <span class="code-tag">&lt;entry key=<span class="code-quote">"dataFormat"</span> value=<span class="code-quote">"PAYLOAD"</span>/&gt;</span>
     <span class="code-tag">&lt;/cxf:properties&gt;</span>      
     
</pre>
</div></div>

<p>You can produce a Camel message with attachment to send to a CXF endpoint in Payload mode.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
Exchange exchange = context.createProducerTemplate().send(<span class="code-quote">"direct:testEndpoint"</span>, <span class="code-keyword">new</span> Processor() {

    <span class="code-keyword">public</span> void process(Exchange exchange) <span class="code-keyword">throws</span> Exception {
        exchange.setPattern(ExchangePattern.InOut);
        List&lt;Element&gt; elements = <span class="code-keyword">new</span> ArrayList&lt;Element&gt;();
        elements.add(DOMUtils.readXml(<span class="code-keyword">new</span> StringReader(MtomTestHelper.REQ_MESSAGE)).getDocumentElement());
        CxfPayload&lt;SoapHeader&gt; body = <span class="code-keyword">new</span> CxfPayload&lt;SoapHeader&gt;(<span class="code-keyword">new</span> ArrayList&lt;SoapHeader&gt;(),
            elements);
        exchange.getIn().setBody(body);
        exchange.getIn().addAttachment(MtomTestHelper.REQ_PHOTO_CID, 
            <span class="code-keyword">new</span> DataHandler(<span class="code-keyword">new</span> ByteArrayDataSource(MtomTestHelper.REQ_PHOTO_DATA, <span class="code-quote">"application/octet-stream"</span>)));

        exchange.getIn().addAttachment(MtomTestHelper.REQ_IMAGE_CID, 
            <span class="code-keyword">new</span> DataHandler(<span class="code-keyword">new</span> ByteArrayDataSource(MtomTestHelper.requestJpeg, <span class="code-quote">"image/jpeg"</span>)));

    }
    
});

<span class="code-comment">// process response 
</span>
CxfPayload&lt;SoapHeader&gt; out = exchange.getOut().getBody(CxfPayload.class);
Assert.assertEquals(1, out.getBody().size());

Map&lt;<span class="code-object">String</span>, <span class="code-object">String</span>&gt; ns = <span class="code-keyword">new</span> HashMap&lt;<span class="code-object">String</span>, <span class="code-object">String</span>&gt;();
ns.put(<span class="code-quote">"ns"</span>, MtomTestHelper.SERVICE_TYPES_NS);
ns.put(<span class="code-quote">"xop"</span>, MtomTestHelper.XOP_NS);

XPathUtils xu = <span class="code-keyword">new</span> XPathUtils(ns);
Element ele = (Element)xu.getValue(<span class="code-quote">"<span class="code-comment">//ns:DetailResponse/ns:photo/xop:Include"</span>, out.getBody().get(0),
</span>                                   XPathConstants.NODE);
<span class="code-object">String</span> photoId = ele.getAttribute(<span class="code-quote">"href"</span>).substring(4); <span class="code-comment">// skip <span class="code-quote">"cid:"</span>
</span>
ele = (Element)xu.getValue(<span class="code-quote">"<span class="code-comment">//ns:DetailResponse/ns:image/xop:Include"</span>, out.getBody().get(0),
</span>                                   XPathConstants.NODE);
<span class="code-object">String</span> imageId = ele.getAttribute(<span class="code-quote">"href"</span>).substring(4); <span class="code-comment">// skip <span class="code-quote">"cid:"</span>
</span>
DataHandler dr = exchange.getOut().getAttachment(photoId);
Assert.assertEquals(<span class="code-quote">"application/octet-stream"</span>, dr.getContentType());
MtomTestHelper.assertEquals(MtomTestHelper.RESP_PHOTO_DATA, IOUtils.readBytesFromStream(dr.getInputStream()));
   
dr = exchange.getOut().getAttachment(imageId);
Assert.assertEquals(<span class="code-quote">"image/jpeg"</span>, dr.getContentType());

BufferedImage image = ImageIO.read(dr.getInputStream());
Assert.assertEquals(560, image.getWidth());
Assert.assertEquals(300, image.getHeight());

</pre>
</div></div>

<p>You can also consume a Camel message received from a CXF endpoint in Payload mode.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> <span class="code-keyword">static</span> class MyProcessor <span class="code-keyword">implements</span> Processor {

    @SuppressWarnings(<span class="code-quote">"unchecked"</span>)
    <span class="code-keyword">public</span> void process(Exchange exchange) <span class="code-keyword">throws</span> Exception {
        CxfPayload&lt;SoapHeader&gt; in = exchange.getIn().getBody(CxfPayload.class);
        
        <span class="code-comment">// verify request
</span>        Assert.assertEquals(1, in.getBody().size());
        
        Map&lt;<span class="code-object">String</span>, <span class="code-object">String</span>&gt; ns = <span class="code-keyword">new</span> HashMap&lt;<span class="code-object">String</span>, <span class="code-object">String</span>&gt;();
        ns.put(<span class="code-quote">"ns"</span>, MtomTestHelper.SERVICE_TYPES_NS);
        ns.put(<span class="code-quote">"xop"</span>, MtomTestHelper.XOP_NS);

        XPathUtils xu = <span class="code-keyword">new</span> XPathUtils(ns);
        Element ele = (Element)xu.getValue(<span class="code-quote">"<span class="code-comment">//ns:Detail/ns:photo/xop:Include"</span>, in.getBody().get(0),
</span>                                           XPathConstants.NODE);
        <span class="code-object">String</span> photoId = ele.getAttribute(<span class="code-quote">"href"</span>).substring(4); <span class="code-comment">// skip <span class="code-quote">"cid:"</span>
</span>        Assert.assertEquals(MtomTestHelper.REQ_PHOTO_CID, photoId);

        ele = (Element)xu.getValue(<span class="code-quote">"<span class="code-comment">//ns:Detail/ns:image/xop:Include"</span>, in.getBody().get(0),
</span>                                           XPathConstants.NODE);
        <span class="code-object">String</span> imageId = ele.getAttribute(<span class="code-quote">"href"</span>).substring(4); <span class="code-comment">// skip <span class="code-quote">"cid:"</span>
</span>        Assert.assertEquals(MtomTestHelper.REQ_IMAGE_CID, imageId);

        DataHandler dr = exchange.getIn().getAttachment(photoId);
        Assert.assertEquals(<span class="code-quote">"application/octet-stream"</span>, dr.getContentType());
        MtomTestHelper.assertEquals(MtomTestHelper.REQ_PHOTO_DATA, IOUtils.readBytesFromStream(dr.getInputStream()));
   
        dr = exchange.getIn().getAttachment(imageId);
        Assert.assertEquals(<span class="code-quote">"image/jpeg"</span>, dr.getContentType());
        MtomTestHelper.assertEquals(MtomTestHelper.requestJpeg, IOUtils.readBytesFromStream(dr.getInputStream()));

        <span class="code-comment">// create response
</span>        List&lt;Element&gt; elements = <span class="code-keyword">new</span> ArrayList&lt;Element&gt;();
        elements.add(DOMUtils.readXml(<span class="code-keyword">new</span> StringReader(MtomTestHelper.RESP_MESSAGE)).getDocumentElement());
        CxfPayload&lt;SoapHeader&gt; body = <span class="code-keyword">new</span> CxfPayload&lt;SoapHeader&gt;(<span class="code-keyword">new</span> ArrayList&lt;SoapHeader&gt;(),
            elements);
        exchange.getOut().setBody(body);
        exchange.getOut().addAttachment(MtomTestHelper.RESP_PHOTO_CID, 
            <span class="code-keyword">new</span> DataHandler(<span class="code-keyword">new</span> ByteArrayDataSource(MtomTestHelper.RESP_PHOTO_DATA, <span class="code-quote">"application/octet-stream"</span>)));

        exchange.getOut().addAttachment(MtomTestHelper.RESP_IMAGE_CID, 
            <span class="code-keyword">new</span> DataHandler(<span class="code-keyword">new</span> ByteArrayDataSource(MtomTestHelper.responseJpeg, <span class="code-quote">"image/jpeg"</span>)));

    }
}
</pre>
</div></div>

<p><b>Message Mode:</b> Attachments are not supported as it does not process the message at all.</p>

<h3><a name="CXF-SeeAlso"></a>See Also</h3>
<ul>
	<li><a href="/confluence/display/CAMEL/Configuring+Camel" title="Configuring Camel">Configuring Camel</a></li>
	<li><a href="/confluence/display/CAMEL/Component" title="Component">Component</a></li>
	<li><a href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a></li>
	<li><a href="/confluence/display/CAMEL/Getting+Started" title="Getting Started">Getting Started</a></li>
</ul>

    </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/CXF">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=52098&revisedVersion=88&originalVersion=87">View Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/CXF?showComments=true&amp;showCommentArea=true#addcomment">Add Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message