Thursday, August 30, 2012

How to save formulated form of current message to text file in WSO2 ESB.

In some scenarios we need to know what is the currently processing form of  the message, in those times we simply use log mediator to see the message and using properties of log mediator .we can customize it to several ranges. The story is totally ok until that, what if we want to write the current message to text file as xml content or what ever format that we define.Then we do not have out of the box tool but we can formulate one using existing toolkit.In this post we are going to learn how to write a current message to text file with pipe [ | ] as the delimiter.
How we are going to do this?
We use a proxy service with in that , In the In Sequence we call data service endpoint that we deployed earlier.In the Out Sequence we formulate the response message from  data service endpoint using XSLT mediator to desired format. Finally write the formulated content in the file via VFS transport.
Step-1 : Start the back end service.
For the convenient we use the data service that described in the Implement MDM pattern using WSO2 ESB and WSO2 DSS–Part: 1 as the back end service. If you want to test the scenario you may download WSO2 DSS (Data Service Server.) and deploy the CaR (Carbon Application Archive) which can be downloaded from here.
Response message of the back end service.

19,Quaker Ridge Rd,06801
2 Jeff Firrelli
1000,Coney Island Ave,11230
3 Gerard Bondur
2962,Dunedin CvGermantown,38138
4 Vanauf George
1500,Vance Ave Memphis,38104
5 King Tom
1577,Lobby Ave,38804
Step-2 : Create the xslt file.


There are two important configurations that should discuss in this xslt file.
This element is the very important ,this ensures that , until formulated content goes to the file keep it as a xml content because esb expect the message always in xml format otherwise it will give error saying  Unexpected character …at…
This is where we get the values of the response message and put the delimiter in-between them and  make the desired text message.
Step-3 : Enable VSF transport and other configuration enabled.
We need to enable VFS transport and PlainTextFormatter to make sure file saving mechanism works fine.
1. Open the axis2.xml file which resides in ESB_HOME/repository/conf directory
2. Go to Transport Outs (Senders)  section.
3. Uncomment line
this will enable the VFS transport.
4. Go to  Message Formatters section.
5. Add below line within <messageFormatters> tag
6.Start the ESB.
Step-4 : Create the Proxy Service.

0 1.0 0
0 1.0 0
Now there are no surprises we know what this proxy service do and how it works.
Final Output.
1|Diane|Murphy|19,Quaker Ridge Rd,068012|Jeff|Firrelli|1000,Coney Island Ave,112303|Gerard|Bondur|2962,Dunedin CvGermantown,381384|Vanauf|George|1500,Vance Ave Memphis,381045|King|Tom|1577,Lobby Ave,38804
All the configuration files related to this post can be download from here.
         [1] -