Thing which seemed very Thingish inside you is quite different when it gets out into the open and has other people looking at it

Friday, August 24, 2012

How to send mails using WSO2 ESB (sending your payload data to e-mail)

Suppose you have a web service which returns some data and you want to send that data into a mail... WSO2 ESB provides an easy mechanism to do this by creating a proxy service.

First get your endpoint (web service endpoint which returns data ).

Step 1 - Enable mail transport in axis2.xml.

Go to your axis2.xml (under WSO2ESB_HOME/repository/conf/axis2/), and uncomment mailto transportSender as shown below.

 <transportSender name="mailto" class="org.apache.axis2.transport.mail.MailTransportSender">
        <parameter name="mail.smtp.host">smtp.gmail.com</parameter>
        <parameter name="mail.smtp.port">587</parameter>
        <parameter name="mail.smtp.starttls.enable">true</parameter>
        <parameter name="mail.smtp.auth">true</parameter>
        <parameter name="mail.smtp.user">synapse.demo.0</parameter>
        <parameter name="mail.smtp.password">mailpassword</parameter>
        <parameter name="mail.smtp.from">synapse.demo.0@gmail.com</parameter>
  </transportSender>

Step 2 Creating the proxy Service.

Go WSO2 ESB, start up the server. Under Main menu -> Axis2 Services -> Add -> Proxy Service. Click on Custom Proxy Service. Give an appropriate Proxy Service Name. (Give the WSDL URL as your requirement)

Step 3 - Define Endpoint

In the define endpoint section click on define inline, click on create -> Address Endpoint and give the address end point of your service which you get data from. I am going to invoke the Data Service which i created in my previous blog. And my Address endpoint will be



Step 4 - Creating the Out Sequence. Go to Out sequence -> Define Inline

In the In Out sequance We need to add a
A Log Mediator - Which logs the incoming message
Three Property Mediators
    1. Subject - Which will be the subject of our mail
    2. MessageType - Message Type of our mail
    3. ContentType - Content Type of our mail
    4. Send mediator - Send the mail to our email address

To add the log mediator, Click on add child -> Core -> Log.  And in the log mediator give the Log level as full.

To add the three properties,  Click on add child -> Core -> Property. And add three properties one by one.
property name="Subject" value="CEP Event" scope="transport" type="STRING"
property name="MessageType" value="text/html" scope="axis2" type="STRING"
property name="ContentType" value="text/html" scope="axis2"  type="STRING"

To add the send mediator Click on add child -> Core -> Send. And Select Endpoint Type -> Define inline -> Address Endpoint -> mailto:amani.soysa@gmail.com

Once you add the three properties and the log mediator your out sequance editor should look like below.



Click save and close and finish creating your proxy Service.


<proxy xmlns="http://ws.apache.org/ns/synapse" name="MyMailProzy" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
   <target>
      <inSequence />
      <outSequence>
         <log level="full" />
         <property name="Subject" value="CEP Event" scope="transport" />
         <property name="MessageType" value="text/html" scope="axis2" type="STRING" />
         <property name="ContentType" value="text/html" scope="axis2" />
         <property name="OUT_ONLY" value="true" scope="default" type="STRING" />
         <send>
            <endpoint>
               <address uri="mailto:amani.soysa@gmail.com" />
            </endpoint>
         </send>
      </outSequence>
      <endpoint>
         <address uri="http://localhost:9765/services/PersonsDataService" />
      </endpoint>
   </target>
   <publishWSDL uri="http://localhost:9765/services/PersonsDataService?wsdl" />
   <description></description>
</proxy>



You can invoke your proxy service by going to try it in the service list, and you should get a mail according to your payload. My next blog post will show how we can receive emails to WSO2 ESB using a standard email client.






Thursday, August 23, 2012

A Song best suited for WSO2 ESB!! :)


Who - Magic Bus 


Songwriters: PETER TOWNSHEND

Every day I get in the queue (Too much, the Magic Bus)
To get on the bus that takes me to you (Too much, the Magic Bus)
I'm so nervous, I just sit and smile (Too much, the Magic Bus)
Your house is only another mile (Too much, the Magic Bus)
Thank you, driver, for getting me here (Too much, the Magic Bus)
You'll be an inspector, have no fear (Too much, the Magic Bus)
I don't want to cause no fuss (Too much, the Magic Bus)
But can I buy your Magic Bus? (Too much, the Magic Bus)
Nooooooooo!

I don't care how much I pay (Too much, the Magic Bus)
I wanna drive my bus to my baby each day (Too much, the Magic Bus)
*[Magic Bus, Magic Bus, Magic Bus
Magic Bus, Magic Bus, Magic Bus
Give me a hundred (Magic Bus)
I won't take under (Magic Bus)
Goes like thunder (Magic Bus)
It's a bus-age wonder (Magic Bus)

Magic Bus, Magic Bus, Magic Bus, Magic Bus
I want it, I want it, I want it...(You can't have it!)
Think how much you'll save...(You can't have it!)]
I want it, I want it, I want it, I want it ... (You can't have it!)

Thruppence and sixpence every day
Just to drive to my baby
Thruppence and sixpence each day
'Cause I drive my baby every way

Magic Bus, Magic Bus, Magic Bus, Magic Bus, Magic Bus...
I want the Magic Bus, I want the Magic Bus, I want the Magic Bus...

I said, now I've got my Magic Bus (Too much, the Magic Bus)
I said, now I've got my Magic Bus (Too much, the Magic Bus)
I drive my baby every way (Too much, the Magic Bus)
Each time I go a different way (Too much, the Magic Bus)
I want it, i want it, I want it, I want it ...

Every day you'll see the dust (Too much, the Magic Bus)
As I drive my baby in my Magic Bus (Too much, the Magic Bus)

Wednesday, August 22, 2012

How to use WSO2 Payload mediator - Calling data service insertion using payload mediator

In my previous blog I showed how to use an iterate mediator to iterate through a soap message. In this post I am going to explain how WSO2 ESB payload mediator works.

Lets say you have a service which provides set of data and you want to call a data service insert operation. This message is generated from a data service, which access a database table and get set of records from the database. Please refer "How to create a MYSQL data service using WSO2 data services Server" If you want to create the data service and generate the below Request,

<Keys xmlns="http://ws.wso2.org/dataservice">
<Key>
    <P_Id>1</P_Id>
    <LastName>Soysa</LastName>
    <FirstName>Amani</FirstName>
    <Address>361 Kotte Road Nugegoda</Address>
    <City>Colombo</City>
 </Key>
 <Key>
    <P_Id>2</P_Id>
    <LastName>Bishop</LastName>
    <FirstName>Peter</FirstName>
    <Address>300 Technology BuildingHouston</Address>
    <City>London</City>
 </Key>
 <Key>
    <P_Id>3</P_Id>
    <LastName>Clark</LastName>
    <FirstName>James</FirstName>
    <Address>Southampton</Address>
    <City>London</City>
 </Key>
 <Key>
    <P_Id>4</P_Id>
    <LastName>Carol</LastName>
    <FirstName>Dilan</FirstName>
    <Address>A221 LSRC Box 90328 </Address>
    <City>Durham</City>
 </Key>
</Keys>

Lets see how we can use this data set which will come to WSO2 ESB as a soap request and we need to extract soap payload data and send them to a data service. First we need to use the iterate mediator  which will iterate the soap request if you have more than one data set. And we need to create the data service soap request using the payload mediator.

   <payloadFactory>
  <format>
     <p:InsertPerson xmlns:p="http://ws.wso2.org/dataservice">
        <p:P_Id>?</p:P_Id>
        <p:LastName>?</p:LastName>
        <p:FirstName>?</p:FirstName>
        <p:Address>?</p:Address>
        <p:City>?</p:City>
     </p:InsertPerson>
  </format>
  <args>
     <arg expression="//P_Id/text()" />
     <arg expression="//LastName/text()" />
     <arg expression="//FirstName/text()" />
     <arg expression="//Address/text()" />
     <arg expression="//City/text()" />
  </args>
</payloadFactory>

Once we create the payload mediator then we can create a send mediator to insert data to data service

  <send>
     <endpoint>
        <address uri="http://localhost:9765/services/MyFirstDSS/" />
     </endpoint>
  </send>

When you add everything together your proxy service will look like shown below.

<proxy xmlns="http://ws.apache.org/ns/synapse" name="AssetProxyService" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
  <target>
     <inSequence>
         <iterate xmlns:m="http://ws.wso2.org/dataservice" id="iter1" expression="//m:Keys/m:Key">
           <target>
              <sequence>
                 <payloadFactory>
                    <format>
                       <p:InsertPerson xmlns:p="http://ws.wso2.org/dataservice">
                          <p:P_Id>?</p:P_Id>
                          <p:LastName>?</p:LastName>
                          <p:FirstName>?</p:FirstName>
                          <p:Address>?</p:Address>
                          <p:City>?</p:City>
                       </p:InsertPerson>
                    </format>
                    <args>
                       <arg expression="//P_Id/text()" />
                       <arg expression="//LastName/text()" />
                       <arg expression="//FirstName/text()" />
                       <arg expression="//Address/text()" />
                       <arg expression="//City/text()" />
                    </args>
                 </payloadFactory>
                 <send>
                    <endpoint>
                       <address uri="http://localhost:9765/services/MyFirstDSS/" />
                    </endpoint>
                 </send>
              </sequence>
           </target>
        </iterate>
     </inSequence>
  </target>
  <description />
</proxy>

Wednesday, August 15, 2012

How to Use Iterator Mediator to Iterate through SOAP message Using WSO2 ESB

Lets say you have a soap response coming from a your service and you need to go through that soap message and get/transform that data and send to another service ... and you have no way of doing it??? WSO2 ESB provide a solution to do this in few easy steps.
Lets look at the following soap message. This message is generated from a data service, which access a database table and get set of records from the database. Please refer "How to create a MYSQL data service using WSO2 data services Server" If you want to create the data service and generate the below response.

SOAP response.



<Keys xmlns="http://ws.wso2.org/dataservice">


  <Key>
     <P_Id>1</P_Id>
     <LastName>Soysa</LastName>
     <FirstName>Amani</FirstName>
     <Address>361 Kotte Road Nugegoda</Address>
     <City>Colombo</City>
  </Key>
  <Key>
     <P_Id>2</P_Id>
     <LastName>Bishop</LastName>
     <FirstName>Peter</FirstName>
     <Address>300 Technology BuildingHouston</Address>
     <City>London</City>
  </Key>
  <Key>
     <P_Id>3</P_Id>
     <LastName>Clark</LastName>
     <FirstName>James</FirstName>
     <Address>Southampton</Address>
     <City>London</City>
  </Key>
  <Key>
     <P_Id>4</P_Id>
     <LastName>Carol</LastName>
     <FirstName>Dilan</FirstName>
     <Address>A221 LSRC Box 90328 </Address>
     <City>Durham</City>
  </Key>
</Keys>

Lets see how we can create a proxy service to get these data and we will log the retrieve data using a log mediator.
Before we start you need to have wso2ESB server and start it up. And login to ESB management console.

Step 1 -  Creating the Custom Proxy - Basics Settings

First give proxy service Name and the targetted WSDL file as shown below. (For the WSDL file you can give WSDL file of the Data service which can be access through the data service-> Service Dashboard)



Step 2 - Defining the Endpoint

Once you are done with basic settings then you need to click on next and Define the endpoint. Click on define endpoint inline  and give the address end point of the created data service.

Step 3 - Defining the Out Sequence.

You need to put the iterator mediator inside the out sequence in order to iterate through the incoming soap message. Therefore, go to Define Out Sequence -> Define Inline -> Add 
Click on Add child ->Advance -> Iterate. 


When you scroll down you will get a wizard where you need to enter some information. Give 
Iterate ID - iter1
Iterate Expression*  //m:Keys/m:Key { Dont forget to add the name space to the iterate expression}  
Namespace prefix m 
Namespace url  http://ws.wso2.org/dataservice    

  
Lets add some log mediators to print the data as shown below (to add log mediator got to add child under target click on core and log).

Once you have added the log mediator and a send mediator inside the target. Once you have done it your design view editor should look like below.



Click on save and close to go back to the proxy service editor and click on finish to finish creating the proxy service.

Once you create the proxy service, your synapse configuration should look like below.


<proxy xmlns="http://ws.apache.org/ns/synapse" name="IteratorProxyService" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">

  <target>
     <outSequence>
        <iterate xmlns:m="http://ws.wso2.org/dataservice" id="iter1" expression="//m:Keys/m:Key">
           <target>
              <sequence>
                 <log level="custom">
                    <property name="P_Id" expression="//m:P_Id/text()" />
                    <property name="LastName" expression="//m:LastName/text()" />
                    <property name="FirstName" expression="//m:FirstName/text()" />
                    <property name="Address" expression="//m:Address/text()" />
                    <property name="City" expression="//m:City/text()" />
                 </log>
                 <send />
              </sequence>
           </target>
        </iterate>
     </outSequence>
     <endpoint>
        <address uri="http://localhost:9765/services/MyFirstDSS/" />
     </endpoint>
  </target>
  <description></description>
</proxy>

If you go to the try it feature inside the service dashboard of your proxy service. You can invoke GetPeople operation to test your proxy service.
Once you invoke that following log will be logged in to your console window

[2012-08-15 14:37:59,078]  INFO - LogMediator P_Id = 1, LastName = Soysa, FirstName = Amani, Address = 361 Kotte Road Nugegoda, City = Colombo
[2012-08-15 14:37:59,084]  INFO - LogMediator P_Id = 2, LastName = Bishop, FirstName = Peter, Address = 300 Technology BuildingHouston, City = London
[2012-08-15 14:37:59,089]  INFO - LogMediator P_Id = 3, LastName = Clark, FirstName = James, Address = Southampton, City = London
[2012-08-15 14:37:59,092]  INFO - LogMediator P_Id = 4, LastName = Carol, FirstName = Dilan, Address = A221 LSRC Box 90328 , City = Durham





Friday, August 3, 2012

Creating Music Notation using LiliPond

Lilipond is a music engraving program, which produce high quality music notation and send them into PDF format. One of the most interesting thing you can do using lilipond is, you can use lilipond for your music programming. If you follow their syntax properly you can create music applications very easily.


Lets get a kick start on lilipond.
Before you begin you need to download lilipond depending on your operating system.

For this demostration I am going to show how you can create the popular song "Twinkle Twinkle Little Star" Using lili pond.

There are three main things we need to consider when creating a song.in lili Pond

1. How to create the melody ? (the treble clef notes)
2. How are we going to accompany them? (base notes)
3. Are we going to produce a midi file to our notation.

Creating a melody

In lilipond most of the pitches can be given relatively to a given octave. for example

\relative c {
  \clef bass
  c d e f
  g a b c
  d e f g
}
 

Above notes are relative to the middle octave. And if you want to create octaves lower o higher you can use " ,  " (comma)  or " ' ". This is called octave changing mark in lilipond. And also default time signature is 4/4 and default clef is treble clef. Sharp {#} are defined as ("is") and flats (b) are defined as ("es"). For example if you want to create a# then the notation is like "ais". Ok those are the very basic principles of lilipond but if you want to create more complicate notations please refer the use guide.

Lets start our song. Here we are not using relatives we are using standard notation. So I am going to create a lead sheet which only has the treble clef notes and the chords are indicated on top of each bar.

\score {
{
<<
\chords  {f1 c1 g1 d1:m g1 d1:m f1 c1  }
\new Staff { \time 4/4c'4 c'4 g'4 g'4 a'4 a'4 g'2 f'4 f'4 e'4 e'4 d'4 d'4 c'2 f'4 f'4 e'4 e'4 d'4 d'4 c'2 f'4 f'4 e'4 e'4 d'4 d'4 c'2 }
>>
}
 \layout { }
\midi { }
}

Score represents its a lead sheet. And to represent chords  you need the notation "\chords" here you can define what type of chords you want and the duration on that chord.
   f1 - means fmajor and it will last  4 beats (breave).
   d1:m - means its DMinor it will also last 4 beats.
Like wise you can define your codes

By using new Staff you can give music notations such as crochests, minims rest etc depending on the cleff. If you dont have a clef then the default is treble. you can also define time signature, And the duration of the notes are given by numbers. C4 - one beat C2 - two beats , C1- four beats etc.

Lets look at our twinkle twinkle song notation

Creating MIDI

 
If you want to create a midi file as well you need to put /midi() then it will create a midi file of the song you created.

Creating Piano sheets

Creating piano sheets can also be done similarly. Only difference is you need have two staves other than one.

Example ..

\relative c'' {
  \new PianoStaff <<
    \new Staff { \time 4/4 c4 e8 g8 g4, e4 e}
    \new Staff { \clef bass c,2 e4 g e2 b'4 g}
  >>
}

Creating Fret Sheets.

If you want to create guitar tabs for notation for your songs there is an option to create fretsheets. In order to do that you need to include FretBoards  liberary and give your notation as shown below.

Example...

\include "predefined-guitar-fretboards.ly"
<<
\context FretBoards {
  \chordmode {
    c1:m e
  }
}
\relative c'' {
    \new Staff { \time 4/4 c4 e g g, e2 e} 
}
>>







 
 
 
 
     

How to create service specific logs for WSO2 ESB


In this post I am going to talk how you can create differant log files depending on your proxy service using WSO2 ESB. Lets say you have a proxy service deployed in ESB and you need to filter out the logs generated by that given proxy service. You can  do this in three easy steps using wso2 logging-mgt feature.

Step 1 - Creating a proxy service with LogMediator


In order to try this out you need to create a proxy service which uses log-mediator so we can easily generate logs comming from that proxy service.

So this is my proxy service


<proxy xmlns="http://ws.apache.org/ns/synapse" name="MyProxyService" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">

  <target>
     <inSequence>
        <log level="full">
           <property name="MESSAGE" value="****************Inside in sequance*********************" />
        </log>
     </inSequence>
     <outSequence>
        <log level="full">
           <property name="Message2" value="************Inside out sequance ************************" />
        </log>
        <send />
     </outSequence>
     <endpoint>
     </endpoint>
  </target>
</proxy>

Step2 - Adding Service Level log appender to send logs to differant log file location


WSo2 logging-mgt feature use standard apache log4j framework therefore, you need to change log4j properties in order to create appenders. We will create a new appender call MY_PROXY_APPENDER


log4j.additivity.MY_PROXY_APPENDER=false
log4j.appender.MY_PROXY_APPENDER=org.apache.log4j.DailyRollingFileAppender
log4j.appender.MY_PROXY_APPENDER.File=${carbon.home}/repository/logs/${instance.log}/wso2-esb-my-proxy${instance.log}.log
log4j.appender.MY_PROXY_APPENDER.Append=true
log4j.appender.MY_PROXY_APPENDER.layout=org.apache.log4j.PatternLayout
log4j.appender.MY_PROXY_APPENDER.layout.ConversionPattern=%d{HH:mm:ss,SSS} [%X{ip}-%X{host}] [%t] %5p %c{1} %m%


Here we are defining what kind of an appender, we will use DailyRollingFileAppender you can change the appender type depending on your need such as DBAppender,SyslogAppender etc And also you need to give appender properties like file location, which patternlayout you need to use and the log pattern you need. ALl these are configurable depending on your need.

Step 3 - Adding your logger in the root logger.


You need to aknowlege the newly created Appender in log4j.logger to map MyProxyServic to the SERVICE_LOGGER. The logger will be under Service_Logger so you need to specify your appender as shown below.

 log4j.category.SERVICE_LOGGER.MyProxyService =INFO, PROXY_APPENDER


Thats it!! you can go to {CARBON_HOME}/repository/logs to view your newly created proxy service specific log file!!!!

Tuesday, July 31, 2012

Enabling VFS transport marks certain services as 'faulty'. How to resolve this?


When enabling the VFS transport in the WSO2 admin console there are a number of axis2 services which are marked as being faulty. None of these services (the default Echo service is one of these) have the VFS transport configured explicity in their transports list.
The message shown in the admin console for each of these faulty services is 'Unable to configure the service echo for the VFS transport: Service doesn't have configuration information for transport vfs. This service is being marked as faulty and will not be available over the VFS transport.'

Why it happens? and How to Resolve it?
   
This is because when you enable VFS transport that transport gets enabled to other services as well (including axis2 services) But they don't have necessarry properties needed for VFS to be properly enabled. So when you enable VFS ESB default axis2 services like echo/version services will get faulty. There are two ways to resolve this issue. Since WSO2 ESB is not meant to deploy Application service and it should be used as routing/hub purposes you can undeploy those services. However, if you still want to keep those axis2 services you can go to services.xml and add http and https transports as shown below.

<service name="echo">
   <schema elementFormDefaultQualified="false"/>
   <description>
       This service echos the input provided to it.
   </description>
     <transports>
      <transport>https</transport>
      <transport>http</transport>
   </transports>
   <parameter name="ServiceClass" locked="true">org.wso2.carbon.core.services.echo.Echo</parameter>
</service>

Once you added them you need to redeploy the services.

Monday, July 9, 2012

How To Use Validate Mediator to Validate the SOAP request using WSO2 ESB

Lets say you have a service with the following soap request and you need to validate the input message,  through a proxy service, for those kind of instances you can use WSO2 ESB Validate Mediator.

For this demonstration I am going to create a proxy service which is deployed and hosted under my tenant in StratosLive you can use the same service for sample purposes. This is a service which takes an input string and send a response along with the given input String.

My End point is
http://appserver.stratoslive.wso2.com/services/t/amani123.com/HelloService/              
The WSDL for my Service
http://appserver.stratoslive.wso2.com/services/t/amani123.com/HelloService?wsdl

First Lets Create a proxy service which access this service

 <proxy name="MyValidatorProxy"
          transports="https http"
          startOnLoad="true"
          trace="disable">
      <description/>
      <target>
         <inSequence>
            <log level="full">
               <property name="Message" value="Inside Insequance"/>
            </log>
            <send>
               <endpoint>
                  <address uri="http://appserver.stratoslive.wso2.com/services/t/amani123.com/HelloService/"/>
               </endpoint>
            </send>
         </inSequence>
         <outSequence>
            <send/>
         </outSequence>
      </target>
      <publishWSDL uri="http://appserver.stratoslive.wso2.com/services/t/amani123.com/HelloService?wsdl"/>
   </proxy>

Go to ESB Management Console and copy paste the following code in Source View (Under Main -> Service Bus -> Source View) and click on update.

This will create a proxy service which will create the service I hosted in my tenant.

You can invoke this service by going into Services List and click on try it. Which is a client we use to invoke the service.

Now When you click on the try-it, you will get redirected to a page which has a request and a response.
So for a given request like this

<body>
   <p:greet xmlns:p="http://www.wso2.org/types">
      <!--0 to 1 occurrence-->
      <name>Amani</name>
   </p:greet>
</body>

You should get a response like this.

<ns:greetResponse xmlns:ns="http://www.wso2.org/types">
   <return>Hello World, Amani !!!</return>
</ns:greetResponse>

So lets see how we can validate the request. Lets say you want to make sure the value of the name element is not empty and you want to enforce that for every service innovation there must be a value for the Name element. Then you need to use the ESB validate Mediator.

In order to create a proxy with a validate mediator we need to create a XSD schema. I have created a schema to validate the name element of the request.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://www.wso2.org/types"
attributeFormDefault="qualified" elementFormDefault="unqualified"
targetNamespace="http://www.wso2.org/types">
<xs:element name="greet">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="1" name="name" >
 <xs:simpleType>
        <xs:restriction base="xs:string">
            <xs:minLength value="1" />
        </xs:restriction>
    </xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

Here the main thing you need to focus is this part

<xs:element minOccurs="1" name="name" >
 <xs:simpleType>
        <xs:restriction base="xs:string">
            <xs:minLength value="1" />
        </xs:restriction>
    </xs:simpleType&gt;
</xs:element>

We are using the restriction element which says to restrict the type as String and the input should be a non-zero value (basically this means element cannot be empty). Now we need to upload this in the registry.

Uploading the validation schema in the registry.

Go to Browse in the Registry menu. And there click on config and under that click on Add New Resource. And upload the file as xsd file.

Add Validator Mediator

<validate>
        <schema key="conf:/HelloSchema.xsd"/>
               <on-fail>
                  <makefault version="soap11">
                     <code xmlns:tns="http://www.w3.org/2003/05/soap-envelope" value="tns:Receiver"/>
                     <reason value="Invalid Request!!!"/>
                     <role/>
                  </makefault>
                  <log level="full" />
                  <property name="RESPONSE" value="true"/>
                  <header name="To" action="remove"/>
                  <send/>
                  <drop/>
               </on-fail>
            </validate>

In here we add the Schema from the registry, If the validation fails it will send a message saying "Invalid Request" and we will terminate the flow using the drop mediator.

Now we will ad this validate mediator in the in sequence of our proxy service.

<proxy xmlns="http://ws.apache.org/ns/synapse" name="MyValidatorProxy" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
   <target>
      <inSequence>
         <log level="full">
            <property name="Message" value="Inside Insequance"/>
         </log>
         <validate>
            <schema key="conf:/HelloSchema.xsd"/>
            <on-fail>
               <makefault version="soap11">
                  <code xmlns:tns="http://www.w3.org/2003/05/soap-envelope" value="tns:Receiver"/>
                  <reason value="Invalid Request!!!"/>
                  <role/>
               </makefault>
               <log level="full"/>
               <property name="RESPONSE" value="true"/>
               <header name="To" action="remove"/>
               <send/>
               <drop/>
            </on-fail>
         </validate>
         <send>
            <endpoint>
               <address uri="http://appserver.stratoslive.wso2.com/services/t/amani123.com/HelloService/"/>
            </endpoint>
         </send>
      </inSequence>
      <outSequence>
         <send/>
      </outSequence>
   </target>
   <publishWSDL uri="http://appserver.stratoslive.wso2.com/services/t/amani123.com/HelloService?wsdl"/>
   <description></description>
</proxy>
                              
Now if you invoke this using try it without a content in the name element you will get the message as Invalid Request.

Request
<body>
   <p:greet xmlns:p="http://www.wso2.org/types">
      <!--0 to 1 occurrence-->
    <name></name>
   </p:greet>
</body>

Response
<soapenv:Fault xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <faultcode>tns:Receiver</faultcode>
   <faultstring>Invalid Request!!!</faultstring>
</soapenv:Fault>


Saturday, June 30, 2012

How Logging works with Log4j


In my last blog I explained the importance of logging in your enterprise application. In this post I am going to explain how we can used logging using Log4J logging framework.
 As always using a framework makes your life much easier and efficient. And main use of using a framework is it takes care of the underline platform. So when it comes to log4j which is an open source project created by Apache foundation and currently there are three logging frameworks
  1.  Log4j for java 
  2.   log4cxx for C++
  3.   log4net for the Microsoft .NET framework.

So lets look at how log4j works...

 Log4j takes care of logging using three main components.  1) Loggers, 2) Appenders, 3) Layouts
  In simple terms if you want to know what these three main components does is mainly logger logs to an appender in a particular layout (pattern/style). But lets look at these three components in details

Loggers 

If you look it in the simplest way Loggers are actually logical class of file names.  These names are known to  your java application. For example if you have a class call HelloWorld.
The  standard logger should be 

Logger logger = Logger.getLogger("HelloWorld.class");

Logger should be defined class level. So one of the main advantage of using a framework for logging is you can control what you log in a hierarchical manner. If you put system.printline instead of using a logging platform you cannot actually disable some printlines while others are enabled. So here  using a logger you can actually define your log lines in an hierarchical order.

Loggers have 5 different hierarchical categories. I’l list them down by their order
 



 
Fatal  -  “The FATAL level designates very severe error events that will presumably lead the application to abort.” Fatal is the highest log level used to indicate that application is in a severe stage and application will get terminated due to this condition.
                            log.fatal(“Program termination”);

Error – “The ERROR level designates error events that might still allow the application to continue running.” Errors are not soo serious as Fatal  and program can still run with this condition however, it implies that program is going through an unexpected behavior in the execution flow. For example when the application is suppose to read a file and if that file not found you can log the exception using log.error(e.getMessage) to tell the user that this file is not there in the system.

Warn – warnings are used to tell the user that there is a chance of having a harmful situation due to a certain condition.  “The WARN level designates potentially harmful situations.”

Info – “ The INFO level designates informational messages that highlight the progress of the application at coarse-grained level.” Info is the most commonly used logger method. It is used to highlight the process of the event flow in an application.

Debug -   “The DEBUG Level designates fine-grained informational events that are most useful to debug an application.” This is used for debugging purposes if you need to debug your application you can enable debug level logs and see the execution flow.

Trace – “The TRACE Level designates finer-grained informational events than the DEBUG”  Gives more detail information than debug level. The lowest logger level. This is enable mainly to see finer grained information regarding the application.

Appenders

Another important feature of a logging API is to send logs to different locations. Depending on the user requirements logs should be sent to the console, remote monitor, o file systems. This is achieved by the appenders. Appender is responsible for the log destination.  There are couple of pre-define appenders in log4j.



  1. ConsoleAppender - Sends log events to the System.out or System.err using a layout specified by the user. The default target is System.out
  2. FileAppender - Sends log events to a file. (DailyRollingFileAppender, RollingFileAppender)
  3. SoketAppender - Sends events to a remote log server, usually a SoketNode
  4. JMSAppender - Sends events to a JMS topic.
  5. NTEventLogAppender - Sends  events to the NT event log system.
  6. SyslogAppender - Sends events to a remote syslog daemon.

    And many more .. You can also define your own appender so that it will send log events to differant/multiple destnations. You can easily do it by extending AppenderSkeleton class. I will explain how that can be done in my next blogs.

    Layouts

    Layouts allow you to format your log messages before it is sent to the log destination. It actually stype your log message which can be very useful for filtering purposes and better monitoring approaches. By adding a pattern to your layout, you can exclude/include log event properties such as date time, log level, logger, message etc. You can also define your of layout by extending the log4j Layout class . Howeverm there are couple of predefine layouts in log4j such as PatternLayout, SimpleLayout, DateLayout, HTMLLayout, and XMLLayout.
    Defining the layout in log4j.
     
    log4j.appender.CARBON_LOGFILE.layout=org.wso2.carbon.utils.logging.TenantAwarePatternLayout
    log4j.appender.CARBON_LOGFILE.layout.ConversionPattern=TID: [%T] [%S] [%d] %P%5p {%c} - %x %m {%c}%n
    log4j.appender.CARBON_LOGFILE.layout.TenantPattern=%U%@%D [%T] [%S]

    Sample patternlayout using log4j

    TID: [-1234] [Application Server] [2012-06-30 20:26:17,156]  INFO {org.wso2.carbon.coordination.core.services.impl.ZKCoordinationService} -  Coordination service disabled. {org.wso2.carbon.coordination.core.services.impl.ZKCoordinationService}





    Tuesday, June 26, 2012

    Monitor your application with system logs !!!


    Tired of finding an issue with a current running application??? Your application started giving unexpected behavior and can’t figure out why? .. Cannot monitor your application properly? THEN LOGGING IS YOUR SOLUTION!!!!
     
    Why Logging ???

    Logging is a way of storing information about events happening during a program execution. Logging can be very useful when it comes to identify the event flow/ program execution or unexpected behavior of an execution such as errors/exceptions (error tracking) and it can be also useful when it comes to monitor performance.  Almost every developer use Logging!!! Even if they don’t use a special framework to do logging. For instance even if you are not using a particular logging frame work you most probably be using  system.printline in order to track  useful information and print them on the console so that you can monitor your application’s behavior and it’s current activities.

    Importance of using a Logging Framework ?

    However, using a logging frame work is a major plus point. Mostly because logs  are the very lifeline of your production application and it should not be taken lightly or as an afterthought. For example let’s say you don’t use logging at all and your application is out of development environment and your application is giving an unexpected behavior, it will be like finding a needle in a haystack to figure out what’s gone wrong in your application if you don’t use proper logging using a proper logging framework. 
      
    Of cause using a logging framework is a plus point when it comes to enterprise applications. Because by using a logging frame work you can easily define log levels and filter logs accordingly. Further, when it comes to distributed applications (such as web/remote application) logging is a very crucial task because system administrators always needs to keep eye of what happening and  always need to see the logs for a given period of time. So filtering logs and viewing logs is also very important. This is why Logging has become the most frequent implementation of the Monitoring attribute and also in most cases it is the only implementation of monitoring (Mainly because it is the most easiest thing in the world for a busy developer to just put lnfor.log() in the code. However, apart from the ease of use logging frameworks also allow you to send logs to many different parties such as file systems, consoles, and databases. Which makes it easy for a system admin to filter logs from timestamp, log levels, and log messages.

    Drawbacks of Logging

    Even though, logging is a very important thing when it comes to developing application there is such thing  as too much logging and it’s important to know how to log efficiently.  Before we look into that lets see the common draw backs of bad logging.
    Performance – There can be performance hit in your application if you don’t put the logs in the right way. One thing is if you have too many logs at unwanted places it can be a major performance hit and also it will reduce the speed of your application. And also pollute and increase the size of your code.

    Scalability issues – When you log unwanted information or not have proper log levels in your application you might end up having huge log files. And it will increase the size of your log files. And it can eat up your software and hard ware resources.

    Security – Logging can be security vulnerability because logs can carry security information such as passwords of user accounts and other sensitive information which may expose a system vulnerabilities.
    These are very important points because our ultimate goal is to have efficient application which can be monitored efficiently at all time.

     
    Best Practices of Logging
    Therefore I would like to list down some of the best practices when it comes for logging however, I would like to discuss them in details in my next blog post.


    1. Use an appropriate logging framework depending on your need - The proper selection of logging frame work will lead you to store and filter your information in an orderly fashion)
    2. Proper use of Log levels - There are different levels of log levels choosing the right level at the right time can increase the performance and the scalability of your application.
    3. Do Not Log unwanted information – Always make sure you know what you are logging and also make sure logging will not give side effects to your application. (Make sure you avoid null pointer exception while logging ;) ) And ALWAYS be concise and descriptive when you log 
    4.  Make sure you use your own pattern. Logging frameworks let you design your own pattern layout which helps you filter your logs in an efficient manner. Make sure you use features of logging frame work when its needed.