Encoding And Decoding XML Using WSO2 ESB

Hi again...,

This time I am going to narrate how to encode and decode xml payload in the mediation flow of WSO2  ESB.
There are many use cases this might be wanted, and there are ways to configure the message format that should be sent and the message format that should be received at message builder and formatted level. Despite of that fact,  there can be instances where you might want to do the transformation in the mediation flow.
Typical example will be sending a xml payload to a Data Service (you might be using WSO2 DSS) in order to save it in DB as a string. Here if you do not encode the payload as a string Data Services Server will try to parse the xml payload as the input to the Data service which will of course generate an error message.
You can use script mediator to encode the payload.

1. Main>>Service Bus>>Local Entries in Management Console of ESB
2. Select "Local Entries" item.

3. Add two inline XML type local entries as "payloadEncoderScript" and "payloadDecoderScript".

4. To the content of payloadEncoderScript insert the following text and save it.

What is done by this script is that when this script mediator is called the body has "payload" node. Inside that payload node comes the payload data that should be encoded. After encoding that data, we set it to a node called "encodedPayloadData" and save to a property called "ENCODED_ORIGINAL_PAYLOAD". Now we can access this property later to get the encoded payload later in the mediation flow.

5. To the content of payloadDecodererScript insert the following text and save it.

In the same way as done in encoding, when the payloadDecodererScript is called the body has string content in "encodedPayload" node. We get it, decode it and set it to a node called "ent:decodedPayloadData " and save the whole body to a property called "DECODED_ORIGINAL_PAYLOAD" which can be accessed later in the mediation flow.

We are done with the script now. Now it is time to look around how to USE it....!!

Calling Script Mediators within WSO2 ESB

It is not a big deal. In this example I will put all the things needed in one sequence.

1. First create a payload which needs to be encoded and set it to the body.

2. Now we need to encode the part inside "ds:payloaddata" node and ultimately we like to have a payload like

for that just call the mediator as

 3. Now you have the payload in the property "ENCODED_ORIGINAL_PAYLOAD". You can do anything with it. For example create following payload.

You will get the payload mentioned at 2 as the result. This payload is the payload we have to call-out or send for a data service to insert into a database column.

4. When getting data from data service we will say following is the message format.


Decode the payload calling the script as below.

5. In order to recreate the XML payload we have to save it as an OM element

6. Now you can use that OM element in the way you desire. (Enrich as a child to a payload you have or directly put it to the body)

Very simple and straightforward.
Hope this  post will be of help.

Hasitha Hiranya

1 comment:

  1. Not sure how old this post is... the following script can be used for encoding data... it is in ruby though!

    require "base64"
    require 'rexml/document'
    include REXML

    xmldoc = $mc.getPayloadXML()
    value = Base64.encode64(XPath.first(xmldoc, "//xmlnodename").text)