Tuesday, March 29, 2016

Brief Idea of WSO2 ESB Connectors

What is connector?

Connectors allow you to connect and interact with services which are provided by outside parties. A connector is a collection of templates which define set of operations which are given by outside party. Connectors are used to wrap the API of an external service. For writing a connector, we should research the API provided by the service. Then we have to decide which API we are going to use to write our connector. After that we can implement the operations which are provided by the API. Most of outside parties offer two services such as REST and SOAP in their APIs.

Why Connector?

I am pretty sure that most of the people think why we need to use connector. There are many external parties providing API to use their methods and tables. Most of us know about Google Blog API which is providing many methods such as getting get Blog, get comment, get post and etc. If you want to use those functions in your software, then you have to implement all those functions in your software. But in a connector already the methods are implemented. By using connector in your code you could save your time by recreating it. As all you know WSO2 products are open source, you can download the connectors and can use it.

Writing ESB Connector

You can clone ESB connectors from the following git hub link "https://github.com/wso2/esb-connectors".
$ git clone https://github.com/wso2/esb-connectors

After you clone there are lot of connectors you can get. They can be SOAP connector or REST connector or Java connector. So you can select one of the connector according your task and modify it. 

Open a connector according to your API in IntelliJ. You can see below structure. 

Here you can see some folder structure.
  • Repository: Here you have to put your WSO2 ESB which you downloaded.
  • Resources: Here you have to implement your API methods. Some outside party can provide set of grouped methods. So that set methods must come in one folder. 
    • config: This folder contains init.xml and component.xml.
      • init.xml: Initialize the connector environment. Here we specify username, password, API url, Access token and etc which are related to initialize the API.
      • component.xml: This file is included in every module (like folder). This defines available methods in the module. In this folder we define init.xml and its desvription. component.xml will be like this.
        <component name="config" type="synapse/template"> 
              <subComponents> 
                  <component name="init"> 
                       <file>init.xml</file> 
                       <description>Configuration.</description> 
                   </component> 
             </subComponents>
         </component>
    •  icon: Here you have to put API's icons.

      After that you can create own folder structure and within the folder you can create your synapse template like below.
      <template name="method" xmlns="http://ws.apache.org/ns/synapse"> 
         <parameter name="param" description="The parameter which needs to give to API./>    
           <sequence> 
              <property name="uri.var.param" expression="$func:param"/>
                <call> 
                   <endpoint> 
                        <http method="GET" uri-template="{uri.var.URL}"/> 
                   </endpoint> 
               </call> 
          </sequence> 
      </template>
  • connector.xml: This defines connector name and dependent modules.
    <?xml version="1.0" encoding="UTF-8"?>
    <connector> 
       <component name="compo" package="org.wso2.carbon.connector"> 
             <dependency component="config"/>        
            <description>WSO2 Connector for API.</description> 
      </component> 
    </connector>
  • pom.xml: Contains the required dependencies for connector core libraries and relevant synapse libraries as well as maven repositories for a specific connector.

    In my next blog I will cover Integration Test using TestNG. :)


3 comments: