| <?xml version="1.0" encoding="ISO-8859-1" ?> |
| <!-- |
| ~ Licensed to the Apache Software Foundation (ASF) under one |
| ~ or more contributor license agreements. See the NOTICE file |
| ~ distributed with this work for additional information |
| ~ regarding copyright ownership. The ASF licenses this file |
| ~ to you under the Apache License, Version 2.0 (the |
| ~ "License"); you may not use this file except in compliance |
| ~ with the License. You may obtain a copy of the License at |
| ~ |
| ~ http://www.apache.org/licenses/LICENSE-2.0 |
| ~ |
| ~ Unless required by applicable law or agreed to in writing, |
| ~ software distributed under the License is distributed on an |
| ~ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| ~ KIND, either express or implied. See the License for the |
| ~ specific language governing permissions and limitations |
| ~ under the License. |
| --> |
| |
| <document> |
| <properties> |
| <title>Apache Synapse - Sample 353</title> |
| </properties> |
| <body> |
| <section name="Sample 353: Using Ruby Scripts for Mediation"> |
| <div class="xmlConf"><definitions xmlns="http://ws.apache.org/ns/synapse"> |
| |
| <localEntry key="stockquoteScript" |
| src="file:repository/conf/sample/resources/script/stockquoteTransform.rb"/> |
| |
| <sequence name="main"> |
| <in> |
| <!-- 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 --> |
| <send> |
| <endpoint name="stockquote"> |
| <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="rb" key="stockquoteScript" function="transformResponse"/> |
| <send/> |
| </out> |
| </sequence> |
| |
| </definitions></div> |
| <p> |
| The external script referenced by the configuration contains the following Ruby |
| scriplet. |
| </p> |
| <div class="xmlConf"><x><![CDATA[ |
| require 'rexml/document' |
| include REXML |
| |
| def transformRequest(mc) |
| newRequest= Document.new '<m:getQuote xmlns:m="http://services.samples">'<< |
| '<m:request><m:symbol></m:symbol></m:request></m:getQuote>' |
| newRequest.root.elements[1].elements[1].text = mc.getPayloadXML().root.elements[1].get_text |
| mc.setPayloadXML(newRequest) |
| end |
| |
| def transformResponse(mc) |
| newResponse = Document.new '<m:CheckPriceResponse xmlns:m="http://www.apache-synapse.org/test"><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) |
| end |
| ]]></x></div> |
| <subsection name="Objective"> |
| <p> |
| The script mediator of Synapse can be programmed using any BSF compatible |
| programming language. <a href="sample250.html">Sample 250</a> shows how to |
| configure it using JavaScript. This sample shows how to configure the script |
| mediator with Ruby. |
| </p> |
| </subsection> |
| <subsection name="Pre-requisites"> |
| <p> |
| <ul> |
| <li> |
| This sample uses Ruby so first setup support for this in Synapse as described at |
| <a href="setup/script.html#ruby">Configuring JRuby</a> |
| <br /> |
| </li> |
| <li> |
| Deploy the SimpleStockQuoteService in the sample Axis2 server and start Axis2 |
| </li> |
| <li> |
| 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. |
| </li> |
| <li> |
| Start Synapse using the configuration numbered 353 (repository/conf/sample/synapse_sample_353.xml) |
| <div class="command"> |
| Unix/Linux: sh synapse.sh -sample 353<br/> |
| Windows: synapse.bat -sample 353 |
| </div> |
| </li> |
| </ul> |
| </p> |
| </subsection> |
| <subsection name="Executing the Client"> |
| <p> |
| This sample is identical to <a href="sample350.html">sample 350</a> with the |
| only difference being the use of Ruby instead of JavaScript. Use the stock |
| quote client to send a custom quote request as follows. |
| </p> |
| <div class="command">ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dmode=customquote</div> |
| <p> |
| The Ruby scriplets will transform the requests and responses as they flow through |
| the service bus. |
| </p> |
| </subsection> |
| </section> |
| <p><a href="../samples.html">Back to Catalog</a></p> |
| </body> |
| </document> |