Repository: camel
Updated Branches:
  refs/heads/master a37a29512 -> 68bd495b7


CAMEL-8777 Added the support of google service account in camel-google-calendar 
with thanks to Carl


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/68bd495b
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/68bd495b
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/68bd495b

Branch: refs/heads/master
Commit: 68bd495b78953e329a129e1425a30c2c5a6ada76
Parents: a37a295
Author: Willem Jiang <willem.ji...@gmail.com>
Authored: Thu May 21 21:59:43 2015 +0800
Committer: Willem Jiang <willem.ji...@gmail.com>
Committed: Thu May 21 22:14:56 2015 +0800

----------------------------------------------------------------------
 .../BatchGoogleCalendarClientFactory.java       | 40 ++++++++++++++++----
 .../calendar/GoogleCalendarClientFactory.java   |  4 +-
 .../calendar/GoogleCalendarComponent.java       |  9 ++++-
 .../calendar/GoogleCalendarConfiguration.java   | 32 ++++++++++++++++
 .../src/test/resources/test-options.properties  |  2 +
 5 files changed, 76 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/68bd495b/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/BatchGoogleCalendarClientFactory.java
----------------------------------------------------------------------
diff --git 
a/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/BatchGoogleCalendarClientFactory.java
 
b/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/BatchGoogleCalendarClientFactory.java
index 34577fd..67d94f7 100644
--- 
a/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/BatchGoogleCalendarClientFactory.java
+++ 
b/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/BatchGoogleCalendarClientFactory.java
@@ -16,18 +16,23 @@
  */
 package org.apache.camel.component.google.calendar;
 
+import java.io.File;
 import java.util.Collection;
 
 import com.google.api.client.auth.oauth2.Credential;
 import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
+import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
+import com.google.api.client.http.HttpTransport;
 import com.google.api.client.http.javanet.NetHttpTransport;
 import com.google.api.client.json.jackson2.JacksonFactory;
 import com.google.api.services.calendar.Calendar;
 
+
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class BatchGoogleCalendarClientFactory implements 
GoogleCalendarClientFactory {
+
     private static final Logger LOG = 
LoggerFactory.getLogger(BatchGoogleCalendarClientFactory.class);
     private NetHttpTransport transport;
     private JacksonFactory jsonFactory;
@@ -38,16 +43,23 @@ public class BatchGoogleCalendarClientFactory implements 
GoogleCalendarClientFac
     }
 
     @Override
-    public Calendar makeClient(String clientId, String clientSecret, 
Collection<String> scopes, String applicationName, String refreshToken, String 
accessToken) {
+    public Calendar makeClient(String clientId, String clientSecret,
+            Collection<String> scopes, String applicationName, String 
refreshToken,
+            String accessToken, String emailAddress, String p12FileName) {
+                               
         Credential credential;
         try {
-            credential = authorize(clientId, clientSecret, scopes);
-
-            if (refreshToken != null && !"".equals(refreshToken)) {
-                credential.setRefreshToken(refreshToken);
-            } 
-            if (accessToken != null && !"".equals(accessToken)) {
-                credential.setAccessToken(accessToken);
+         // if emailAddress and p12FileName values are present, assume Google 
Service Account
+            if (null != emailAddress && !"".equals(emailAddress) && null != 
p12FileName && !"".equals(p12FileName)) {
+                credential = authorizeServiceAccount(emailAddress, 
p12FileName, scopes);
+            } else {
+                credential = authorize(clientId, clientSecret, scopes);
+                if (refreshToken != null && !"".equals(refreshToken)) {
+                    credential.setRefreshToken(refreshToken);
+                } 
+                if (accessToken != null && !"".equals(accessToken)) {
+                    credential.setAccessToken(accessToken);
+                }
             }
             return new Calendar.Builder(transport, jsonFactory, 
credential).setApplicationName(applicationName).build();
         } catch (Exception e) {
@@ -65,4 +77,16 @@ public class BatchGoogleCalendarClientFactory implements 
GoogleCalendarClientFac
             .setClientSecrets(clientId, clientSecret)
             .build();
     }
+    
+    private Credential authorizeServiceAccount(String emailAddress, String 
p12FileName, Collection<String> scopes) throws Exception {
+        HttpTransport httpTransport = 
GoogleNetHttpTransport.newTrustedTransport();
+        GoogleCredential credential = new GoogleCredential.Builder()
+                .setTransport(httpTransport)
+                .setJsonFactory(jsonFactory)
+                .setServiceAccountId(emailAddress)
+                .setServiceAccountPrivateKeyFromP12File(new File(p12FileName))
+                .setServiceAccountScopes(scopes)
+                .build();
+        return credential;
+    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/68bd495b/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarClientFactory.java
----------------------------------------------------------------------
diff --git 
a/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarClientFactory.java
 
b/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarClientFactory.java
index 5f4dd30..74347a2 100644
--- 
a/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarClientFactory.java
+++ 
b/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarClientFactory.java
@@ -22,6 +22,8 @@ import com.google.api.services.calendar.Calendar;
 
 public interface GoogleCalendarClientFactory {
 
-    Calendar makeClient(String clientId, String clientSecret, 
Collection<String> scopes, String applicationName, String refreshToken, String 
accessToken);
+    Calendar makeClient(String clientId, String clientSecret, 
Collection<String> scopes,
+            String applicationName, String refreshToken, String accessToken,
+            String emailAddress, String p12FileName);
 
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/68bd495b/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarComponent.java
----------------------------------------------------------------------
diff --git 
a/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarComponent.java
 
b/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarComponent.java
index 7a101aa..6cd83dc 100644
--- 
a/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarComponent.java
+++ 
b/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarComponent.java
@@ -47,8 +47,11 @@ public class GoogleCalendarComponent extends 
AbstractApiComponent<GoogleCalendar
 
     public Calendar getClient() {
         if (client == null) {
-            client = 
getClientFactory().makeClient(configuration.getClientId(), 
configuration.getClientSecret(), configuration.getScopes(), 
-                configuration.getApplicationName(), 
configuration.getRefreshToken(), configuration.getAccessToken());
+            client = 
getClientFactory().makeClient(configuration.getClientId(), 
+                                                   
configuration.getClientSecret(), configuration.getScopes(), 
+                                                   
configuration.getApplicationName(), configuration.getRefreshToken(), 
+                                                   
configuration.getAccessToken(), configuration.getEmailAddress(),
+                                                   
configuration.getP12FileName());
         }
         return client;
     }
@@ -67,6 +70,7 @@ public class GoogleCalendarComponent extends 
AbstractApiComponent<GoogleCalendar
 
     /**
      * To use the shared configuration
+     * @param configuration
      */
     @Override
     public void setConfiguration(GoogleCalendarConfiguration configuration) {
@@ -76,6 +80,7 @@ public class GoogleCalendarComponent extends 
AbstractApiComponent<GoogleCalendar
     /**
      * To use the GoogleCalendarClientFactory as factory for creating the 
client.
      * Will by default use {@link BatchGoogleCalendarClientFactory}
+     * @param clientFactory
      */
     public void setClientFactory(GoogleCalendarClientFactory clientFactory) {
         this.clientFactory = clientFactory;

http://git-wip-us.apache.org/repos/asf/camel/blob/68bd495b/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarConfiguration.java
----------------------------------------------------------------------
diff --git 
a/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarConfiguration.java
 
b/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarConfiguration.java
index 8ebf320..ac04cba 100644
--- 
a/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarConfiguration.java
+++ 
b/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarConfiguration.java
@@ -47,6 +47,9 @@ public class GoogleCalendarConfiguration {
     private String clientId;
 
     @UriParam
+    private String emailAddress;
+
+    @UriParam
     private String clientSecret;
 
     @UriParam
@@ -58,12 +61,16 @@ public class GoogleCalendarConfiguration {
     @UriParam
     private String applicationName;
 
+    @UriParam
+    private String p12FileName;
+
     public GoogleCalendarApiName getApiName() {
         return apiName;
     }
 
     /**
      * What kind of operation to perform
+     * @param apiName
      */
     public void setApiName(GoogleCalendarApiName apiName) {
         this.apiName = apiName;
@@ -75,6 +82,7 @@ public class GoogleCalendarConfiguration {
 
     /**
      * What sub operation to use for the selected operation
+     * @param methodName
      */
     public void setMethodName(String methodName) {
         this.methodName = methodName;
@@ -86,17 +94,29 @@ public class GoogleCalendarConfiguration {
 
     /**
      * Client ID of the calendar application
+     * @param clientId
      */
     public void setClientId(String clientId) {
         this.clientId = clientId;
     }
 
+    public String getEmailAddress() {
+        return emailAddress;
+    }
+
+    public void setEmailAddress(String emailAddress) {
+        this.emailAddress = emailAddress;
+    }
+
+
+
     public String getClientSecret() {
         return clientSecret;
     }
 
     /**
      * Client secret of the calendar application
+     * @param clientSecret
      */
     public void setClientSecret(String clientSecret) {
         this.clientSecret = clientSecret;
@@ -108,6 +128,7 @@ public class GoogleCalendarConfiguration {
 
     /**
      * OAuth 2 access token. This typically expires after an hour so 
refreshToken is recommended for long term usage.
+     * @param accessToken
      */
     public void setAccessToken(String accessToken) {
         this.accessToken = accessToken;
@@ -119,6 +140,7 @@ public class GoogleCalendarConfiguration {
 
     /**
      * OAuth 2 refresh token. Using this, the Google Calendar component can 
obtain a new accessToken whenever the current one expires - a necessity if the 
application is long-lived.
+     * @param refreshToken
      */
     public void setRefreshToken(String refreshToken) {
         this.refreshToken = refreshToken;
@@ -130,6 +152,7 @@ public class GoogleCalendarConfiguration {
 
     /**
      * Google calendar application name. Example would be 
"camel-google-calendar/1.0"
+     * @param applicationName
      */
     public void setApplicationName(String applicationName) {
         this.applicationName = applicationName;
@@ -141,9 +164,18 @@ public class GoogleCalendarConfiguration {
 
     /**
      * Specifies the level of permissions you want a calendar application to 
have to a user account. See 
https://developers.google.com/google-apps/calendar/auth for more info.
+     * @param scopes
      */
     public void setScopes(List<String> scopes) {
         this.scopes = scopes;
     }
 
+    public String getP12FileName() {
+        return p12FileName;
+    }
+
+    public void setP12FileName(String p12FileName) {
+        this.p12FileName = p12FileName;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/68bd495b/components/camel-google-calendar/src/test/resources/test-options.properties
----------------------------------------------------------------------
diff --git 
a/components/camel-google-calendar/src/test/resources/test-options.properties 
b/components/camel-google-calendar/src/test/resources/test-options.properties
index b24be6f..301fce0 100644
--- 
a/components/camel-google-calendar/src/test/resources/test-options.properties
+++ 
b/components/camel-google-calendar/src/test/resources/test-options.properties
@@ -24,3 +24,5 @@ clientSecret=
 applicationName=camel-google-calendar/1.0
 #accessToken=
 refreshToken=
+emailAddress=
+p12FileName=

Reply via email to