Modify payload by adding a child element using Enrich mediator

Isuru Uyanage
Think Integration
Published in
2 min readDec 8, 2018
image courtesy: https://www.wallpaperup.com/

When to use?

The Enrich Mediator can process a message based on a given source configuration and then perform the specified action on the message by using the target configuration. It is often used to do slight modifications to the payload. There are main three target actions as “Replace”, “Child” and “Sibling”. In this scenario, we focus on adding a child element to the payload.

Sample use case

Assume that we have our original payload to be sent as below.

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.samples" xmlns:xsd="http://services.samples/xsd">
<soapenv:Header />
<soapenv:Body>
<ser:getQuote>
<!--Optional:-->
<ser:request>
<!--Optional:-->
<xsd:symbol>WSO2</xsd:symbol>
</ser:request>
</ser:getQuote>
</soapenv:Body>
</soapenv:Envelope>

Before sending this to the endpoint, we need to modify this payload by adding a child element to <ser:request> element. The child element will be <xsd:symbol>SUN</xsd:symbol>

Required Payload

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.samples" xmlns:xsd="http://services.samples/xsd">
<soapenv:Header />
<soapenv:Body>
<ser:getQuote>
<!--Optional:-->
<ser:request>
<!--Optional:-->
<xsd:symbol>WSO2</xsd:symbol>
<xsd:symbol>SUN</xsd:symbol>
</ser:request>
</ser:getQuote>
</soapenv:Body>
</soapenv:Envelope>

This slight modification to the payload can be done using enrich mediator

<?xml version="1.0" encoding="UTF-8"?>
<inSequence xmlns="http://ws.apache.org/ns/synapse">
<log level="full" />
<enrich>
<source clone="true" type="inline">
<xsd:symbol xmlns:xsd="http://services.samples/xsd">SUN</xsd:symbol>
</source>
<target xmlns:ser="http://services.samples" xmlns:xsd="http://services.samples/xsd" action="child" type="custom" xpath="//ser:getQuote/ser:request" />
</enrich>
<log level="full" />
<respond />
</inSequence>

Development guidelines

  1. Create the following sequence and add to the proxy service.
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse" name="Enrich2" startOnLoad="true" statistics="disable" trace="disable" transports="http,https">
<target>
<inSequence>
<log level="full" />
<enrich>
<source clone="true" type="inline">
<xsd:symbol xmlns:xsd="http://services.samples/xsd">SUN</xsd:symbol>
</source>
<target xmlns:ser="http://services.samples" xmlns:xsd="http://services.samples/xsd" action="child" xpath="//ser:getQuote/ser:request" />
</enrich>
<log level="full" />
<respond />
</inSequence>
</target>
<description />
</proxy>

2. Invoke the proxy with the following payload. Use SOAP client as SOAP UI.

http://localhost:8280/services/Enrich2

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.samples" xmlns:xsd="http://services.samples/xsd">
<soapenv:Header />
<soapenv:Body>
<ser:getQuote>
<!--Optional:-->
<ser:request>
<!--Optional:-->
<xsd:symbol>WSO2</xsd:symbol>
</ser:request>
</ser:getQuote>
</soapenv:Body>
</soapenv:Envelope>

Above proxy will add the child element <xsd:symbol>SUN</xsd:symbol> to the <ser:request> element before the payload is sent to the endpoint.

You will see the following respond which is the actual request enriched.

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://services.samples/xsd" xmlns:ser="http://services.samples">
<soap:Body>
<ser:getQuote>
<!--Optional:-->
<ser:request>
<!--Optional:-->
<xsd:symbol>IBM</xsd:symbol>
<xsd:symbol>SUN</xsd:symbol>
</ser:request>
</ser:getQuote>
</soap:Body>
</soap:Envelope>

--

--