<?xml version="1.0" encoding="iso-8859-1"?>
<!--
  ~  Copyright (c) 2009, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
  ~
  ~  WSO2 Inc. 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.
  -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
  <title>WSO2 ESB - Rule Samples </title>
</head>

<body>
<h1>Running the Rule samples with WSO2 Enterprise Service Bus (ESB) </h1>

<h2>Introduction</h2>

<p>Rule mediator integrates the WSO2 rules component into the WSO2 ESB to
provide you with a suitable capability to define your organization's
dynamic integration decisions in terms of rules.This guide provides you with a
seven samples, which are quite simple. The goal of the samples is to make you
an expertise in using rule mediator. Please follow the configuration guide of
the rule mediator before proceeding with the samples if you have not already
done so.</p>

<h2 id="Sample600">Sample 600 : Rule Mediator as a Filter - Simple Rule Based
Routing </h2>
<pre>&lt;!-- Simple rule based routing  of messages - same as filter mediator --&gt;
&lt;definitions xmlns="http://ws.apache.org/ns/synapse"&gt;
    &lt;in&gt;
        &lt;rule&gt;
            &lt;ruleset &gt;
                &lt;source&gt;
                    &lt;package name="SimpleRoutingRules"
                             xmlns="http://drools.org/drools-5.0"
                             xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
                             xs:schemaLocation="http://drools.org/drools-5.0 drools-4.0.xsd"&gt;
                        &lt;import name="org.apache.synapse.MessageContext"/&gt;
                        &lt;rule name="Invoke IBM "&gt;
                            &lt;lhs&gt;
                                &lt;pattern object-type="MessageContext" identifier="mc"&gt;&lt;/pattern&gt;
                                &lt;pattern object-type="String" identifier="symbol"&gt;&lt;/pattern&gt;
                                &lt;eval&gt;symbol.equals("IBM")&lt;/eval&gt;
                            &lt;/lhs&gt;
                            &lt;rhs&gt;mc.setProperty("execute_children","true");&lt;/rhs&gt;
                        &lt;/rule&gt;
                    &lt;/package&gt;
                &lt;/source&gt;
                &lt;creation&gt;
                    &lt;property name="source" value="xml"/&gt;
                &lt;/creation&gt;
            &lt;/ruleset&gt;
            &lt;session type="stateless"/&gt;
            &lt;facts&gt;
                &lt;fact name="mc" type="context"/&gt;
                &lt;fact name="symbol" type="java.lang.String"
                      expression="//m0:getQuote/m0:request/m0:symbol/child::text()"
                      xmlns:m0="http://services.samples"/&gt;
            &lt;/facts&gt;
            &lt;childMediators&gt;
                &lt;send&gt;
                    &lt;endpoint&gt;
                        &lt;address uri="http://localhost:9000/services/SimpleStockQuoteService"/&gt;
                    &lt;/endpoint&gt;
                &lt;/send&gt;
                &lt;drop/&gt;
            &lt;/childMediators&gt;
        &lt;/rule&gt;
    &lt;/in&gt;
    &lt;out&gt;
        &lt;send/&gt;
    &lt;/out&gt;

&lt;/definitions&gt;
</pre>

<p><strong>Objective</strong>: Introduction to simple rule based routing </p>

<p><strong>Prerequisites:</strong><br />
Start the Synapse configuration numbered 600: (i.e. ./wso2esb-samples.sh -sn
600)</p>

<p>Start the Axis2 server and deploy the SimpleStockQuoteService if not already
deployed.</p>

<p>Run the client as </p>
<pre> ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/</pre>

<div>
<p>Or as </p>
</div>
<pre> ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=IBM</pre>

<p>Then, you will get stock quote price in the client side.</p>

<p>In this sample , with in the rule mediator , the value, which is extracted
from the SOAP envelope using XPath
<strong>'//m0:getQuote/m0:request/m0:symbol'</strong>, is added to the rule
engine as a string fact . The message context property named
<strong>"execute_children" is</strong> to indicate it is need to execute the
child mediators of rule mediator. That property is set only if the symbol is
IBM. </p>

<p>Now use following commands</p>
<pre> ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=SUN</pre>

<div>
</div>

<p>and </p>
<pre>ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=MSFT</pre>

<div>

<div>
<p>Then , you will get errors . This is because , 'symbol' is not equal to
"IBM".</p>
</div>

<h2 id="Sample601">Sample 601: Rule Mediator as a Filter - Simple Rule Based
Routing (Keeping Ruleset in the Registry)</h2>
<pre>&lt;!-- Simple rule based routing  of messages - same as filter mediator --&gt;
&lt;definitions xmlns="http://ws.apache.org/ns/synapse"&gt;

    &lt;!-- the SimpleURLRegistry allows access to a URL based registry (e.g. file:/// or http://) --&gt;
    &lt;registry provider="org.wso2.carbon.mediation.registry.ESBRegistry"&gt;
        &lt;!-- the root property of the simple URL registry helps resolve a resource URL as root + key --&gt;
        &lt;parameter name="root"&gt;file:repository/samples/resources/&lt;/parameter&gt;
        &lt;!-- all resources loaded from the URL registry would be cached for this number of milli seconds --&gt;
        &lt;parameter name="cachableDuration"&gt;15000&lt;/parameter&gt;
    &lt;/registry&gt;


    &lt;localEntry key="rule-script-key"
                src="file:repository/samples/resources/rule/simple_rule_base.xml"/&gt;

    &lt;in&gt;

        &lt;rule&gt;
            &lt;ruleset &gt;
                &lt;source key="rule-script-key"/&gt;
                &lt;creation&gt;
                    &lt;property name="source" value="xml"/&gt;
                &lt;/creation&gt;
            &lt;/ruleset&gt;
            &lt;session type="stateless"/&gt;
            &lt;facts&gt;
                &lt;fact name="mc" type="context"/&gt;
                &lt;fact name="symbol" type="java.lang.String"
                      expression="//m0:getQuote/m0:request/m0:symbol/child::text()"
                      xmlns:m0="http://services.samples"/&gt;
            &lt;/facts&gt;

            &lt;childMediators&gt;
                &lt;send&gt;
                    &lt;endpoint&gt;
                        &lt;address uri="http://localhost:9000/services/SimpleStockQuoteService"/&gt;
                    &lt;/endpoint&gt;
                &lt;/send&gt;
                &lt;drop/&gt;
            &lt;/childMediators&gt;

        &lt;/rule&gt;

    &lt;/in&gt;

    &lt;out&gt;
        &lt;send/&gt;
    &lt;/out&gt;

&lt;/definitions&gt;</pre>
</div>

<h4 id="rule"><strong>rule script - simple_rule_base.xml</strong></h4>
<pre>&lt;package name="SimpleRoutingRules"
         xmlns="http://drools.org/drools-5.0"
         xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
         xs:schemaLocation="http://drools.org/drools-5.0 drools-4.0.xsd"&gt;
    &lt;import name="org.apache.synapse.MessageContext"/&gt;
    &lt;rule name="Invoke IBM "&gt;
        &lt;lhs&gt;
            &lt;pattern object-type="MessageContext" identifier="mc"&gt;
            &lt;/pattern&gt;
            &lt;pattern object-type="String" identifier="symbol"&gt;
            &lt;/pattern&gt;
            &lt;eval&gt;symbol.equals("IBM")&lt;/eval&gt;
        &lt;/lhs&gt;
        &lt;rhs&gt;
            mc.setProperty("execute_children","true");
        &lt;/rhs&gt;
    &lt;/rule&gt;
&lt;/package&gt;</pre>

<p><strong>Objective</strong>: Introduction to simple rule based routing </p>

<p><strong>Prerequisites:</strong><br />
Start the Synapse configuration numbered 601: (i.e. ./wso2esb-samples.sh -sn
601)<br />
Start the Axis2 server and deploy the SimpleStockQuoteService if not already
deployed</p>

<p>Please tests this sample same as above (600) sample because this has same
functionality as above one.</p>

<h2 id="Sample602">Sample 602 : Rule Mediator as a Filter - Simple rule based
routing (Using the Drools Native Language )</h2>
<pre>&lt;!--  Simple rule based routing  of messages - same as filter mediator --&gt;
&lt;!-- The use of Drools native language --&gt;

&lt;definitions xmlns="http://ws.apache.org/ns/synapse"&gt;

    &lt;!-- the SimpleURLRegistry allows access to a URL based registry (e.g. file:/// or http://) --&gt;
    &lt;registry provider="org.wso2.carbon.mediation.registry.ESBRegistry"&gt;
        &lt;!-- the root property of the simple URL registry helps resolve a resource URL as root + key --&gt;
        &lt;parameter name="root"&gt;file:repository/samples/resources/&lt;/parameter&gt;
        &lt;!-- all resources loaded from the URL registry would be cached for this number of milli seconds --&gt;
        &lt;parameter name="cachableDuration"&gt;15000&lt;/parameter&gt;
    &lt;/registry&gt;


    &lt;localEntry key="rule-script-key"
                src="file:repository/samples/resources/rule/simple_rule_base.drl"/&gt;

    &lt;in&gt;

        &lt;rule&gt;
            &lt;ruleset &gt;
                &lt;source key="rule-script-key"/&gt;
            &lt;/ruleset&gt;
            &lt;session type="stateless"/&gt;
            &lt;facts&gt;
                &lt;fact name="mc" type="context"/&gt;
                &lt;fact name="symbol" type="java.lang.String"
                      expression="//m0:getQuote/m0:request/m0:symbol/child::text()"
                      xmlns:m0="http://services.samples"/&gt;
            &lt;/facts&gt;
            &lt;childMediators&gt;
                &lt;send&gt;
                    &lt;endpoint&gt;
                        &lt;address uri="http://localhost:9000/services/SimpleStockQuoteService"/&gt;
                    &lt;/endpoint&gt;
                &lt;/send&gt;
                &lt;drop/&gt;
            &lt;/childMediators&gt;

        &lt;/rule&gt;

    &lt;/in&gt;

    &lt;out&gt;
        &lt;send/&gt;
    &lt;/out&gt;

&lt;/definitions&gt;</pre>

<h4 id="rule1"><strong><bdo dir="ltr">rule script - simple_rule_base.drl
</bdo></strong></h4>
<pre>&lt;drl&gt;
    &lt;![CDATA[
package SimpleRoutingRules;

import org.apache.synapse.MessageContext;

rule InvokeIBM

when

mc : MessageContext()
symbol: String()
eval( symbol.equals("IBM") )

then

mc.setProperty("execute_children","true");

end

]]&gt;
&lt;/drl&gt;</pre>

<p><strong>Objective</strong>: Introduction to simple rule based routing (Using
Drools native language )</p>

<p><strong>Prerequisites:</strong><br />
Start the Synapse configuration numbered 602: (i.e. ./wso2esb-samples.sh -sn
602)<br />
Start the Axis2 server and deploy the SimpleStockQuoteService if not already
deployed</p>

<p>Please tests this sample same as sample 600 because this has same
functionality as it.</p>

<h2 id="Sample603">Sample 603 : Advance Rule Based Routing - Switching Routing
Decision According to the Rules - Rule Mediator as Switch mediator </h2>
<pre>&lt;!-- Advance rule based routing - Switching routing decision from rules  --&gt;

&lt;definitions xmlns="http://ws.apache.org/ns/synapse"&gt;
    &lt;!-- the SimpleURLRegistry allows access to a URL based registry (e.g. file:/// or http://) --&gt;
    &lt;registry provider="org.wso2.carbon.mediation.registry.ESBRegistry"&gt;
        &lt;!-- the root property of the simple URL registry helps resolve a resource URL as root + key --&gt;
        &lt;parameter name="root"&gt;file:repository/samples/resources/&lt;/parameter&gt;
        &lt;!-- all resources loaded from the URL registry would be cached for this number of milli seconds --&gt;
        &lt;parameter name="cachableDuration"&gt;15000&lt;/parameter&gt;
    &lt;/registry&gt;


    &lt;localEntry key="rule-script-key"
                src="file:repository/samples/resources/rule/advanced_rule_base.xml"/&gt;

    &lt;sequence name="ibmSequence"&gt;
        &lt;send&gt;
            &lt;endpoint&gt;
                &lt;address uri="http://localhost:9000/services/SimpleStockQuoteService"/&gt;
            &lt;/endpoint&gt;
        &lt;/send&gt;
    &lt;/sequence&gt;

    &lt;sequence name="myFaultHandler"&gt;
        &lt;makefault&gt;
            &lt;code value="tns:Receiver" xmlns:tns="http://www.w3.org/2003/05/soap-envelope"/&gt;
            &lt;reason expression="get-property('ERROR_MESSAGE')"/&gt;
        &lt;/makefault&gt;

        &lt;property name="RESPONSE" value="true"/&gt;
        &lt;header name="To" expression="get-property('ReplyTo')"/&gt;
        &lt;send/&gt;
    &lt;/sequence&gt;

    &lt;sequence name="sunSequence" onError="myFaultHandler"&gt;

        &lt;send&gt;
            &lt;endpoint&gt;
                &lt;address uri="http://localhost:9009/services/NonExistentStockQuoteService"/&gt;
            &lt;/endpoint&gt;
        &lt;/send&gt;
        &lt;drop/&gt;

    &lt;/sequence&gt;

    &lt;sequence name="msftSequence" onError="myFaultHandler"&gt;

        &lt;send&gt;
            &lt;endpoint&gt;
                &lt;address uri="http://bogus:9000/services/NonExistentStockQuoteService"/&gt;
            &lt;/endpoint&gt;
        &lt;/send&gt;
        &lt;drop/&gt;

    &lt;/sequence&gt;

    &lt;in&gt;
        &lt;rule&gt;
            &lt;ruleset "&gt;
                &lt;source key="rule-script-key"/&gt;
                &lt;creation&gt;
                    &lt;property name="source" value="xml"/&gt;
                &lt;/creation&gt;
            &lt;/ruleset&gt;
            &lt;session type="stateless"/&gt;
            &lt;facts&gt;
                &lt;fact name="mc" type="context"/&gt;
                &lt;fact name="ibmSeq" key="ibmSequence" type="mediator"/&gt;
                &lt;fact name="sunSeq" key="sunSequence" type="mediator"/&gt;
                &lt;fact name="msftSeq" key="msftSequence" type="mediator"/&gt;
                &lt;fact name="symbol" type="java.lang.String"
                      expression="//m0:getQuote/m0:request/m0:symbol/child::text()"
                      xmlns:m0="http://services.samples"/&gt;
            &lt;/facts&gt;
        &lt;/rule&gt;

        &lt;drop/&gt;

    &lt;/in&gt;

    &lt;out&gt;
        &lt;send/&gt;
    &lt;/out&gt;
&lt;/definitions&gt;</pre>

<h4 id="rule2"><strong>rule script - advanced_rule_base.xml </strong></h4>
<pre>&lt;package name="AdvancedRoutingRules"
         xmlns="http://drools.org/drools-5.0"
         xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
         xs:schemaLocation="http://drools.org/drools-5.0 drools-4.0.xsd"&gt;

    &lt;import name="org.apache.synapse.MessageContext"/&gt;
    &lt;import name="org.apache.synapse.Mediator"/&gt;
    &lt;import name="org.apache.synapse.mediators.base.SequenceMediator"/&gt;

    &lt;rule name="Invoke IBM "&gt;
        &lt;lhs&gt;
            &lt;pattern object-type="MessageContext" identifier="mc"&gt;
            &lt;/pattern&gt;
            &lt;pattern object-type="String" identifier="symbol"&gt;
            &lt;/pattern&gt;
            &lt;pattern identifier="seq" object-type="SequenceMediator"&gt;
                &lt;field-binding field-name="name" identifier="a4"/&gt;
                &lt;field-constraint field-name="name"&gt;
                    &lt;literal-restriction evaluator="==" value="ibmSequence"/&gt;
                &lt;/field-constraint&gt;
            &lt;/pattern&gt;
            &lt;eval&gt;symbol.equals("IBM")&lt;/eval&gt;
        &lt;/lhs&gt;

        &lt;rhs&gt;
            ((Mediator)seq).mediate(mc);
        &lt;/rhs&gt;
    &lt;/rule&gt;

    &lt;rule name="Invoke SUN "&gt;
        &lt;lhs&gt;
            &lt;pattern object-type="MessageContext" identifier="mc"&gt;
            &lt;/pattern&gt;
            &lt;pattern object-type="String" identifier="symbol"&gt;
            &lt;/pattern&gt;
            &lt;pattern identifier="seq" object-type="SequenceMediator"&gt;
                &lt;field-binding field-name="name" identifier="a4"/&gt;
                &lt;field-constraint field-name="name"&gt;
                    &lt;literal-restriction evaluator="==" value="sunSequence"/&gt;
                &lt;/field-constraint&gt;
            &lt;/pattern&gt;
            &lt;eval&gt;symbol.equals("SUN")&lt;/eval&gt;
        &lt;/lhs&gt;

        &lt;rhs&gt;
            ((Mediator)seq).mediate(mc);
        &lt;/rhs&gt;
    &lt;/rule&gt;

    &lt;rule name="Invoke MFST "&gt;
        &lt;lhs&gt;
            &lt;pattern object-type="MessageContext" identifier="mc"&gt;
            &lt;/pattern&gt;
            &lt;pattern object-type="String" identifier="symbol"&gt;
            &lt;/pattern&gt;
            &lt;pattern identifier="seq" object-type="SequenceMediator"&gt;
                &lt;field-binding field-name="name" identifier="a4"/&gt;
                &lt;field-constraint field-name="name"&gt;
                    &lt;literal-restriction evaluator="==" value="msftSequence"/&gt;
                &lt;/field-constraint&gt;
            &lt;/pattern&gt;
            &lt;eval&gt;symbol.equals("MSFT")&lt;/eval&gt;
        &lt;/lhs&gt;

        &lt;rhs&gt;
            ((Mediator)seq).mediate(mc);
        &lt;/rhs&gt;
    &lt;/rule&gt;

&lt;/package&gt;
       </pre>

<p><strong>Objective:</strong> Advance rule based routing - Switching routing
decision according to the rules - Rule mediator as Switch mediator. </p>

<p><strong>Prerequisites:</strong><br />
Start the Synapse configuration numbered 603: i.e. (i.e. ./wso2esb-samples.sh
-sn 603)</p>

<p>Start the Axis2 server and deploy the SimpleStockQuoteService if not already
deployed </p>

<p>This sample shows how can rule mediator act as Switch mediator. In this
sample, the use of input with type <strong>'sequence'</strong> is being
introducing. The type 'sequence' indicates the synapse sequences. This enables
to use defined sequences (reference or in-lined) with in rule script.</p>

<p>In rule script , there are three cases each for 'IBM','SUN' and 'MSFT'. When
condition is match , then corresponding rule will be got fire. </p>

<p>Invoke IBM rule by running client as </p>
<pre>ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ </pre>

<div>
</div>

<p>You will get stock quote price successfully </p>

<p>Then invoke SUN rule by running client as </p>
<pre> ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=SUN</pre>

<div>
</div>

<p>Then will get </p>
<pre>&lt;soapenv:Fault xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"&gt;&lt;faultcode&gt;soapenv:Server&lt;/faultcode&gt;&lt;faultstring&gt;java.net.ConnectException: Connection refused&lt;/faultstring&gt;&lt;detail /&gt;&lt;/soapenv:Fault&gt;</pre>

<div>
</div>

<p>Then invoke MSFT rule by running client as </p>
<pre> ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=MSFT</pre>

<div>
</div>

<p>Then will get</p>
<pre>&lt;soapenv:Fault xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"&gt;&lt;faultcode&gt;soapenv:Server&lt;/faultcode&gt;&lt;faultstring&gt;java.net.UnknownHostException: bogus&lt;/faultstring&gt;&lt;detail /&gt;&lt;/soapenv:Fault&gt;</pre>

<div>
</div>

<h2 id="Sample604">Sample 604 : Simple Message Transformation - Rule Mediator
for Message Transformation</h2>
<pre>&lt;!-- Simple rule based transformation (changing message )  --&gt;

&lt;definitions xmlns="http://ws.apache.org/ns/synapse"&gt;
    &lt;!-- the SimpleURLRegistry allows access to a URL based registry (e.g. file:/// or http://) --&gt;
    &lt;registry provider="org.wso2.carbon.mediation.registry.ESBRegistry"&gt;
        &lt;!-- the root property of the simple URL registry helps resolve a resource URL as root + key --&gt;
        &lt;parameter name="root"&gt;file:repository/samples/resources/&lt;/parameter&gt;
        &lt;!-- all resources loaded from the URL registry would be cached for this number of milliseconds --&gt;
        &lt;parameter name="cachableDuration"&gt;15000&lt;/parameter&gt;
    &lt;/registry&gt;


    &lt;localEntry key="rule-script-key"
                src="file:repository/samples/resources/rule/always_ibm.xml"/&gt;
    &lt;in&gt;

        &lt;rule&gt;

            &lt;ruleset &gt;
                &lt;source key="rule-script-key"/&gt;
                &lt;creation&gt;
                        &lt;property name="source" value="xml"/&gt;
                &lt;/creation&gt;
            &lt;/ruleset&gt;
            &lt;session type="stateless"/&gt;
            &lt;facts&gt;
                &lt;fact name="symbol" expression="//m0:getQuote/m0:request/m0:symbol/child::text()"
                      type="java.lang.String" xmlns:m0="http://services.samples"/&gt;
            &lt;/facts&gt;
            &lt;results&gt;
                &lt;result name="symbol" expression="//m0:getQuote/m0:request/m0:symbol"
                        type="java.lang.String" xmlns:m0="http://services.samples"/&gt;
            &lt;/results&gt;

        &lt;/rule&gt;

        &lt;send&gt;
            &lt;endpoint&gt;
                &lt;address uri="http://localhost:9000/services/SimpleStockQuoteService"/&gt;
            &lt;/endpoint&gt;
        &lt;/send&gt;

    &lt;/in&gt;

    &lt;out&gt;
        &lt;send/&gt;
    &lt;/out&gt;

&lt;/definitions&gt;</pre>

<h4 id="rule3"><strong>rule script - always_ibm.xml </strong></h4>
<pre>&lt;package name="SimpleMessageTransformationRules"
         xmlns="http://drools.org/drools-5.0"
         xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
         xs:schemaLocation="http://drools.org/drools-5.0 drools-4.0.xsd"&gt;
    &lt;rule name="Invoke Always IBM "&gt;
        &lt;lhs&gt;
            &lt;pattern object-type="String" identifier="symbol"&gt;
            &lt;/pattern&gt;
            &lt;eval&gt;symbol.equals("MSFT") || symbol.equals("SUN")&lt;/eval&gt;
        &lt;/lhs&gt;
        &lt;rhs&gt;
            update(drools.getWorkingMemory().getFactHandle(symbol),"IBM");
        &lt;/rhs&gt;
    &lt;/rule&gt;

&lt;/package&gt;</pre>

<p><strong>Objective:</strong> Simple message transformation - Rule mediator
for message transformation. </p>

<p><strong>Prerequisites:</strong></p>

<p>Start the Synapse configuration numbered 604: (i.e. ./wso2esb-samples.sh -sn
604)</p>

<p>Start the Axis2 server and deploy the SimpleStockQuoteService if not already
deployed</p>

<p>In this sample , a simple transformation is happened . If the symbol is
either SUN or MSFT , then it will changed into IBM in the SOAP envelope and
then invoke the external service.</p>

<p>Run client as </p>
<pre>ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=MSFT</pre>

<p>Or as </p>
<pre>ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=SUN</pre>

<div>
</div>

<p>Then check the axis2server log or console . </p>
<pre>Mon Mar 03 16:33:05 IST 2008 samples.services.SimpleStockQuoteService :: Generating quote for : IBM</pre>

<h2 id="Sample605">Sample 605 : Simple Message Transformation </h2>
<pre>&lt;!-- Simple rule based transformation (changing message ) --&gt;
&lt;definitions xmlns="http://ws.apache.org/ns/synapse"&gt;
    &lt;!-- the SimpleURLRegistry allows access to a URL based registry (e.g. file:/// or http://) --&gt;
    &lt;registry provider="org.wso2.carbon.mediation.registry.ESBRegistry"&gt;
        &lt;!-- the root property of the simple URL registry helps resolve a resource URL as root + key --&gt;
        &lt;parameter name="root"&gt;file:repository/samples/resources/&lt;/parameter&gt;
        &lt;!-- all resources loaded from the URL registry would be cached for this number of milli seconds --&gt;
        &lt;parameter name="cachableDuration"&gt;15000&lt;/parameter&gt;
    &lt;/registry&gt;

    &lt;localEntry key="rule-script-key"
                src="file:repository/samples/resources/rule/commission_rule.xml"/&gt;
    &lt;in&gt;
        &lt;send&gt;
            &lt;endpoint&gt;
                &lt;address uri="http://localhost:9000/services/SimpleStockQuoteService"/&gt;
            &lt;/endpoint&gt;
        &lt;/send&gt;
    &lt;/in&gt;

    &lt;out&gt;
        &lt;rule&gt;
            &lt;ruleset &gt;
                &lt;source key="rule-script-key"/&gt;
                &lt;creation&gt;
                        &lt;property name="source" value="xml"/&gt;
                &lt;/creation&gt;
            &lt;/ruleset&gt;
            &lt;session type="stateless"/&gt;
            &lt;facts&gt;
                &lt;fact name="symbol" type="java.lang.String"
                      expression="self::node()//m0:return/m1:symbol/child::text()"
                      xmlns:m0="http://services.samples"
                      xmlns:m1="http://services.samples/xsd"/&gt;
                &lt;fact name="price" type="java.lang.Double"
                      expression="self::node()//m0:return/m1:last/child::text()"
                      xmlns:m0="http://services.samples"
                      xmlns:m1="http://services.samples/xsd"/&gt;
            &lt;/facts&gt;
            &lt;results&gt;
                &lt;result name="price" type="java.lang.Double"
                        expression="self::node()//m0:return/m1:last"
                        xmlns:m0="http://services.samples"
                        xmlns:m1="http://services.samples/xsd"/&gt;
            &lt;/results&gt;
        &lt;/rule&gt;
        &lt;send/&gt;
    &lt;/out&gt;
&lt;/definitions&gt;</pre>

<h4 id="rule4"><strong>rule script - commission_rule.xml </strong></h4>
<pre>&lt;package name="SimpleMessageTransformationRules"
         xmlns="http://drools.org/drools-5.0"
         xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
         xs:schemaLocation="http://drools.org/drools-5.0 drools-4.0.xsd"&gt;

    &lt;rule name="Get commission form SUN and MSFT but not from IBM "&gt;

        &lt;lhs&gt;
            &lt;pattern object-type="String" identifier="symbol"&gt;
            &lt;/pattern&gt;
            &lt;pattern object-type="Double" identifier="price"&gt;
            &lt;/pattern&gt;
            &lt;eval&gt;symbol.equals("MSFT") || symbol.equals("SUN")&lt;/eval&gt;
        &lt;/lhs&gt;

        &lt;rhs&gt;
            Double newPrice = price.doubleValue()+ 3000.68;
            update(drools.getWorkingMemory().getFactHandle(price),newPrice);
            retract(drools.getWorkingMemory().getFactHandle(symbol));
        &lt;/rhs&gt;
    &lt;/rule&gt;

&lt;/package&gt;</pre>

<p><strong>Objective</strong>: Simple message transformation - Rule mediator
for message transformation. </p>

<p><strong>Prerequisites:</strong></p>

<p>Start the Synapse configuration numbered 605: (i.e. ./wso2esb-samples.sh -sn
605)<br />
Start the Axis2 server and deploy the SimpleStockQuoteService if not already
deployed.</p>

<p>This sample also illustrates a simple transformation using rule mediator. In
here, for MSFT and SUN a commission is added to the price.(3000.68).</p>

<p>Invoke synapse(esb) with symbol IBM as </p>
<pre>ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ </pre>

<p>Then you will get the price as usual value .</p>
<pre>Standard :: Stock price = $81.35674789750315</pre>

<p>Then invoke synapse(esb) with symbol MSFT or SUN as</p>
<pre>ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=SUN</pre>

<div>
<p>Then you will get the price whic is always above 3000.</p>
<pre> Standard :: Stock price = $3082.1975970450094</pre>
</div>

<h2 id="Sample606">Sample 606 : Advanced Transformation and Use of Custom Java
Objects </h2>
<pre>&lt;!-- Transformation with custom Java object --&gt;

&lt;definitions xmlns="http://ws.apache.org/ns/synapse"&gt;

    &lt;!-- the SimpleURLRegistry allows access to a URL based registry (e.g. file:/// or http://) --&gt;
    &lt;registry provider="org.wso2.carbon.mediation.registry.ESBRegistry"&gt;
        &lt;!-- the root property of the simple URL registry helps resolve a resource URL as root + key --&gt;
        &lt;parameter name="root"&gt;file:repository/samples/resources/&lt;/parameter&gt;
        &lt;!-- all resources loaded from the URL registry would be cached for this number of milliseconds --&gt;
        &lt;parameter name="cachableDuration"&gt;15000&lt;/parameter&gt;
    &lt;/registry&gt;

    &lt;localEntry key="rule-script-forward-key"
                src="file:repository/samples/resources/rule/tranform_forward_rule.drl"/&gt;
    &lt;localEntry key="rule-script-back-key"
                src="file:repository/samples/resources/rule/tranform_back_rule.drl"/&gt;

    &lt;in&gt;

        &lt;rule&gt;
            &lt;ruleset&gt;
                &lt;source key="rule-script-forward-key"/&gt;
            &lt;/ruleset&gt;
            &lt;session type="stateless"/&gt;
            &lt;facts&gt;
                &lt;fact name="mc" type="context"/&gt;
                &lt;fact name="symbol"
                      expression="self::node()//m0:CheckPriceRequest/m0:Code/child::text()"
                      xmlns:m0="http://services.samples" type="java.lang.String"/&gt;
                
            &lt;/facts&gt;
            &lt;results&gt;
                &lt;result name="CheckPriceRequest" expression="self::node()//m0:CheckPriceRequest"
                      xmlns:m0="http://services.samples"
                      type="org.wso2.carbon.rule.samples.GetQuoteRequest"/&gt;
            &lt;/results&gt;

            &lt;childMediators&gt;
                &lt;send&gt;
                    &lt;endpoint&gt;
                        &lt;address uri="http://localhost:9000/services/SimpleStockQuoteService"/&gt;
                    &lt;/endpoint&gt;
                &lt;/send&gt;
                &lt;drop/&gt;
            &lt;/childMediators&gt;
        &lt;/rule&gt;

    &lt;/in&gt;

    &lt;out&gt;

        &lt;rule&gt;
            &lt;ruleset&gt;
                &lt;source key="rule-script-back-key"/&gt;
            &lt;/ruleset&gt;
            &lt;session type="stateless"/&gt;
            &lt;facts&gt;
                &lt;fact name="symbol" type="java.lang.String"
                      expression="self::node()//m0:return/m1:symbol/child::text()"
                      xmlns:m0="http://services.samples"
                      xmlns:m1="http://services.samples/xsd"/&gt;
                &lt;fact name="price" type="java.lang.Double"
                      expression="self::node()//m0:return/m1:last/child::text()"
                      xmlns:m0="http://services.samples"
                      xmlns:m1="http://services.samples/xsd"/&gt;
            &lt;/facts&gt;

            &lt;results&gt;
                &lt;result name="response" expression="self::node()//m0:return"
                        type="org.wso2.carbon.rule.samples.CheckPriceResponse"
                        xmlns:m0="http://services.samples"/&gt;
            &lt;/results&gt;

        &lt;/rule&gt;

        &lt;send/&gt;

    &lt;/out&gt;
&lt;/definitions&gt;
</pre>

<h4 id="rule5"><strong>rule script for request transformation -
tranform_forward_rule.drl </strong></h4>
<pre>&lt;drl&gt;
    &lt;![CDATA[
package ForwardTransformation;

import org.wso2.carbon.rule.samples.GetQuoteRequest ;
import org.apache.synapse.MessageContext;

rule ForwardTransformation

when

symbol : String()
mc : MessageContext()
eval(symbol != null )

then

GetQuoteRequest request = new GetQuoteRequest();

request.setSymbol(symbol);
insert(request);
mc.setProperty("execute_children","true");
end

]]&gt;
&lt;/drl&gt;</pre>

<h4 id="rule6"><strong>rule script for response transformation -
tranform_back_rule.drl </strong></h4>
<pre>&lt;drl&gt;
    &lt;![CDATA[
package BackwardTransformation;

import org.wso2.carbon.rule.samples.CheckPriceResponse;

rule BackwardTransformation

when

price : Double()
symbol : String()
eval( price != null &amp;&amp; symbol != null )

then

CheckPriceResponse response = new CheckPriceResponse();

response.setCode(symbol);
response.setPrice(price);

insert(response);

end

]]&gt;
&lt;/drl&gt;</pre>

<p><strong>Objective:</strong> Advanced transformation using rule mediator. </p>

<p><strong>Prerequisites:</strong></p>

<p>Start the Synapse configuration numbered 606: (i.e. ./wso2esb-samples.sh -sn
606)<br />
Start the Axis2 server and deploy the SimpleStockQuoteService if not already
deployed.</p>

<p>The classes <strong>GetQuoteRequest.java</strong> and
<strong>CheckPriceResponse.java</strong> which are shown in bellow, have to be
compiled and put in classpath. Easy way may be putting as a jar inside
<strong>repository/components/lib</strong>. Pleas note that these classes need
to be group to package <strong>org.wso2.carbon.rule.samples</strong>. </p>

<p>This sample also illustrates a advanced transformation using rule mediator.
</p>

<p>This sample illustrates the functionality offered by XSLT sample (sample 8
in the synapse (esb) samples). Both of the request and response are
transformed.</p>

<p>In this sample , use Java objects to archive the easy transformation. if the
a <strong>toXML()</strong> method returns a valid XML , it is possible to
attached it into SOPA envelope. It is same concepts as serializing object into
XML . if there is no method of <strong>toXML</strong> , then Java Object is
serialized into an XML based <strong>Axis2 POJO to XML binding. </strong></p>

<p><strong>For input transformation</strong></p>
<pre> package org.wso2.carbon.rule.samples;

/**
 *
 */
public class GetQuoteRequest {
    private String symbol;

    public String getSymbol() {
        return symbol;
    }

    public void setSymbol(String symbol) {
        this.symbol = symbol;
    }

    public String toXML() {
        return "&lt;m:getQuote xmlns:m=\"http://services.samples\"&gt;" +
                "&lt;m:request&gt;" +
                "&lt;m:symbol&gt;" + symbol + "&lt;/m:symbol&gt;" +
                "&lt;/m:request&gt;" +
                "&lt;/m:getQuote&gt;";
    }
}</pre>

<p><strong>For output transformation</strong></p>
<pre> package org.wso2.carbon.rule.samples;

/**
 *
 */
public class CheckPriceResponse {
    private String code;
    private Double price;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public String toXML() {
        return "&lt;m:CheckPriceResponse xmlns:m=\"http://services.samples\" " +
                "xmlns:m1=\"http://services.samples/xsd\"&gt;" +
                "&lt;m1:Code&gt;" + code + "&lt;/m1:Code&gt;" +
                "&lt;m1:Price&gt;" + price + "&lt;/m1:Price&gt;" +
                "&lt;/m:CheckPriceResponse&gt;";
    }
}</pre>

<p>This is just a example to show what kind of cool thing can be done using
rule mediator. </p>

<p>Runs the client in custom mode as follows </p>
<pre>ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dmode=customquote</pre>
</body>
</html>
