Friday, September 5, 2014

Protocol Conversion from REST to SOAP using Messaging Bridge Enterprise Integration Pattern (EIP) and Content Type Negotiation using WSO2 ESB

Say you have a SOAP service. You need to expose it as a REST service. Furthermore, you may need other mediations such as XML to JSON conversion to happen.

This could be done by implementing the Messaging Bridge Enterprise Integration Pattern (EIP).

Furthermore, with the WSO2 ESB Content type negotiations (conversions) such as XML to JSON is just a simple configuration that would take only a few seconds.

For the back-end service we'll use the ZooDataService we built in a previous example using WSO2 DSS to expose the data as SOAP services: Expose Data as a Service using WSO2 Data Services Server (DSS)

Now that we've got a service we'll see how to do the SOAP to REST conversion, and the XML to JSON conversion.

1. First lets take a look at the SOAP to REST Conversion:

For the SOAP to REST conversion we will adhere to the Messaging Bridge Integration pattern as mentioned above.

We will create an API in the WSO2 ESB for this purpose and expose the SOAP service as a REST service via the said API.

Below is the architecture for our system:

Now lets look at how to Create the ESB API:

Go to the ESB management console and add an API

Switch to Source View and enter the following configuration:

<api xmlns="" name="ZooData" context="/zoodata">
   <resource methods="GET" uri-template="/{animalID}">
         <payloadFactory media-type="xml">
               <p:getAnimalByID xmlns:p="">
                  <a:AnimalID xmlns:a="">$1</a:AnimalID>
               <arg evaluator="xml" expression="get-property('uri.var.animalID')"></arg>
         <log level="full"></log>
               <address uri="" format="soap12"></address>
         <property name="messageType" value="application/json" scope="axis2"></property>

Now you can invoke the service Restully using the following GET request:


you will get the following XML output:


2. Next we'll look at how to convert the content types (Content Type Negotiation):

To convert the content type from XML to JSON  in the outgoing response simple change the value attribute of the 'messageType' property to 'application/json'. This works vice versa as well.

<property name="messageType" value="application/json" scope="axis2"></property>

now you'll get the output in JSON format:


Now since you have achieved protocol & content type conversion you can use the WSO2 API Manager to expose this as a Managed API if you wish. You will be using the API Façade pattern in doing so.

For further information on API Façade pattern refer:

Expose Data as a Service using WSO2 Data Services Server (DSS)

You run a Zoo... yeah that's right a Zoo. You have a set of animals and each of them are renowned for something. You have their information which include an id for each animal, their country of origin, the name you gave them and what they are known for.

You need to expose this animal information as a web service.

So what do you do.

  • You open up eclipse
  • Create a web services project
  • Add the db drivers
  • Create the DAO layer
  • Create the DTO ....
WHoA WHOaaa... STOP ... 

That's ancient science... in the time u did all that you could have had your services exposed and be already testing them. 

How ?

Well this amazing little product called the WSO2 Data Services Server (DSS for short). It'll make your life so much more easy when it comes to exposing your data as services.

In this example we'll look at how to expose data as a SOAP service in a few minutes with just a few clicks, without writing a single line of code.

Yep .. your heard me.. without a single line of code .. 

What you need:
  1. MySQL Database and JDBC driver (or any other db + driver of your choice)
  2. WSO2 DSS 

Step 1:

Create a database called CUSTOMER_DB
create database Zoo_db

Create table CUSTOMER

CREATE TABLE `animal` (
  `AnimalID` varchar(90) NOT NULL PRIMARY KEY,
  `Species` varchar(200) DEFAULT NULL,
  `Name` varchar(200) DEFAULT NULL,
  `Country` varchar(200) DEFAULT NULL,
  `Speciality` varchar(200) DEFAULT NULL

Insert the animal data:

insert into animal values ("001", "Penguin", "Skipper", "Madagaskar", "Leading a pack of Penguins");
insert into animal values ("002", "Penguin", "Kowalsky", "Madagaskar", "Research & Development");
insert into animal values ("003", "Penguin", "Rico", "Madagaskar", "Demolitions");
insert into animal values ("004", "Penguin", "Private", "Madagaskar", "hmmm.. saying random feminine stuff");
insert into animal values ("005", "Lemur", "Julian", "Madagaskar", "Dance Unit");
insert into animal values ("006", "Lemur", "Maurice", "Madagaskar", "Maintenance");
insert into animal values ("007", "Lemur", "Mort", "Madagaskar", "Feet Hugging");

Step 2:
  • Download and Unzip WSO2 DSS
  • Add the MySQL connector JAR to the <DSS_HOME>\repository\components\dropins directory
  • Start the server by running the wso2server.bat (Windows) or (Linux) in <DSS_HOME>/bin directory
  • The management console will be accessible on https://localhost:9443/carbon/ - 9443 is the default port. If you want to change this you can do it by offsetting the port in <DSS_HOME>\repository\conf\carbon.xml - Increment the <Offset>0</Offset> value. All ports used by DSS will be incremented accordingly.
  • Login with default admin username/password admin:admin
  • Using the controls on the left go to Configure -> Data Sources -> Add New Data Source

  • Choose RDBMS, Give a name, Enter Driver String and URL, username/password as shown

Test and save the DataSource

Next go to Main, Under Services -> DataServices click Generate:

Select the created ZooDS from the dropdown and give the DB name we created (Zoo_db)
And then select the table for which you want the services generated for. In our case the animal DB

click next and you'll get the option to select whether to create separate services for the different operations (select, insert, delete etc.) or bundle them to one service. We'll select to expose all in one service and give it the name "ZooDataService"

Click next and then Finish. This will create a set of services that expose all the operations such as Select, Insert, Delete etc. as soap servicesNow your service will be listed under Services -> list

Now you can try out the different operations using the 'Try This Service' option which you'll see

And there you go ... Who said exposing data required hours of coding ?

So to sum it up WSO2 DSS is like Rico from Penguins of Madagaskar... you'll know what I mean if u've watched it... Tools at your fingertip.. Fast and Easy .. Just like that ;)