This documentation is for WSO2 Enterprise Service Bus version 5.0.0. For the latest ESB, view the latest WSO2 Enterprise Integrator documentation.

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.


  • 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 is as follows:


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.

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

  • No labels