Modify the response using Enrich Mediator.
We can use enrich mediator to slight changes to request as well as the response. In this post we shall look how we can do a modification to the response using enrich mediator.
Prerequisites
- SimpleStockQuote service should be up and running.
How to start SimpleStockQuote Service?
- Download WSO2 Enterprise Integrator from here
- Start WSO2 EI
<EI-HOME>/bin/integrator.sh
3. Start the axis2Server
<EI_HOME>/samples/axis2Server/axis2server.sh
4. Build the SimpleStockQuote Service
<EI-HOME>/samples/axis2Server/src/SimpleStockQuoteService
ant
Use case
We call getQuote function from SimpleStockService. For WSO2, we are getting a response something similar as below.
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Body>
<ns:getQuoteResponse xmlns:ns="http://services.samples">
<ns:return xsi:type="ax21:GetQuoteResponse" xmlns:ax21="http://services.samples/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ax21:change>4.170813321068287</ax21:change>
<ax21:earnings>12.273500568992642</ax21:earnings>
<ax21:high>-56.152347549331566</ax21:high>
<ax21:last>56.18419783158677</ax21:last>
<ax21:lastTradeTimestamp>Thu Dec 13 12:29:10 IST 2018</ax21:lastTradeTimestamp>
<ax21:low>-55.79132470970274</ax21:low>
<ax21:marketCap>3.656174426248211E7</ax21:marketCap>
<ax21:name>WSO2 Company</ax21:name>
<ax21:open>58.147423643888686</ax21:open>
<ax21:peRatio>-17.951812639677684</ax21:peRatio>
<ax21:percentageChange>6.756373358966599</ax21:percentageChange>
<ax21:prevClose>61.73153997674016</ax21:prevClose>
<ax21:symbol>WSO2</ax21:symbol>
<ax21:volume>18288</ax21:volume>
</ns:return>
</ns:getQuoteResponse>
</soapenv:Body>
</soapenv:Envelope>
Using Enrich Mediator, we are going to add the below new element
<ax21:newvalue>testsamplevalue</ax21:newvalue>
to this response before it is displayed to the user. It will be a sibling to <ax21:volume></ax21:volume> element.
Development Guidelines
- Log in to Management Console.
- Create the following proxy.
<?xml version="1.0" encoding="UTF-8"?><proxy xmlns="http://ws.apache.org/ns/synapse" name="Enrich1" startOnLoad="true" statistics="disable" trace="disable" transports="http,https">
<target>
<outSequence>
<log level="full"/>
<enrich>
<source clone="true" type="inline">
<ax21:newvalue xmlns:ax21="http://services.samples/xsd">testsamplevalue</ax21:newvalue>
</source>
<target xmlns:ax21="http://services.samples/xsd" xmlns:ns="http://services.samples" action="sibling" xpath="//ns:getQuoteResponse/ns:return/ax21:volume"/>
</enrich>
<log level="full"/>
<respond/>
</outSequence>
<endpoint>
<address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
</endpoint>
</target>
<description/>
</proxy>
Invoke the proxy with following payload.
http://localhost:8280/services/Enrich1
<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>IBM</xsd:symbol>
</ser:request>
</ser:getQuote>
</soapenv:Body>
</soapenv:Envelope>
You will get the following response.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns:getQuoteResponse xmlns:ns="http://services.samples">
<ns:return xsi:type="ax21:GetQuoteResponse" xmlns:ax21="http://services.samples/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ax21:change>-2.2907353148243903</ax21:change>
<ax21:earnings>-8.696106301884303</ax21:earnings>
<ax21:high>-57.1441874679246</ax21:high>
<ax21:last>58.40162055158143</ax21:last>
<ax21:lastTradeTimestamp>Thu Dec 13 14:02:32 IST 2018</ax21:lastTradeTimestamp>
<ax21:low>-58.22046166555887</ax21:low>
<ax21:marketCap>4.049562971853946E7</ax21:marketCap>
<ax21:name>IBM Company</ax21:name>
<ax21:open>-57.85954308529621</ax21:open>
<ax21:peRatio>-19.21186061993837</ax21:peRatio>
<ax21:percentageChange>4.015883574014052</ax21:percentageChange>
<ax21:prevClose>-57.04187565713465</ax21:prevClose>
<ax21:symbol>IBM</ax21:symbol>
<ax21:volume>19375</ax21:volume>
<ax21:newvalue>testsamplevalue</ax21:newvalue>
</ns:return>
</ns:getQuoteResponse>
</soapenv:Body>
</soapenv:Envelope>
For further reading please refer: