Reading Time: 5 minutes

Transformers in Mule are simple objects that convert the current message from one type to another. The interface for a transformer is simple, but there are some tips and tricks for getting the most out of transformers. For this post we will define a transformer the converts from an Order object to HTML so that we can email the details of an order to a customer…

First, there are two abstract transformer classes you can extend.

latest report
Learn why we are the Leaders in API management and iPaaS

The AbstractTransformer gives you access to the source payload to transform and the encoding to use (if required).

Import org.mule.transform.AbstractTransformer;

public class OrderToHtmlTransformer implements AbstractTransformer
{
    public Object doTransform(Object src, String encoding) throws TransformerException
}

The AbstractMessageAwareTransformer gives you access to the current message and the encoding to use. The advantage of this transformer is that you can transform the message headers and attachments too. If you do change the headers or attachments you can change them directly on the message passed in, you always return the transformed message payload from the transform() method.

Import org.mule.transform.AbstractMessageAwareTransformer;

public class OrderToHtmlTransformer implements AbstractMessageAwareTransformer
{
    public Object transform(MuleMessage message, String encoding) throws TransformerException
}

Registering Source and Return Types

Mule transformers allow the developer to specify which source types the transformer can accept and what type it will return. This allows Mule to validate the incoming message before invoking the transformer and validate the output before sending the message on. Also, Mule supports transformation discovery (automatic transformations) and this requires the source and return types to be set. I will write more about this in another post.

For example, using our Order bean to HTML transformer, you would specify in the constructor that the transformer only know how to convert message payloads of type Order:

public class OrderToHtmlTransformer implements AbstractMessageAwareTransformer
{
    public OrderToHtmlTransformer() {
        registerSourceType(Order.class);
        setReturnClass(String.clsass);
        setName("OrderToHTML");
}

}

This means I don’t need to do any type checking in the transform() method:

public Object transform(MuleMessage message, String encoding) throws TransformerException
{
    Order order = (Order)message.getPayload();
    ....
}

You can add more than one source type, in which case you need to check for each type in the transform() method.

Notice also that we set the name of the transformer. Usually, this is done from the Xml configuration when the transformer is declared. If no name is set Mule will generate one based on the first source type and return class; in this case the generated name would be “OrderToString”.

Full Implementation

We are extending the AbstractMessageAwareTransformer, this means we have access to the message headers and can use them in our transformer. In our example, we grab the transactionId from the message header and we use the encoding in the output HTML.

public Object transform(MuleMessage message, String encoding) throws TransformerException
{
    Order order = (Order)message.getPayload();
    StringBuffer html = new StringBuffer();
    html.append(“”);
    html.append(“”);
    html.append(“”);
    html.append(“Dear “).append(order.getCustomer().getName()).append(“

”);
    html.append(“Thank you for your order.  Your transaction reference is: ”);
    html.append(message.getProperty(“transactionId”).append(“”);
    html.append(“(“”);
    return html.toString();
}