By Yogananda

“Remain calm, serene, always in command of yourself. You will then find out how easy it is to get along.”
-By Yogananda

Thursday, December 19, 2013

PayloadFactory Vs Enrich Mediator in WSO2 ESB



When you need to customize the payload of request or a response in WSO2 ESB, then here is the way to go. You can use PayloadFactory mediator to replace the payload with the different payload or you can use enrich mediator to modify the existing payload.

  • The following steps will explain how can a payload be replaced with a different payload using PayloadFactory mediator:


<property name="id" expression="//id" scope="default" type="STRING"/>
<property name="name" expression="//name" scope="default" type="STRING"/>

<payloadFactory media-type="xml">
        <format>
            <m:response xmlns:m="http://services.samples">
                <m:customer>
                    <m:id>$1</m:id>
                    <m:name>$2</m:name>
                </m:customer>
            </m:response>
        </format>
        <args>
            <arg evaluator="xml" expression="$ctx:id"/>
            <arg evaluator="xml" expression="$ctx:name"/>
        </args>
    </payloadFactory>

Where in the argument, the id and name are set using the message context. From the request or response, these id and name can be parsed through the xpath and set it as a property as above.


  • The following steps will explain how can a payload be replaced with a different payload using Enrich mediator:


<property name="id" expression="//id" scope="default" type="STRING"/>
<property name="name" expression="//name" scope="default" type="STRING"/>

<enrich>
            <source type="inline">
                <response >
                <customer>
                    <id>xxx</id>
                    <name>xxxx</name>
                </customer>
            </response>
            </source>
            <target type="body"/>
 </enrich>
<enrich>
            <source type="property" clone="true" property="id"/>
            <target xpath="//s:response/s:customer/s:id" xmlns:s="http://ws.apache.org/ns/synapse"/>
</enrich>
 <enrich>
            <source type="property" clone="true" property="message"/>
            <target xpath="//s:response/s:customer/s:name" xmlns:s="http://ws.apache.org/ns/synapse"/>
</enrich>

In this case, the first enrich will replace the body with the specified payload. Then the second and third enrich will replace the values of the element in the body using the available property.