2.6. Transforming Message Content


Overview

Apache Camel supports a variety of approaches to transforming message content. In addition to a simple native API for modifying message content, Apache Camel supports integration with several different third-party libraries and transformation standards. The following kinds of transformations are discussed in this section:

Simple transformations

The Java DSL has a built-in API that enables you to perform simple transformations on incoming and outgoing messages. For example, the rule shown in Example 2.1, “Simple Transformation of Incoming Messages” appends the text, World!, to the end of the incoming message body.

Example 2.1. Simple Transformation of Incoming Messages

from("SourceURL").setBody(body().append(" World!")).to("TargetURL");
Where the setBody() command replaces the content of the incoming message's body. You can use the following API classes to perform simple transformations of the message content in a router rule:
  • org.apache.camel.model.ProcessorDefinition
  • org.apache.camel.builder.Builder
  • org.apache.camel.builder.ValueBuilder

ProcessorDefinition class

The org.apache.camel.model.ProcessorDefinition class defines the DSL commands you can insert directly into a router rule—for example, the setBody() command in Example 2.1, “Simple Transformation of Incoming Messages”. Table 2.5, “Transformation Methods from the ProcessorDefinition Class” shows the ProcessorDefinition methods that are relevant to transforming message content:
Table 2.5. Transformation Methods from the ProcessorDefinition Class
MethodDescription
Type convertBodyTo(Class type) Converts the IN message body to the specified type.
Type removeFaultHeader(String name) Adds a processor which removes the header on the FAULT message.
Type removeHeader(String name) Adds a processor which removes the header on the IN message.
Type removeProperty(String name) Adds a processor which removes the exchange property.
ExpressionClause<ProcessorDefinition<Type>> setBody() Adds a processor which sets the body on the IN message.
Type setFaultBody(Expression expression) Adds a processor which sets the body on the FAULT message.
Type setFaultHeader(String name, Expression expression) Adds a processor which sets the header on the FAULT message.
ExpressionClause<ProcessorDefinition<Type>> setHeader(String name) Adds a processor which sets the header on the IN message.
Type setHeader(String name, Expression expression) Adds a processor which sets the header on the IN message.
ExpressionClause<ProcessorDefinition<Type>> setOutHeader(String name) Adds a processor which sets the header on the OUT message.
Type setOutHeader(String name, Expression expression) Adds a processor which sets the header on the OUT message.
ExpressionClause<ProcessorDefinition<Type>> setProperty(String name) Adds a processor which sets the exchange property.
Type setProperty(String name, Expression expression) Adds a processor which sets the exchange property.
ExpressionClause<ProcessorDefinition<Type>> transform() Adds a processor which sets the body on the OUT message.
Type transform(Expression expression) Adds a processor which sets the body on the OUT message.

Builder class

The org.apache.camel.builder.Builder class provides access to message content in contexts where expressions or predicates are expected. In other words, Builder methods are typically invoked in the arguments of DSL commands—for example, the body() command in Example 2.1, “Simple Transformation of Incoming Messages”. Table 2.6, “Methods from the Builder Class” summarizes the static methods available in the Builder class.
Table 2.6. Methods from the Builder Class
MethodDescription
static <E extends Exchange> ValueBuilder<E> body() Returns a predicate and value builder for the inbound body on an exchange.
static <E extends Exchange,T> ValueBuilder<E> bodyAs(Class<T> type) Returns a predicate and value builder for the inbound message body as a specific type.
static <E extends Exchange> ValueBuilder<E> constant(Object value) Returns a constant expression.
static <E extends Exchange> ValueBuilder<E> faultBody() Returns a predicate and value builder for the fault body on an exchange.
static <E extends Exchange,T> ValueBuilder<E> faultBodyAs(Class<T> type) Returns a predicate and value builder for the fault message body as a specific type.
static <E extends Exchange> ValueBuilder<E> header(String name) Returns a predicate and value builder for headers on an exchange.
static <E extends Exchange> ValueBuilder<E> outBody() Returns a predicate and value builder for the outbound body on an exchange.
static <E extends Exchange> ValueBuilder<E> outBodyAs(Class<T> type) Returns a predicate and value builder for the outbound message body as a specific type.
static ValueBuilder property(String name) Returns a predicate and value builder for properties on an exchange.
static ValueBuilder regexReplaceAll(Expression content, String regex, Expression replacement) Returns an expression that replaces all occurrences of the regular expression with the given replacement.
static ValueBuilder regexReplaceAll(Expression content, String regex, String replacement) Returns an expression that replaces all occurrences of the regular expression with the given replacement.
static ValueBuilder sendTo(String uri) Returns an expression processing the exchange to the given endpoint uri.
static <E extends Exchange> ValueBuilder<E> systemProperty(String name) Returns an expression for the given system property.
static <E extends Exchange> ValueBuilder<E> systemProperty(String name, String defaultValue) Returns an expression for the given system property.

ValueBuilder class

The org.apache.camel.builder.ValueBuilder class enables you to modify values returned by the Builder methods. In other words, the methods in ValueBuilder provide a simple way of modifying message content. Table 2.7, “Modifier Methods from the ValueBuilder Class” summarizes the methods available in the ValueBuilder class. That is, the table shows only the methods that are used to modify the value they are invoked on (for full details, see the API Reference documentation).
Table 2.7. Modifier Methods from the ValueBuilder Class
MethodDescription
ValueBuilder<E> append(Object value) Appends the string evaluation of this expression with the given value.
Predicate contains(Object value) Create a predicate that the left hand expression contains the value of the right hand expression.
ValueBuilder<E> convertTo(Class type) Converts the current value to the given type using the registered type converters.
ValueBuilder<E> convertToString() Converts the current value a String using the registered type converters.
Predicate endsWith(Object value)  
<T> T evaluate(Exchange exchange, Class<T> type)  
Predicate in(Object... values)  
Predicate in(Predicate... predicates)  
Predicate isEqualTo(Object value) Returns true, if the current value is equal to the given value argument.
Predicate isGreaterThan(Object value) Returns true, if the current value is greater than the given value argument.
Predicate isGreaterThanOrEqualTo(Object value) Returns true, if the current value is greater than or equal to the given value argument.
Predicate isInstanceOf(Class type) Returns true, if the current value is an instance of the given type.
Predicate isLessThan(Object value) Returns true, if the current value is less than the given value argument.
Predicate isLessThanOrEqualTo(Object value) Returns true, if the current value is less than or equal to the given value argument.
Predicate isNotEqualTo(Object value) Returns true, if the current value is not equal to the given value argument.
Predicate isNotNull() Returns true, if the current value is not null.
Predicate isNull() Returns true, if the current value is null.
Predicate matches(Expression expression)  
Predicate not(Predicate predicate) Negates the predicate argument.
ValueBuilder prepend(Object value) Prepends the string evaluation of this expression to the given value.
Predicate regex(String regex)  
ValueBuilder<E> regexReplaceAll(String regex, Expression<E> replacement) Replaces all occurrencies of the regular expression with the given replacement.
ValueBuilder<E> regexReplaceAll(String regex, String replacement) Replaces all occurrencies of the regular expression with the given replacement.
ValueBuilder<E> regexTokenize(String regex) Tokenizes the string conversion of this expression using the given regular expression.
ValueBuilder sort(Comparator comparator) Sorts the current value using the given comparator.
Predicate startsWith(Object value) Returns true, if the current value matches the string value of the value argument.
ValueBuilder<E> tokenize() Tokenizes the string conversion of this expression using the comma token separator.
ValueBuilder<E> tokenize(String token) Tokenizes the string conversion of this expression using the given token separator.

Marshalling and unmarshalling

You can convert between low-level and high-level message formats using the following commands:
  • marshal()— Converts a high-level data format to a low-level data format.
  • unmarshal() — Converts a low-level data format to a high-level data format.
Apache Camel supports marshalling and unmarshalling of the following data formats:
  • Java serialization — Enables you to convert a Java object to a blob of binary data. For this data format, unmarshalling converts a binary blob to a Java object, and marshalling converts a Java object to a binary blob. For example, to read a serialized Java object from an endpoint, SourceURL, and convert it to a Java object, you use a rule like the following:
    from("SourceURL").unmarshal().serialization()
    .<FurtherProcessing>.to("TargetURL");
    Or alternatively, in Spring XML:
    <camelContext id="serialization" xmlns="http://camel.apache.org/schema/spring">
      <route>
        <from uri="SourceURL"/>
        <unmarshal>
          <serialization/>
        </unmarshal>
        <to uri="TargetURL"/>
      </route>
    </camelContext>
  • JAXB — Provides a mapping between XML schema types and Java types (see https://jaxb.dev.java.net/). For JAXB, unmarshalling converts an XML data type to a Java object, and marshalling converts a Java object to an XML data type. Before you can use JAXB data formats, you must compile your XML schema using a JAXB compiler to generate the Java classes that represent the XML data types in the schema. This is called binding the schema. After the schema is bound, you define a rule to unmarshal XML data to a Java object, using code like the following:
    org.apache.camel.spi.DataFormat jaxb = new org.apache.camel.model.dataformat.JaxbDataFormat("GeneratedPackageName");
    
    from("SourceURL").unmarshal(jaxb)
    .<FurtherProcessing>.to("TargetURL");
    where GeneratedPackagename is the name of the Java package generated by the JAXB compiler, which contains the Java classes representing your XML schema.
    Or alternatively, in Spring XML:
    <camelContext id="jaxb" xmlns="http://camel.apache.org/schema/spring">
      <route>
        <from uri="SourceURL"/>
        <unmarshal>
          <jaxb prettyPrint="true" contextPath="GeneratedPackageName"/>
        </unmarshal>
        <to uri="TargetURL"/>
      </route>
    </camelContext>
  • XMLBeans — Provides an alternative mapping between XML schema types and Java types (see http://xmlbeans.apache.org/). For XMLBeans, unmarshalling converts an XML data type to a Java object and marshalling converts a Java object to an XML data type. For example, to unmarshal XML data to a Java object using XMLBeans, you use code like the following:
    from("SourceURL").unmarshal().xmlBeans()
    .<FurtherProcessing>.to("TargetURL");
    Or alternatively, in Spring XML:
    <camelContext id="xmlBeans" xmlns="http://camel.apache.org/schema/spring">
      <route>
        <from uri="SourceURL"/>
        <unmarshal>
          <xmlBeans prettyPrint="true"/>
        </unmarshal>
        <to uri="TargetURL"/>
      </route>
    </camelContext>
  • XStream — Provides another mapping between XML types and Java types (see http://xstream.codehaus.org/). XStream is a serialization library (like Java serialization), enabling you to convert any Java object to XML. For XStream, unmarshalling converts an XML data type to a Java object, and marshalling converts a Java object to an XML data type. For example, to unmarshal XML data to a Java object using XStream, you use code like the following:
    from("SourceURL").unmarshal().xstream()
    .<FurtherProcessing>.to("TargetURL");
    Note
    The XStream data format is currently not supported in Spring XML.
Red Hat logoGithubRedditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

© 2024 Red Hat, Inc.