FOREACH MEDIATOR
In the previous note, we saw how we can use custom classes as Mediators.
Moving on to cover other important mediation techniques, I was going through online documentation for 'FOREACH MEDIATOR' using this link -
when I realized, the sample code provided can be made more illustrative and a bit easier.
So code buddies, here we go with our version to create a small demo, using foreach mediator.
1. Kicking off with a sequence generation -
Declaring 'main2' as our sequence to imbibe foreach mediator -
xml version -
<?xml
version="1.0" encoding="UTF-8"?>
<sequence
name="main2" trace="enable"
xmlns="http://ws.apache.org/ns/synapse">
<in>
<foreach
expression="//m0:getQuote/m0:request"
xmlns:m0="http://services.samples"
xmlns:ns="http://org.apache.synapse/xsd"
xmlns:ns3="http://org.apache.synapse/xsd">
<sequence>
<payloadFactory media-type="xml">
<format>
<m0:checkPriceRequest>
<m0:code>$1</m0:code>
</m0:checkPriceRequest>
</format>
<args>
<arg evaluator="xml"
expression="//m0:request/m0:symbol"/>
</args>
</payloadFactory>
</sequence>
</foreach>
<log
level="full"/>
</in>
<out/>
</sequence>
2. Defining an API to use the above sequence -
Let's call it 'foreachAPI' -
XML version -
<api
xmlns="http://ws.apache.org/ns/synapse" name="foreachAPI"
context="/fe">
<resource
methods="POST GET" inSequence="main2"
outSequence="main2"/>
</api>
3. Let's also enable the debug logging, for the above API -
4. Time to hit the API.
Let's use the same XML provided in the tutorial i.e. stockQuoteReq.xml -
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:m0="http://services.samples"
xmlns:xsd="http://services.samples/xsd">
<soap:Header/>
<soap:Body>
<m0:getQuote>
<m0:request><m0:symbol>IBM</m0:symbol></m0:request>
<m0:request><m0:symbol>WSO2</m0:symbol></m0:request>
<m0:request><m0:symbol>MSFT</m0:symbol></m0:request>
</m0:getQuote>
</soap:Body>
</soap:Envelope>
Save it locally, and invade the directory where it is parked. Run the below curl command -
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:m0="http://services.samples"
xmlns:xsd="http://services.samples/xsd">
<soap:Header/>
<soap:Body>
<m0:getQuote>
<m0:request><m0:symbol>IBM</m0:symbol></m0:request>
<m0:request><m0:symbol>WSO2</m0:symbol></m0:request>
<m0:request><m0:symbol>MSFT</m0:symbol></m0:request>
</m0:getQuote>
</soap:Body>
</soap:Envelope>
Save it locally, and invade the directory where it is parked. Run the below curl command -
curl -d @stockQuoteReq.xml -H "Content-Type: application/soap+xml;charset=UTF-8" "http://172.16.2.34:8280/fe/foreachAPI"
5. Output obtained, all the things done using console, no need to deploy any code 😃
o/p xml -
<?xml
version="1.0" encoding="UTF-8"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:m0="http://services.samples"
xmlns:xsd="http://services.samples/xsd">
<soap:Body>
<m0:getQuote>
<m0:checkPriceRequest>
<m0:code>IBM</m0:code>
</m0:checkPriceRequest>
<m0:checkPriceRequest>
<m0:code>WSO2</m0:code>
</m0:checkPriceRequest>
<m0:checkPriceRequest>
<m0:code>MSFT</m0:code>
</m0:checkPriceRequest>
</m0:getQuote>
</soap:Body>
</soap:Envelope>
Thanks a lot for sharing the appreciation! :)
ReplyDeleteWill surely try to add more of such matter once time permits. :)
"It is actually a great and helpful piece of information. I am satisfied that you simply shared this helpful information with us. Please stay us informed like this. Thanks for sharing.
ReplyDeleteDigital Marketing Training Course in Chennai | Digital Marketing Training Course in Anna Nagar | Digital Marketing Training Course in OMR | Digital Marketing Training Course in Porur | Digital Marketing Training Course in Tambaram | Digital Marketing Training Course in Velachery
"
It's very Wonderful to visit your site...Enjoy Reading your Articles...informative Blogs About Java...Keep doing the same
ReplyDeleteDigital Marketing Training Course in Chennai | Digital Marketing Training Course in Anna Nagar | Digital Marketing Training Course in OMR | Digital Marketing Training Course in Porur | Digital Marketing Training Course in Tambaram | Digital Marketing Training Course in Velachery