loopswso2endpointuritemplatewso2-esb

WSO2 ESB calling parameterized endpoint Looping on Parameters


In my usecase i have to chain two service calls. In particular:

1) The first call returns an xml listing several IDs

2) i have to iterate through the returned ID list and to make an ID parameterized service call for each id-item.

3) Finally i have to collect a whole response made up of each single ID-service-response.

Suppose the first service call returns a response like this one:

<result>     
    <Link>
        <Id>93451</Id>
    </Link>
    <Link>
        <Id>93450</Id>
    </Link>
    ...

The second step is to perform a series of calling to parameterized endpoint like this:

http://myEndpoint/entry/eutils/efetch.fcgi?db=pubmed&rettype=abstract&retmode=xml&id=<ID>

each call of which returns an xml response like this:

<response>
    <field1>value1</field1>
    <field2>value2</field2>
    <field3>value3</field3>
<response>

I have to collect a whole response like this one:

<finalResponse>
    <response>
        <field1>value1</field1>
        <field2>value2</field2>
        <field3>value3</field3>
    <response>
    <response>
        <field1>value1</field1>
        <field2>value2</field2>
        <field3>value3</field3>
    <response>
    <response>
        <field1>value1</field1>
        <field2>value2</field2>
        <field3>value3</field3>
    <response>
</finalResponse>

How can i do? Could you give me some example? Thanks


Solution

  • You need to use iterate mediator and aggregate mediator in combination. Here is a sample code, but you may need to do some modification in order to make it work for your requirements.

    <definitions xmlns="http://ws.apache.org/ns/synapse">
        <proxy name="SampleProxy">
            <target>
                <inSequence>
                    <iterate expression="//result/link/id" preservePayload="true"
                             attachPath="//link">
                        <target>
                            <property name="uri.var.servicepath" expression="//link/id/text()"/>
                            <sequence>
                                <send>
                                    <endpoint key="MyEndpoint"/>
                                </send>
                            </sequence>
                        </target>
                    </iterate>
                </inSequence>
                <outSequence>
                    <property name="FinalResponse" scope="default">
                        <finalResponse />
                    </property>
                    <aggregate>
                        <onComplete expression="//response"
                                    enclosingElementProperty="FinalResponse">
                            <send/>
                        </onComplete>
                    </aggregate>
                </outSequence>
            </target>
        </proxy>
    
        <endpoint xmlns="http://ws.apache.org/ns/synapse" name="MyEndpoint">
            <http uri-template="http://myEndpoint/entry/eutils/efetch.fcgi?db=pubmed&amp;rettype=abstract&amp;retmode=xml&amp;id={ID}" method="GET">
            </http>
        </endpoint>
    </definitions>
    

    Full documentation on related sample here. Find how you can parameterize you url here.