Running the Rule samples with WSO2 Enterprise Service Bus (ESB)

Introduction

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.

Sample 600 : Rule Mediator as a Filter - Simple Rule Based Routing

<!-- Simple rule based routing  of messages - same as filter mediator -->
<definitions xmlns="http://ws.apache.org/ns/synapse">
    <in>
        <rule>
            <ruleset >
                <source>
                    <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">
                        <import name="org.apache.synapse.MessageContext"/>
                        <rule name="Invoke IBM ">
                            <lhs>
                                <pattern object-type="MessageContext" identifier="mc"></pattern>
                                <pattern object-type="String" identifier="symbol"></pattern>
                                <eval>symbol.equals("IBM")</eval>
                            </lhs>
                            <rhs>mc.setProperty("execute_children","true");</rhs>
                        </rule>
                    </package>
                </source>
                <creation>
                    <property name="source" value="xml"/>
                </creation>
            </ruleset>
            <session type="stateless"/>
            <facts>
                <fact name="mc" type="context"/>
                <fact name="symbol" type="java.lang.String"
                      expression="//m0:getQuote/m0:request/m0:symbol/child::text()"
                      xmlns:m0="http://services.samples"/>
            </facts>
            <childMediators>
                <send>
                    <endpoint>
                        <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
                    </endpoint>
                </send>
                <drop/>
            </childMediators>
        </rule>
    </in>
    <out>
        <send/>
    </out>

</definitions>

Objective: Introduction to simple rule based routing

Prerequisites:
Start the Synapse configuration numbered 600: (i.e. ./wso2esb-samples.sh -sn 600)

Start the Axis2 server and deploy the SimpleStockQuoteService if not already deployed.

Run the client as

 ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/

Or as

 ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=IBM

Then, you will get stock quote price in the client side.

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

Now use following commands

 ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=SUN

and

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=MSFT

Then , you will get errors . This is because , 'symbol' is not equal to "IBM".

Sample 601: Rule Mediator as a Filter - Simple Rule Based Routing (Keeping Ruleset in the Registry)

<!-- Simple rule based routing  of messages - same as filter mediator -->
<definitions xmlns="http://ws.apache.org/ns/synapse">

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


    <localEntry key="rule-script-key"
                src="file:repository/samples/resources/rule/simple_rule_base.xml"/>

    <in>

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

            <childMediators>
                <send>
                    <endpoint>
                        <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
                    </endpoint>
                </send>
                <drop/>
            </childMediators>

        </rule>

    </in>

    <out>
        <send/>
    </out>

</definitions>

rule script - simple_rule_base.xml

<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">
    <import name="org.apache.synapse.MessageContext"/>
    <rule name="Invoke IBM ">
        <lhs>
            <pattern object-type="MessageContext" identifier="mc">
            </pattern>
            <pattern object-type="String" identifier="symbol">
            </pattern>
            <eval>symbol.equals("IBM")</eval>
        </lhs>
        <rhs>
            mc.setProperty("execute_children","true");
        </rhs>
    </rule>
</package>

Objective: Introduction to simple rule based routing

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

Please tests this sample same as above (600) sample because this has same functionality as above one.

Sample 602 : Rule Mediator as a Filter - Simple rule based routing (Using the Drools Native Language )

<!--  Simple rule based routing  of messages - same as filter mediator -->
<!-- The use of Drools native language -->

<definitions xmlns="http://ws.apache.org/ns/synapse">

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


    <localEntry key="rule-script-key"
                src="file:repository/samples/resources/rule/simple_rule_base.drl"/>

    <in>

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

        </rule>

    </in>

    <out>
        <send/>
    </out>

</definitions>

rule script - simple_rule_base.drl

<drl>
    <![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

]]>
</drl>

Objective: Introduction to simple rule based routing (Using Drools native language )

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

Please tests this sample same as sample 600 because this has same functionality as it.

Sample 603 : Advance Rule Based Routing - Switching Routing Decision According to the Rules - Rule Mediator as Switch mediator

<!-- Advance rule based routing - Switching routing decision from rules  -->

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


    <localEntry key="rule-script-key"
                src="file:repository/samples/resources/rule/advanced_rule_base.xml"/>

    <sequence name="ibmSequence">
        <send>
            <endpoint>
                <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
            </endpoint>
        </send>
    </sequence>

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

        <property name="RESPONSE" value="true"/>
        <header name="To" expression="get-property('ReplyTo')"/>
        <send/>
    </sequence>

    <sequence name="sunSequence" onError="myFaultHandler">

        <send>
            <endpoint>
                <address uri="http://localhost:9009/services/NonExistentStockQuoteService"/>
            </endpoint>
        </send>
        <drop/>

    </sequence>

    <sequence name="msftSequence" onError="myFaultHandler">

        <send>
            <endpoint>
                <address uri="http://bogus:9000/services/NonExistentStockQuoteService"/>
            </endpoint>
        </send>
        <drop/>

    </sequence>

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

        <drop/>

    </in>

    <out>
        <send/>
    </out>
</definitions>

rule script - advanced_rule_base.xml

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

    <import name="org.apache.synapse.MessageContext"/>
    <import name="org.apache.synapse.Mediator"/>
    <import name="org.apache.synapse.mediators.base.SequenceMediator"/>

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

        <rhs>
            ((Mediator)seq).mediate(mc);
        </rhs>
    </rule>

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

        <rhs>
            ((Mediator)seq).mediate(mc);
        </rhs>
    </rule>

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

        <rhs>
            ((Mediator)seq).mediate(mc);
        </rhs>
    </rule>

</package>
       

Objective: Advance rule based routing - Switching routing decision according to the rules - Rule mediator as Switch mediator.

Prerequisites:
Start the Synapse configuration numbered 603: i.e. (i.e. ./wso2esb-samples.sh -sn 603)

Start the Axis2 server and deploy the SimpleStockQuoteService if not already deployed

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

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

Invoke IBM rule by running client as

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ 

You will get stock quote price successfully

Then invoke SUN rule by running client as

 ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=SUN

Then will get

<soapenv:Fault xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><faultcode>soapenv:Server</faultcode><faultstring>java.net.ConnectException: Connection refused</faultstring><detail /></soapenv:Fault>

Then invoke MSFT rule by running client as

 ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=MSFT

Then will get

<soapenv:Fault xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><faultcode>soapenv:Server</faultcode><faultstring>java.net.UnknownHostException: bogus</faultstring><detail /></soapenv:Fault>

Sample 604 : Simple Message Transformation - Rule Mediator for Message Transformation

<!-- Simple rule based transformation (changing message )  -->

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


    <localEntry key="rule-script-key"
                src="file:repository/samples/resources/rule/always_ibm.xml"/>
    <in>

        <rule>

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

        </rule>

        <send>
            <endpoint>
                <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
            </endpoint>
        </send>

    </in>

    <out>
        <send/>
    </out>

</definitions>

rule script - always_ibm.xml

<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">
    <rule name="Invoke Always IBM ">
        <lhs>
            <pattern object-type="String" identifier="symbol">
            </pattern>
            <eval>symbol.equals("MSFT") || symbol.equals("SUN")</eval>
        </lhs>
        <rhs>
            update(drools.getWorkingMemory().getFactHandle(symbol),"IBM");
        </rhs>
    </rule>

</package>

Objective: Simple message transformation - Rule mediator for message transformation.

Prerequisites:

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

Start the Axis2 server and deploy the SimpleStockQuoteService if not already deployed

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.

Run client as

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=MSFT

Or as

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=SUN

Then check the axis2server log or console .

Mon Mar 03 16:33:05 IST 2008 samples.services.SimpleStockQuoteService :: Generating quote for : IBM

Sample 605 : Simple Message Transformation

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

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

    <out>
        <rule>
            <ruleset >
                <source key="rule-script-key"/>
                <creation>
                        <property name="source" value="xml"/>
                </creation>
            </ruleset>
            <session type="stateless"/>
            <facts>
                <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"/>
                <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"/>
            </facts>
            <results>
                <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"/>
            </results>
        </rule>
        <send/>
    </out>
</definitions>

rule script - commission_rule.xml

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

    <rule name="Get commission form SUN and MSFT but not from IBM ">

        <lhs>
            <pattern object-type="String" identifier="symbol">
            </pattern>
            <pattern object-type="Double" identifier="price">
            </pattern>
            <eval>symbol.equals("MSFT") || symbol.equals("SUN")</eval>
        </lhs>

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

</package>

Objective: Simple message transformation - Rule mediator for message transformation.

Prerequisites:

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

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

Invoke synapse(esb) with symbol IBM as

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ 

Then you will get the price as usual value .

Standard :: Stock price = $81.35674789750315

Then invoke synapse(esb) with symbol MSFT or SUN as

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=SUN

Then you will get the price whic is always above 3000.

 Standard :: Stock price = $3082.1975970450094

Sample 606 : Advanced Transformation and Use of Custom Java Objects

<!-- Transformation with custom Java object -->

<definitions xmlns="http://ws.apache.org/ns/synapse">

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

    <localEntry key="rule-script-forward-key"
                src="file:repository/samples/resources/rule/tranform_forward_rule.drl"/>
    <localEntry key="rule-script-back-key"
                src="file:repository/samples/resources/rule/tranform_back_rule.drl"/>

    <in>

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

            <childMediators>
                <send>
                    <endpoint>
                        <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
                    </endpoint>
                </send>
                <drop/>
            </childMediators>
        </rule>

    </in>

    <out>

        <rule>
            <ruleset>
                <source key="rule-script-back-key"/>
            </ruleset>
            <session type="stateless"/>
            <facts>
                <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"/>
                <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"/>
            </facts>

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

        </rule>

        <send/>

    </out>
</definitions>

rule script for request transformation - tranform_forward_rule.drl

<drl>
    <![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

]]>
</drl>

rule script for response transformation - tranform_back_rule.drl

<drl>
    <![CDATA[
package BackwardTransformation;

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

rule BackwardTransformation

when

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

then

CheckPriceResponse response = new CheckPriceResponse();

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

insert(response);

end

]]>
</drl>

Objective: Advanced transformation using rule mediator.

Prerequisites:

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

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

This sample also illustrates a advanced transformation using rule mediator.

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

In this sample , use Java objects to archive the easy transformation. if the a toXML() 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 toXML , then Java Object is serialized into an XML based Axis2 POJO to XML binding.

For input transformation

 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 "<m:getQuote xmlns:m=\"http://services.samples\">" +
                "<m:request>" +
                "<m:symbol>" + symbol + "</m:symbol>" +
                "</m:request>" +
                "</m:getQuote>";
    }
}

For output transformation

 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 "<m:CheckPriceResponse xmlns:m=\"http://services.samples\" " +
                "xmlns:m1=\"http://services.samples/xsd\">" +
                "<m1:Code>" + code + "</m1:Code>" +
                "<m1:Price>" + price + "</m1:Price>" +
                "</m:CheckPriceResponse>";
    }
}

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

Runs the client in custom mode as follows

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dmode=customquote