Merge pull request #9 from igarashitm/camel-example-salesforce-consumer
Add camel-salesforce consumer example
diff --git a/examples/camel-example-salesforce-consumer/README.adoc b/examples/camel-example-salesforce-consumer/README.adoc
new file mode 100644
index 0000000..1e2b728
--- /dev/null
+++ b/examples/camel-example-salesforce-consumer/README.adoc
@@ -0,0 +1,64 @@
+== Salesforce Streaming API Example
+
+This example shows how to use Salesforce Streaming API.
+
+=== Configuring Salesforce
+This examples uses OAuth 2.0 Refresh Token Flow, which requires 3 credentials collected from Salesforce - `Client ID`, `Client Secret` and `Refresh Token`. You will need to put your own credentials in `src/main/resources/application.properties` file.
+
+NOTE: you might want to sign up with your own Developer account at developer.salesforce.com. After you have done that, you’ll need to create a Connected Application for your integration.
+
+NOTE: This was written with Salesforce Lightening user interface. Classic user interface might have slightly different menu and/or workflow.
+
+Here is an example workflow of creating a Connected Application and get required credentials. For more details, please check Salesforce online manual
+https://help.salesforce.com/articleView?id=extend_code_overview.htm&type=5
+
+==== Creating a Connected Application
+. Go to `PLATFORM TOOLS > Apps > App Manager` in a navigation tree at the left of Salesforce Setup screen
+. Click `New Connected App` button at the upper right
+. Enter `camelk_knative_source_salesforce` for Connected App Name
+. Enter your email address for Contact Email
+. Check Enable OAuth Settings
+. Enter `https://login.salesforce.com/services/oauth2/success` for Callback URL
+. Choose 2 OAuth scopes, `Access and manage your data (api)` and `Perform requests on your behalf at any time (refresh_token, offline_access)`, then click Add button
+. Click Save button and then click Continue button
+. You will be redirected to `Manage Connected Apps` page. `Consumer Key` and `Consumer Secret` are available in API section. Use `Consumer Key` as a `Client ID` and `Consumer Secret` as a `Client Secret`.
+. Go to `PLATFORM TOOLS > Apps > Connected Apps > Manage Connected Apps`
+. Click `Edit` next to the `camelk_knative_source_salesforce`
+. In OAuth Policies section,
+.. Choose `Relax IP restrictions` for IP Relaxation
+.. Choose `Refresh token is valid until revoked`
+. Click `Save`
+
+==== Getting an OAuth refresh token
+In your browser go to the URL, with substituting the `__YOUR_CLIENT_ID_HERE__` with your connected application Consumer Key:
+
+```
+https://login.salesforce.com/services/oauth2/authorize?response_type=token&client_id=YOUR_CLIENT_ID_HERE&redirect_uri=https://login.salesforce.com/services/oauth2/success&display=touch
+```
+
+Allow access to the application, and you’ll end up on a page with `refresh_token`, something like:
+
+```
+https://login.salesforce.com/services/oauth2/success#access_token=..&refresh_token=<refresh_token>&instance_url=...&issued_at=...&signature=...&scope=...&token_type=Bearer
+```
+
+
+=== How to run
+
+You can run this example using
+
+ mvn compile camel:run
+
+=== Custom topic name and operation flags
+
+You can specify your topic name (`salesforce.topic`) and turn on/off each operation to be notified (for example, set true for `camel.component.salesforce.config.notifyForOperationCreate` to receive Contact created event) in `src/main/resources/application.properties` file.
+
+=== Help and contributions
+
+If you hit any problem using Camel or have some feedback, then please
+https://camel.apache.org/support.html[let us know].
+
+We also love contributors, so
+https://camel.apache.org/contributing.html[get involved] :-)
+
+The Camel riders!
diff --git a/examples/camel-example-salesforce-consumer/pom.xml b/examples/camel-example-salesforce-consumer/pom.xml
new file mode 100644
index 0000000..0d7702f
--- /dev/null
+++ b/examples/camel-example-salesforce-consumer/pom.xml
@@ -0,0 +1,91 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.camel.example</groupId>
+ <artifactId>examples</artifactId>
+ <version>3.5.0-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>camel-example-salesforce-consumer</artifactId>
+ <name>Camel :: Example :: Salesforce :: Consumer</name>
+ <description>An example that uses Salesforce Rest Streaming API</description>
+ <packaging>jar</packaging>
+
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ <category>Social</category>
+ </properties>
+
+ <dependencyManagement>
+ <dependencies>
+ <!-- Add Camel BOM -->
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-bom</artifactId>
+ <version>${camel.version}</version>
+ <type>pom</type>
+ <scope>import</scope>
+ </dependency>
+ </dependencies>
+ </dependencyManagement>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-core</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-main</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-jackson</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-salesforce</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-simple</artifactId>
+ <version>${slf4j-version}</version>
+ </dependency>
+
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-maven-plugin</artifactId>
+ <version>${camel.version}</version>
+ <configuration>
+ <logClasspath>false</logClasspath>
+ <mainClass>org.apache.camel.example.salesforce.Application</mainClass>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
diff --git a/examples/camel-example-salesforce-consumer/src/main/java/org/apache/camel/example/salesforce/Application.java b/examples/camel-example-salesforce-consumer/src/main/java/org/apache/camel/example/salesforce/Application.java
new file mode 100644
index 0000000..da2d882
--- /dev/null
+++ b/examples/camel-example-salesforce-consumer/src/main/java/org/apache/camel/example/salesforce/Application.java
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.example.salesforce;
+
+import org.apache.camel.main.Main;
+
+public final class Application {
+
+ private Application() {
+ // noop
+ }
+
+ public static void main(String[] args) throws Exception {
+ Main camelMain = new Main();
+ camelMain.configure().addRoutesBuilder(new SalesforceRouteBuilder());
+ camelMain.run(args);
+ }
+}
diff --git a/examples/camel-example-salesforce-consumer/src/main/java/org/apache/camel/example/salesforce/SalesforceRouteBuilder.java b/examples/camel-example-salesforce-consumer/src/main/java/org/apache/camel/example/salesforce/SalesforceRouteBuilder.java
new file mode 100644
index 0000000..a30e058
--- /dev/null
+++ b/examples/camel-example-salesforce-consumer/src/main/java/org/apache/camel/example/salesforce/SalesforceRouteBuilder.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.example.salesforce;
+
+import org.apache.camel.builder.RouteBuilder;
+
+public class SalesforceRouteBuilder extends RouteBuilder {
+
+ @Override
+ public void configure() {
+ from("salesforce:{{salesforce.topic}}")
+ .unmarshal().json()
+ .choice()
+ .when(header("CamelSalesforceEventType").isEqualTo("created"))
+ .log("New Salesforce contact was created: [ID:${body[Id]}, Name:${body[Name]}, Email:${body[Email]}, Phone: ${body[Phone]}]")
+ .when(header("CamelSalesforceEventType").isEqualTo("updated"))
+ .log("A Salesforce contact was updated: [ID:${body[Id]}, Name:${body[Name]}, Email:${body[Email]}, Phone: ${body[Phone]}]")
+ .when(header("CamelSalesforceEventType").isEqualTo("undeleted"))
+ .log("A Salesforce contact was undeleted: [ID:${body[Id]}, Name:${body[Name]}, Email:${body[Email]}, Phone: ${body[Phone]}]")
+ .when(header("CamelSalesforceEventType").isEqualTo("deleted"))
+ .log("A Salesforce contact was deleted: [ID:${body[Id]}]");
+ }
+}
diff --git a/examples/camel-example-salesforce-consumer/src/main/resources/application.properties b/examples/camel-example-salesforce-consumer/src/main/resources/application.properties
new file mode 100644
index 0000000..af883b7
--- /dev/null
+++ b/examples/camel-example-salesforce-consumer/src/main/resources/application.properties
@@ -0,0 +1,32 @@
+## ---------------------------------------------------------------------------
+## Licensed to the Apache Software Foundation (ASF) under one or more
+## contributor license agreements. See the NOTICE file distributed with
+## this work for additional information regarding copyright ownership.
+## The ASF licenses this file to You under the Apache License, Version 2.0
+## (the "License"); you may not use this file except in compliance with
+## the License. You may obtain a copy of the License at
+##
+## http://www.apache.org/licenses/LICENSE-2.0
+##
+## Unless required by applicable law or agreed to in writing, software
+## distributed under the License is distributed on an "AS IS" BASIS,
+## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+## See the License for the specific language governing permissions and
+## limitations under the License.
+## ---------------------------------------------------------------------------
+# Salesforce credentials
+camel.component.salesforce.login-config.client-id=<Your Client ID>
+camel.component.salesforce.login-config.client-secret=<Your Client Secret>
+camel.component.salesforce.login-config.refresh-token=<Your Refresh Token>
+# Other configuration
+camel.component.salesforce.config.raw-payload=true
+camel.component.salesforce.login-config.type=REFRESH_TOKEN
+camel.component.salesforce.config.notifyForFields=ALL
+salesforce.topic=camelTestTopic
+camel.component.salesforce.config.updateTopic=true
+camel.component.salesforce.config.notifyForOperationCreate=true
+camel.component.salesforce.config.notifyForOperationUpdate=true
+camel.component.salesforce.config.notifyForOperationDelete=true
+camel.component.salesforce.config.notifyForOperationUndelete=true
+camel.component.salesforce.config.sObjectQuery=SELECT Id, Name, Email, Phone FROM Contact
+
diff --git a/examples/camel-example-salesforce-consumer/src/main/resources/simplelogger.properties b/examples/camel-example-salesforce-consumer/src/main/resources/simplelogger.properties
new file mode 100644
index 0000000..28533a4
--- /dev/null
+++ b/examples/camel-example-salesforce-consumer/src/main/resources/simplelogger.properties
@@ -0,0 +1,2 @@
+org.slf4j.simpleLogger.log.org.apache.camel=info
+
diff --git a/examples/pom.xml b/examples/pom.xml
index d3ec65f..c37d44f 100644
--- a/examples/pom.xml
+++ b/examples/pom.xml
@@ -146,6 +146,7 @@
-->
<module>camel-example-route-throttling</module>
<module>camel-example-routetemplate</module>
+ <module>camel-example-salesforce-consumer</module>
<module>camel-example-servlet-rest-blueprint</module>
<module>camel-example-servlet-tomcat</module>
<module>camel-example-splunk</module>