Friday, October 16, 2015

Adding namespace and prefix to tags inside payload in wso2 ESB using XSLT

In this post I will share how to add namespace and prefix to tags inside payload in wso2 ESB using XSLT Mediator


We are getting following soap request


  1. <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">  
  2.    <soapenv:Body>  
  3.     <catalog>       
  4.        <product>    
  5.           <number>100</number>    
  6.           <name>BaseBall</name>    
  7.           <colourChoices>Black</Currency>    
  8.           <price>$50</DateTime>  
  9.        </product> 
  10.       </ExchangeRate 
  11.     </catalog>  
  12.    <soapenv:Body>  
  13. <soapenv:Envelope>  


Then we want to add ex0 prefix to catalog tag and namespace must be http://sample.com/test//blog/Sample 


So we need to create sample.xslt style sheet like below


  1.   <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">  
  2.     <xsl:output indent="yes"/>  
  3.     <xsl:strip-space elements="*"/>  
  4.     <!--match all the nodes and attributes-->  
  5.     <xsl:template match="node()|@*">  
  6.         <xsl:copy>  
  7.             <xsl:apply-templates select="node()|@*">  
  8.         </xsl:apply-templates></xsl:copy>  
  9.     </xsl:template>  
  10.     <!--Select the element need to be apply the namespace and prefix -->  
  11.     <xsl:template match="catalog">  
  12.         <!--Define the namespace with prefix ns0 -->  
  13.         <xsl:element name="ex0:{name()}" namespace="http://sample.com/test/blog/Sample">  
  14.             <!--apply to above selected node-->  
  15.             <xsl:apply-templates select="node()|@*">  
  16.         </xsl:apply-templates></xsl:element>  
  17.     </xsl:template>  
  18. </xsl:stylesheet>  



Then Go to Manage-> Service Bus-> Local Entries and add the created sample.xslt file.


Then in your proxy add xslt mediator as <xslt key="sample.xslt"/> and refer the sample.xslt. Add a log mediator after that to check the changed payload


  1. <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">  
  2.    <soapenv:Body>  
  3.       <ex0:catalog xmlns:ex0="http://sample.com/test/blog/Sample">  
  4.          <product>  
  5.             <number>100</number>  
  6.             <name>BaseBall</name>  
  7.             <colourChoices>Black</colourChoices>  
  8.             <price>50</price>  
  9.          </product>  
  10.       </ex0:catalog>  
  11.    </soapenv:Body>  
  12. </soapenv:Envelope>
 





Remove auto generated from the generated payload

In this post I will explain how to remove  auto generated <jsonObject> from the generated payload

Assume that you will get a json request to ESB as following

{"name": "Jack","location": "Australia"}

Then add a property mediator to proxy
<property name="messageType" value="text/xml" scope="axis2"/>

Get the following payload in wso2carbon.log after enable the wire log

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header/>
   <soapenv:Body>
   <jsonObject>
      <name>Jack</name>
      <location>Australia</location>
   </jsonObject>
   </soapenv:Body>
</soapenv:Envelope>

To remove jsonObject from the payload add an enrich mediator 

<enrich>
            <source clone="true" xpath="$body//jsonObject//name"/>
            <target type="body"/>
</enrich>

Then add a log mediator after  enrich mediator and see the carbon.log

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header/>
   <soapenv:Body>
      <name>Jack</name>
      <location>Australia</location>
  </soapenv:Body>
</soapenv:Envelope>

You can use following proxy and check the full scenario explained above.

<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="abcd"
       transports="https,http"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
         <property name="messageType" value="text/xml" scope="axis2"/>
         <enrich>
            <source clone="true" xpath="$body//jsonObject//name"/>
            <target type="body"/>
         </enrich>
         <log level="full"/>
         <send>
            <endpoint>
               <address uri="http://www.google.com"/>
            </endpoint>
         </send>
      </inSequence>
   </target>
   <description/>
</proxy>

Thanks

Thursday, October 15, 2015

Send images for REST API in WSO2 ESB

In this tutorial I will explain how to send images for REST API in WSO2 ESB from SOAP UI

1. Set up the SOAP UI to send images as multi part request (described to previous post). 
The url for endpoint is created by localhost:port/<context >/<url_mapping>  which is defined when creating the API in ESB

2. Now SOAP UI is ready to send the request.Then ESB to receive messages of the image/jpeg content type, 
add the following configurations to the 
<ESB_HOME>/repository/conf/axis2/axis2.xml file

In the Message Builders section:

<messageBuilder contentType="image/jpeg"
                        class="org.wso2.carbon.relay.BinaryRelayBuilder"/>
In the Message Formatters section:
<messageFormatter contentType="image/jpeg"
                        class="org.wso2.carbon.relay.ExpandingMessageFormatter"/>
3. Create the API as following 

  • Go to Main->Service Bus->APIS and click ADD API
  • Then give the API name ,context and click Add Resource
  • Then Add Resource details like methods, url style, in sequence etc. and click update
  • Then go to source view and you will be able to similar API as below.
<api xmlns="http://ws.apache.org/ns/synapse" name="PublicTrafficViolationAPI" context="/dpservice/api/publictrafficviolation">
   <resource methods="POST" url-mapping="/save">
      <inSequence>
         <send>
            <endpoint>
               <address uri="http://localhost:9000/services/SimpleStockQuoteService"></address>
            </endpoint>
         </send>
      </inSequence>
      <outSequence>
         <send></send>
      </outSequence>
   </resource>
</api>
3. Enable the wire in log4j.properties by un-commenting log4j.logger.org.apache.synapse.transport.
http.wire=DEBUG inside <ESB_HOME>/repository/conf

4. Send the http request through SOAP UI and have a look to the wso2carbon.log 
(<ESB_HOME>/repository/logs)

5. You will be able to see similar log as below. ">>" indicate the incoming request to ESB 
and "<<" indicated the outgoing request from ESB

Thanks

Tuesday, October 13, 2015

Sending MultiPart/FormData requests via SOAP UI


  In this post, we will have a quick look into the multipart/form-data requests in soapUI.

1. Create a REST project in SOAP UI and set its HTTP Request to POST



2. Choose multipart/form-data from the Media Type drop down. Click on + icon at the bottom left corner of the attachment window to browse and attach a file 



3. Now it is ready to send the file. Click on green arrow to send. You will be able to see the Content Type of request is set to  multipart/form-data and attachment Content Type is set to image/jpeg


Thursday, August 20, 2015

XSD refered another XSD in wso2 ESB, Governes Registry

1. Install MySQL in Ubuntu and create set of tables which are mapped to governance entries

  • To install mysql : sudo apt-get install mysql-server
  • To log into mysql: mysql -u root -p
  • To view the status :  sudo service mysql status
  • Create database : CREATE DATABASE GOV_REG_DB
  • Use the created database :  use GOV_REG_DB
  •  Run the sql script inside esb to create tables : source /home/sachini/Desktop/wso2/software/WSO2/wso2esb-4.8.1/dbscripts/mysql.sql

 2. Configure wso2 ESB and wso2 governance registry to connect with MYsql

  • Go to <ESB_HOME>/repository/conf/datasources and open master-datasources.xml and add following code to add a datasource(change the DB name according to your DB)
    
<datasource>
   <name>GOV_REG_DB</name>
   <description>The datasource used for registry and user manager</description>
   <jndiConfig>
      <name>jdbc/GOV_REG_DB</name>
   </jndiConfig>
   <definition type="RDBMS">
      <configuration>
         <url>jdbc:mysql://localhost:3306/GOV_REG_DB</url>
         <username>root</username>
         <password />
         <driverClassName>com.mysql.jdbc.Driver</driverClassName>
         <maxActive>50</maxActive>
         <maxWait>60000</maxWait>
         <testOnBorrow>true</testOnBorrow>
         <validationQuery>SELECT 1</validationQuery>
         <validationInterval>30000</validationInterval>
      </configuration>
   </definition>
</datasource>
  • Go to <ESB_HOME>/repository/conf  and add following to registry.xml
                    <dbConfig name="sharedregistry">
                                 <dataSource>jdbc/GOV_REG_DB</dataSource>
                       </dbConfig>

                      <remoteInstance url="https://localhost:9444/registry">
                           <id>instanceid</id>
                           <dbConfig>sharedregistry</dbConfig>
                           <readOnly>false</readOnly>
                           <enableCache>true</enableCache>
                           <registryRoot>/</registryRoot>
                           <cacheId>root@jdbc:mysql://localhost:3306/GOV_REG_DB</cacheId>
                     </remoteInstance>

                    <mount path="/_system/governance" overwrite="true">
                        <instanceId>instanceid</instanceId>
                        <targetPath>/_system/governance</targetPath>
                     </mount>
  •  Go to wso2greg-4.6.0/repository/conf and add following to master-datasources.xml 
     <dbConfig name="sharedregistry">
         <dataSource>jdbc/GOV_REG_DB</dataSource>
   </dbConfig>

   <remoteInstance url="https://localhost:9444/registry">
           <id>instanceid</id>
           <dbConfig>sharedregistry</dbConfig>
           <readOnly>false</readOnly>
           <enableCache>true</enableCache>
           <registryRoot>/</registryRoot>
           <cacheId>root@jdbc:mysql://localhost:3306/GOV_REG_DB</cacheId>
   </remoteInstance>

   <mount path="/_system/governance" overwrite="true">
            <instanceId>instanceid</instanceId>
            <targetPath>/_system/governance</targetPath>
   </mount>

 After that the database will be mounted to governance registry.
 Restart the ESB and governance registry.

3. Upload the wsdl,xsd files(listing5.wsdl, listing4.xsd, listing3.xsd) to governance registry as a gar file




 Then you will be able to see the wsdl under /_system/governance/trunk/wsdls/listing5/listing5.wsdl and both xsd under /_system/governance/trunk/schemas/listing3/listing3.xsd and /_system/governance/trunk/schemas/listing4/listing4.xsd


Like that in ESB console you can see those files as below



 4. Change the reference schema location for listing3.xsd in listing5.wsdl file according to relative path

schemaLocation="../../schemas/listing3/listing3.xsd"


Like that open listing3.xsd and change the reference schema location like

schemaLocation="../listing4/listing4.xsd" (according to relative path from listing3 folder)

Blogger Widgets