This documentation is for WSO2 Enterprise Service Bus version 4.7.0 . View documentation for the latest release.
Skip to end of metadata
Go to start of metadata

Objective: Using scripts in mediation (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 script mediators 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.


  • Start the Synapse configuration numbered 350: i.e. -sn 350
  • Start the Axis2 server and deploy the SimpleStockQuoteService if not already done.
<definitions xmlns="">
    <localEntry key="stockquoteScript" src="file:repository/samples/resources/script/stockquoteTransform.js"/>
    <sequence name="main">
            <!-- transform the custom quote request into a standard quote request expected by the service -->
            <script language="js" key="stockquoteScript" function="transformRequest"/>
                    <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
            <!-- transform the standard response back into the custom format the client expects -->
            <script language="js" key="stockquoteScript" function="transformResponse"/>
The content of stockquoteTransform.js
function transformRequest(mc) {
    var symbol = mc.getPayloadXML()..*::Code.toString();
            <m:getQuote xmlns:m="http://services.samples">

function transformResponse(mc) {
    var symbol = mc.getPayloadXML()..*::symbol.toString();
    var price = mc.getPayloadXML()..*::last.toString();
            <m:CheckPriceResponse xmlns:m="http://services.samples/xsd">
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.


  • No labels