Processing large payloads with WSO2 ESB script mediator iteratively
Let’s get started
The approach we would be using is to iterate over the large response and pass each section through the script mediator. After which we will be aggregating the tranformed sections to display the modified and transformed response.
1) Let’s create a mock service which returns the response mentioned in this text file. I have used the online tool http://www.mocky.io/ to generate this response. The invocation URL for my mock response is http://www.mocky.io/v2/57a881a3110000b6171d4546
2) Then we need to create the In sequence. In the ESB in the Service bus section , select the Sequences option and add a new sequence. This is the source view of the in sequence we will be using which simply has a endpoint invocation. This is the in sequence configuration and you can also download it from HERE
<sequence name="sendSequence" xmlns="http://ws.apache.org/ns/synapse">
<property name="test" value="Execution Started"/>
<http method="GET" uri-template="http://www.mocky.io/v2/57a881a3110000b6171d4546"/>
3) Next we need to create the out sequence which does the needed transformation. I have listed what happens within the sequence to get a better understanding.
- I have used the response generated from the above invocation here which has an array of json objects. Since the objects inside the array are repititive we can split it using the iterate mediator. (In this sequence the splitting is done by the ARR_ITEM expression)
- After splitting it using the iterate mediator we can manipulate each JSON object by passing it through the script mediator.
- At the end we are placing an aggregate mediator which will aggregate all the modified JSON elements which went through the iterate mediator and display the response.
- We will be storing the aggregated response inside the rootElement property as json objects. Follow is the sequence which executes this logic. This is the outsequence of our API.
Simillar to above you need to add another sequence which we would be using as the out sequence. This is the sequence source view corresponding to the out sequence described above. You can also download the sequence HERE
<sequence xmlns="http://ws.apache.org/ns/synapse" name="iterOutTest">
<property name="rootElement" scope="default">
<property name="iterator" value="in the iterate"/>
<arg evaluator="json" expression="$."/>
var payLoad = mc.getPayloadJSON();
var catalogItem = payLoad.ARR_ITEM;
catalogItem.CT.CT_ITEM.ED = "456";
catalogItem.DISCRIPTIONS = "This is the modified description";
<messageCount min="-1" max="-1"/>
<property name="MSG" value="Aggregated Msg"/>
4) After we have created the sequences we need to then add it to the API. Refer the below screenshot. We need to add the above created in and out sequences to the API respectively
Here you can find the xml configuration corresponding to the above API. Also you can download the API from HERE.
<api xmlns="http://ws.apache.org/ns/synapse" name="IterateAggregateAPI" context="/iterate">
<resource methods="POST" uri-template="/*" inSequence="sendSequence" outSequence="iterateOUT"/>
5) Using the soap UI or any other rest client you can invoke the API and observe how the response payload is iterated and processed to display the aggregated and tranformed response.
I hope that following this tutorial you were able to understand how large payloads could be split using common elements to process using the script mediator.
Creating Custom Mediators - Enterprise Service Bus 4.9.0 - WSO2 Documentation
WSO2 ESB comes with an assortment of mediators to filter, transform, route and manipulate messages. When you have a…
Iterate Mediator - Enterprise Service Bus 4.9.0 - WSO2 Documentation
When you use ForEach mediator, you can only loop through segments of the message and do changes to a particular segment…
Aggregate Mediator - Enterprise Service Bus 4.9.0 - WSO2 Documentation
The Aggregate mediator implements the Aggregator enterprise integration pattern . It combines (aggregates) the response…
Creating APIs - Enterprise Service Bus 4.9.0 - WSO2 Documentation
This documentation is for WSO2 Enterprise Service Bus version 4.9.0 . View documentation for the latest release.