Transforming using a Simple Expression

When you want to transform a message in a relatively straightforward way, you use Camel's transform statement along with one of the Expression Languages provided by the framework. For example, Camel's Simple Expression Language provides you with a quick, inline mechanism for straightforward transformations.

This recipe will show you how to use Camel's Simple Expression Language to transform the message body.

Getting ready

The Java code for this recipe is located in the org.camelcookbook.transformation.simple package. Spring XML files are located under src/main/resources/META-INF/spring and are prefixed with simple.

How to do it...

In a Camel route, use a transform DSL statement containing the Expression Language code to do your transformation.

In the XML DSL, this is written as follows:

<route>
  <from uri="direct:start"/>
  <transform>
    <simple>Hello ${body}</simple>
  </transform>
</route>

In the Java DSL, the same route is expressed as:

from("direct:start")
  .transform(simple("Hello ${body}"));

In this example, the message transformation prefixes the incoming message with the phrase Hello using the Simple Expression Language.

The processing step after the transform statement will see the transformed message content in the body of the exchange.

How it works...

Camel's Simple Expression Language is quite good at manipulating the String content through its access to all aspects of the message being processed, through its rich String and logical operators.

The result of your Simple Expression becomes the new message body after the transform step. This includes predicates such as using Simple's logical operators, to evaluate a true or false condition; the results of that Boolean operation become the new message body containing a String: "true" or "false".

Tip

The advantage of using a distinct transform step within a route, as opposed to embedding it within a processor, is that the logic is clearly visible to other programmers. Ensure that the expression embedded within your route is kept simple so as to not distract the next developer from the overall purpose of the integration. It is best to move more complex (or just lengthy) transformation logic into its own subroute, and invoke it using direct: or seda:. See the Reusing routing logic by connecting routes recipe in Chapter 1, Structuring Routes for more details.

There's more...

The transform statement will work with any Expression Language available in Camel, so if you need more powerful message processing capabilities you can leverage scripting languages such as Groovy or JavaScript (among many others) as well. The Transforming inline with XQuery recipe will show you how to use the XQuery Expression Language to do transformations on XML messages.

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.42.116