Warning: A non-numeric value encountered in /home/customer/www/archive.ricston.com/public_html/wp-content/themes/Divi/functions.php on line 5766

This is the next blog post in the series of posts related to building a new transformer which can add attachments to your messages. See my previous posts on the design for this transformer and its test cases

Normal Mule transformers accept the message payload in its current format and convert it to something different. If we look at the built-in JMSMessageToObject transformer, for example, we can see that a JMS message that was read off a JMS queue will be converted to a java.lang.object instead.

If you check out the Mule source code, you can see that transformers can either inherit from the AbstractTransformer class or can implement the Transformer interface directly. The advantage of using the abstract class is that certain things are done for you, however, this is a design choice you can make while rolling your own transformer.

If we take this into consideration when looking at our transformer, we can see that we’re not really interested in the payload of the message but more interested in the message itself. There are two ways we can gain access to the message inside a transformer:
1 We can gain a handle to the current event context (encapsulated in the MuleEventContext class) and manipulate the message through this.
2 We can inherit from the AbstractMessageAwareTransformer which will work on the entire message.

Obviously, option 2 is a little neater 🙂

The AbstractMessageAwareTransformer is available in the org.mule.transformer package and has a single method that needs to be implemented:

public Object transform(MuleMessage arg0, String arg1)
throws TransformerException {

This method will provide you with the entire MuleMessage for your use. The second parameter will indicate what outputEncoding to use.