## Apache Synapse

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

### Pre-requisites

- This sample uses Ruby, so first setup support for this in Synapse as described at Configuring JRuby
- Deploy the SimpleStockQuoteService in the sample Axis2 server and start Axis2
- Synapse does not ship with a Ruby engine by default. Therefore you should download the Ruby engine from JRuby site and copy the downloaded jar file to the 'lib' directory of Synapse.
- Start Synapse using the configuration numbered 354 (repository/conf/sample/synapse_sample_354.xml)
  Unix/Linux: sh synapse.sh -sample 354
  Windows: synapse.bat -sample 354

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