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 Proxy Example
Date Sun, 17 Oct 2010 09:48: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+Proxy+Example">CXF
Proxy Example</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
        <br/>
                         <h4>Changes (1)</h4>
                                 
    
<div id="page-diffs">
            <table class="diff" cellpadding="0" cellspacing="0">
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >The reply from the real web service
is then logged and use as reply for the proxied web service as well. <br> <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words">{snippet:id=e1|lang=xml|url=camel/trunk/examples/camel-example-cxf-proxy/src/main/resources/<span
class="diff-added-chars"style="background-color: #dfd;">META-INF/spring/</span>camel-config.xml}</span>
<br></td></tr>
            <tr><td class="diff-unchanged" > <br>h4. Enrich bean <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
        </table>
</div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="CXFProxyExample-CXFProxyExample"></a>CXF Proxy Example</h2>
<p><b>Available as of Camel 2.5</b></p>

<p>This example is located in the <tt>examples/camel-example-cxf-proxy</tt>
directory of the Camel distribution.<br/>
There is a <tt>README.txt</tt> file with instructions how to run it.</p>

<p>If you use maven then you can easily run it from the command line using:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
mvn jetty:run
</pre>
</div></div>

<h3><a name="CXFProxyExample-About"></a>About</h3>

<p>This example demonstrates how you can use <a href="/confluence/display/CAMEL/CXF"
title="CXF">CXF</a> to proxy a real web service. For example in this case we want
Camel to validate and enrich the input message before its send to the real web service. We
do this to ensure the data is correct and that missing information is added automatic by Camel.
In real life you may want to do this in case a client send bad/faulty data to a web service,
and its costly/not possible to update and fix that client. Instead lets fix it by a proxy
with Camel.</p>

<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>Pure HTTP proxy</b><br
/>If you want a pure http based proxy then you can use <a href="/confluence/display/CAMEL/Jetty"
title="Jetty">Jetty</a> or <a href="/confluence/display/CAMEL/SERVLET" title="SERVLET">SERVLET</a>
as front end in Camel. Then delegate those incoming HTTP requests to the real web service.
You may want to do this if you want a simple straight thru proxy where the message should
not be altered by Camel.<br/>
By using the <a href="/confluence/display/CAMEL/CXF" title="CXF">CXF</a> component
you get access to all the web service features Apache CXF provides, where as with <a href="/confluence/display/CAMEL/Jetty"
title="Jetty">Jetty</a> its just pure HTTP proxy.</td></tr></table></div>

<h3><a name="CXFProxyExample-Implementation"></a>Implementation</h3>

<p>Since this is a example it provides both the Camel application and the real web service
in the same JVM. In your use-case the real webs ervice will be hosted on another server etc.</p>

<h4><a name="CXFProxyExample-SpringXML"></a>Spring XML</h4>

<p>In the Spring XML file we have define the <a href="/confluence/display/CAMEL/CXF"
title="CXF">CXF</a> proxy endpoint using the <tt>&lt;cxf:cxfEndpoint&gt;</tt>
tag.<br/>
And the real web service is the Spring bean with the id <tt>realWebService</tt>.</p>

<p>As you can see in the Camel route we use a <a href="/confluence/display/CAMEL/CXF"
title="CXF">CXF</a> consumer to proxy the web service. Then we route the message
to the enrich bean which validates and add the missing information. Then we just use the <a
href="/confluence/display/CAMEL/HTTP" title="HTTP">HTTP</a> component to send the
web service request to the real web service.<br/>
The reply from the real web service is then logged and use as reply for the proxied web service
as well.</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:camel</span>=<span class="code-quote">"http://camel.apache.org/schema/spring"</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.xsd
       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
       http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd"&gt;

    <span class="code-tag"><span class="code-comment">&lt;!-- needed cxf imports
--&gt;</span></span>
    <span class="code-tag">&lt;import resource=<span class="code-quote">"classpath:META-INF/cxf/cxf.xml"</span>/&gt;</span>
    <span class="code-tag">&lt;import resource=<span class="code-quote">"classpath:META-INF/cxf/cxf-extension-soap.xml"</span>/&gt;</span>
    <span class="code-tag">&lt;import resource=<span class="code-quote">"classpath:META-INF/cxf/cxf-extension-http-jetty.xml"</span>/&gt;</span>

    <span class="code-tag"><span class="code-comment">&lt;!-- use a bean to
start and stop the real web service (is not Camel specific) --&gt;</span></span>
    &lt;!-- in a real use-case the real web service would be located on another server
         but we simulate this in the same JVM --&gt;
    &lt;bean id=<span class="code-quote">"realWebService"</span> class=<span
class="code-quote">"org.apache.camel.example.cxf.proxy.RealWebServiceBean"</span>
          init-method=<span class="code-quote">"start"</span> destroy-method=<span
class="code-quote">"stop"</span>&gt;
        <span class="code-tag"><span class="code-comment">&lt;!-- this is
the url of the real web service we have proxied --&gt;</span></span>
        <span class="code-tag">&lt;property name=<span class="code-quote">"url"</span>
value=<span class="code-quote">"http://localhost:9081/real-webservice"</span>/&gt;</span>
    <span class="code-tag">&lt;/bean&gt;</span>

    <span class="code-tag"><span class="code-comment">&lt;!-- a bean to enrich
the input --&gt;</span></span>
    <span class="code-tag">&lt;bean id=<span class="code-quote">"enrichBean"</span>
class=<span class="code-quote">"org.apache.camel.example.cxf.proxy.EnrichBean"</span>/&gt;</span>

    <span class="code-tag"><span class="code-comment">&lt;!-- this is the
CXF webservice we use as front end --&gt;</span></span>
    &lt;cxf:cxfEndpoint id=<span class="code-quote">"reportIncident"</span>
                     address=<span class="code-quote">"http://localhost:9080/camel-example-cxf-proxy/webservices/incident"</span>
                     endpointName=<span class="code-quote">"s:ReportIncidentEndpoint"</span>
    		         serviceName=<span class="code-quote">"s:ReportIncidentEndpointService"</span>
                     wsdlURL=<span class="code-quote">"etc/report_incident.wsdl"</span>
    	             <span class="code-keyword">xmlns:s</span>=<span class="code-quote">"http://reportincident.example.camel.apache.org"</span>/&gt;

    <span class="code-tag"><span class="code-comment">&lt;!-- this is the
camel route which proxy the web service and forward it to the real web service --&gt;</span></span>
    <span class="code-tag">&lt;camelContext xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>

        <span class="code-tag">&lt;route&gt;</span>
            <span class="code-tag"><span class="code-comment">&lt;!-- cxf
consumer using MESSAGE format --&gt;</span></span>
            <span class="code-tag">&lt;from uri=<span class="code-quote">"cxf:bean:reportIncident?dataFormat=MESSAGE"</span>/&gt;</span>
            <span class="code-tag"><span class="code-comment">&lt;!-- log
input received --&gt;</span></span>
            <span class="code-tag">&lt;to uri=<span class="code-quote">"log:input"</span>/&gt;</span>
            <span class="code-tag"><span class="code-comment">&lt;!-- enrich
the input by ensure the incidentId parameter is set --&gt;</span></span>
            <span class="code-tag">&lt;to uri=<span class="code-quote">"bean:enrichBean"</span>/&gt;</span>
            <span class="code-tag"><span class="code-comment">&lt;!-- send
proxied request to real web service --&gt;</span></span>
            <span class="code-tag">&lt;to uri=<span class="code-quote">"http://localhost:9081/real-webservice?throwExceptionOnFailure=false"</span>/&gt;</span>
            <span class="code-tag"><span class="code-comment">&lt;!-- log
answer from real web service --&gt;</span></span>
            <span class="code-tag">&lt;to uri=<span class="code-quote">"log:output"</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>

<h4><a name="CXFProxyExample-Enrichbean"></a>Enrich bean</h4>

<p>The enrich bean is Java code which in our simple example will just set the incidentId
parameter to the fixed value of 456. In your implementation you can of course do a lot more.</p>

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

    <span class="code-keyword">public</span> Document enrich(Document doc) {
        Node node = doc.getElementsByTagName(<span class="code-quote">"incidentId"</span>).item(0);
        <span class="code-object">String</span> incident = node.getTextContent();

        <span class="code-comment">// here we enrich the document by changing the incident
id to another value
</span>        <span class="code-comment">// you can of course <span class="code-keyword">do</span>
a lot more in your use-<span class="code-keyword">case</span>
</span>        node.setTextContent(<span class="code-quote">"456"</span>);
        <span class="code-object">System</span>.out.println(<span class="code-quote">"Incident
was "</span> + incident + <span class="code-quote">", changed to 456"</span>);

        <span class="code-keyword">return</span> doc;
    }
}
</pre>
</div></div>

<h3><a name="CXFProxyExample-Runningtheexample"></a>Running the example</h3>

<p>You start the example from the command line using the maven goal <tt>mvn jetty:run</tt>.
There is also a <tt>build.xml</tt> file for ANT users.<br/>
You can also start the Camel application from your IDE, by running the <tt>org.apache.camel.example.cxf.proxy.MyMain</tt>
main class.</p>

<p>You can then use SoapUI or another web service client and send a request to the <tt><a
href="http://localhost:9080/camel-example-cxf-proxy/webservices/incident" class="external-link"
rel="nofollow">http://localhost:9080/camel-example-cxf-proxy/webservices/incident</a></tt>
url. The wsdl is located at: <tt><a href="http://localhost:9080/camel-example-cxf-proxy/webservices/incident?wsdl"
class="external-link" rel="nofollow">http://localhost:9080/camel-example-cxf-proxy/webservices/incident?wsdl</a></tt>.</p>

<p>The console should then output progress.</p>

<h3><a name="CXFProxyExample-Sampleoutput"></a>Sample output</h3>

<p>Here is some sample output from the console:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
2010-09-26 12:20:46,974 [main           ] INFO  DefaultCamelContext            - Apache Camel
2.5-SNAPSHOT (CamelContext: camel-1) started in 0.858 seconds
2010-09-26 12:20:55,685 [tp-1790017034-1] INFO  input                          - Exchange[ExchangePattern:InOut,
BodyType:<span class="code-keyword">null</span>, Body:&lt;soapenv:Envelope
xmlns:soapenv=<span class="code-quote">"http:<span class="code-comment">//schemas.xmlsoap.org/soap/envelope/"</span>
xmlns:rep=<span class="code-quote">"http://reportincident.example.camel.apache.org"</span>&gt;
</span>   &lt;soapenv:Header/&gt;
   &lt;soapenv:Body&gt;
      &lt;rep:inputReportIncident&gt;
         &lt;incidentId&gt;&lt;/incidentId&gt;
         &lt;incidentDate&gt;2010-09-26&lt;/incidentDate&gt;
         &lt;givenName&gt;Ibsen&lt;/givenName&gt;
         &lt;familyName&gt;Claus&lt;/familyName&gt;
         &lt;summary&gt;Bla bla&lt;/summary&gt;
         &lt;details&gt;More bla&lt;/details&gt;
         &lt;email&gt;davsclaus@apache.org&lt;/email&gt;
         &lt;phone&gt;12345678&lt;/phone&gt;
      &lt;/rep:inputReportIncident&gt;
   &lt;/soapenv:Body&gt;
&lt;/soapenv:Envelope&gt;]
Incident was , changed to 456



Invoked real web service: id=456 by Ibsen Claus



2010-09-26 12:20:55,997 [tp-1790017034-1] INFO  output                         - Exchange[ExchangePattern:InOut,
BodyType:org.apache.camel.converter.stream.CachedOutputStream.WrappedInputStream, Body:
&lt;soap:Envelope xmlns:soap=<span class="code-quote">"http:<span class="code-comment">//schemas.xmlsoap.org/soap/envelope/"</span>&gt;
</span>    &lt;soap:Body&gt;
        &lt;ns2:outputReportIncident xmlns:ns2=<span class="code-quote">"http:<span
class="code-comment">//reportincident.example.camel.apache.org"</span>&gt;
</span>            &lt;code&gt;OK;456&lt;/code&gt;
        &lt;/ns2:outputReportIncident&gt;
    &lt;/soap:Body&gt;
&lt;/soap:Envelope&gt;]
</pre>
</div></div>

<h3><a name="CXFProxyExample-SeeAlso"></a>See Also</h3>
<ul class="alternate" type="square">
	<li><a href="/confluence/display/CAMEL/Examples" title="Examples">Examples</a></li>
	<li><a href="/confluence/display/CAMEL/CXF" title="CXF">CXF</a></li>
	<li><a href="/confluence/display/CAMEL/HTTP" title="HTTP">HTTP</a></li>
	<li><a href="/confluence/display/CAMEL/Jetty" title="Jetty">Jetty</a></li>
	<li><a href="/confluence/display/CAMEL/SERVLET" title="SERVLET">SERVLET</a></li>
	<li><a href="/confluence/display/CAMEL/SOAP" title="SOAP">SOAP</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+Proxy+Example">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=23339001&revisedVersion=5&originalVersion=4">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/CXF+Proxy+Example?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message