This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.

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">
    <in>
        <!-- transform the custom quote request into a standard quote requst expected by the service -->
        <script language="js"><![CDATA[
               var symbol = mc.getPayloadXML()..*::Code.toString();
               mc.setPayloadXML(
                  <m:getQuote xmlns:m="http://services.samples/xsd">
                     <m:request>
                        <m:symbol>{symbol}</m:symbol>
                     </m:request>
                  </m:getQuote>);
        ]]></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"><![CDATA[
               var symbol = mc.getPayloadXML()..*::symbol.toString();
               var price = mc.getPayloadXML()..*::last.toString();
               mc.setPayloadXML(
                  <m:CheckPriceResponse xmlns:m="http://services.samples/xsd">
               <m:Code>{symbol}</m:Code>
               <m:Price>{price}</m:Price>
                  </m:CheckPriceResponse>);
            ]]></script>
        <send/>
    </out>
</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.