Friday, January 22, 2016

[WSO2 ESB] Iterate and Aggregate mediators

Lets assume we have two services.

First : This returns list of market details
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <marketDetails xmlns="http://market.test.com">
            <marketDetail>
                <market>
                    <id>100</id>
                    <openTime>10.00AM</openTime>
                    <closeTime>4.00PM</closeTime>
                    <name>New York</name>
                </market>
                <market>
                    <id>200</id>
                    <openTime>9.00AM</openTime>
                    <closeTime>5.PM</closeTime>
                    <name>London</name>
                </market>
            </marketDetail>
        </marketDetails>
    </soapenv:Body>
</soapenv:Envelope>
Second: This returns the symbols of each market.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <symbols xmlns="http://market.test.com">
            <symbol>
                <id>20</id>
                <name>Google</name>
                <qty>2000</qty>
                <unitPrice>6</unitPrice>
            </symbol>
            <symbol>
                <id>30</id>
                <name>IBM</name>
                <qty>1000</qty>
                <unitPrice>5</unitPrice>
            </symbol>
            <symbol>
                <id>30</id>
                <name>WSO2</name>
                <qty>1000</qty>
                <unitPriceice>5.5</unitPriceice>
            </symbol>
        </symbols>
    </soapenv:Body>
</soapenv:Envelope>
So we need to build a new service which returns all the symbols of all markets.

1. We need to iterate through the first response to get the market id and invoke the second service.
<property xmlns:m0="http://market.test.com" name="MARKET_COUNT"
          expression="count(//m0:marketDetails/m0:marketDetail/m0:market)"/>
<iterate xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://org.apache.synapse/xsd"
         preservePayload="true"
         attachPath="//m0:marketDetails/m0:marketDetail/m0:market"
         expression="//m0:marketDetails/m0:marketDetail/m0:market/m0:id"
         id="MARKET_ITERATOR">
<target>
    <sequence>
        <payloadFactory media-type="xml">
            <format>
                <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
                               xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                    <soap:Body>
                        <getMarketSymbols xmlns="http://market.test.com">
                            <marketId>$1</marketId>
                        </getMarketSymbols>
                    </soap:Body>
                </soap:Envelope>
            </format>
            <args>
                <arg xmlns:m0="http://market.test.com" 
expression="//m0:marketDetails/m0:marketDetail/m0:market/m0:id" evaluator="xml"/>
            </args>
        </payloadFactory>
        <property name="messageType" value="application/soap+xml" scope="axis2"/>
        <call>
            <endpoint xmlns="http://ws.apache.org/ns/synapse" name="SymbolEndpoint">
                <address uri="https://test.com/services/market">
                    <timeout>
                        <duration>30000</duration>
                        <responseAction>fault</responseAction>
                    </timeout>
                    <suspendOnFailure>
                        <errorCodes>-1</errorCodes>
                        <initialDuration>0</initialDuration>
                        <progressionFactor>1.0</progressionFactor>
                        <maximumDuration>0</maximumDuration>
                    </suspendOnFailure>
                    <markForSuspension>
                        <errorCodes>-1</errorCodes>
                    </markForSuspension>
                </address>
            </endpoint>
        </call>
       <loopback/>
    </sequence>
</target>
</iterate>
2. Use the aggregate mediator to collect all the symbols.
<property name="ECNCLOSING_ELEMENT" scope="default">
    <wrapper xmlns=""/>
</property>

<aggregate id="MARKET_ITERATOR">
<completeCondition>
    <messageCount min="{get-property('MARKET_COUNT')}" max="-1"/>
</completeCondition>
<onComplete xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
            xmlns:ns="http://org.apache.synapse/xsd"
            xmlns:m0="http://market.test.com"
            expression="//m0:symbols/m0:symbol"
            enclosingElementProperty="ECNCLOSING_ELEMENT">
</onComplete>
</aggregate>
</iterate>