camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Validate
Date Wed, 26 May 2010 20:04:00 GMT
<html>
<head>
    <base href="http://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="http://cwiki.apache.org/confluence/display/CAMEL/Validate">Validate</a></h2>
    <h4>Page  <b>added</b> by             <a href="http://cwiki.apache.org/confluence/display/~muellerc">Christian
Mueller</a>
    </h4>
         <br/>
    <div class="notificationGreySide">
         <h2><a name="Validate-Validate"></a>Validate</h2>
<p><b>Available as of Camel 2.3</b></p>

<p>Validate can be used to validate a message. Imagine you consume text files and before
processing each file you want to be sure the content is valid.</p>

<p>You can use the validate DSL with all kind of Predicates and Expressions. Validate
will just evaluated the Predicate/Expression and if its false an PredicateValidationException
is thrown.</p>

<h3><a name="Validate-UsingfromJavaDSL"></a>Using from Java DSL</h3>
<p>In the route below it will read the file content and validate it against a regular
expression. </p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"file:<span class="code-comment">//inbox"</span>)
</span>  .validate(body(<span class="code-object">String</span>.class).regex(<span
class="code-quote">"^\\w{10}\\,\\d{2}\\,\\w{24}$"</span>))
.to(<span class="code-quote">"bean:MyServiceBean.processLine"</span>);
</pre>
</div></div>

<p>Validate is not limited to the message body. You can also validate the message header.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"file:<span class="code-comment">//inbox"</span>)
</span>  .validate(header(<span class="code-quote">"bar"</span>).isGreaterThan(100))
.to(<span class="code-quote">"bean:MyServiceBean.processLine"</span>);
</pre>
</div></div>

<p>And sure, you can use validate together with <a href="http://camel.apache.org/simple.html"
class="external-link" rel="nofollow">simple</a>.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"file:<span class="code-comment">//inbox"</span>)
</span>  .validate(simple(<span class="code-quote">"${in.header.bar} == 100"</span>))
.to(<span class="code-quote">"bean:MyServiceBean.processLine"</span>);
</pre>
</div></div>


<h3><a name="Validate-UsingfromSpringDSL"></a>Using from Spring DSL</h3>

<p>To use validate in the Spring DSL, the easiest way is to use <a href="http://camel.apache.org/simple.html"
class="external-link" rel="nofollow">simple</a>.</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">"file://inbox"</span>/&gt;</span>
  <span class="code-tag">&lt;validate&gt;</span>
    <span class="code-tag">&lt;simple&gt;</span>${body} regex ^\\w{10}\\,\\d{2}\\,\\w{24}$<span
class="code-tag">&lt;/simple&gt;</span>
  <span class="code-tag">&lt;/validate&gt;</span>
  <span class="code-tag">&lt;beanRef ref=<span class="code-quote">"myServiceBean"</span>
method=<span class="code-quote">"processLine"</span>/&gt;</span>
<span class="code-tag">&lt;/route&gt;</span>

<span class="code-tag">&lt;bean id=<span class="code-quote">"myServiceBean"</span>
class=<span class="code-quote">"com.mycompany.MyServiceBean"</span>/&gt;</span>
</pre>
</div></div>

<p>The XML DSL to validate the message header would 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">"file://inbox"</span>/&gt;</span>
  <span class="code-tag">&lt;validate&gt;</span>
    <span class="code-tag">&lt;simple&gt;</span>${in.header.bar} == 100<span
class="code-tag">&lt;/simple&gt;</span>
  <span class="code-tag">&lt;/validate&gt;</span>
  <span class="code-tag">&lt;beanRef ref=<span class="code-quote">"myServiceBean"</span>
method=<span class="code-quote">"processLine"</span>/&gt;</span>
<span class="code-tag">&lt;/route&gt;</span>

<span class="code-tag">&lt;bean id=<span class="code-quote">"myServiceBean"</span>
class=<span class="code-quote">"com.mycompany.MyServiceBean"</span>/&gt;</span>
</pre>
</div></div>

<h4><a name="Validate-UsingThisPattern"></a>Using This Pattern</h4>

<p>If you would like to use this EIP Pattern then please read the <a href="/confluence/display/CAMEL/Getting+Started"
title="Getting Started">Getting Started</a>, you may also find the <a href="/confluence/display/CAMEL/Architecture"
title="Architecture">Architecture</a> useful particularly the description of <a
href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a> and <a
href="/confluence/display/CAMEL/URIs" title="URIs">URIs</a>. Then you could try out
some of the <a href="/confluence/display/CAMEL/Examples" title="Examples">Examples</a>
first before trying this pattern out.</p>
    </div>
    <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>
       <a href="http://cwiki.apache.org/confluence/display/CAMEL/Validate">View Online</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/CAMEL/Validate?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
           </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message