Castor

Since Camel 2.1

Castor is a Data Format which uses the Castor XML library to unmarshal an XML payload into Java objects or to marshal Java objects into an XML payload.

As usually you can use either Java DSL or Spring XML to work with Castor Data Format.

Using the Java DSL

from("direct:order").
  marshal().castor().
  to("activemq:queue:order");

For example the following uses a named DataFormat of Castor which uses default Castor data binding features.

CastorDataFormat castor = new CastorDataFormat ();

from("activemq:My.Queue").
  unmarshal(castor).
  to("mqseries:Another.Queue");

If you prefer to use a named reference to a data format which can then be defined in your Registry such as via your Spring XML file. e.g.

from("activemq:My.Queue").
  unmarshal("mycastorType").
  to("mqseries:Another.Queue");

If you want to override default mapping schema by providing a mapping file you can set it as follows.

CastorDataFormat castor = new CastorDataFormat ();
castor.setMappingFile("mapping.xml");

Also if you want to have more control on Castor Marshaller and Unmarshaller you can access them as below.

castor.getMarshaller();
castor.getUnmarshaller();

Using Spring XML

The following example shows how to use Castor to unmarshal using Spring configuring the castor data type

<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
  <route>
    <from uri="direct:start"/>
    <unmarshal>
      <castor validation="true" />
    </unmarshal>
    <to uri="mock:result"/>
  </route>
</camelContext>

This example shows how to configure the data type just once and reuse it on multiple routes. You have to set the <castor> element directly in <camelContext>.

<camelContext>
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
  <dataFormats>
    <castor id="myCastor"/>
  </dataFormats>

  <route>
    <from uri="direct:start"/>
    <marshal ref="myCastor"/>
    <to uri="direct:marshalled"/>
  </route>
  <route>
    <from uri="direct:marshalled"/>
    <unmarshal ref="myCastor"/>
    <to uri="mock:result"/>
  </route>

</camelContext>

Options

The Castor dataformat supports 9 options, which are listed below.

Name Default Java Type Description

mappingFile

String

Path to a Castor mapping file to load from the classpath.

whitelistEnabled

true

Boolean

Define if Whitelist feature is enabled or not

allowedUnmarshallObjects

String

Define the allowed objects to be unmarshalled. You can specify the FQN class name of allowed objects, and you can use comma to separate multiple entries. It is also possible to use wildcards and regular expression which is based on the pattern defined by link org.apache.camel.util.EndpointHelper#matchPattern(String, String). Denied objects takes precedence over allowed objects.

deniedUnmarshallObjects

String

Define the denied objects to be unmarshalled. You can specify the FQN class name of deined objects, and you can use comma to separate multiple entries. It is also possible to use wildcards and regular expression which is based on the pattern defined by link org.apache.camel.util.EndpointHelper#matchPattern(String, String). Denied objects takes precedence over allowed objects.

validation

true

Boolean

Whether validation is turned on or off. Is by default true.

encoding

UTF-8

String

Encoding to use when marshalling an Object to XML. Is by default UTF-8

packages

String[]

Add additional packages to Castor XmlContext

classes

String[]

Add additional class names to Castor XmlContext

contentTypeHeader

false

Boolean

Whether the data format should set the Content-Type header with the type from the data format if the data format is capable of doing so. For example application/xml for data formats marshalling to XML, or application/json for data formats marshalling to JSon etc.

Spring Boot Auto-Configuration

When using Spring Boot make sure to use the following Maven dependency to have support for auto configuration:

<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-castor-starter</artifactId>
  <version>x.x.x</version>
  <!-- use the same version as your Camel core version -->
</dependency>

The component supports 10 options, which are listed below.

Name Description Default Type

camel.dataformat.castor.allowed-unmarshall-objects

Define the allowed objects to be unmarshalled. You can specify the FQN class name of allowed objects, and you can use comma to separate multiple entries. It is also possible to use wildcards and regular expression which is based on the pattern defined by link org.apache.camel.util.EndpointHelper#matchPattern(String, String). Denied objects takes precedence over allowed objects.

String

camel.dataformat.castor.classes

Add additional class names to Castor XmlContext

String[]

camel.dataformat.castor.content-type-header

Whether the data format should set the Content-Type header with the type from the data format if the data format is capable of doing so. For example application/xml for data formats marshalling to XML, or application/json for data formats marshalling to JSon etc.

false

Boolean

camel.dataformat.castor.denied-unmarshall-objects

Define the denied objects to be unmarshalled. You can specify the FQN class name of deined objects, and you can use comma to separate multiple entries. It is also possible to use wildcards and regular expression which is based on the pattern defined by link org.apache.camel.util.EndpointHelper#matchPattern(String, String). Denied objects takes precedence over allowed objects.

String

camel.dataformat.castor.enabled

Enable castor dataformat

true

Boolean

camel.dataformat.castor.encoding

Encoding to use when marshalling an Object to XML. Is by default UTF-8

UTF-8

String

camel.dataformat.castor.mapping-file

Path to a Castor mapping file to load from the classpath.

String

camel.dataformat.castor.packages

Add additional packages to Castor XmlContext

String[]

camel.dataformat.castor.validation

Whether validation is turned on or off. Is by default true.

true

Boolean

camel.dataformat.castor.whitelist-enabled

Define if Whitelist feature is enabled or not

true

Boolean

ND

Dependencies

To use Castor in your camel routes you need to add the a dependency on camel-castor which implements this data format.

If you use maven you could just add the following to your pom.xml, substituting the version number for the latest & greatest release (see the download page for the latest versions).

<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-castor</artifactId>
  <version>x.x.x</version>
</dependency>