Add batch CF for refreshToken scenarios

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

Branch: refs/heads/master
Commit: 1bfb63fd5b844ca5275afe2a3f66561e4e1d6b97
Parents: 30e1fdb
Author: Jonathan Anstey <jans...@gmail.com>
Authored: Mon Sep 8 00:24:47 2014 -0230
Committer: Jonathan Anstey <jans...@gmail.com>
Committed: Mon Sep 8 09:06:11 2014 -0230

----------------------------------------------------------------------
 .../drive/BatchGoogleDriveClientFactory.java    | 59 +++++++++++++++++
 .../drive/DefaultGoogleDriveClientFactory.java  | 63 ------------------
 .../google/drive/GoogleDriveClientFactory.java  |  2 +-
 .../google/drive/GoogleDriveConfiguration.java  |  2 -
 .../google/drive/GoogleDriveEndpoint.java       |  4 +-
 .../InteractiveGoogleDriveClientFactory.java    | 69 ++++++++++++++++++++
 6 files changed, 131 insertions(+), 68 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/1bfb63fd/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/BatchGoogleDriveClientFactory.java
----------------------------------------------------------------------
diff --git 
a/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/BatchGoogleDriveClientFactory.java
 
b/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/BatchGoogleDriveClientFactory.java
new file mode 100644
index 0000000..bb9c883
--- /dev/null
+++ 
b/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/BatchGoogleDriveClientFactory.java
@@ -0,0 +1,59 @@
+/**
+ * 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.component.google.drive;
+
+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.http.javanet.NetHttpTransport;
+import com.google.api.client.json.jackson2.JacksonFactory;
+import com.google.api.services.drive.Drive;
+
+public class BatchGoogleDriveClientFactory implements GoogleDriveClientFactory 
{
+    private NetHttpTransport transport;
+    private JacksonFactory jsonFactory;
+
+    public BatchGoogleDriveClientFactory() {
+        this.transport = new NetHttpTransport();
+        this.jsonFactory = new JacksonFactory();
+    }
+
+    @Override
+    public Drive makeClient(String clientId, String clientSecret, 
Collection<String> scopes, String applicationName, String refreshToken) {
+        Credential credential;
+        try {
+            credential = authorize(clientId, clientSecret, scopes, 
refreshToken);
+            return new Drive.Builder(transport, jsonFactory, 
credential).setApplicationName(applicationName).build();
+        } catch (Exception e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+        return null;
+    }
+
+    // Authorizes the installed application to access user's protected data.
+    private Credential authorize(String clientId, String clientSecret, 
Collection<String> scopes, String refreshToken) throws Exception {
+        // authorize
+        return new GoogleCredential.Builder()
+        .setJsonFactory(jsonFactory)
+        .setTransport(transport)
+        .setClientSecrets(clientId, clientSecret)
+        .build()
+        .setRefreshToken(refreshToken);
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/1bfb63fd/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/DefaultGoogleDriveClientFactory.java
----------------------------------------------------------------------
diff --git 
a/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/DefaultGoogleDriveClientFactory.java
 
b/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/DefaultGoogleDriveClientFactory.java
deleted file mode 100644
index da9e796..0000000
--- 
a/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/DefaultGoogleDriveClientFactory.java
+++ /dev/null
@@ -1,63 +0,0 @@
-/**
- * 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.component.google.drive;
-
-import java.util.Collection;
-
-import com.google.api.client.auth.oauth2.Credential;
-import 
com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
-import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
-import 
com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
-import com.google.api.client.http.javanet.NetHttpTransport;
-import com.google.api.client.json.jackson2.JacksonFactory;
-import com.google.api.client.util.store.FileDataStoreFactory;
-import com.google.api.services.drive.Drive;
-
-public class DefaultGoogleDriveClientFactory implements 
GoogleDriveClientFactory {
-    private NetHttpTransport transport;
-    private JacksonFactory jsonFactory;
-    private FileDataStoreFactory dataStoreFactory;
-    private static final java.io.File DATA_STORE_DIR = new 
java.io.File(".google_drive");
-
-    public DefaultGoogleDriveClientFactory() {
-        this.transport = new NetHttpTransport();
-        this.jsonFactory = new JacksonFactory();
-    }
-
-    @Override
-    public Drive makeClient(String clientId, String clientSecret, 
Collection<String> scopes, String applicationName) {
-        Credential credential;
-        try {
-            credential = authorize(clientId, clientSecret, scopes);
-            return new Drive.Builder(transport, jsonFactory, 
credential).setApplicationName(applicationName).build();
-        } catch (Exception e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
-        return null;
-    }
-
-    // Authorizes the installed application to access user's protected data.
-    private Credential authorize(String clientId, String clientSecret, 
Collection<String> scopes) throws Exception {
-        dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
-        // set up authorization code flow
-        // TODO refresh token support too
-        GoogleAuthorizationCodeFlow flow = new 
GoogleAuthorizationCodeFlow.Builder(transport, jsonFactory, clientId, 
clientSecret, scopes).setDataStoreFactory(dataStoreFactory).build();
-        // authorize
-        return new AuthorizationCodeInstalledApp(flow, new 
LocalServerReceiver()).authorize("user");
-    }
-}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/1bfb63fd/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveClientFactory.java
----------------------------------------------------------------------
diff --git 
a/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveClientFactory.java
 
b/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveClientFactory.java
index b183279..10a0089 100644
--- 
a/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveClientFactory.java
+++ 
b/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveClientFactory.java
@@ -22,6 +22,6 @@ import com.google.api.services.drive.Drive;
 
 public interface GoogleDriveClientFactory {
 
-    public abstract Drive makeClient(String clientId, String clientSecret, 
Collection<String> scopes, String applicationName);
+    public abstract Drive makeClient(String clientId, String clientSecret, 
Collection<String> scopes, String applicationName, String refreshToken);
 
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/1bfb63fd/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveConfiguration.java
----------------------------------------------------------------------
diff --git 
a/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveConfiguration.java
 
b/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveConfiguration.java
index eb0db54..2f8b19c 100644
--- 
a/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveConfiguration.java
+++ 
b/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveConfiguration.java
@@ -16,8 +16,6 @@
  */
 package org.apache.camel.component.google.drive;
 
-import java.util.Map;
-
 import org.apache.camel.spi.UriParam;
 import org.apache.camel.spi.UriParams;
 

http://git-wip-us.apache.org/repos/asf/camel/blob/1bfb63fd/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveEndpoint.java
----------------------------------------------------------------------
diff --git 
a/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveEndpoint.java
 
b/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveEndpoint.java
index 2406493..d69a458 100644
--- 
a/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveEndpoint.java
+++ 
b/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveEndpoint.java
@@ -114,7 +114,7 @@ public class GoogleDriveEndpoint extends 
AbstractApiEndpoint<GoogleDriveApiName,
     
     private Drive getClient() {
         if (client == null) {
-            client = 
getClientFactory().makeClient(configuration.getClientId(), 
configuration.getClientSecret(), DEFAULT_SCOPES, 
configuration.getApplicationName());
+            client = 
getClientFactory().makeClient(configuration.getClientId(), 
configuration.getClientSecret(), DEFAULT_SCOPES, 
configuration.getApplicationName(), configuration.getRefreshToken());
         }
         return client;
     }
@@ -126,7 +126,7 @@ public class GoogleDriveEndpoint extends 
AbstractApiEndpoint<GoogleDriveApiName,
 
     public GoogleDriveClientFactory getClientFactory() {
         if (clientFactory == null) {
-            clientFactory = new DefaultGoogleDriveClientFactory();
+            clientFactory = new InteractiveGoogleDriveClientFactory();
         }
         return clientFactory;
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/1bfb63fd/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/InteractiveGoogleDriveClientFactory.java
----------------------------------------------------------------------
diff --git 
a/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/InteractiveGoogleDriveClientFactory.java
 
b/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/InteractiveGoogleDriveClientFactory.java
new file mode 100644
index 0000000..a3965ac
--- /dev/null
+++ 
b/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/InteractiveGoogleDriveClientFactory.java
@@ -0,0 +1,69 @@
+/**
+ * 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.component.google.drive;
+
+import java.util.Collection;
+
+import com.google.api.client.auth.oauth2.Credential;
+import 
com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
+import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
+import 
com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
+import com.google.api.client.http.javanet.NetHttpTransport;
+import com.google.api.client.json.jackson2.JacksonFactory;
+import com.google.api.client.util.store.FileDataStoreFactory;
+import com.google.api.services.drive.Drive;
+
+public class InteractiveGoogleDriveClientFactory implements 
GoogleDriveClientFactory {
+    private NetHttpTransport transport;
+    private JacksonFactory jsonFactory;
+    private FileDataStoreFactory dataStoreFactory;
+    private static final java.io.File DATA_STORE_DIR = new 
java.io.File(".google_drive");
+
+    public InteractiveGoogleDriveClientFactory() {
+        this.transport = new NetHttpTransport();
+        this.jsonFactory = new JacksonFactory();
+    }
+
+    @Override
+    public Drive makeClient(String clientId, String clientSecret, 
Collection<String> scopes, String applicationName, String refreshToken) {
+        Credential credential;
+        try {
+            credential = authorize(clientId, clientSecret, scopes);
+            return new Drive.Builder(transport, jsonFactory, 
credential).setApplicationName(applicationName).build();
+        } catch (Exception e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+        return null;
+    }
+
+    /**
+     * This method interactively creates the necessary authorization tokens on 
first run, 
+     * and stores the tokens in the data store. Subsequent runs will no longer 
require interactivity
+     * as long as the credentials file is not removed.
+     */
+    private Credential authorize(String clientId, String clientSecret, 
Collection<String> scopes) throws Exception {
+        dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
+        // set up authorization code flow
+        GoogleAuthorizationCodeFlow flow = new 
GoogleAuthorizationCodeFlow.Builder(transport, jsonFactory, clientId, 
clientSecret, scopes)
+          .setDataStoreFactory(dataStoreFactory)
+          .setAccessType("offline")
+          .build();
+        // authorize
+        return new AuthorizationCodeInstalledApp(flow, new 
LocalServerReceiver()).authorize("user");
+    }
+}
\ No newline at end of file

Reply via email to