minor refactorings
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/77848e1c Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/77848e1c Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/77848e1c Branch: refs/heads/master Commit: 77848e1c4517c50e49f6acece774d929a06003ea Parents: 68530db Author: Jonathan Anstey <jans...@gmail.com> Authored: Thu Sep 4 00:28:56 2014 -0230 Committer: Jonathan Anstey <jans...@gmail.com> Committed: Mon Sep 8 09:06:10 2014 -0230 ---------------------------------------------------------------------- .../drive/DefaultGoogleDriveClientFactory.java | 71 ++++++++++++++++++ .../google/drive/GoogleDriveClientFactory.java | 11 +++ .../google/drive/GoogleDriveConsumer.java | 5 +- .../google/drive/GoogleDriveEndpoint.java | 75 +++++--------------- .../google/drive/GoogleDriveProducer.java | 5 +- .../src/test/resources/test-options.properties | 6 +- 6 files changed, 104 insertions(+), 69 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/77848e1c/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 new file mode 100644 index 0000000..5ad6b17 --- /dev/null +++ b/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/DefaultGoogleDriveClientFactory.java @@ -0,0 +1,71 @@ +/** + * 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; + // TODO Directory to store user credentials + private static final java.io.File DATA_STORE_DIR = new java.io.File(System.getProperty("user.home"), ".store/drive_sample"); + + public DefaultGoogleDriveClientFactory() { + this.transport = new NetHttpTransport(); + this.jsonFactory = new JacksonFactory(); + } + + /* + * (non-Javadoc) + * + * @see + * org.apache.camel.component.google.drive.GoogleDriveClientFactory#makeClient + * (java.lang.String, java.lang.String, java.util.Collection) + */ + @Override + public Drive makeClient(String clientId, String clientSecret, Collection<String> scopes) { + Credential credential; + try { + credential = authorize(clientId, clientSecret, scopes); + return new Drive.Builder(transport, jsonFactory, credential).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/77848e1c/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 new file mode 100644 index 0000000..669c253 --- /dev/null +++ b/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveClientFactory.java @@ -0,0 +1,11 @@ +package org.apache.camel.component.google.drive; + +import java.util.Collection; + +import com.google.api.services.drive.Drive; + +public interface GoogleDriveClientFactory { + + public abstract Drive makeClient(String clientId, String clientSecret, Collection<String> scopes); + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/77848e1c/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveConsumer.java ---------------------------------------------------------------------- diff --git a/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveConsumer.java b/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveConsumer.java index 3490c0c..43ef2d4 100644 --- a/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveConsumer.java +++ b/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveConsumer.java @@ -39,13 +39,10 @@ public class GoogleDriveConsumer extends AbstractApiConsumer<GoogleDriveApiName, @Override protected Object doInvokeMethod(Map<String, Object> properties) throws RuntimeCamelException { AbstractGoogleClientRequest request = (AbstractGoogleClientRequest) super.doInvokeMethod(properties); - // TODO set any generic params, like OAuth token, etc. try { return request.execute(); } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + throw new RuntimeCamelException(e); } - return null; } } http://git-wip-us.apache.org/repos/asf/camel/blob/77848e1c/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 852438f..e2b5dfa 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 @@ -17,7 +17,6 @@ package org.apache.camel.component.google.drive; import java.util.Arrays; -import java.util.Collections; import java.util.List; import java.util.Map; @@ -33,13 +32,6 @@ import org.apache.camel.component.google.drive.internal.GoogleDriveApiName; import org.apache.camel.component.google.drive.internal.GoogleDriveConstants; import org.apache.camel.component.google.drive.internal.GoogleDrivePropertiesHelper; -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; import com.google.api.services.drive.DriveScopes; @@ -51,14 +43,7 @@ public class GoogleDriveEndpoint extends AbstractApiEndpoint<GoogleDriveApiName, private Object apiProxy; private Drive client; - // TODO these need to be configurable - private NetHttpTransport transport = new NetHttpTransport(); - private JacksonFactory jsonFactory = new JacksonFactory(); - private FileDataStoreFactory dataStoreFactory; - - // TODO Directory to store user credentials - private static final java.io.File DATA_STORE_DIR = new java.io.File( - System.getProperty("user.home"), ".store/drive_sample"); + private GoogleDriveClientFactory clientFactory; private static final List<String> DEFAULT_SCOPES = Arrays.asList(DriveScopes.DRIVE_FILE, DriveScopes.DRIVE_APPS_READONLY, DriveScopes.DRIVE_METADATA_READONLY, DriveScopes.DRIVE); @@ -66,7 +51,6 @@ public class GoogleDriveEndpoint extends AbstractApiEndpoint<GoogleDriveApiName, public GoogleDriveEndpoint(String uri, GoogleDriveComponent component, GoogleDriveApiName apiName, String methodName, GoogleDriveConfiguration endpointConfiguration) { super(uri, component, apiName, methodName, GoogleDriveApiCollection.getCollection().getHelper(apiName), endpointConfiguration); - } public Producer createProducer() throws Exception { @@ -92,25 +76,9 @@ public class GoogleDriveEndpoint extends AbstractApiEndpoint<GoogleDriveApiName, protected String getThreadProfileName() { return GoogleDriveConstants.THREAD_PROFILE_NAME; } - - // Authorizes the installed application to access user's protected data. - private Credential authorize() 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, configuration.getClientId(), configuration.getClientSecret(), - DEFAULT_SCOPES).setDataStoreFactory(dataStoreFactory) - .build(); - // authorize - return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user"); - } - @Override protected void afterConfigureProperties() { - // TODO create API proxy, set connection properties, etc. - switch ((GoogleDriveApiName) apiName) { case DRIVE_FILES: apiProxy = getClient().files(); @@ -132,20 +100,7 @@ public class GoogleDriveEndpoint extends AbstractApiEndpoint<GoogleDriveApiName, break; case DRIVE_PROPERTIES: apiProxy = getClient().properties(); - break; -// Still need to support these drive APIs -// case DRIVE_CHANNELS: -// apiProxy = getClient().channels(); -// break; -// case DRIVE_CHILDREN: -// apiProxy = getClient().children(); -// break; -// case DRIVE_PARENTS: -// apiProxy = getClient().parents(); -// break; -// case DRIVE_REALTIME: -// apiProxy = getClient().realtime(); -// break; + break; case DRIVE_REPLIES: apiProxy = getClient().replies(); break; @@ -156,23 +111,27 @@ public class GoogleDriveEndpoint extends AbstractApiEndpoint<GoogleDriveApiName, throw new IllegalArgumentException("Invalid API name " + apiName); } } - - public Drive getClient() { + + private Drive getClient() { if (client == null) { - Credential credential; - try { - credential = authorize(); - client = new Drive.Builder(transport, jsonFactory, credential).build(); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + getClientFactory().makeClient(configuration.getClientId(), configuration.getClientSecret(), DEFAULT_SCOPES); } return client; } - + @Override public Object getApiProxy(ApiMethod method, Map<String, Object> args) { return apiProxy; } + + public GoogleDriveClientFactory getClientFactory() { + if (clientFactory == null) { + clientFactory = new DefaultGoogleDriveClientFactory(); + } + return clientFactory; + } + + public void setClientFactory(GoogleDriveClientFactory clientFactory) { + this.clientFactory = clientFactory; + } } http://git-wip-us.apache.org/repos/asf/camel/blob/77848e1c/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveProducer.java b/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveProducer.java index 485cf61..3579453 100644 --- a/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveProducer.java +++ b/components/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveProducer.java @@ -40,14 +40,11 @@ public class GoogleDriveProducer extends AbstractApiProducer<GoogleDriveApiName, @Override protected Object doInvokeMethod(ApiMethod method, Map<String, Object> properties) throws RuntimeCamelException { AbstractGoogleClientRequest request = (AbstractGoogleClientRequest) super.doInvokeMethod(method, properties); - // TODO set any generic params, like OAuth token, etc. try { return request.execute(); } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + throw new RuntimeCamelException(e); } - return null; } protected String getThreadProfileName() { http://git-wip-us.apache.org/repos/asf/camel/blob/77848e1c/components/camel-google-drive/src/test/resources/test-options.properties ---------------------------------------------------------------------- diff --git a/components/camel-google-drive/src/test/resources/test-options.properties b/components/camel-google-drive/src/test/resources/test-options.properties index 8e21b12..710ec6a 100644 --- a/components/camel-google-drive/src/test/resources/test-options.properties +++ b/components/camel-google-drive/src/test/resources/test-options.properties @@ -19,6 +19,6 @@ ## Login properties for Google Drive Component ##################################### ## Application client id and secret -clientId= -clientSecret= -applicationName= +clientId=907911893802-1tvr7lrt5f8k4sco2rigdo5kf4lfht4d.apps.googleusercontent.com +clientSecret=g53qS_MTceqexaoZC9YtTOI4 +applicationName=camel-google-drive/1.0 \ No newline at end of file