Since we're on a major migration process of this website, some component documents here are out of sync right now. In the meantime you may want to look at the asciidoc in the repository: https://github.com/apache/camel/blob/master/README.md https://github.com/apache/camel/blob/master/components/readme.adoc

Custom DataFormat

You can use your custom Data Format implementation with Camel. All you have to do is to implement the DataFormat interface. For example in the following we will implement a reverse data format as shown below:

Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'
public static final class MyReverseDataFormat extends ServiceSupport implements DataFormat {

    public void marshal(Exchange exchange, Object graph, OutputStream stream) throws Exception {
        byte[] bytes = exchange.getContext().getTypeConverter().mandatoryConvertTo(byte[].class, graph);
        String body = reverseBytes(bytes);
        stream.write(body.getBytes());
    }

    public Object unmarshal(Exchange exchange, InputStream stream) throws Exception {
        byte[] bytes = exchange.getContext().getTypeConverter().mandatoryConvertTo(byte[].class, stream);
        String body = reverseBytes(bytes);
        return body;
    }

    private String reverseBytes(byte[] data) {
        StringBuilder sb = new StringBuilder(data.length);
        for (int i = data.length - 1; i >= 0; i--) {
            char ch = (char) data[i];
            sb.append(ch);
        }
        return sb.toString();
    }

    @Override
    protected void doStart() throws Exception {
        // noop
    }

    @Override
    protected void doStop() throws Exception {
        // noop
    }
}

And to use it in Java DSL:

Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'
from("direct:a")
    .marshal().custom("reverse")
    .to("mock:a");

from("direct:b")
    .unmarshal().custom("reverse")
    .to("mock:b");

Notice we use custom to refer to the Data Format in the Registry. In Java DSL you can also provide the instance directly as shown:

from("direct:a")
    .marshal(new MyReverseDataFormat())
    .to("mock:a");

And likewise to use it in XML DSL:

Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'
<!-- this is our custom data format implementation -->
<bean id="reverse" class="org.apache.camel.impl.RefDataFormatTest$MyReverseDataFormat"/>

<camelContext xmlns="http://camel.apache.org/schema/spring">
  <route>
    <from uri="direct:a"/>
    <marshal>
      <!-- refer to my custom data format -->
      <custom ref="reverse"/>
    </marshal>
    <to uri="mock:a"/>
  </route>

  <route>
    <from uri="direct:b"/>
    <unmarshal>
      <!-- refer to my custom data format -->
      <custom ref="reverse"/>
    </unmarshal>
    <to uri="mock:b"/>
  </route>
</camelContext>

Notice in the XML DSL example above we use <custom> to refer to a custom data format. This requires Camel 2.8 or better. In older releases you would have to use the ref attribute as shown below. Notice the ref attribute has been @deprecated and you should prefer to use the <custom> way:

 <marshal ref="reverse"/>
© 2004-2015 The Apache Software Foundation.
Apache Camel, Camel, Apache, the Apache feather logo, and the Apache Camel project logo are trademarks of The Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their respective owners.
Graphic Design By Hiram