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


Blogger Widgets