This documentation is for WSO2 Enterprise Service Bus version 4.9.0 . View documentation for the latest release.

All docs This doc
||
Skip to end of metadata
Go to start of metadata

Objective: Demonstrate how to use scripts in mediation using the Script mediator

The ESB Script Mediator is a ESB extension, and thus all prerequisites are not bundled by default with the ESB distribution. Before you use some scripts, you may need to manually add the required JAR files to the ESB lib directory, and optionally perform other installation tasks as may be required by the individual scripting language. This is explained in the Samples Setup guide.

Prerequisites:

  • Start the Synapse configuration numbered 350: i.e. wso2esb-samples.sh -sn 350
  • Start the Axis2 server and deploy the SimpleStockQuoteService if not already done.
<definitions xmlns="http://ws.apache.org/ns/synapse">
    <localEntry key="stockquoteScript" src="file:repository/samples/resources/script/stockquoteTransform.js"/>
    <sequence name="main">
        <in>
            <!-- transform the custom quote request into a standard quote request expected by the service -->
            <script language="js" key="stockquoteScript" function="transformRequest"/>
            <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" key="stockquoteScript" function="transformResponse"/>
            <send/>
        </out>
    </sequence>
</definitions>
The content of stockquoteTransform.js is as follows:
function transformRequest(mc) {
    var symbol = mc.getPayloadXML()..*::Code.toString();
    mc.setPayloadXML(
            <m:getQuote xmlns:m="http://services.samples">
                <m:request>
                    <m:symbol>{symbol}</m:symbol>
                </m:request>
            </m:getQuote>);
}

function transformResponse(mc) {
    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>);
}

This sample is similar to sample 8, but instead of using XSLT, the transformation is done with JavaScript and E4X. The script used in this example has two functions, 'transformRequest' and 'transformResponse', and the Synapse configuration uses the function attribute to specify which function should be invoked. 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.

For additional examples, see Using Scripts in Mediation (Script Mediator).

  • No labels