This documentation is for WSO2 ESB version 4.5.1. View documentation for the latest release.

Sample 351: In-line script mediation with JavaScript

Objective: Introduction to in-line script mediation

Prerequisites:

  • Start the Synapse configuration numbered 351: i.e. wso2esb-samples.sh -sn 351
  • Start the Axis2 server and deploy the SimpleStockQuoteService if not already done.
<definitions xmlns="http://ws.apache.org/ns/synapse">
    <sequence name="main">
        <in>
            <!-- transform the custom quote request into a standard quote requst expected by the service -->
            <script language="js">
                var symbol = mc.getPayloadXML()..*::Code.toString();
                mc.setPayloadXML(
                &lt;m:getQuote xmlns:m="http://services.samples"&gt;
                &lt;m:request&gt;
                &lt;m:symbol&gt;{symbol}&lt;/m:symbol&gt;
                &lt;/m:request&gt;
                &lt;/m:getQuote&gt;);
            </script>
            <send>
                <endpoint>
                    <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
                </endpoint>
            </send>
        </in>
        <out>
            <!-- transform the standard response back into the custom format the client expects -->
            <script language="js">
                var symbol = mc.getPayloadXML()..*::symbol.toString();
                var price = mc.getPayloadXML()..*::last.toString();
                mc.setPayloadXML(
                &lt;m:CheckPriceResponse xmlns:m="http://services.samples/xsd"&gt;
                &lt;m:Code&gt;{symbol}&lt;/m:Code&gt;
                &lt;m:Price&gt;{price}&lt;/m:Price&gt;
                &lt;/m:CheckPriceResponse&gt;);
            </script>
            <send/>
        </out>
    </sequence>
</definitions>

The functionality of this sample is similar to that of sample 350 and sample 8. It demonstrates how to use in-line scripts in the mediation within the ESB. Use the stock quote client to send a custom quote request as described in sample 350.