[WSO2 ESB]Enrich Mediator with ‘type’ Property

Let’s say we have a requirement where, we want to add a new tag as a child in our response message, before we send it.

Original response message:


<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Body>
<ns:greetResponse xmlns:ns="http://www.wso2.org/types">
<return>Hello World, denu !!!</return>
</ns:greetResponse>
</soapenv:Body>
</soapenv:Envelope>

The response we finally need:


<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Body>
<ns:greetResponse xmlns:ns="http://www.wso2.org/types">
<return>Hello World, denu !!!</return>
<numOfHolders>2</numOfHolders>
</ns:greetResponse>
</soapenv:Body>
</soapenv:Envelope>

So, the tag we want to additionally add is:


<numOfHolders>2</numOfHolders>

And the value (2) of above node, should not be a hard coded value. We want it to be fetched dynamically.

So, let’s say we get this value from a property as below:


<property name="dynamicMessage" value="2" type="STRING"/>

Now, let’s see how we can create such a custom response.

We can use WSO2 ESB, enrich mediator for this purpose.

Steps:

1.Add the empty element <numOfHolders></numOfHolders> as a child to the original response.


<enrich>
<source type="inline" clone="true">
<numOfHolders xmlns=""/>
</source>
<target xmlns:ns="http://www.wso2.org/types"
action="child"
xpath="//ns:greetResponse"/>
</enrich>

If you don’t have any namespace for your new element, amke sure to put xmlns=””, otherwise, a default namespace would be created, and will make it difficult for you to use xpath expressions as expected.

2.Add the value we get from the property as the value of the new node.


<enrich>
<source type="property" clone="true" property="dynamicMessage"/>
<target xpath="//numOfHolders"/>
</enrich>

So, the final, configuration would be something like,


<enrich>
<source type="inline" clone="true">
<numOfHolders xmlns=""/>
</source>
<target xmlns:ns="http://www.wso2.org/types"
action="child"
xpath="//ns:greetResponse"/>
</enrich>
<enrich>
<source type="property" clone="true" property="dynamicMessage"/>
<target xpath="//numOfHolders"/>
</enrich>

Author: denuwanthi

Graduate of Department of Computer Science & Engineering, University of Moratuwa, Sri Lanka.Currently working in WSO2 Lanka (pvt)Ltd.

Leave a comment