richard
richard

Reputation: 2053

wso2 esb PayloadFactory how to support array

In ESB, we can create a request by using payloadFactory. But payload factory is a pre-configured json format, and replace the placehold with value. If the request includes an array, it is very difficult. Any suggestion? Payload factory Sample

<payloadFactory media-type="json">
            <format>
             {
                "tableId":"$1",
                "min":"$2",
                "max":"$3"
            }
            </format>
            <args>
                <arg evaluator="xml" expression="$func:tableId"/>
                <arg evaluator="xml" expression="$func:min"/>
                <arg evaluator="xml" expression="$func:max"/>
            </args>
        </payloadFactory>

Upvotes: 2

Views: 1567

Answers (2)

Martin Hald
Martin Hald

Reputation: 676

Like Rajeev already mentioned this can be achived using the script mediator. I've done this for a paging functionality to read the whole content from a db in packages.

Here's an example of what I did, hope that helps as well.

<script language="js">print("Start JS");
        var pageSize = 500;
        var pagesRest = mc.getProperty("result_count") % pageSize;
        var pages = ((mc.getProperty("result_count")-pagesRest)/pageSize)+1;
        var xmlResponse = mc.getPayloadXML();
        var rowCount= mc.getProperty("result_count");
        print("rowCount:  " + rowCount+"    pages:"+pages);
        var rowData = {
            rows : []
        };
        var resultCount=0;

        for(var i=1;i&lt;=pages;i++)
        {
            print("Building page "+i);
            rowData.rows[i] = {};
            rowData.rows[i].from = "" + (i-1)*pageSize ;
            rowData.rows[i].to= "" + (pageSize * i);
            rowData.rows[i].orderBy = "caseid";

            resultCount+=pageSize;
        }

        mc.setPayloadJSON(rowData);

Regards Martin

Upvotes: 4

Rajeev Sampath
Rajeev Sampath

Reputation: 2747

For more dynamic/complex payload modifications you can use the Script Mediator where you can write some javascript code to build the payload.

Upvotes: 1

Related Questions