Child pages
  • Sample 600 : Simple Message Transformation - Rule Mediator for Message Transformation

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Objective: Introduction to simple rule based routing Simple message transformation - Rule mediator for message transformation.

Code Block
languagehtml/xml
<!-- Simple rule based routingtransformation (changing of messages - same as filter mediator message )  -->
<definitions xmlns="http://ws.apache.org/ns/synapse">

   <in> <sequence name="main">
      <rule>  <in>

         <ruleset >  <rule xmlns="http://wso2.org/carbon/rules">
             <source>   <source>soapBody</source>
                <target <package nameaction="SimpleRoutingRulesreplace"                              xmlnsxmlns:m0="http://drools.org/drools-5.0services.samples" resultXpath="//m0:symbol"
                             xmlns:xsxpath="http://www.w3.org/2001/XMLSchema-instance"m0:getQuote/m0:request/m0:symbol">soapBody</target>
                <ruleSet>
            xs:schemaLocation="http://drools.org/drools-5.0 drools-4.0.xsd">                         <import name="org.apache.synapse.MessageContext"<properties/>
                        <rule nameresourceType="regular"Invoke IBM  sourceType="inline">
                        <![CDATA[ package SimpleRoutingRules;

<lhs>                        rule InvokeIBM
       <pattern object-type="MessageContext" identifier="mc"></pattern>               when
                 <pattern object-type="String" identifier="symbol"></pattern>     symbol: String()
                        eval(  <eval>symbolsymbol.equals("IBMMSFT")</eval> || symbol.equals("SUN") )
                        then
</lhs>                             <rhs>mc.setProperty("execute_children","true");</rhs>update(drools.getWorkingMemory().getFactHandle(symbol),"IBM");
                        end
 </rule>                      </package> ]]>
               </source>     </rule>
           <creation>     </ruleSet>
               <property <input namenamespace="sourcehttp://services.samples" valuewrapperElementName="xmlgetQuote"/>
                </creation>             </ruleset>
            <session type="stateless"/>
<fact xmlns:m0="http://services.samples" elementName="symbol" namespace="http://services.samples"
           <facts>                 <fact nametype="mcjava.lang.String" typexpath="context//m0:getQuote/m0:request/m0:symbol/child::text()"/>
                <fact name="symbol" type="java.lang.String"</input>
                        expression<output namespace="http://m0:getQuote/m0:request/m0:symbol/child::text()"services.samples" wrapperElementName="getQuote">
                    <fact  xmlns:m0elementName="symbol" namespace="http://services.samples" type="java.lang.String"/>
            </facts>    </output>
        <childMediators>    </rule>
            <send>
 
                  <endpoint>
                        <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
   
                </endpoint>
  
             </send>

               <drop/></in>
        <out>
   </childMediators>         <<send/rule>>
    </in>     <out>
        <send/></out>
    </out>sequence>

</definitions>

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
  • 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 the client as

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

Or as

Code Block
ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=IBMSUN

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

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

and

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

...

check the axis2server log or console

Code Block
Wed July 04 16:33:05 IST 2012 samples.services.SimpleStockQuoteService :: Generating quote for : IBM