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: Script mediators using Ruby

<definitions xmlns="">

    <localEntry key="stockquoteScript" src="file:repository/samples/resources/script/stockquoteTransform.rb"/>
        <!-- transform the custom quote request into a standard quote request expected by the service -->
        <script language="rb" key="stockquoteScript" function="transformRequest"/>

        <!-- send message to real endpoint referenced by name "stockquote" and stop -->
            <endpoint name="stockquote">
                <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
        <!-- transform the standard response back into the custom format the client expects -->
        <script language="rb" key="stockquoteScript" function="transformResponse"/>
require 'rexml/document'
include REXML

def transformRequest(mc)
   newRequest= '<m:getQuote xmlns:m="http://services.samples/xsd">'<<
   newRequest.root.elements[1].elements[1].text = mc.getPayloadXML().root.elements[1].get_text

def transformResponse(mc)
   newResponse = '<m:CheckPriceResponse xmlns:m="http://services.samples/xsd"><m:Code>' <<
   newResponse.root.elements[1].text = mc.getPayloadXML().root.elements[1].elements[1].get_text
   newResponse.root.elements[2].text = mc.getPayloadXML().root.elements[1].elements[2].get_text


This sample uses Ruby so first set up Ruby support as described in Configuring the ESB for Script Mediator Support.

  • Start the Synapse configuration numbered 353: i.e. wso2esb-samples -sn 353
  • Start the Axis2 server and deploy the SimpleStockQuoteService if not already done

This sample is functionally equivalent to sample # 350 (#351 and #8) but instead uses a Ruby script using the JRuby interpreter. The script has two functions, 'transformRequest' and 'transformResponse', and the Synapse configuration specifies to be invoked when used. Execute the stock quote client to send a custom stock quote as per Sample 350 and check the received stock quote response.

  • No labels