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

Saturday, March 26, 2016

Adding JMS Message Store with HornetQ Broker

1) Download HornetQ Broker : http://hornetq.jboss.org/downloads.html

2) Configure HornetQ:

  • Go to HornetQ_Home/config/stand-alone/non-clustered/
  • You will find hornetq_jms.xml
  • Change it as follows:

Note: This will create queue connection factory, topic connection factory and a queue. Queue will be used as storing place for JMS message store.

 <connection-factory name="QueueConnectionFactory">  
    <xa>false</xa>  
    <connectors>  
      <connector-ref connector-name="netty"/>  
    </connectors>  
    <entries>  
      <entry name="/QueueConnectionFactory"/>  
    </entries>  
 </connection-factory>  
 <connection-factory name="TopicConnectionFactory">  
    <xa>false</xa>  
    <connectors>  
      <connector-ref connector-name="netty"/>  
    </connectors>  
    <entries>  
      <entry name="/TopicConnectionFactory"/>  
    </entries>  
 </connection-factory>  
 <queue name="wso2">  
    <entry name="/queue/mySampleQueue"/>  
 </queue>  


3) Configure ESB: 

  • Go to ESB_Home/repository/components/lib/
  • Add hornetq-all.jar: https://docs.google.com/a/wso2.com/file/d/0B_g60lAUxtLhd2RmZXJWUVkwWXM/edit
  • Go to ESB_Home/repository/conf/axis2/
  • You will find axis2.xml
  • Change it as follows:

Uncomment follow line:

 <transportSender name="jms" class="org.apache.axis2.transport.jms.JMSSender"/>  

Add these lines:

 <transportReceiver name="jms"  
  class="org.apache.axis2.transport.jms.JMSListener">  
  <parameter name="myTopicConnectionFactory" locked="false">  
  <parameter name="java.naming.factory.initial" locked="false">org.jnp.interfaces.NamingContextFactory</parameter>  
  <parameter name="java.naming.factory.url.pkgs" locked="false">org.jboss.naming:org.jnp.interfaces</parameter>  
  <parameter name="java.naming.provider.url" locked="false">jnp://localhost:1099</parameter>  
  <parameter name="transport.jms.ConnectionFactoryJNDIName"  
   locked="false">TopicConnectionFactory</parameter>  
  <parameter name="transport.jms.ConnectionFactoryType"  
   locked="false">topic</parameter>  
  </parameter>  
  <parameter name="myQueueConnectionFactory" locked="false">  
  <parameter name="java.naming.factory.initial" locked="false">org.jnp.interfaces.NamingContextFactory</parameter>  
  <parameter name="java.naming.factory.url.pkgs" locked="false">org.jboss.naming:org.jnp.interfaces</parameter>  
  <parameter name="java.naming.provider.url" locked="false">jnp://localhost:1099</parameter>  
  <parameter name="transport.jms.ConnectionFactoryJNDIName"  
   locked="false">QueueConnectionFactory</parameter>  
  <parameter name="transport.jms.ConnectionFactoryType"  
   locked="false">queue</parameter>  
  </parameter>  
  <parameter name="default" locked="false">  
  <parameter name="java.naming.factory.initial" locked="false">org.jnp.interfaces.NamingContextFactory</parameter>  
  <parameter name="java.naming.factory.url.pkgs" locked="false">org.jboss.naming:org.jnp.interfaces</parameter>  
  <parameter name="java.naming.provider.url" locked="false">jnp://localhost:1099</parameter>  
  <parameter name="transport.jms.ConnectionFactoryJNDIName"  
   locked="false">QueueConnectionFactory</parameter>  
  <parameter name="transport.jms.ConnectionFactoryType"  
   locked="false">queue</parameter>  
  </parameter>  
 </transportReceiver>  

  • You have to change JNDI properties of ESB
  • Go to ESB_HOME/repository/conf
  • And you will find jndi.properties file. Open and edit it. Add following line:
 connectionfactory.QueueConnectionFactory = jnp://localhost:1099  

Note: if you need you can also add queues and topics here.

4) Start HornetQ (HornetQ_home/bin)

 sudo ./run.sh  

5) Start ESB server (ESB_Home/bin)

 sh wso2server.sh  

6) Go to management console (https://<IP>:9443/carbon/admin/login.jsp)

7) Go to message stores (Home > Manage > Service Bus > Message Stores)

8) Add JMS message store (Add message stores > Add JMS message store)

9) Give relevant details to add message store

  • Name: As you want
  • Initial Context Factory: org.jnp.interfaces.NamingContextFactory
  • Provider URL: jnp://localhost:1099
  • JNDI Queue Name: queue/mySampleQueue
    Add other details accordingly. 

Note: Remember to remove user name and password. If you are using them, you have to configure HornetQ with that username and password.

10) Click "Save". Then your JMS message store will be created.

Yea, You may feel like "Tadaaa". But for me, it took long time to figure this out ;) 

Happy coding :)



Thursday, March 10, 2016

Adding a sequence to ESB local registry

Hi all,

Its' WSO2 ESB. Lets add an sequence to ESB local registry. Adding sequences/or resources in to governance or configuration registry is easy. Because you can do it easily through Management Console (UI).

Lets consider about local registry...


       1.  Login to ESB

       2.  Browse registry (Home > Registry > Browse)                                                                        

   
      3.  Then u can see config, governance and local registries under _system


      4.  Click on "local"

      5.  Click on "Add resource"


      6.  Give the sequence xml file. (You have to create separate xml file which contains sequence.)
      
<?xml version="1.0" encoding="UTF-8"?>
<sequence xmlns="http://ws.apache.org/ns/synapse" name="localSeq">
    <log level="full">
        <property name="Message" value="This is on store sequence which is stored in local registry" />
    </log>
</sequence>
         
        (U have to save this xml file with the name of sequence. Ex: localSeq.xml)

       7. Click on "Add". Then it will be add to local.           


                                                                         
        8.  Then you have to point it via some mediator or from somewhere. I am using store mediator and pointing it as follows:
<store messageStore="reg" sequence="local:/localSeq.xml" />
             Use syntax "local:/localSeq.xml"