Integrating WSO2 ESB Connectors in real world integration Scenarios

For a moment consider some of the third party services we use daily. For an example We can consider Twitter as such a service. When we consider this fact we can identify a lot of service we use in our day to day life. Usually we use APIs to connect to these services as developers. WSO2 ESB Connectors allow you to interact with such third parties’ API from ESB Message flow. With these connectors you can access the API interact with it through the exposed functionalities.

Eg: With the Twitter Connector you can access the twitter API and then retrieve tweets of a user.

At the moment you can find more than hundred of connectors at Connector Repo. And you can build one connector at a time simply with the maven build tool.

Working with one connector can be much simpler as well as less frequently used. Consider a scenario in which we are communicating with two such third parties’ APIs. Such scenarios are complex to handle and messy in configuring them. With WSO2 ESB Connectors you can easily acquire such integrations for real world scenarios as well as business scenarios.

During this blog post Let’s have a look at such real world scenario which addresses an important project management task.


Integration Scenario with RedMine, Google Spreadsheet and Gmail.

In this integration scenario my objective is to track the expired Redmine assignments to a particular user/all the users and then retrieve the information as well as sending an email notification and log them in to google spreadsheet.

In order to accomplish this task we need to have the Redmine Connector, Google Spreadsheet Connector and the Gmail Connector. After building the connectors, before you write the configuration you need to upload them to ESB. Make sure to enable the connector after uploading them.

Enable Connectors

Configuring connectors

Gmail Connector
<gmail.init>
<username>your_user_name</username> <oauthAccessToken>your_access_token</oauthAccessToken>
</gmail.init>

Redmine Connector
<redmine.init>
<apiUrl>https://redmine.wso2.com</apiUrl>
<username>User_Name</username>
           <password>Password</password>
           <responseType>xml</responseType>
</redmine.init>

Google Spreadsheet Connector
<googlespreadsheet.oAuth2init>
           <oauthConsumerKey>Consumer_Key</oauthConsumerKey>
           <oauthConsumerSecret>Consumer_Secret</oauthConsumerSecret>
           <oauthAccessToken>Access_Token</oauthAccessToken>
<oauthRefreshToken>Refresh_Token</oauthRefreshToken>
</googlespreadsheet.oAuth2init>

Note: For the development purposes you can access Google OAuth 2.0 Playground in order to get the access tokens for gmail and google spreadsheet connectors.


<?xml version="1.0" encoding="UTF-8"?> <definitions xmlns="http://ws.apache.org/ns/synapse"> <registry provider="org.wso2.carbon.mediation.registry.WSO2Registry"> <parameter name="cachableDuration">15000</parameter> </registry> <taskManager provider="org.wso2.carbon.mediation.ntask.NTaskTaskManager"/> <import name="gmail" package="org.wso2.carbon.connector" status="enabled"/> <import name="redmine" package="org.wso2.carbon.connector" status="enabled"/> <import name="evernote" package="org.wso2.carbon.connector" status="enabled"/> <import name="googlespreadsheet" package="org.wso2.carbon.connectors" status="enabled"/> <proxy name="evernote_gtask_proxy" transports="https http" startOnLoad="true" trace="disable"> <description/> <target> <inSequence onError="faultHandlerSeq">

<redmine.init>
<apiUrl>https://redmine.wso2.com</apiUrl>
<username>User_Name</username>
            <password>Password</password>
            <responseType>xml</responseType>
</redmine.init>
<redmine.listIssues> <statusId>*</statusId> <limit>2</limit> <assignedToId>me</assignedToId> </redmine.listIssues>
<property name="cur_date" expression="get-property('SYSTEM_DATE', 'yyyy-MM-dd')" scope="default"/> <iterate continueParent="true" preservePayload="true" attachPath="//issues" expression="//issues/issue"> <target> <sequence> <log level="custom"> <property name="ITERATOR" value="Iterating Over the redmine feature list"/> </log> <property name="issue-id" expression="//issues/issue/id"/> <property name="project-name" expression="//issues/issue/id/@name"/> <property name="description" expression="//issues/issue/description"/> <property name="due-date" expression="//issues/issue/due_date"/> <script language="js">var current_date = mc.getProperty("cur_date").split("-"); var due_date = mc.getProperty("due_date"); if (due_date === null){ mc.setProperty("is_due","false"); } else{ var due_date_arr = due_date.split("-"); var due_date_obj = new Date(due_date_arr[0],due_date_arr[1],due_date_arr[2]); var cur_date_obj = new Date(current_date[0],current_date[1],current_date[2]); if((cur_date_obj&gt;due_date_obj)&gt;0){ mc.setProperty("is_due","true"); } else{ mc.setProperty("is_due","true"); } } </script> <gmail.init> <username>username</username> <oauthAccessToken>access_token</oauthAccessToken> </gmail.init> <filter source="get-property('is_due')" regex="true"> <then> <gmail.sendMail> <subject>Subject</subject> <toRecipients>recipients</toRecipients> <textContent>Email_Body</textContent> </gmail.sendMail> </then> </filter> </sequence> </target> </iterate> <script language="js">var current_date = mc.getProperty("cur_date").split("-"); var issues = mc.getPayloadXML().issue; var returnCsv = "Issue_ID,Project_Name,Due_Date\n"; for(i=0;i&lt;issues.length();i++){ var id = issues[i].id; var name = issues[i].project.@name; var due_date = issues[i].due_date; if (due_date != null){ var due_date_arr = due_date.split("-"); var due_date_obj = new Date(due_date_arr[0],due_date_arr[1],due_date_arr[2]); var cur_date_obj = new Date(current_date[0],current_date[1],current_date[2]); if((cur_date_obj&gt;due_date_obj)&gt;0){ mc.setProperty("task_due","true"); returnCsv=returnCsv+id+","+name+","+due_date+"\n"; } else{ mc.setProperty("task_due","false"); } } }                mc.setPayloadXML(                    &lt;text&gt;{returnCsv}&lt;/text&gt; );</script> <log level="full"/> <googlespreadsheet.oAuth2init> <oauthConsumerKey>consumer_key</oauthConsumerKey> <oauthConsumerSecret>consumer_secret</oauthConsumerSecret> <oauthAccessToken>access_token</oauthAccessToken> <oauthRefreshToken>refresh_token</oauthRefreshToken> </googlespreadsheet.oAuth2init> <filter source="get-property('task_due')" regex="true"> <then> <googlespreadsheet.importCSV> <spreadsheetName>spread_sheet_name</spreadsheetName> <worksheetName>work_sheet_name</worksheetName> <batchEnable>true</batchEnable> <batchSize>10</batchSize> </googlespreadsheet.importCSV> </then> </filter> <respond/> </inSequence> <outSequence> <log level="full"/> <send/> </outSequence> </target> </proxy> <localEntry key="csv_transform"> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="text" indent="no" encoding="UTF-8"/> <xsl:template match="/"> Issue_ID,Project_Name,Due_Date <xsl:value-of select="//issues/issue/id"/> <xsl:text>,</xsl:text> <xsl:value-of select="//issues/issue/project/@name"/> <xsl:text>,</xsl:text> <xsl:value-of select="//issues/issue/due_date"/> </xsl:template> </xsl:stylesheet> <description/> </localEntry> <sequence name="fault"> <log level="full"> <property name="MESSAGE" value="Executing default 'fault' sequence"/> <property name="ERROR_CODE" expression="get-property('ERROR_CODE')"/> <property name="ERROR_MESSAGE" expression="get-property('ERROR_MESSAGE')"/> </log> <drop/> </sequence> <sequence name="main"> <in> <log level="full"/> <filter source="get-property('To')" regex="http://localhost:9000.*"> <send/> </filter> </in> <out> <send/> </out> <description>The main sequence for the message mediation</description> </sequence> </definitions>


According to the above configuration, at first via the redmine connector we get the corresponding redmine issues. Then we use the iterate mediator to iterate through the result list. Depending on the result, use the script mediator to extract the required data fields and append the data to the payload accordingly. Then use the google spreadsheet connector to write them to the spreadsheet.

Get the Connectors Here https://www.dropbox.com/s/pq4rtzrrurtw0rm/Connectors.zip?dl=0

Comments

  1. Where do I configure the connector? In "Connecor List" there is no option for configuration.

    ReplyDelete
    Replies
    1. You configure the connector with synapse syntax. Which has been shown in the shared code snippet

      Delete

  2. Also I can not configure.

    Where do you set up the connector?

    There is no configuration option in the web mager environment.

    ReplyDelete

Post a Comment

Popular posts from this blog

Enabling Custom SSL Profiles in WSO2 ESB 4.8.1

Using WSO2 ESB HTTP Endpoints to define Restful Endpoints