fb
WSO2 Tutorial 2 min

WSO-2-EASY: Changing message content with PayloadFactory Mediator

Rob Blaauboer
Rob Blaauboer
Integration Consultant & WSO2 Trainer
WSO2 logo klein
Scroll
WSO2_logo_klein.png

When you want to change the content of a message a PayLoadFactory Mediator is one way to do so. Basically what the PayloadFactory Mediator does is change (i.e. transform or replace) part of the message defined by format with the args defined later on.

Syntax

The syntax of the PayloadFactory mediator is as follows:

<payloadFactory media-type="xml | json">

    <format ../>

    <args>      

        <arg (value="string" | expression=" {xpath} | {json} ")/>*

    </args>

</payloadFactory>

The media-type indicates the format of the message that is going to be changed, either JSON or XML. In the <format> section we indicate what the message will look like, basically mentioning the elements. The actual content will be added by the <args> part.

Banner-WSO2-Community-1-8

Three transformations

In this article we will perform three transformations on a message:

  1. Adding elements to the message
  2. Changing the date format from US notation (mm/dd/yyyy) to EU notation (dd/mm/yyyy)
  3. Renaming one field <date> to <date_ordered> and removing the <clientnr>

The xml that is send to the proxy is always the same (of course in a soap message)

order>

      <ordernr>10291</ordernr>

      <date>12/22/2015</date>

      <clientnr>6690</clientnr>

</order>

Tools

To develop and test this simple example I use Development Studio 3.8.0 from WSO2 and SoapUI. Because this is a simple example without an associated backend service (it only shows the PayLoadFactory capabilities) I am using a Respond Mediator in order to send the changed message back.

In all cases the graphical overview of the three proxies is as follows:

7_jan_no_1.png

Note that the Log Mediators are not even necessary, they are simply there to show message content on the console.

7_jan_no_2.png
7_jan_no_3.png

Changing dates

Again, the XML for this proxy is almost the same as the previous one:

7_jan_no_4.png

The difference is that the date value is changed using an XPATH function called substring and concat that cuts and pastes the date field in the required format, in essence switching day and month.

As you can see the date is switched in the response message.

7_jan_no_5.png

Renaming and removing

Finally, we rename the <date> field to <date_ordered> and remove the <clientnr>. This results in a minimal change in the PayLoadFactory Mediator.

7_jan_no_6.png

The SoapUI response shows the message we just created.

7_jan_no_7.png

The PayLoadFactory Mediator is one powerful way to transform messages, adding, deleting or changing the message or it’s values.

Working daily with WSO2 products we sometimes encounter things that even we did not know yet, but in fact are so handy we want to share it with the whole world. We have created a new group of articles called WSO-2-EASY that will show some of best tips and tricks that will help you to either save time or create tidier code  

 WSO2Easy.png    
   

Full API lifecycle Management Selection Guide

WHITEPAPER

smartmockups l0qqucke