Showing posts with label Mediator. Show all posts
Showing posts with label Mediator. Show all posts

Sunday, April 10, 2016

[WSO2][ESB][Class Mediator]How to create an Empty OMElement Array?

Hi all,

Let's find out very interesting topic. While using store mediator in ESB, I just wanted to create an empty OMElement array.

If I clarify inside mediation within ESB, when it receive a message, it may convert into OMElements which will is used to next mediation process. Therefore, I can't send empty OMElement array from outside as it is an inside process of ESB (Just like sending SOAP or REST messages).

So we have to convert the message into OMElements within ESB.In my case, I have to add empty OMElement array to the message.

ESB is a marvellous middleware which will provide magical operations. Here it comes "Class Mediator". The Class mediator will retrieve a class and it may act as custom mediator.

So Class Mediator helped me to do my task.

Let's begin...

1) Write a java class which will set OMElement array to the message.

When writing java class which is going to deploy as a "Class Mediator", it must extend from "Abstract Mediator"

In here I used message context (org.apache.synapse.MessageContext) to add empty array.

As a property set new array without any elements.

Sample Configuration

 package org.wso2.test;  
 import java.util.ArrayList;  
 import org.apache.synapse.MessageContext;  
 import org.apache.synapse.mediators.AbstractMediator;  
 public class OMElementEmptyArray  
 extends AbstractMediator {  
   public boolean mediate(MessageContext context) {  
     context.setProperty("EMPTY_ARRAY", new ArrayList());  
     System.out.println("Routed through Class Mediator");  
     return true;  
   }  
 }  

2) Java class packaged as .jar and should be inside $ESB_HOME/repository/components/lib

3) ESB should be up and running.

4) Login to ESB and create an API which will add class mediator.

Sample Configuration:

 <api context="/SerializeProperty" name="StoreMediatorSerialize">  
     <resource methods="GET" protocol="http" url-mapping="/serializeOMArray">  
       <inSequence>  
         <class name="org.wso2.test.OMElementEmptyArray"/>  
       </inSequence>  
       <outSequence/>  
       <faultSequence/>  
     </resource>  
   </api>  

5) Invoke API.

Sample Invocation through REST Client:

GET      http://10.100.7.120:8280/SerializeProperty

Terminal will print follow log:

Routed through Class Mediator


Happy Coding !!!


Wednesday, March 30, 2016

[WSO2 ESB]How to retrive dynamic file which is saved in local folder?

Lets check out the way to retrieve file through ESB mediators.

In here I used

  • XML file which is contains a sequence (File which is saved in local folder)
  • Use Property mediator to retrieve file 
  • Use sequence mediator to run the dynamic file which is saved in local folder (In here I selected sequence mediator, because I saved sequence within XML file)
Lets begin...


  1. XML file should be put into a folder

  • In here Pre-defined sequence added to XML file and saved it with sequence name (dynamic_seq_2.xml). 
Sample configuration:

 <sequence name="dynamic_seq_2" xmlns="http://ws.apache.org/ns/synapse">  
   <in>  
     <log level="full">  
       <property name="message" value="*** Test Message ***"/>  
     </log>  
   </in>  
    <send>  
       <endpoint>  
         <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>  
       </endpoint>  
     </send>  
 </sequence>  

  • Saved "dynamic_seq_2.xml" file in "$ESB_HOME/repository/samples/resources/sequence/"
2. Start axis2 server.

Note: Sample axis2 server comes with ESB pack ($ESB_HOME/samples/axis2Server/). I used it.

          I used SimpleStockQuote service as the backend. Therefore you have to deploy SimpleStockQuote service if you didn't do.

Navigate to $ESB_HOME/samples/axis2Server/src/SimpleStockQuote/ through terminal.
Run "ant" command
 $ ant  

          Then start axis2 server.

Navigate to $ESB_HOME/samples/axis2Server/ through terminal.
Start server
 $ sh axis2server.sh  

3  Start WSO2 ESB server

4. Navigate to Source View (Home > Manage > Service Bus  > Source View  > Service Bus Configuration)

Change registry provider as "ESBRegistry" and give path to the folder which you saved dynamic sequence.

Sample configuration:

 <registry provider="org.wso2.carbon.mediation.registry.ESBRegistry">  
     <parameter name="root">file:repository/samples/resources/</parameter>  
     <parameter name="cachableDuration">15000</parameter>  
 </registry>  

5. Restart ESB

6. Navigate to sequences (Home > Manage > Service Bus > Sequences) and Click on "Add sequences"

7. Create a sequence

   Add property which will have path to retrieve dynamic sequence.

   Add sequence mediator with dynamic key. Give previously created property as Referring sequence.

Sample Configuration

 <sequence name="seq5">  
     <property name="property" scope="default" type="STRING" value="sequence/dynamic_seq_2.xml"/>  
     <sequence key="{get-property('property')}"  
      xmlns:ns3="http://org.apache.synapse/xsd" xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"/>  
 </sequence>  

8.  Create a proxy service with sequence.

Sample Configuration:

 <proxy name="dynamicKeySeq" startOnLoad="true" trace="disable" transports="http https">  
     <description/>  
     <target inSequence="seq5"/>  
 </proxy>  

9.  Invoke proxy service.

Sample Message:

 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.samples" xmlns:xsd="http://org.apache.synapse/xsd">  
   <soapenv:Body>  
    <ser:placeOrder>  
      <!--Optional:-->  
      <ser:order>  
       <!--Optional:-->  
       <xsd:price>91.7473384496671</xsd:price>  
       <!--Optional:-->  
       <xsd:quantity>7616</xsd:quantity>  
       <!--Optional:-->  
       <xsd:symbol>IBM</xsd:symbol>  
      </ser:order>  
    </ser:placeOrder>  
   </soapenv:Body>  
 </soapenv:Envelope>  

Then ESB logs should be indicating redirecting through dynamic sequence.

[2016-03-30 10:32:50,908]  INFO - LogMediator To: http://localhost:8280/services/dynamicKeySeq, WSAction: urn:placeOrder, SOAPAction: urn:placeOrder, ReplyTo: http://www.w3.org/2005/08/addressing/anonymous, MessageID: urn:uuid:02abb47f-bd35-4717-98e3-b3f1f7e2b73e, Direction: request, message = *** Test Message ***, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://org.apache.synapse/xsd" xmlns:ser="http://services.samples"><soapenv:Header xmlns:wsa="http://www.w3.org/2005/08/addressing"><wsa:Action>urn:placeOrder</wsa:Action><wsa:To>http://localhost:8280/services/dynamicKeySeq</wsa:To></soapenv:Header><soapenv:Body>
      <ser:placeOrder>
         <!--Optional:-->
         <ser:order>
            <!--Optional:-->
            <xsd:price>91.7473384496671</xsd:price>
            <!--Optional:-->
            <xsd:quantity>7616</xsd:quantity>
            <!--Optional:-->
            <xsd:symbol>IBM</xsd:symbol>
         </ser:order>
      </ser:placeOrder>
   </soapenv:Body></soapenv:Envelope>

Note: Configuring part of ESB to retrieve local files, done with first 4 steps. Retrieving part done through adding mediator. Confirming and validation part done through sequence mediator logs.

According to your requirements, can modify mediators and relevant configurations.

All comments and value additions are warmly welcome !! :)

Tuesday, March 29, 2016

[WSO2 ESB] How to read a value of SOAP element using property mediator

Hi all,

Property mediator in WSO2 ESB is a very useful mediator and can be use for various mediation purposes. In here I used property mediator to read SOAP element value.

1. I created a sequence with property mediator. Sample Configuration as follows:

 <sequence name="propertySeq">  
     <property expression="//ser:getSimpleQuote/ser:symbol/text()"  
       name="testProperty" scope="default" type="STRING"  
       xmlns:ns="http://org.apache.synapse/xsd"  
       xmlns:ser="http://services.samples" xmlns:xsd="http://sample.wso2.org/xsd"/>  
     <log level="custom">  
       <property expression="get-property('testProperty')"  
         name="PropertyValue" xmlns:ns="http://org.apache.synapse/xsd"/>  
     </log>  
   </sequence>  

2. I created a proxy service with above sequence.

 <proxy name="propertyProx" startOnLoad="true" trace="disable" transports="http https">  
     <description/>  
     <target endpoint="SimpleStockQuote" inSequence="propertySeq"/>  
 </proxy>  
 <endpoint name="SimpleStockQuote">  
     <address uri="http://localhost:9000/services/SimpleStockQuoteService/"/>  
 </endpoint>  

3. Invoke proxy service. 

Note: Through property mediator, I will retrieve value of "symbol" and used logs to indicated retruved value.

Sample SOAP message will be as follows:

 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.samples">  
   <soapenv:Header/>  
   <soapenv:Body>  
    <ser:getSimpleQuote>  
      <!--Optional:-->  
      <ser:symbol>WSO2</ser:symbol>  
    </ser:getSimpleQuote>  
   </soapenv:Body>  
 </soapenv:Envelope>  

Logs should be illustrate as follows: 

 [2016-03-29 18:28:22,380] INFO - LogMediator PropertyValue = WSO2  

Note: According to message type and according to the content you want to add to property mediator, you can change above configurations.

Happy Coding !! :)

Enrich Mediator - WSO2 ESB

Hi all,

Let’s discuss about Enrich mediator of WSO2 ESB. You can find it under core mediators.



And you also can find more information from WSO2 ESB Documentation. Before move to functionalities, I will give small description about “Source” and “Target”



Source - What will be using to do enriching ? (Ex: Property, inline content, body content etc.)
Target - Where and Action going to do Within the enrich process ? (Ex: Replace, add child to payload etc.)

I will here, list down main functionalities of Enrich mediator with regard to its available options. (I will give example scenarios for different Target Actions)

There are main three target actions as “Replace”, “Child” and “Sibling”. With the different source types (Inline, Property, Body etc.), you can perform number of different actions. I present basic sample scenarios for Target Actions.

1. Replace

Config
<sequence name="enrich_seq_3" xmlns="http://ws.apache.org/ns/synapse">
    <property name="testProperty" scope="default" type="STRING" value="WSO2"/>
    <enrich>
       <source clone="true" property="testProperty" type="property"/>
       <target action="replace" type="custom"
           xmlns:ns="http://org.apache.synapse/xsd"
           xmlns:ser="http://services.samples" xpath="//ser:getSimpleQuote/ser:symbol/text()"/>
    </enrich>
    <log level="full"/>
</sequence>

Request
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.samples">
  <soapenv:Header/>
  <soapenv:Body>
     <ser:getSimpleQuote>
        <!--Optional:-->
        <ser:symbol>IBM</ser:symbol>
     </ser:getSimpleQuote>
  </soapenv:Body>
</soapenv:Envelope>

After Enriching
<?xml version='1.0' encoding='UTF-8'?>
  <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.samples">
     <soapenv:Body>    
        <ser:getSimpleQuote>       
           <!--Optional:-->       
              <ser:symbol>WSO2</ser:symbol>    
           </ser:getSimpleQuote>   
        </soapenv:Body>
     </soapenv:Envelope>

2. Adding Child

Config
<sequence name="enrich_seq_11" xmlns="http://ws.apache.org/ns/synapse">
    <log level="full"/>
    <enrich>
       <source clone="true" type="body"/>
       <target action="child" type="body"/>
    </enrich>
    <log level="full"/>
</sequence>

Request
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.samples">
  <soapenv:Header/>
  <soapenv:Body>
     <ser:getSimpleQuote>
        <!--Optional:-->
        <ser:symbol>IBM</ser:symbol>
     </ser:getSimpleQuote>
  </soapenv:Body>
</soapenv:Envelope>

After Enriching

<?xml version='1.0' encoding='UTF-8'?>
  <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.samples">   
     <soapenv:Header xmlns:wsa="http://www.w3.org/2005/08/addressing"/>   
     <soapenv:Body>    
        <ser:getSimpleQuote>       
           <!--Optional:-->       
              <ser:symbol>IBM</ser:symbol>
              <ser:getSimpleQuote>       
                 <!--Optional:-->       
                    <ser:symbol>IBM</ser:symbol>    
                 </ser:getSimpleQuote>
              </ser:getSimpleQuote>   
           </soapenv:Body>
        </soapenv:Envelope>

3. Adding Sibling

Config
<sequence name="enrich_seq_11" xmlns="http://ws.apache.org/ns/synapse">
   <log level="full"/>
   <enrich>
       <source clone="true" type="body"/>
       <target action="sibling" type="body"/>
   </enrich>
   <log level="full"/>
</sequence>

Request
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.samples">
  <soapenv:Header/>
  <soapenv:Body>
     <ser:getSimpleQuote>
        <!--Optional:-->
        <ser:symbol>IBM</ser:symbol>
     </ser:getSimpleQuote>
  </soapenv:Body>
</soapenv:Envelope>

After enriching
<?xml version='1.0' encoding='UTF-8'?>
  <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.samples">   
     <soapenv:Header xmlns:wsa="http://www.w3.org/2005/08/addressing"/>   
     <soapenv:Body>    
        <ser:getSimpleQuote>       
           <!--Optional:-->       
              <ser:symbol>IBM</ser:symbol>    
           </ser:getSimpleQuote>
           <ser:getSimpleQuote>       
              <!--Optional:-->       
            <ser:symbol>IBM</ser:symbol>    
              </ser:getSimpleQuote>   
           </soapenv:Body>
        </soapenv:Envelope>

Note: As I mentioned earlier, this is basic functionalities u can do with Enrich mediator. Try out it through different aspects and find new mediation patterns ;)

Value additions are always welcome :)

Monday, February 29, 2016

Property Mediator - WSO2 ESB

Hi all,

Property mediator is another special mediator in WSO2 ESB. Property Mediator will not change message which will flow through ESB. But it has special qualities like retrieve properties set on message context. It is very useful as we check message properties with given commands.

Property mediator is conditionally content aware mediator. That means, it will not know message context without giving proper commands. We can give commands to see content and get action upon that.

Can retrieve properties using
      get-property(prop-name)
      
When defining this function, it has to be identify relevant scope. If a property has no defined scope, it defaults to the Synapse message context scope.

General syntax for property mediator.

<property name="string" [action=set|remove] [type="string"] (value="literal" | expression="xpath") [scope=default|transport|axis2|axis2-client] [pattern="regex" [group="integer"]]>
<xml-element/>
?
</property>

Ex:
<?xml version="1.0" encoding="UTF-8"?>
<property xmlns:ns="http://org.apache.synapse/xsd" expression="get-property('axis2','jms.message.delivery.count')" name="JMSXDeliveryCount" scope="default" type="INTEGER" />
              

Refere WSO2 Documentation if you keen to know parameters and to get more knowledge :)
Link: https://docs.wso2.com/display/ESB490/Property+Mediator

Note: Remember functionality of property mediator and different properties which can defined through its scope. It is a sure thing, that u will need "property mediator" if u are an ESB user :)

Waiting for value additions :)