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)


Sunday, June 23, 2013

How to create a MySQL database and a datasource using WSO2 Appfactory


With the beta release of WSO2 App factory, it enables you to create a database and datasource for the applications you create using App Factory.
Current latest version supports MySQL.
This blog post explains you steps to create a database and a datasource and how to point them in a Web application (WAR) created using WSO2 App Factory.

For more information on WSO2 App Factory :



To create a WAR/ Web app supports the datasource

  1. Registered user can successfully login to WSO2 app factory and create a web application. As an example I will be creating a web application named ColourFindApp.


    Application Creation

  2. Once I created the application, I will be assigning user roles for the application using the Team page.
  3. Then a developer or the app owner which has developer rights will be checking out the source code using GIT and do the necessary code level changes via Developer studio and then commit the code.
    http://ushanib.blogspot.com/2013/02/steps-to-checkout-and-commit-wso2.html
  4. As an example assume in my source code, I refer my datasource as colour. So the sample code in my application source code will be something like below.
    E.g.,

    …............
      Hashtable hashtable = new Hashtable();  
 hashtable.put("java.naming.factory.initial","org.wso2.carbon.tomcat.jndi.CarbonJavaURLContextFactory");  
 Context initContext = new InitialContext(hashtable);  
 ds = (DataSource) initContext.lookup("jdbc/colour");  
 Connection conn = ds.getConnection();  
 Statement statement = conn.createStatement();   
        …............

  1. So the datasource I will be creating should be named as colour.
Databases Permission level

User Role Permission
App Owner Can create and configure databases in Development, Testing and Staging
Developer Can create and configure databases in Development and Testing environments
QA/Tester Can create and configure databases in Testing and Staging environments
Dev-Ops Can create and configure databases in Staging and Production environments
  1. A user can login to the system and select the resource section to create a database.
  2. User can create the database as per the above permission. Visibility of Database environment will be as above user role permission. User will be able to select the environment from the drop down. DB User and template can be created inline or separately and select it form the drop down.
  3. Created users and the templates for each environment will be loaded depends on the selected database environment.


                                              Database Creation


    Datasource Permission Level
User Role Permission
App Owner Can create and edit datsources in Development, Testing and Staging
Developer Can create and edit datasources in Development and Testing environments
QA/Tester Can configure created datasources in Testing and Staging environments
Dev-Ops Can configure created datadources in Staging and Production environments
  1. App owner can create a datasource and provide the database url created initially. E.g., I create a database named COL_DB and Datasource as colour.
  2. Then a datasource will be copied in to testing and staging as well along with the provided database URL. E.g., If I provide the database URL of the development stage it will copy the database URL of development stage in testing and staging as well.
    E.g.,
    jdbc:mysql://rss.dev.appfactorypreview.wso2.com:3306/COL_DB

                                Shows the datasources visibility as an APP Owner

  3. If a developer logs in to the system, he or she will see the datasources created in Dveloement and testing stages.
  4. A Developer or a testing user can create another database in testing environment and select the datasource created and the stage as Testing and provide the newly created database URL of the testing stage. E.g., Database created in testing : COL_DB in testing storage server and select the colour as the datasource and the stage as testing and the testing DB URL and update it.

    E.g.,
    jdbc:mysql://rss.test.appfactorypreview.wso2.com:3306/COL_DB
  5. This will enable the different users to easily update their datasource with their desired database location and test the application.
  6. Depends on the stage selected, existing database URL and attached users will be loaded.
  7. If not a developer or a tester can test their application with the datasource created initially which points to the database in the development without editing and configuring it to the different environment.
  8. When promoting the application in to proceeding stage it will refer the database pointed to the promoted environment.
  9. Only Dev Ops have permission to configure the production environment. Therefore DevOps will have to select the stage as Production and provide the database URL accordingly.