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

Introduction

This Sample demonstrates how to use JavaScript for mediation using the Script mediator

Prerequisites

For a list of general prerequisites, see Prerequisites to Start the ESB Samples.

Building the sample

The XML configuration for this sample is as follows: 

<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> 

This configuration file  synapse_sample_350.xml is available in the <ESB_HOME>/repository/samples directory.

To build the sample

  1. Start the ESB with the sample 350 configuration. For instructions on starting a sample ESB configuration, see Starting the ESB with a sample configuration.
    The operation log keeps running until the server starts, which usually takes several seconds. Wait until the server has fully booted up and displays a message similar to "WSO2 Carbon started in n seconds."

  2. Start the Axis2 server. For instructions on starting the Axis2 server, see Starting the Axis2 server.

  3. Deploy the back-end service SimpleStockQuoteService. For instructions on deploying sample back-end services, see Deploying sample back-end services.

The script used in this sample  stockquoteTransform.js is as follows:

JavaScript
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>);
}

The script has two functions, transformRequest and transformResponse, and the ESB configuration uses the function attribute to specify which function should be invoked. 

Executing the sample

The sample client used here is the Stock Quote Client, which can operate in several modes. For further details on this sample client and its operation modes, see Stock Quote Client.

To execute the sample client

Run the following command from the <ESB_HOME>/samples/axis2Client directory.

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dmode=customquote

Analyzing the output

You will see that the ESB uses the Script mediator and the function specified in the configuration to convert the custom request to a standard quote request. You will also see that the response received is subsequently transformed and sent back to the client.

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

 

  • No labels