Concept of Connector is a very useful extension point when it comes to integration with other SaaS providers such as Google, Twitter, Salesforce, JIRA etc. WSO2 connector store [1] provides a comprehensive set of out of the box connectors.
However, sometimes you might find that there is no OOTB connector to achieve your requirement, in that case you may have to write your own custom connector. There are three types of custom connectors you can implement [4].
- Soap-based connectors
- REST-based connector
- Java API-based connectors
In this blog post we are going to explore how REST-based connectors work. Sample we follow is very simple it just do an API call to
URL: https://www.googleapis.com/books/v1/volumes?q={uri.var.searchQuery}
Method: Get
However, what is more important is: how it is packaged as a connector so that it can be used within your mediation sequence.
Step 01: Generate connector from maven archetype.
mvn org.apache.maven.plugins:maven-archetype-plugin:2.4:generate -DarchetypeGroupId=org.wso2.carbon.extension.archetype -DarchetypeArtifactId=org.wso2.carbon.extension.esb.connector-archetype -DarchetypeVersion=2.0.4 -DgroupId=org.wso2.carbon.esb.connector -DartifactId=org.wso2.carbon.esb.connector.googlebooks -Dversion=1.0.0 -DarchetypeRepository=http://maven.wso2.org/nexus/content/repositories/wso2-public/
Make sure you put the connector name as googleBooks and Y at the end.
Open the generated project from your favorite IDE. Just remove config and sample modules for simplicity
Step 02: Add a new directory: googlebooks_volume under /src/main/resources. Create listVolume.xml and component.xml files inside the googlebooks_volume directory. Also edit connector.xml in the src/main/resources accordingly. Finally create an icon directory inside /src/main/resources. You can find necessary icons from [3].
listVolume.xml
<template name=”listVolume” xmlns=”http://ws.apache.org/ns/synapse"> <parameter name=”searchQuery” description=”Full-text search query string.” /> <sequence> <property name=”uri.var.searchQuery” expression=”$func:searchQuery” /> <call> <endpoint> <http method=”get” uri-template=”https://www.googleapis.com/books/v1/volumes?q={uri.var.searchQuery}" /> </endpoint> </call> </sequence> </template>
component.xml
<?xml version=”1.0" encoding=”UTF-8"?> <component name=”googlebooks_volume” type=”synapse/template”> <subComponents> <component name=”listVolume”> <file>listVolume.xml</file> <description>Lists volumes that satisfy the given query.</description> </component> </subComponents> </component>
connector.xml
<?xml version=”1.0" encoding=”UTF-8"?> <connector> <component name=”googleBooks” package=”org.wso2.carbon.connector” > <dependency component=”googlebooks_volume”/> <description>wso2 sample connector library</description> </component> <icon>icon/icon-small.gif</icon></connector>
Final version of your source code structure will look like below.
Now you can build the project with Maven command: mvn clean install. At the end of the build process you will connector archive created in the target directory.
Step 03: Create a New Integration Project to apply your connector to the mediation sequence.
Add connector to Configs module from file system
Add proxy service to the Configs module as below. Also you will notice that the custom connector appears in the mediator pallet.
<?xml version=”1.0" encoding=”UTF-8"?> <proxy xmlns=”http://ws.apache.org/ns/synapse" name=”googlebooks_listVolume” transports=”https,http” statistics=”disable” trace=”disable” startOnLoad=”true”> <target> <inSequence> <property name=”searchQuery” expression=”json-eval($.searchQuery)”/> <googleBooks.listVolume> <searchQuery>{$ctx:searchQuery}</searchQuery> </googleBooks.listVolume> <respond/> </inSequence> </target> <description/> </proxy>
Step 04: Create connector exporter project
Step 05: Now everything is ready to test your project
Make sure you deploy both Exporter module as well as Configs module
Finally you can test your integration with the postman and get a list of books.
References
[1] https://store.wso2.com/store/pages/top-assets
[2] https://apim.docs.wso2.com/en/latest/integrate/develop/customizations/creating-new-connector/
[3] https://svn.wso2.org/repos/wso2/scratch/connectors/icons/
[4] https://docs.wso2.com/display/ESBCONNECTORS/WSO2+ESB+Connectors