Thing which seemed very Thingish inside you is quite different when it gets out into the open and has other people looking at it
Showing posts with label SOAP. Show all posts
Showing posts with label SOAP. Show all posts

Wednesday, August 22, 2012

How to use WSO2 Payload mediator - Calling data service insertion using payload mediator

In my previous blog I showed how to use an iterate mediator to iterate through a soap message. In this post I am going to explain how WSO2 ESB payload mediator works.

Lets say you have a service which provides set of data and you want to call a data service insert operation. This message is generated from a data service, which access a database table and get set of records from the database. Please refer "How to create a MYSQL data service using WSO2 data services Server" If you want to create the data service and generate the below Request,

<Keys xmlns="http://ws.wso2.org/dataservice">
<Key>
    <P_Id>1</P_Id>
    <LastName>Soysa</LastName>
    <FirstName>Amani</FirstName>
    <Address>361 Kotte Road Nugegoda</Address>
    <City>Colombo</City>
 </Key>
 <Key>
    <P_Id>2</P_Id>
    <LastName>Bishop</LastName>
    <FirstName>Peter</FirstName>
    <Address>300 Technology BuildingHouston</Address>
    <City>London</City>
 </Key>
 <Key>
    <P_Id>3</P_Id>
    <LastName>Clark</LastName>
    <FirstName>James</FirstName>
    <Address>Southampton</Address>
    <City>London</City>
 </Key>
 <Key>
    <P_Id>4</P_Id>
    <LastName>Carol</LastName>
    <FirstName>Dilan</FirstName>
    <Address>A221 LSRC Box 90328 </Address>
    <City>Durham</City>
 </Key>
</Keys>

Lets see how we can use this data set which will come to WSO2 ESB as a soap request and we need to extract soap payload data and send them to a data service. First we need to use the iterate mediator  which will iterate the soap request if you have more than one data set. And we need to create the data service soap request using the payload mediator.

   <payloadFactory>
  <format>
     <p:InsertPerson xmlns:p="http://ws.wso2.org/dataservice">
        <p:P_Id>?</p:P_Id>
        <p:LastName>?</p:LastName>
        <p:FirstName>?</p:FirstName>
        <p:Address>?</p:Address>
        <p:City>?</p:City>
     </p:InsertPerson>
  </format>
  <args>
     <arg expression="//P_Id/text()" />
     <arg expression="//LastName/text()" />
     <arg expression="//FirstName/text()" />
     <arg expression="//Address/text()" />
     <arg expression="//City/text()" />
  </args>
</payloadFactory>

Once we create the payload mediator then we can create a send mediator to insert data to data service

  <send>
     <endpoint>
        <address uri="http://localhost:9765/services/MyFirstDSS/" />
     </endpoint>
  </send>

When you add everything together your proxy service will look like shown below.

<proxy xmlns="http://ws.apache.org/ns/synapse" name="AssetProxyService" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
  <target>
     <inSequence>
         <iterate xmlns:m="http://ws.wso2.org/dataservice" id="iter1" expression="//m:Keys/m:Key">
           <target>
              <sequence>
                 <payloadFactory>
                    <format>
                       <p:InsertPerson xmlns:p="http://ws.wso2.org/dataservice">
                          <p:P_Id>?</p:P_Id>
                          <p:LastName>?</p:LastName>
                          <p:FirstName>?</p:FirstName>
                          <p:Address>?</p:Address>
                          <p:City>?</p:City>
                       </p:InsertPerson>
                    </format>
                    <args>
                       <arg expression="//P_Id/text()" />
                       <arg expression="//LastName/text()" />
                       <arg expression="//FirstName/text()" />
                       <arg expression="//Address/text()" />
                       <arg expression="//City/text()" />
                    </args>
                 </payloadFactory>
                 <send>
                    <endpoint>
                       <address uri="http://localhost:9765/services/MyFirstDSS/" />
                    </endpoint>
                 </send>
              </sequence>
           </target>
        </iterate>
     </inSequence>
  </target>
  <description />
</proxy>

Wednesday, August 15, 2012

How to Use Iterator Mediator to Iterate through SOAP message Using WSO2 ESB

Lets say you have a soap response coming from a your service and you need to go through that soap message and get/transform that data and send to another service ... and you have no way of doing it??? WSO2 ESB provide a solution to do this in few easy steps.
Lets look at the following soap message. This message is generated from a data service, which access a database table and get set of records from the database. Please refer "How to create a MYSQL data service using WSO2 data services Server" If you want to create the data service and generate the below response.

SOAP response.



<Keys xmlns="http://ws.wso2.org/dataservice">


  <Key>
     <P_Id>1</P_Id>
     <LastName>Soysa</LastName>
     <FirstName>Amani</FirstName>
     <Address>361 Kotte Road Nugegoda</Address>
     <City>Colombo</City>
  </Key>
  <Key>
     <P_Id>2</P_Id>
     <LastName>Bishop</LastName>
     <FirstName>Peter</FirstName>
     <Address>300 Technology BuildingHouston</Address>
     <City>London</City>
  </Key>
  <Key>
     <P_Id>3</P_Id>
     <LastName>Clark</LastName>
     <FirstName>James</FirstName>
     <Address>Southampton</Address>
     <City>London</City>
  </Key>
  <Key>
     <P_Id>4</P_Id>
     <LastName>Carol</LastName>
     <FirstName>Dilan</FirstName>
     <Address>A221 LSRC Box 90328 </Address>
     <City>Durham</City>
  </Key>
</Keys>

Lets see how we can create a proxy service to get these data and we will log the retrieve data using a log mediator.
Before we start you need to have wso2ESB server and start it up. And login to ESB management console.

Step 1 -  Creating the Custom Proxy - Basics Settings

First give proxy service Name and the targetted WSDL file as shown below. (For the WSDL file you can give WSDL file of the Data service which can be access through the data service-> Service Dashboard)



Step 2 - Defining the Endpoint

Once you are done with basic settings then you need to click on next and Define the endpoint. Click on define endpoint inline  and give the address end point of the created data service.

Step 3 - Defining the Out Sequence.

You need to put the iterator mediator inside the out sequence in order to iterate through the incoming soap message. Therefore, go to Define Out Sequence -> Define Inline -> Add 
Click on Add child ->Advance -> Iterate. 


When you scroll down you will get a wizard where you need to enter some information. Give 
Iterate ID - iter1
Iterate Expression*  //m:Keys/m:Key { Dont forget to add the name space to the iterate expression}  
Namespace prefix m 
Namespace url  http://ws.wso2.org/dataservice    

  
Lets add some log mediators to print the data as shown below (to add log mediator got to add child under target click on core and log).

Once you have added the log mediator and a send mediator inside the target. Once you have done it your design view editor should look like below.



Click on save and close to go back to the proxy service editor and click on finish to finish creating the proxy service.

Once you create the proxy service, your synapse configuration should look like below.


<proxy xmlns="http://ws.apache.org/ns/synapse" name="IteratorProxyService" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">

  <target>
     <outSequence>
        <iterate xmlns:m="http://ws.wso2.org/dataservice" id="iter1" expression="//m:Keys/m:Key">
           <target>
              <sequence>
                 <log level="custom">
                    <property name="P_Id" expression="//m:P_Id/text()" />
                    <property name="LastName" expression="//m:LastName/text()" />
                    <property name="FirstName" expression="//m:FirstName/text()" />
                    <property name="Address" expression="//m:Address/text()" />
                    <property name="City" expression="//m:City/text()" />
                 </log>
                 <send />
              </sequence>
           </target>
        </iterate>
     </outSequence>
     <endpoint>
        <address uri="http://localhost:9765/services/MyFirstDSS/" />
     </endpoint>
  </target>
  <description></description>
</proxy>

If you go to the try it feature inside the service dashboard of your proxy service. You can invoke GetPeople operation to test your proxy service.
Once you invoke that following log will be logged in to your console window

[2012-08-15 14:37:59,078]  INFO - LogMediator P_Id = 1, LastName = Soysa, FirstName = Amani, Address = 361 Kotte Road Nugegoda, City = Colombo
[2012-08-15 14:37:59,084]  INFO - LogMediator P_Id = 2, LastName = Bishop, FirstName = Peter, Address = 300 Technology BuildingHouston, City = London
[2012-08-15 14:37:59,089]  INFO - LogMediator P_Id = 3, LastName = Clark, FirstName = James, Address = Southampton, City = London
[2012-08-15 14:37:59,092]  INFO - LogMediator P_Id = 4, LastName = Carol, FirstName = Dilan, Address = A221 LSRC Box 90328 , City = Durham