camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Babak Vahdat (Confluence)" <conflue...@apache.org>
Subject [CONF] Apache Camel > SOAP
Date Sun, 27 Oct 2013 07:08:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/en/2176/1/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/SOAP">SOAP</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~bvahdat">Babak
Vahdat</a>
    </h4>
        <br/>
                         <h4>Changes (2)</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" >{code} <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >When using XML DSL there is a
version attribute you can set on the <span class="diff-changed-words">&lt;soap<span
class="diff-added-chars"style="background-color: #dfd;">jaxb</span>&gt;</span>
element. <br></td></tr>
            <tr><td class="diff-unchanged" >{code:xml} <br>    &lt;!--
Defining a ServiceInterfaceStrategy for retrieving the element name when marshalling --&gt;
<br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >  &lt;from uri=&quot;direct:start&quot;/&gt;
<br>  &lt;marshal&gt; <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words">&lt;soap<span
class="diff-added-chars"style="background-color: #dfd;">jaxb</span></span>
contentPath=&quot;com.example.customerservice&quot; version=&quot;1.2&quot;
elementNameStrategyRef=&quot;myNameStrategy&quot;/&gt; <br></td></tr>
            <tr><td class="diff-unchanged" >  &lt;/marshal&gt; <br>
 &lt;to uri=&quot;jms:myQueue&quot;/&gt; <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="SOAP-SOAPDataFormat"></a>SOAP DataFormat</h2>
<p><b>Available as of Camel 2.3</b></p>

<p>SOAP is a <a href="/confluence/display/CAMEL/Data+Format" title="Data Format">Data
Format</a> which uses JAXB2 and JAX-WS annotations to marshal and unmarshal SOAP payloads.
It provides the basic features of Apache CXF without need for the CXF Stack.</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>Supported
SOAP versions</b><br />SOAP 1.1 is supported by default. SOAP 1.2 is supported
from Camel 2.11 onwards.</td></tr></table></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>Namespace prefix mapping</b><br
/>See <a href="/confluence/display/CAMEL/JAXB" title="JAXB">JAXB</a> for details
how you can control namespace prefix mappings when marshalling using <a href="/confluence/display/CAMEL/SOAP"
title="SOAP">SOAP</a> data format.</td></tr></table></div>

<h3><a name="SOAP-ElementNameStrategy"></a>ElementNameStrategy</h3>

<p>An element name strategy is used for two purposes. The first is to find a xml element
name for a given object and soap action when marshaling the object into a SOAP message. The
second is to find an Exception class for a given soap fault name.</p>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Strategy </th>
<th class='confluenceTh'> Usage </th>
</tr>
<tr>
<td class='confluenceTd'> QNameStrategy </td>
<td class='confluenceTd'> Uses a fixed qName that is configured on instantiation. Exception
lookup is not supported </td>
</tr>
<tr>
<td class='confluenceTd'> TypeNameStrategy </td>
<td class='confluenceTd'> Uses the name and namespace from the @XMLType annotation of
the given type. If no namespace is set then package-info is used. Exception lookup is not
supported </td>
</tr>
<tr>
<td class='confluenceTd'> ServiceInterfaceStrategy </td>
<td class='confluenceTd'> Uses information from a webservice interface to determine
the type name and to find the exception class for a SOAP fault</td>
</tr>
</tbody></table>
</div>


<p>If you have generated the web service stub code with cxf-codegen or a similar tool
then you probably will want to use the ServiceInterfaceStrategy. In the case you have no annotated
service interface you should use QNameStrategy or TypeNameStrategy.</p>

<h3><a name="SOAP-UsingtheJavaDSL"></a>Using the Java DSL</h3>

<p>The following example uses a named DataFormat of <em>soap</em> which
is configured with the package com.example.customerservice to initialize the <a href="http://java.sun.com/javase/6/docs/api/javax/xml/bind/JAXBContext.html"
class="external-link" rel="nofollow">JAXBContext</a>. The second parameter is the
ElementNameStrategy. The route is able to marshal normal objects as well as exceptions.  (Note
the below just sends a SOAP Envelope to a queue. A web service provider would actually need
to be listening to the queue for a SOAP call to actually occur, in which case it would be
a one way SOAP request. If you need request reply then you should look at the next example.)</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
SoapJaxbDataFormat soap = new SoapJaxbDataFormat("com.example.customerservice", new ServiceInterfaceStrategy(CustomerService.class));
from("direct:start")
  .marshal(soap)
  .to("jms:myQueue");
</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>See also</b><br
/>As the SOAP dataformat inherits from the <a href="/confluence/display/CAMEL/JAXB"
title="JAXB">JAXB</a> dataformat most settings apply here as well</td></tr></table></div>

<h4><a name="SOAP-UsingSOAP1.2"></a>Using SOAP 1.2</h4>
<p><b>Available as of Camel 2.11</b></p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
SoapJaxbDataFormat soap = new SoapJaxbDataFormat("com.example.customerservice", new ServiceInterfaceStrategy(CustomerService.class));
soap.setVersion("1.2");
from("direct:start")
  .marshal(soap)
  .to("jms:myQueue");
</pre>
</div></div>

<p>When using XML DSL there is a version attribute you can set on the &lt;soapjaxb&gt;
element.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: xml; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
    &lt;!-- Defining a ServiceInterfaceStrategy for retrieving the element name when marshalling
--&gt;
    &lt;bean id="myNameStrategy" class="org.apache.camel.dataformat.soap.name.ServiceInterfaceStrategy"&gt;
    	&lt;constructor-arg value="com.example.customerservice.CustomerService"/&gt;
	&lt;constructor-arg value="true"/&gt;
    &lt;/bean&gt;
</pre>
</div></div>

<p>And in the Camel route</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: xml; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
&lt;route&gt;
  &lt;from uri="direct:start"/&gt;
  &lt;marshal&gt;
    &lt;soapjaxb contentPath="com.example.customerservice" version="1.2" elementNameStrategyRef="myNameStrategy"/&gt;
  &lt;/marshal&gt;
  &lt;to uri="jms:myQueue"/&gt;
&lt;/route&gt;
</pre>
</div></div>


<h3><a name="SOAP-MultipartMessages"></a>Multi-part Messages</h3>
<p><b>Available as of Camel 2.8.1</b> </p>

<p>Multi-part SOAP messages are supported by the ServiceInterfaceStrategy. The ServiceInterfaceStrategy
must be initialized with a service interface definition that is annotated in accordance with
JAX-WS 2.2 and meets the requirements of the Document Bare style. The target method must meet
the following criteria, as per the JAX-WS specification:  1) it must have at most one <tt>in</tt>
or <tt>in/out</tt> non-header parameter, 2) if it has a return type other than
<tt>void</tt> it must have no <tt>in/out</tt> or <tt>out</tt>
non-header parameters, 3) if it it has a return type of <tt>void</tt> it must
have at most one <tt>in/out</tt> or <tt>out</tt> non-header parameter.</p>

<p>The ServiceInterfaceStrategy should be initialized with a boolean parameter that
indicates whether the mapping strategy applies to the request parameters or response parameters.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
ServiceInterfaceStrategy strat =  new ServiceInterfaceStrategy(com.example.customerservice.multipart.MultiPartCustomerService.class,
true);
SoapJaxbDataFormat soapDataFormat = new SoapJaxbDataFormat("com.example.customerservice.multipart",
strat);
</pre>
</div></div>

<h4><a name="SOAP-MultipartRequest"></a>Multi-part Request</h4>

<p>The payload parameters for a multi-part request are initiazlied using a <tt>BeanInvocation</tt>
object that reflects the signature of the target operation. The camel-soap DataFormat maps
the content in the <tt>BeanInvocation</tt> to fields in the SOAP header and body
in accordance with the JAX-WS mapping when the <tt>marshal()</tt> processor is
invoked. </p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
BeanInvocation beanInvocation = new BeanInvocation();

// Identify the target method
beanInvocation.setMethod(MultiPartCustomerService.class.getMethod("getCustomersByName", 
    GetCustomersByName.class, com.example.customerservice.multipart.Product.class));

// Populate the method arguments
GetCustomersByName getCustomersByName = new GetCustomersByName();
getCustomersByName.setName("Dr. Multipart");
                
Product product = new Product();
product.setName("Multiuse Product");
product.setDescription("Useful for lots of things.");
                
Object[] args = new Object[] {getCustomersByName, product};

// Add the arguments to the bean invocation
beanInvocation.setArgs(args);

// Set the bean invocation object as the message body
exchange.getIn().setBody(beanInvocation); 
</pre>
</div></div>

<h4><a name="SOAP-MultipartResponse"></a>Multi-part Response</h4>

<p>A multi-part soap response may include an element in the soap body and will have
one or more elements in the soap header.  The camel-soap DataFormat will unmarshall the element
in the soap body (if it exists) and place it onto the body of the out message in the exchange.
 Header elements will <b>not</b> be marshaled into their JAXB mapped object types.
 Instead, these elements are placed into the camel out message header <tt>org.apache.camel.dataformat.soap.UNMARSHALLED_HEADER_LIST</tt>.
The elements will appear either as element instance values, or as JAXBElement values, depending
upon the setting for the <tt>ignoreJAXBElement</tt> property. This property is
inherited from camel-jaxb.</p>

<p>You can also have the camel-soap DataFormate ignore header content all-together by
setting the <tt>ignoreUnmarshalledHeaders</tt> value to <tt>true</tt>.
</p>

<h4><a name="SOAP-HolderObjectmapping"></a>Holder Object mapping</h4>

<p>JAX-WS specifies the use of a type-parameterized <tt>javax.xml.ws.Holder</tt>
object for <tt>In/Out</tt> and <tt>Out</tt> parameters.  A <tt>Holder</tt>
object may be used when building the <tt>BeanInvocation</tt>, or you may use an
instance of the parameterized-type directly.  The camel-soap DataFormat marshals Holder values
in accordance with the JAXB mapping for the class of the <tt>Holder</tt>'s value.
 No mapping is provided for <tt>Holder</tt> objects in an unmarshalled response.</p>

<h3><a name="SOAP-Examples"></a>Examples</h3>

<h4><a name="SOAP-Webserviceclient"></a>Webservice client</h4>

<p>The following route supports marshalling the request and unmarshalling a response
or fault. </p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
String WS_URI = "cxf://http://myserver/customerservice?serviceClass=com.example.customerservice&amp;dataFormat=MESSAGE";
SoapJaxbDataFormat soapDF = new SoapJaxbDataFormat("com.example.customerservice", new ServiceInterfaceStrategy(CustomerService.class));
from("direct:customerServiceClient")
  .onException(Exception.class)
    .handled(true)
    .unmarshal(soapDF)
  .end()
  .marshal(soapDF)
  .to(WS_URI)
  .unmarshal(soapDF);
</pre>
</div></div>

<p>The below snippet creates a proxy for the service interface and makes a SOAP call
to the above route. </p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
import org.apache.camel.Endpoint;
import org.apache.camel.component.bean.ProxyHelper;
...

Endpoint startEndpoint = context.getEndpoint("direct:customerServiceClient");
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
// CustomerService below is the service endpoint interface, *not* the javax.xml.ws.Service
subclass
CustomerService proxy = ProxyHelper.createProxy(startEndpoint, classLoader, CustomerService.class);
GetCustomersByNameResponse response = proxy.getCustomersByName(new GetCustomersByName());
</pre>
</div></div>

<h4><a name="SOAP-WebserviceServer"></a>Webservice Server</h4>

<p>Using the following route sets up a webservice server that listens on jms queue customerServiceQueue
and processes requests using the class CustomerServiceImpl. The customerServiceImpl of course
should implement the interface CustomerService. Instead of directly instantiating the server
class it could be defined in a spring context as a regular bean.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
SoapJaxbDataFormat soapDF = new SoapJaxbDataFormat("com.example.customerservice", new ServiceInterfaceStrategy(CustomerService.class));
CustomerService serverBean = new CustomerServiceImpl();
from("jms://queue:customerServiceQueue")
  .onException(Exception.class)
    .handled(true)
    .marshal(soapDF)
  .end()
  .unmarshal(soapDF)
  .bean(serverBean)
  .marshal(soapDF);
</pre>
</div></div>

<h3><a name="SOAP-Dependencies"></a>Dependencies</h3>

<p>To use the SOAP dataformat in your camel routes you need to add the following dependency
to your pom.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: xml; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
&lt;dependency&gt;
  &lt;groupId&gt;org.apache.camel&lt;/groupId&gt;
  &lt;artifactId&gt;camel-soap&lt;/artifactId&gt;
  &lt;version&gt;2.3.0&lt;/version&gt;
&lt;/dependency&gt;
</pre>
</div></div>
    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;" class="grey">
                        <a href="https://cwiki.apache.org/confluence/users/removespacenotification.action?spaceKey=CAMEL">Stop
watching space</a>
            <span style="padding: 0px 5px;">|</span>
                <a href="https://cwiki.apache.org/confluence/users/editmyemailsettings.action">Change
email notification preferences</a>
</div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/SOAP">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=14811197&revisedVersion=24&originalVersion=23">View
Changes</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message