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: Inline script mediation with JavaScript

Objective: Introduction to inline 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 request 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 inline scripts in the mediation within the ESB. Use the stock quote client to issue a custom quote client as follows.

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dmode=customquote
ESB uses the script mediator and the specified Javascript function to convert the custom request to a standard quote request. Subsequently the response received is transformed and sent back to the client.

Â