Tuesday, March 3, 2015

Running a simple demo with the Salesforce Connector with Fuse


JBoss Fuse and Apache Camel provides an easy to use Salesforce Connector to interact with Salesforce.  The component supports producer and consumer endpoints to communicate with Salesforce using Java Data Transfer Objects (DTOs).  There is a companion maven plugin Camel Salesforce Plugin that generates these DTOs.    The Component supports three Salesforce APIs - 1) REST API 2) REST Bulk API 3) REST Streaming API.

Salesforce is best known for its customer relationship management (CRM) product, which is composed of Sales Cloud, Service Cloud, Marketing Cloud, Force.com, Chatter and Work.com.  They provide a developer platform which allows easy testing of Fuse and Salesforce integration.

Salesforce enables access through their APIs through their connected Apps with OAuth.  The image to the right shows the flow of OAuth with Salesforce and you can learn more at https://developer.salesforce.com/page/Digging_Deeper_into_OAuth_2.0_on_Force.com

Salesforce Setup

First a developer is required for our testing which you can get at https://developer.salesforce.com/.   The Name, Email, Username and Password are required to register.   If you already have a Developer Edition organization, verify that you have the “API Enabled” permission. This permission is enabled by default, but may have been changed by an administrator. After the account is created the connected app needs to be setup.

The main parameters to be used with the camel salesforce component are:
  • clientId
  • clientSecret
  • userName
  • password
  • loginUrl (login.salesforce.com)

A connected app (previously known as a 'remote access' app) is an application that integrates with Salesforce using APIs such as the Force.com REST and SOAP APIs. In addition to standard OAuth capabilities, connected apps allow administrators to set various security policies and have explicit control over who may use the applications.

In Salesforce, from Setup, click Create | Apps, and under Connected Apps click New to create a new connected app if you have not already done so.  The Enable OAuth Settings checkbox should be selected.  The full access OAuth Scope should be selected as well.  The Callback URL you supply here is the same as your Web application's callback URL. Usually it is a servlet if you work with Java. It must be secure: http:// does not work, only https://. For development environments, the callback URL is similar to https://localhost:8443/RestTest/oauth/_callback. When you click Save, the Consumer Key is created and displayed, and a Consumer Secret is created (click the link to reveal it).  A quick note that the OAuth 2.0 specification uses “client” instead of “consumer.” Salesforce supports OAuth 2.0.

New Connected App
After the New Connected App has been save then the OAuth Policies need to be set.  The Permitted Users should be set to All users may self authorize.  The IP Relaxation should be set to Relax IP restrictions. 

Connected Apps Management
Fuse Setup

In our simple demo we are just going to pull some data as a consumer by running a simple query on the Opportunity Object.  In future blogs we will do more complicated operations as producer and consumer.  

Camel Route with salesforce component
First  create a new project with a Blueprint Archetype.  JBoss Developer Studio 7.1.1 and Integration Stack 7.0.3 were used.

Project archetype screen creating a new Fuse Project
We will make two changes to the pom.xml.  The first is adding the camel-salesforce dependency and the camel-salesforce-maven-plugin.   The reason we want to add the salesforce maven plugin is that it allows easily building the DTOs.   In the project in the github repository you will find sample DTOs as well as a sample pom.xml.

So we will build the DTO's before updating the route.  In the directory of the workspace you run the following command:

mvn camel-salesforce:generate -DcamelSalesforce.clientId= -DcamelSalesforce.clientSecret= -DcamelSalesforce.userName= -DcamelSalesforce.password=

The four parameters should be updated with the values that were created during the salesforce setup.  The source is created in a default directory,  ${project.build.directory}/generated-sources/camel-salesforce.  Move these over to the java source directory so you can use them.  Keep in mind that if fields are changed in salesforce then the DTOs will need to be regenerated.

We built a route that contains the properties for connecting and contains a quick query to retrieve data.  A simple route was used to start the route and then consume data from Salesforce with a query.

salesforce:query?sObjectQuery=SELECT id,name from Opportunity&sObjectClass=org.apache.camel.salesforce.dto.Opportunity.class
For more detail on the route you can view the code in the repository.  

Running the project

The project is located at https://github.com/jbossdemocentral/fuse-components-salesforce.  The steps are in the readme.  Clone the repository and find the camel-blueprint project in the projects folder.  I put the properties in the blueprint.xml as an example to be included with the route.  I also included a unit test to run the query as well which uses a property file.

Step 1: Import the project into JBDS

Step 2: Modify the blueprint.xml and the test-salesforce-login.properties to contain the correct authentication parameters.

Step 3: Right click on the SalesforceTest.java in the src/test/java folder and select Run As Unit Test.  

Step 4: Right click on the blueprint.xml in the src/main/resources folder and select Run As Local Camel Context (without Test).

In both steps you should see the data in json returned for the opportunities.