Sunday, June 25, 2017

How to start to implement SpringBoot application.

There are 4 different ways to start a SpringBoot application.
1. Create a simple maven project and change the necessary changes to the pom file
2. Spring Initializr (online application)
3. Spring Boot CLI
4. STS IDE

I'll briefly explain above 4 ways to get a basic knowledge about mechanisms.

1. Create a simple maven project and change the necessary changes to the pom file
You can create a simple maven project and then,

       a.) Add the below parent tag
<parent>
 <groupId>org.springframework.boot</groupId>    
 <artifactId>spring-boot-starter-parent</artifactId>        
 <version>1.4.2.RELEASE</version>
</parent>

      b.) Add the spring-boot-starter-web dependency
<dependency> 
 <groupId>org.springframework.boot</groupId>    
 <artifactId>spring-boot-starter-web</artifactId> 
</dependency>

      c.) Add the supported java version (1.8)
<properties>   
 <java.version>1.8</java.version>  
</properties>
Then build the project.

2. Spring Initializr (online application)
Go to the http://start.spring.io & Give necessary information & generate the project

3. Spring Boot CLI
Download spring-boot-cli & go to bin and can write Groovy script to generate the project

4. STS IDE
    a. You can download the STS editor by accessing url [1]
    b. Extract the download file and start the STS IDE
Since, I'v been using the Ubuntu operating system, I downloaded the spring-tool-suite-3.8.4.RELEASE-e4.6.3-linux-gtk-x86_64.tar.gz file. Then extracted it. Finally started the STS file located in <extracted_location>/sts-bundle/sts-3.8.4.RELEASE/
   c. File -> New -> Spring Starter Project
   d. Then you can give a 'name' and select the 'Spring Boot Version' and select the necessary available dependency and finish

[1] https://spring.io/tools/sts/all


WSO2 ESB - How to read the payload content using enrich mediator?

You can enrich mediator to read the payload content very easily.

We will assume that ESB receives a payload as below.

<?xml version='1.0' encoding='utf-8'?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soap:Body>
        <person>
            <name>tharanga wijeweer</name>
        </person>
    </soap:Body>
</soap:Envelope>

Now, need to read this payload & set the name of the person to a property.

<enrich>
    <source xmlns:soap="http://www.w3.org/2003/05/soap-envelope" clone="true" xpath="//soap:Envelope/soap:Body/person/name/text()"/>
    <target type="property" property="PersonName"/>
</enrich>

Above enrich mediator reads the name of the person from the payload and set that value to the PersonName property.

Enjoy..!! 

WSO2 ESB - How to set payload content using enrich mediator?

You can use WSO2 enrich mediator replace a new value to a existing payload as below

We will assume that, ESB received a payload as below:
<?xml version='1.0' encoding='utf-8'?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soap:Body>
        <person>
            <name>tharanga</name>
        </person>
    </soap:Body>
</soap:Envelope>

And if you need to set the new name of person,  you can use the enrich mediator as below:

<property name="PersonName" value="wijeweera"/>
<enrich>
   <source type="property" clone="true" property="PersonName"/>
   <target xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xpath="//soap:Envelope/soap:Body/person/name/text()"/>
</enrich>

PersonName value (wijeweera) will replace with the old value. Then payload will be as below.

<?xml version='1.0' encoding='utf-8'?> <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soap:Body> <person> <name>Wijeweera</name> </person> </soap:Body> </soap:Envelope>

Enjoy..!!

WSO2 - How to assign system date time to a property?

You can get the system time very easily as below

<property name="StringVal" expression="get-property('SYSTEM_DATE')"/>

And also you can format the Date & Time according to your requirement as below.

<property name="StringVal" expression="get-property('SYSTEM_DATE', 'yyyyMMddHHmmss')"/>

Year - y
Month - M
Day - d
Hour - H
Minute - m
Second - s

Enjoy..!

Monday, May 8, 2017

How to read query parameter from the WSO2 ESB?

You can read the HTTP query parameter as below.

For this example, I assumed that proxy received the request with the firstName value and lastName value as query parameter.
http://tharanga:8280/services/Proxy4?firstName=tharanga&lastName=wijeweera

Now you can read the query parameter as below
  <property name="param_a" expression="$url:firstName"/>
  <property name="param_b" expression="$url:lastName"/>

Here, firstName and lastName values will assign to the properties.

How to read HTTP headers in WSO2 ESB?

You can read the HTTP headers as below.

For this example, I assumed that proxy received the request with the firstName as a HTTP header. In the ESB we can get the value as below
<property name="firstNameValue" expression="$trp:firstName"/>

Here firstName header value will assign to the firstNamevalue property.

How to get String size in WSO2 ESB?

You can get the String size using Xpath expression. Find the blow steps:

1. Assign the string value to the property.
2. Get the value using Xpath expression.
 fn:string-length(get-property('StringValue'))

Find the below sample proxy.
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="Proxy1"
       startOnLoad="true"
       statistics="disable"
       trace="disable"
       transports="https,http">
   <target>
      <inSequence>
         <property name="stringvalue" value="Colombo"/>
         <property expression="fn:string-length(get-property('stringvalue'))" name="string_length"/>
         <log level="custom">
            <property expression="get-property('string_length')" name="Length"/>
         </log>
      </inSequence>
   </target>
   <description/>
</proxy>

 After executing this proxy, you can see the length value in console:
[2017-05-09 00:08:10,153]  INFO - LogMediator Lenght = 7.0