Pages

Tuesday 17 June 2014

Read Path Param in wso2 ESB 4.8.1

Read Path Param in wso2 ESB 4.8.1

We can read 'path' parameter in WSO2 ESB. To do so , we can use "uri-template" attribute  of  <resource> tag.

Suppose  we need  to read parameter from an API like "example.com/api/getStatus/{some category}/{some id}.json", So  we should  configure API in ESB as follows :

STEP 1:

Give the first static part of the URL as the 'context' of <api> tag

Example 

<api xmlns="http://ws.apache.org/ns/synapse" name="sample-API" context="/api/getStatus">

STEP 2:

Give the remains part of the URL as the  'uri-template' <resource> tag but dynamic value may write  inside {} brackets.

Example

 <resource methods="GET" uri-template="/{category}/{id}.json">

 How to read Parameter

We can read url path param by using the 'expression' of <property> tag. In <property> tag ,the variable name should same as  mentioned inside {} brackets in <resource> tag.

 Example

<property name="uri.var.category" expression="getproperty('uri.var.category')"></property>
  <property name="uri.var.id" expression="get-property('uri.var.id')"></property>

Wednesday 21 May 2014

Set Json payload in WSO2 ESB 4.8 and 4.8.1


Here i would like to share that how to set dynamic json payload in WSO2 ESB 4.8 and 4.8.1

We can use  'json' media-type property of 'payloadFactory' mediator to do that.

SAMPLE API 

In this API  accepts post request including user and pwd body parameters

 <api xmlns="http://ws.apache.org/ns/synapse" name="test-API" context="/api/test.json">
   <resource methods="POST">
      <inSequence>
         <payloadFactory media-type="json">
            <format>
{"username":"$1","pwd":"$2"}
</format>
            <args>
               <arg evaluator="json" expression="$.user"></arg>
               <arg evaluator="json" expression="$.pwd"></arg>            
            </args>
         </payloadFactory>
         <log level="full"></log>
         <send>
            <endpoint>
               <http method="POST" uri-template="http://example.com/test/enpoint"></http>
            </endpoint>
         </send>
      </inSequence>
   </resource>
</api>

Monday 17 March 2014

Insert CSV data to Database using Kettle


Here i would like to share that how to insert csv file's data to database by the help of Kettle .Refer

Configure the environment

1 - Download kettle .click  here

2 - Start Spoon.bat

3 - Select  File ->New->Transformation

4 - Select Input option from left pane

5 - Chose CSV File Input

6 - Double click on CSV File Input and configure as follows


7 - Select Output option from left pane

8 Chose Table Output

9 - Double click on   Table Outputand configure as follows

* Add a Connection by clicking 'New'





        *Browse Table name
*Browse database feild option and Enter feild mapping

10 - Click OK to complete configuration

Now you can save and start the transformation


Friday 7 March 2014

convert csv file to java object

Here i would like to share that how to convert a csv file to java object.

Records in  sample  csv


NPI
Fisrt Name
Middle Name
Last Name
Designation
1000
Ajmal
A
Abdullah
Software Engineer
2000
Jouhar
O
Oravingal
Software Engineer
3000
Eldho
J
John
Software Engineer

Professional.java


/**
 * 
 * @author Jamsheer T
 * 
 */

public class Professional {

String npi;
String name;
String mname;
String lname;
String desig;

public String getNpi() {
return npi;
}

public void setNpi(String npi) {
this.npi = npi;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getMname() {
return mname;
}

public void setMname(String mname) {
this.mname = mname;
}

public String getLname() {
return lname;
}

public void setLname(String lname) {
this.lname = lname;
}

public String getDesig() {
return desig;
}

public void setDesig(String desig) {
this.desig = desig;
}

}

CSVtoJava.java


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 *
 * @author Jamsheer T
 *
 */
public class CSVtoJava {

public void convertCsvToJava() {
Professional profObject = new Professional();
String csvFileToRead = "C:\\Users\\Jamsheer T\\Desktop\\csv\\prof.csv";
BufferedReader br = null;
String line = "";
String splitBy = ",";
boolean initial =true;

try {

br = new BufferedReader(new FileReader(csvFileToRead));
while ((line = br.readLine()) != null) {



// split on comma(',')
String[] details = line.split(splitBy);

// create car object to store values

// add values from csv to car object
profObject.setNpi(details[0]);
profObject.setName(details[1]);
profObject.setMname(details[2]);
profObject.setLname(details[3]);
profObject.setDesig(details[4]);

if(initial==true){
initial=false;
continue;
}
System.out.println("NPI :" + profObject.getNpi());
System.out.println("NAME :" + profObject.getName());
System.out.println("Designation :" + profObject.getDesig());
System.out.println("----------------------------------");

}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}

}


Main.java


/**
 * 
 * @author Jamsheer T
 * 
 */

public class Main {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

CSVtoJava csvToJavaObject = new CSVtoJava();
csvToJavaObject.convertCsvToJava();
}

}

OUTPUT

NPI :1000
NAME :Ajmal
Designation :Software Engineer
----------------------------------
NPI :2000
NAME :Jouhar
Designation :Software Engineer

----------------------------------
NPI :3000
NAME :Eldho
Designation :Software Engineer

----------------------------------






Convert CSV File to JSON Object in JAVA


Here i would like to share that how to convert a csv file contents to JSON object  in JAVA

Included Libraries

<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>1.3.4</version>
</dependency>

Sample Code


package com.test.csvtojson;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

/**
 *
 * @author Jamsheer T +91 9846716175
 */
public class CSVtoJson {

public static void main(String[] args) { int i; String line = ""; String fileToParse = "C:\\Users\\Jamsheer T\\Desktop\\csv\\ex.csv"; BufferedReader fileReader = null; boolean initial = true; ArrayList<String> header = new ArrayList<String>(); JSONObject record = new JSONObject(); try { fileReader = new BufferedReader(new FileReader(fileToParse)); while ((line = fileReader.readLine()) != null) { i = 0; String[] tokens = line.split(","); if (initial) { for (String token : tokens) { if (token.startsWith("\"")) { token = token.substring(1); } if (token.endsWith("\"")) { token = token.substring(0, token.length() - 1); } header.add(token); } initial = false; continue; } // Get all tokens available in line for (String token : tokens) { if (token.startsWith("\"")) { token = token.substring(1); } if (token.endsWith("\"")) { token = token.substring(0, token.length() - 1); } record.put(header.get(i).toString(), token); i++; if (i == header.size()) break; } System.out.println(record); } } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } } }

Thursday 27 February 2014

Json Response Enable in WSO2 DSS 3.1.1

Do the following activity  to enable json response in WSO2 DSS Server

1. Change the axis2.xml 

Add the following parameter to the axis2.xml file located at  $CARBON_HOME/repository/conf/axis2

<parameter name="httpContentNegotiation">true</parameter>


2. Change  axis2_client.xml

Set 'httpContentNegotiation' parameter as true in   axis2_client.xml file located at  $CARBON_HOME/repository/conf/axis2

<parameter name="httpContentNegotiation">true</parameter>


3.Restart the server

Now  the DSS server has enabled the josn response

Note :- you should add following header to the request

"Accept:application/json"

Tuesday 25 February 2014

AMQP support for WSO2 ESB 4.8

Introduction

WSO2 ESB is a High Performance, Light weight, Open Source Enterprise Service Bus. It also has inbuilt support for integrating different technologies which uses different transports protocols. Some of the well known transports that WSO2 ESB supports are HTTP, HTTPS, POP, IMAP, SMTP, JMS, FIX, TCP, UDP, FTPS, SFTP, CIFS, MLLP, SMS.

AMQP is an application layer level messaging protocol for message oriented architecture. It operates like the same way as HTTP, FTP, SMTP etc, to make systems inter-operate with each other. It address the issues that are faced by systems where the inter-operability is achieved by using well defined API’s (e.g JMS). For example, if your system wants to talk to another system over JMS, you have to implement the JMS API. Whereas AMQP is a wire-level protocol which describe the format of the data that is sent across the network. So irrespective of the implementation language, any system/tool that can create/send/consume/read the AMQP messages which ad-hear to the AMQP data format, gets the ability to inter-operate with each other.

RabbitMQ Java Client is one such tool which allows you to send or receive AMQP messages. Apart from this, RabbitMQ is an AMQP broker implementation, which can be used as an AMQP broker too. In this post i will be explaining about the new AMQP transport for WSO2 ESB, which is implemented using the




The above scenario is considered here. This will demonstrate the ability of  WSO2 ESB to consume/publish AMQP messages from/to an AMQP broker. The “Sender” here can be anything, which is capable of publishing messages to an AMQP queue. Similarly the “Receiver” can be anything, which can consume messages form an AMQP queue. In this post I will be using RabbitMQ Java Client library in sending/receiving AMQP messages in both Sender and Receiver implementation.

A proxy service in ESB will be listening to Q1. When there is a message available in Q1, ESB will consume it. If the proxy defined in such a way that the messages should be sent to an AMQP destination , say Q2, then the consumed messages will be sent to the Q2. The proxy service configuration for this scenario is explained later in this post.

Installing RabbitMQ AMQP transport feature into WSO2 ESB

The RabbitMQ AMQP transport is developed as a separate module for transports project. It is also available as an installable p2 feature, which can be installed via the WSO2 Feature Manager in WSO2 ESB.
Following are the steps to install this feature.

1. Start the ESB server.

2. Download and unzip the p2-repo.zip file to some location. Copy the path (say /home/esb/p2-repo).

3. Go to Configure > Features from the management console view and add a new local repository by giving the path copied above.

4. Select the added repository and tick “Show only the latest versions” and click on Find features. You will see the “Axis2 Transport RabbitMQ AMQP” feature listed. Select that and install it.

5. After successful installation, shutdown the server.
Alternatively you can just copy both the jars found in plugins directory to {ESB_HOME}/repository/components/dropins/ directory.
Now the next thing is to configure the transport (Listener and Sender) in axis2.xml
Configuring RabbitMQ AMQP Transport in WSO2 ESB

1. Add the following configuration items to axis2.xml found in {ESB_HOME}/repository/conf/axis2/axis2.xml

(i) Under transport listeners section add the following RabbitMQ transport listener.

<transportReceiver name="rabbitmq" class="org.apache.axis2.transport.rabbitmq.RabbitMQListener">
   <parameter name="AMQPConnectionFactory" locked="false">
      <parameter name="rabbitmq.server.host.name" locked="false">192.168.0.3</parameter>
      <parameter name="rabbitmq.server.port" locked="false">5672</parameter>
      <parameter name="rabbitmq.server.user.name" locked="false">user</parameter>
      <parameter name="rabbitmq.server.password" locked="false">abc123</parameter>
   </parameter>
</transportReceiver>

The parameters are self explanatory, which are used to create connection to AMQP broker. You can define any number of connection factories under <transportReceiver/> definition.

(ii) Under transport senders section add the following RabbitMQ transport sender.
<transportSender name="rabbitmq"
class="org.apache.axis2.transport.rabbitmq.RabbitMQSender"/>
This is the transport sender which is used for sending AMQP messages out to a queue.

2. Start the ESB server.

Creating a proxy service which works with RabbitMQ AMQP transport
A sample proxy service which consumes and sends AMQP messages from and
to an RabbitMQ AMQP broker

<proxy xmlns="http://ws.apache.org/ns/synapse" name="AMQPProxy"
transports="rabbitmq" statistics="disable" trace="disable"
startOnLoad="true">
   <target>
      <inSequence>
         <log level="full"/>
         <property name="OUT_ONLY" value="true"/>
         <property name="FORCE_SC_ACCEPTED" value="true" scope="axis2"/>
      </inSequence>
      <endpoint>
         <address
         uri="rabbitmq:/AMQPProxy?rabbitmq.server.host.name=192.168.0.3&amp; rabbitmq.server.port=5672&amp; rabbitmq.server.user.name=user&amp; rabbitmq.server.password=abc123&amp; rabbitmq.queue.name=queue2&amp; rabbitmq.exchange.name=exchange2"/>
      </endpoint>
   </target>
   <parameter name="rabbitmq.queue.name">queue1</parameter>
   <parameter name="rabbitmq.exchange.name">exchange1</parameter>
   <parameter name="rabbitmq.connection.factory">AMQPConnectionFactory</parameter>
   <description></description>
</proxy>

1.  Transport configuration (transports=”rabbitmq”)
The proxy is defined under amqp transport as OUT_ONLY proxy service, where it does not expect a response from the endpoint.

2. RabbitMQ AMQP Connection factory configuration (AMQPConnectionFactory)
In here we specify that the name of connection factory which used to listen to the queue to consume messages as a parameter. This connection factory is defined in the AMQP transport listener configuration of axis2.xml. This proxy reads message from the specified queue and sends the messages to the defined in the endpoint address.

3. RabbitMQ Queue name to listen for messages (rabbitmq.queue.name)
This is the queue name that the listener configuration for AMQPConnectionFactory will be listening on. If no name is specfied, then it is assumed the the name of the Proxy Service used as the queue name.

4. RabbitMQ Endpoint configuration
 <address
         uri="rabbitmq:/AMQPProxy?rabbitmq.server.host.name=192.168.0.3&amp; rabbitmq.server.port=5672&amp; rabbitmq.server.user.name=user&amp; rabbitmq.server.password=abc123&amp; rabbitmq.queue.name=queue2&amp; rabbitmq.exchange.name=exchange2"/>

The endpoint address should be of the format specified as above. All the parameters needed for the proxy service to send messages to endpoint should be given in the address uri format as mentioned above. If the exchange name is not specified, then as default, an empty value will be used as the exchange name.

5. RabbitMQ AMQP Transport properties
rabbitmq.server.host.name – Host name of the RabbitMQ server running on.
rabbitmq.server.port – Port value on which the server is running.
rabbitmq.server.user.name – User name to connect to RabbitMQ server.
rabbitmq.server.password – Password of the account to connect to RabbitMQ server.
rabbitmq.server.virtual.host – Virtual host name of the RabbitMQ server running on, if any.
rabbitmq.queue.name – Queue name to send or consume messages.
rabbitmq.exchange.name – Exchange name the queue is bound.

There can be situations where you only want to send AMQP messages out OR only want receive AMQP messages. The above proxy can be changed to suite both of the above scenarios. Also you can change it to work with different transport as-well. For example, a proxy which listen messages over AMQP and send over HTTP or JMS, etc.

Sample Proxy Service in operation

Note : This feature is currently tested for soap messages which are sent and consumed from AMQP broker queues with content type “text/xml”.

1. When the proxy service is created by following the above steps and deployed in ESB, it will be listening to the queue specified in rabbitmq.connection.factory under RabbitMQ AMQP Transport Listener. When there is a message available in queue, it will be consumed by the listener.

2. The consumed message will be then be sent to the endpoint queue specified in the Endpoint configuration of proxy.

A sample java client to send soap xml message to an AMQP queue

In a previous post of mine, I explained on how to use RabbitMQ Java client to send/receive messages. In here i’m using the same way to send and receive messages from/to an RabbitMQ queue.
Note : Give the correct queue name for publishing messages. This will be the same queue where the AMQP Transport listener will be listening on.

ConnectionFactory factory = new ConnectionFactory();
factory.setHost(host);
factory.setUsername(userName);
factory.setPassword(password);
factory.setPort(port);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(queueName, false, false, false, null);
channel.exchangeDeclare(exchangeName, "direct", true);
channel.queueBind(queueName, exchangeName, routingKey);

// The message to be sent
String message = "<soapenv:Envelope
                  xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope">\n" +
                 "<soapenv:Header/>\n" +
                 "<soapenv:Body>\n" +
                 "  <p:greet xmlns:p=\"http://greet.service.kishanthan.org\">\n" +
                 "     <in>" + name + "</in>\n" +
                 "  </p:greet>\n" +
                 "</soapenv:Body>\n" +
                 "</soapenv:Envelope>";

// Populate the AMQP message properties
AMQP.BasicProperties.Builder builder = new
AMQP.BasicProperties().builder();
builder.messageId(messageID);
builder.contentType("text/xml");
builder.replyTo(replyToAddress);
builder.correlationId(correlationId);
builder.contentEncoding(contentEncoding);

// Custom user properties
Map<String, Object> headers = new HashMap<String, Object>();
headers.put("SOAP_ACTION", "greet");
builder.headers(headers);

// Publish the message to exchange
channel.basicPublish(exchangeName, queueName, builder.build(), message.getBytes());

A sample java client to consume the message from an AMQP queue

Note : Give the correct queue name for consuming messages. In here the queue name will be the one configured in the Endpoint configuration of the proxy service.

ConnectionFactory factory = new ConnectionFactory();
factory.setHost(hostName);
factory.setUsername(userName);
factory.setPassword(password);
factory.setPort(port);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(queueName, false, false, false, null);
channel.exchangeDeclare(exchangeName, "direct", true);
channel.queueBind(queueName, exchangeName, routingKey);

// Create the consumer
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(queueName, true, consumer);

// Start consuming messages
while (true) {
   QueueingConsumer.Delivery delivery = consumer.nextDelivery();
   String message = new String(delivery.getBody());
}

The above two java clients can be used to test the scenario where you publish a message to a RabbitMQ AMQP queue, which is consumed by the ESB and published to another queue, which in turns consumed by a java client. When running both the above clients, you will get the messages sent by the Sender client at Receiver side. If the above works as expected, then you have configured RabbitMQ AMQP transport correctly in WSO2 ESB.

Conclusion

This new RabbitMQ AMQP transport implementation will solve the issue of calling an AMQP broker, such as RabbitMQ, directly without the need to use different transport mechanisms, such as JMS. The underlying protocol used in this transport is AMQP.


Install multiple WSO2 Products




 Bellow  describes the default ports that are used for each WSO2 product when the port offset is 0.

  • 9443 - HTTPS (the default URL of the management console is              https://localhost:9443/carbon)
  • 9763 - HTTP servlet transport
For multiple product installation you should change the offset value

How To Change Offset Value

 Edit the default offset configuration defined in the carbon.xml  file located at  $CARBON_HOME/repository/conf/carbon.xml.

 If you change value as 1[ <offset>1<offset>] , then the default URL of the management console is https://localhost:9444/carbon

Thursday 20 February 2014

Setting up WSO2 Carbon Products with MySQL


 Downlaod and install WSO2 Product  and Mysql


 Setup Configuration Files

1. Edit the default database configuration defined in the  master-datasources.xml  file located at  $CARBON_HOME/repository/conf/datasources  directory as below. Both the database configurations in  registry.xml  and  user-mgt.xml  refer this data source.

 <datasource>
       <name>WSO2_CARBON_DB</name>
       <description>The datasource used for registry and user manager</description>
       <jndiConfig>
           <name>jdbc/WSO2CarbonDB</name>
       </jndiConfig>
       <definition type="RDBMS">
           <configuration>
               <url>jdbc:mysql://localhost:3306/wso2</url>
               <username>root1</username>
               <password>root1</password>
               <driverClassName>com.mysql.jdbc.Driver</driverClassName>
               <maxActive>80</maxActive>
               <maxWait>60000</maxWait>
               <minIdle>5</minIdle>
               <testOnBorrow>true</testOnBorrow>
               <validationQuery>SELECT 1</validationQuery>
               <validationInterval>30000</validationInterval>
           </configuration>
       </definition>
</datasource>


The database configuration options

url - The URL of the database.
username  - The name of the database user.
password - The password of the database user.
driverClassName  - The class name of the database driver.
maxActive - The maximum number of active connections that can be allocated from this pool at the same time or negative for no limit.
maxWait - The maximum number of milliseconds that the pool will wait (when there are no available connections) for a connection to be returned before throwing an exception or <= 0 to wait indefinitely.
minIdle - The minimum number of active connections that can remain idle in the pool without extra ones being created or 0 to create none.

Create Database
(Start WSO2 and Automatic Database Creation)

 When you start the server for the first time, use the -Dsetup option. It will create all the tables in the given MySQL database.

For Linux:

wso2server.sh -Dsetup


For Windows:

wso2server.bat -Dsetup