This documentation is for WSO2 ESB version 4.5.1. View documentation for the latest release.

Sample 354: Using In-Lined Ruby Scripts for Mediation

Objective: Script mediators using Ruby (In-lined Ruby Script)

<definitions xmlns="http://ws.apache.org/ns/synapse">
    <sequence name="main">
        <in>
            <script language="rb">

                require 'rexml/document'
                include REXML
                newRequest= Document.new '&lt;m:getQuote xmlns:m="http://services.samples"&gt;&lt;m:request&gt;&lt;m:symbol&gt;...test...&lt;/m:symbol&gt;&lt;/m:request&gt;&lt;/m:getQuote&gt;'
                newRequest.root.elements[1].elements[1].text = $mc.getPayloadXML().root.elements[1].get_text
                $mc.setPayloadXML(newRequest)
            </script>
            <send>
                <endpoint>
                    <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
                </endpoint>
            </send>
        </in>
        <out>
            <script language="rb">

                require 'rexml/document'
                include REXML
                newResponse = Document.new '&lt;m:CheckPriceResponse xmlns:m="http://services.samples/xsd"&gt;&lt;m:Code&gt;&lt;/m:Code&gt;&lt;m:Price&gt;&lt;/m:Price&gt;&lt;/m:CheckPriceResponse&gt;'
                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
                $mc.setPayloadXML(newResponse)
            </script>
            <send/>
        </out>
    </sequence>
</definitions>

Prerequisites:

  • This sample uses Ruby so first setup support for this in ESB as described at Configuring JRuby.
  • Start the Synapse configuration numbered 354: i.e. wso2esb-samples -sn 354
  • Start the Axis2 server and deploy the SimpleStockQuoteService if not already done

This sample is functionally equivalent to Sample 353.

Run the client with

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

Database Interactions in Mediation (DBLookup / DBReport)

Following database mediators use Derby in a client/server configuration by using the network server. Therefore, to proceed with the following samples, you need a working Derby database server and you have to follow the steps in Sample Setup Guide before going through the samples.