We have a use case, where we have to iterate through array of json-object and call an external endpoint based on certain key value. So we have used a iterator mediator, switch mediator(to match condition) and an aggregate mediator.
<?xml version="1.0" encoding="UTF-8"?>
<api context="/propertycheck" name="propertyCheck" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST">
<inSequence>
<iterate attachPath="json-eval($)" expression="json-eval($.main)" id="testingid" preservePayload="true" sequential="true">
<target>
<sequence>
<switch source="json-eval($.sub1)">
<case regex="a">
<log level="full" separator=","/>
<call>
<endpoint>
<http method="post" uri-template="http://localhost:3000/test">
<suspendOnFailure>
<initialDuration>-1</initialDuration>
<progressionFactor>1</progressionFactor>
</suspendOnFailure>
<markForSuspension>
<retriesBeforeSuspension>0</retriesBeforeSuspension>
</markForSuspension>
</http>
</endpoint>
</call>
</case>
<case regex="b">
<log level="full" separator=","/>
<call>
<endpoint>
<http method="post" uri-template="http://localhost:3000/test">
<suspendOnFailure>
<initialDuration>-1</initialDuration>
<progressionFactor>1</progressionFactor>
</suspendOnFailure>
<markForSuspension>
<retriesBeforeSuspension>0</retriesBeforeSuspension>
</markForSuspension>
</http>
</endpoint>
</call>
</case>
<case regex="c">
<log level="full" separator=","/>
<call>
<endpoint>
<http method="post" uri-template="http://localhost:3000/test">
<suspendOnFailure>
<initialDuration>-1</initialDuration>
<progressionFactor>1</progressionFactor>
</suspendOnFailure>
<markForSuspension>
<retriesBeforeSuspension>0</retriesBeforeSuspension>
</markForSuspension>
</http>
</endpoint>
</call>
</case>
<default>
<log>
<property name="default" value="defaultCase"/>
</log>
<payloadFactory media-type="json">
<format>{
"sub1":"My own Response"
}</format>
<args/>
</payloadFactory>
</default>
</switch>
</sequence>
</target>
</iterate>
<log/>
<aggregate id="testingid">
<correlateOn expression="json-eval($)"/>
<completeCondition>
<messageCount max="-1" min="-1"/>
</completeCondition>
<onComplete aggregateElementType="root" expression="json-eval($)">
<log>
<property expression="json-eval($)" name="outout"/>
</log>
</onComplete>
</aggregate>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>
The Above use case works fine, when an external endpoint is called and response is returned to the aggregate mediator, but we have to build the response in the default case with out calling an endpoint, How can be this achieved? As the aggregate mediator will be triggered only after it receives response from a back end service.
We have also followed the answer that was provided in the post : WSO2 ESB, Using Aggregate mediator without send/call in iterate mediator
This also does not seems to work, below is the implementation as per the post answer
<?xml version="1.0" encoding="UTF-8"?>
<api context="/propertycheck" name="propertyCheck" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST">
<inSequence>
<iterate attachPath="json-eval($)" expression="json-eval($.main)" id="testingid" preservePayload="true" sequential="true">
<target>
<sequence>
<switch source="json-eval($.sub1)">
<case regex="a">
<log level="full" separator=","/>
<call>
<endpoint>
<http method="post" uri-template="http://localhost:3000/test">
<suspendOnFailure>
<initialDuration>-1</initialDuration>
<progressionFactor>1</progressionFactor>
</suspendOnFailure>
<markForSuspension>
<retriesBeforeSuspension>0</retriesBeforeSuspension>
</markForSuspension>
</http>
</endpoint>
</call>
</case>
<case regex="b">
<log level="full" separator=","/>
<call>
<endpoint>
<http method="post" uri-template="http://localhost:3000/test">
<suspendOnFailure>
<initialDuration>-1</initialDuration>
<progressionFactor>1</progressionFactor>
</suspendOnFailure>
<markForSuspension>
<retriesBeforeSuspension>0</retriesBeforeSuspension>
</markForSuspension>
</http>
</endpoint>
</call>
</case>
<case regex="c">
<log level="full" separator=","/>
<call>
<endpoint>
<http method="post" uri-template="http://localhost:3000/test">
<suspendOnFailure>
<initialDuration>-1</initialDuration>
<progressionFactor>1</progressionFactor>
</suspendOnFailure>
<markForSuspension>
<retriesBeforeSuspension>0</retriesBeforeSuspension>
</markForSuspension>
</http>
</endpoint>
</call>
</case>
<default>
<log>
<property name="default" value="defaultCase"/>
</log>
<payloadFactory media-type="json">
<format>{
"sub1":"My own Response"
}</format>
<args/>
</payloadFactory>
<property name="RESPONSE" scope="default" type="STRING" value="true"/>
<sequence key="AggregateSequence"/>
</default>
</switch>
</sequence>
</target>
</iterate>
<log/>
<aggregate id="testingid">
<correlateOn expression="json-eval($)"/>
<completeCondition>
<messageCount max="-1" min="-1"/>
</completeCondition>
<onComplete aggregateElementType="root" expression="json-eval($)">
<log>
<property expression="json-eval($)" name="outout"/>
</log>
</onComplete>
</aggregate>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>
AggregateSequence
<?xml version="1.0" encoding="UTF-8"?>
<sequence name="AggregateSequence" statistics="enable" trace="enable" xmlns="http://ws.apache.org/ns/synapse">
<log>
<property expression="json-eval($)" name="request to mediator"/>
</log>
<aggregate id="testingid">
<correlateOn expression="json-eval($)"/>
<completeCondition>
<messageCount max="-1" min="-1"/>
</completeCondition>
<onComplete aggregateElementType="root" expression="json-eval($)">
<log>
<property expression="json-eval($)" name="inside Aggregator"/>
</log>
</onComplete>
</aggregate>
</sequence>
Request
{
"main":[
{
"sub1":"a"
},
{
"sub1":"b"
},
{
"sub1":"c"
},
{
"sub1":"d"
}
]
}
Any help is appreciated. Thanks