Monday, July 20, 2015

How to use Payload Factory in WSO2 ESB

Payload factory mediator is used to transform the message in desired output. It replaces the message of your content; it can be a request, response of any normal content. In the payload we can pass some argument to make it dynamic otherwise in payload we can pass static content as well.

Syntax:
<payloadFactory media-type="xml | json">
    <format ../>
    <args>      
        <arg (value="string" | expression=" {xpath} | {json} ")/>*
    </args>
</payloadFactory>

In above syntax payload factory is defined, which start with keyword ‘payloadFactory’. Inside of payload factory we have to pass format in which we can put any request, response, any structure of xml or any content. If format is dynamic then we have to pass argument to fill its data by <args> tag.

In above format media type can be either json or xml but by default media-type is xml. Ongoing message in payload factory can be changed by the below property.

<property name="messageType" value="application/json" scope="axis2"/>

Here outcome message is being changed in json format.


Example:

<payloadFactory media-type="xml">
        <format>
                <emp:getEmployeeDetails xmlns:deal="http://twodegreesmobile.co.nz/employeedetails">
                        <emp:employeeName>$1</emp:employeeName>
                        <emp:employeePassword>$2</emp:employeePassword>        
                </emp:getEmployeeDetails>
        </format>
        <args>
                <arg expression="get-property('EmpName')"/>
                <arg expression="get-property('EmpPassword')"/>
        </args>
</payloadFactory>
<send>
        <endpoint key="DSS_EmployeeDetailsProxy_EPR"/>
</send>



In above example we are calling an external service as a part of service chaining using payload factory. This service will get employee details from the DSS_EmployeeDetailsProxy service which is a DSS service that is why prefix DSS is used.

In this example we are passing two arguments and if you look into the format tag, argument is getting filled by the $n values. In argument there are 2 values EmpName and EmpPassword and same in the request, we have two placeholder for that with value of $1 and $2, which will be replaced by the argument value.



No comments:

Post a Comment