Saturday, June 29, 2013

How to test Multipart/form-data with attachments and json content type pass-through support for WSO2 ESB.


I hope the reader of this blog has got an idea about WSO2 ESB and the pass-through transport and the different type of content types support for the product.

To test Multipart/form data, we need the below things ready.

  1. A backend service.
  2. tcpmon to view the pass through
  3. A form to submit data
  4. Attachments – text files, json file, an image
  5. WSO2 ESB with the correct synapse configuration of the proxy


Backend Service

  1. As the backend service, I will be setting the offset to 2 and run a WSO2 App Server, which can also be downloaded from http://wso2.com/products/application-server/. I am using 5.1.0.
  2. I will be using the HelloService in unsecured mode.
  3. I assume now that I have the HelloService up and runninn in, http://localhost:9765/services/HelloService

Set up tcp mon

    1. Start the tcpmon in the <ESB_Home>/bin - sh tcpmon.sh

    2. Create 2 listeners as below.
                 1. Listen Port 8281, Target host – localhost, Target port – 8280 –  Before hits the ESB/ HelloProxyService proxy
                 2. Listen Port 9766, target host – localhost, target port – 9765 – After passing through ESB and Before hits the backend


A form to submit data

I will be using a html which contains a form to submit data and to upload attachments and which has the form action set to the our first tcpmon (Before hits the ESB/ HelloProxyService proxy),
form action="http://localhost:8281/services/HelloProxyService/greet"

 <html>  
  <head><title>multipart/form-data - Client</title></head>  
  <body>   
 <form action="http://localhost:8281/services/HelloProxyService/greet" method="POST" enctype="multipart/form-data">  
 User Name: <input type="text" name="name">  
 User id: <input type="text" name="id">  
 User Address: <input type="text" name="add">  
 AGE: <input type="text" name="age">  
  <br>   
 Upload :   
 <input type="file" name="datafile" size="40" multiple>  
 </p>  
  <input type="submit" value="Submit">  
  </form>  
  </body>  
 </html>  


Attachments

  1. As attachments, I have just a text file with some texts in it
  2. A jpeg image
  3. A Json file with a json object.
E.g., jsonfile.txt

 {  
 "name":"John Johnson",  
 "street":"Oslo West 555",  
 "age":33,  
 "phone":"555 1234567"  
 }  


Synapse configurations

  1. The below proxy will allow the user to view the data that has been sent.
    <Port> = 9766 or a different port you provide
 <proxy name="HelloProxyService"  
      transports="https http"  
      startOnLoad="true"  
      trace="disable">  
    <description/>  
    <target>  
      <endpoint>  
       <address uri="http://localhost:<port>/services/HelloService"/>  
      </endpoint>  
      <outSequence>  
       <property name="OUT_ONLY" value="true"/>  
       <send/>  
      </outSequence>  
    </target>  
   </proxy>  


   2. If you want to test whether this form data saved in to a file correctly, you can enable VFS transport in axis2.xml and use the following synapse accordingly. We will be saving it in to a file in the given location.

 <proxy name="HelloProxyService"  
      transports="https http"  
      startOnLoad="true"  
      trace="disable">  
    <description/>  
    <target>  
      <endpoint>  
       <address uri="vfs:file:///home/ushani/Downloads/out"/>  
      </endpoint>  
      <inSequence>  
       <property name="OUT_ONLY" value="true"/>  
      </inSequence>  
      <outSequence>  
       <send/>  
      </outSequence>  
    </target>  
   </proxy>  

To Test :

Open the html form in browser format and fill in the input values and attach the attachment (Json file or a text file or an image) submit and observe in tcpmon.




(Listen Port 8281, Target host – localhost, Target port – 8280 –  Before hits the ESB/ HelloProxyService proxy)




(Listen Port 9766 or a different port, target host – localhost, target port – 9765 or different port which the backend service is run – After passing through ESB and Before hits the backend)