<?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">&lt;definitions xmlns="http://ws.apache.org/ns/synapse"&gt;

    &lt;localEntry key="stockquoteScript"
                src="file:repository/conf/sample/resources/script/stockquoteTransform.rb"/&gt;

    &lt;sequence name="main"&gt;
        &lt;in&gt;
            &lt;!-- transform the custom quote request into a standard quote request expected by the service --&gt;
            &lt;script language="rb" key="stockquoteScript" function="transformRequest"/&gt;

            &lt;!-- send message to real endpoint referenced by name "stockquote" and stop --&gt;
            &lt;send&gt;
                &lt;endpoint name="stockquote"&gt;
                    &lt;address uri="http://localhost:9000/services/SimpleStockQuoteService"/&gt;
                &lt;/endpoint&gt;
            &lt;/send&gt;
        &lt;/in&gt;
        &lt;out&gt;
            &lt;!-- transform the standard response back into the custom format the client expects --&gt;
            &lt;script language="rb" key="stockquoteScript" function="transformResponse"/&gt;
            &lt;send/&gt;
        &lt;/out&gt;
    &lt;/sequence&gt;

&lt;/definitions&gt;</div>
            <p>
                The external script referenced by the configuration contains the following Ruby
                scriplet.
            </p>
            <div class="xmlConf">&lt;x&gt;&lt;![CDATA[
require 'rexml/document'
include REXML

def transformRequest(mc)
   newRequest= Document.new '&lt;m:getQuote xmlns:m=&quot;http://services.samples&quot;&gt;'&lt;&lt;
      '&lt;m:request&gt;&lt;m:symbol&gt;&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)
end

def transformResponse(mc)
   newResponse = Document.new '&lt;m:CheckPriceResponse xmlns:m=&quot;http://www.apache-synapse.org/test&quot;&gt;&lt;m:Code&gt;' &lt;&lt;
      '&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)
end
]]&gt;&lt;/x&gt;</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>