Search code examples
wso2wso2-data-services-serverwso2-esb

What is exactly doing this payloadFactory mediator?


I am absolutly new in WSO2 and I am working on a WSO2 Enterprise Integrator project containing this ESB project section (I think that my question is only related to ESB).

So my doubt is: I have an XML file defining an API. The flow starts with a payloadFactory mediator, something like this:

<?xml version="1.0" encoding="UTF-8"?>
<api context="/xxxTest2" name="xxxTest2" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="GET">
        <inSequence>
            <!--  Create empty message to get all samples from DSS -->
            <!-- Get Sample ID -->
            <payloadFactory media-type="xml">
                <format>
                    <body/>
                </format>
                <args>
                    <arg evaluator="xml" expression="get-property('uri.var.int_val')" xmlns:ns="http://org.apache.synapse/xsd" xmlns:ns3="http://org.apache.synapse/xsd"/>
                </args>
            </payloadFactory>
            ...............................................................
            ...............................................................
            ...............................................................
</api>

This API handle request that I can send using CURL:

curl -v http://MY_SERVER_IP:8280/xxxTest2

Reading the official documentation:

https://docs.wso2.com/display/ESB481/PayloadFactory+Mediator

I know that the payload factory is used to create a content message that have to be sent using something that send an HTTP Request (correct me if this is a wrong assertion).

My doubt is:

the content of this message is empty because it is:

<format>
    <body/>
</format>

But it contains this argument defined into the args element:

<args>
    <arg evaluator="xml" expression="get-property('uri.var.int_val')" xmlns:ns="http://org.apache.synapse/xsd" xmlns:ns3="http://org.apache.synapse/xsd"/>
</args>

So I have 2 doubts:

1) Reading the previous documentation this argument should contain the content value of a $1 variable in the previous payload content (the ... element). But it is empty. It doesn't contain any variable. So why I find this argument?

2) The value of the argument is retrieved with an expression:

expression="get-property('uri.var.int_val')"

What exactly does this expression? What is retrieving?


Solution

  • For passing an argument to Payload factory mediator, you must declare it in the part.

    Let's say you are passing an argument 'name' to the payload factory mediator, your source should look like

    <payloadFactory media-type="xml" xmlns="http://ws.apache.org/ns/synapse">
    <format>
        <name_of_the_element xmlns="">$1</name_of_the_element>
    </format>
    <args>
        <arg evaluator="xml"
            expression="get-property('uri.var.int_val')" literal="false"/>
    </args>
    

    Here, the value of uri.var.int_val will be passed to the element $1. PAyload factory mediator uses positional parameters for its arguments i.e., $1,$2,....$N.

    1. If there is no arguments in the payload you create, you can delete the part in the source. So if your payload is just , then your source will look like,
     <payloadFactory media-type="xml">
                    <format>
                        <body/>
                    </format>
         </payloadFactory>
    
    1. The expression expression="get-property('uri.var.int_val')" extracts the value of int_val from the URI parameters. Thanks