You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Validate

Available as of Camel 2.3

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.

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.

Using from Java DSL

In the route below it will read the file content and validate it against a regular expression.

from("file://inbox")
  .validate(body(String.class).regex("^\\w{10}\\,\\d{2}\\,\\w{24}$"))
.to("bean:MyServiceBean.processLine");

Validate is not limited to the message body. You can also validate the message header.

from("file://inbox")
  .validate(header("bar").isGreaterThan(100))
.to("bean:MyServiceBean.processLine");

And sure, you can use validate together with simple.

from("file://inbox")
  .validate(simple("${in.header.bar} == 100"))
.to("bean:MyServiceBean.processLine");

Using from Spring DSL

To use validate in the Spring DSL, the easiest way is to use simple.

<route>
  <from uri="file://inbox"/>
  <validate>
    <simple>${body} regex ^\\w{10}\\,\\d{2}\\,\\w{24}$</simple>
  </validate>
  <beanRef ref="myServiceBean" method="processLine"/>
</route>

<bean id="myServiceBean" class="com.mycompany.MyServiceBean"/>

The XML DSL to validate the message header would looks like this.

<route>
  <from uri="file://inbox"/>
  <validate>
    <simple>${in.header.bar} == 100</simple>
  </validate>
  <beanRef ref="myServiceBean" method="processLine"/>
</route>

<bean id="myServiceBean" class="com.mycompany.MyServiceBean"/>

Using This Pattern

If you would like to use this EIP Pattern then please read the Getting Started, you may also find the Architecture useful particularly the description of Endpoint and URIs. Then you could try out some of the Examples first before trying this pattern out.

  • No labels