Sample 354: Using Inline Ruby Scripts for Mediation
<definitions xmlns="http://ws.apache.org/ns/synapse">
<sequence name="main">
<in>
<script language="rb">
require 'rexml/document'
include REXML
newRequest= Document.new '<m:getQuote xmlns:m="http://services.samples"><m:request><m:symbol>...test...</m:symbol></m:request></m:getQuote>'
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 '<m:CheckPriceResponse
xmlns:m="http://services.samples/xsd"><m:Code></m:Code><m:Price></m:Price></m:CheckPriceResponse>'
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>
Objective
Shows how to embed Ruby scripts in the Synapse configuration itself.
Executing the Client
Run the sample client as follows.
ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dmode=customquote
The inline Ruby scripts will transform the requests and responses.
Back to Catalog