CAMEL-11398: Google components should throw more friendly exception when the required credentials lack
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/48fc9b29 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/48fc9b29 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/48fc9b29 Branch: refs/heads/camel-2.19.x Commit: 48fc9b29c36d820b08ec2561d0f051e9d0a03560 Parents: e9531a3 Author: Tadayoshi Sato <sato.tadayo...@gmail.com> Authored: Wed Jun 14 21:10:28 2017 +0900 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed Jun 14 19:38:59 2017 +0200 ---------------------------------------------------------------------- components/camel-google-calendar/pom.xml | 2 +- .../calendar/BatchGoogleCalendarClientFactory.java | 17 ++++++++++++----- components/camel-google-drive/pom.xml | 2 +- .../drive/BatchGoogleDriveClientFactory.java | 12 +++++++----- components/camel-google-mail/pom.xml | 2 +- .../google/mail/BatchGoogleMailClientFactory.java | 12 +++++++----- .../google/mail/GmailConfigurationTest.java | 3 +-- 7 files changed, 30 insertions(+), 20 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/48fc9b29/components/camel-google-calendar/pom.xml ---------------------------------------------------------------------- diff --git a/components/camel-google-calendar/pom.xml b/components/camel-google-calendar/pom.xml index 401b3f3..b871594 100644 --- a/components/camel-google-calendar/pom.xml +++ b/components/camel-google-calendar/pom.xml @@ -259,7 +259,7 @@ <childDelegation>false</childDelegation> <useFile>true</useFile> <forkCount>1</forkCount> - <reuseForks>true</reuseForks> + <reuseForks>true</reuseForks> <forkedProcessTimeoutInSeconds>300</forkedProcessTimeoutInSeconds> <excludes> <exclude>**/*XXXTest.java</exclude> http://git-wip-us.apache.org/repos/asf/camel/blob/48fc9b29/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 5783e89..cae5b52 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 @@ -26,6 +26,7 @@ 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.apache.camel.RuntimeCamelException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -44,11 +45,18 @@ public class BatchGoogleCalendarClientFactory implements GoogleCalendarClientFac public Calendar makeClient(String clientId, String clientSecret, Collection<String> scopes, String applicationName, String refreshToken, String accessToken, String emailAddress, String p12FileName, String user) { + boolean serviceAccount = false; + // if emailAddress and p12FileName values are present, assume Google Service Account + if (null != emailAddress && !"".equals(emailAddress) && null != p12FileName && !"".equals(p12FileName)) { + serviceAccount = true; + } + if (!serviceAccount && (clientId == null || clientSecret == null)) { + throw new IllegalArgumentException("clientId and clientSecret are required to create Google Calendar client."); + } - Credential credential; try { - // if emailAddress and p12FileName values are present, assume Google Service Account - if (null != emailAddress && !"".equals(emailAddress) && null != p12FileName && !"".equals(p12FileName)) { + Credential credential; + if (serviceAccount) { credential = authorizeServiceAccount(emailAddress, p12FileName, scopes, user); } else { credential = authorize(clientId, clientSecret, scopes); @@ -61,9 +69,8 @@ public class BatchGoogleCalendarClientFactory implements GoogleCalendarClientFac } return new Calendar.Builder(transport, jsonFactory, credential).setApplicationName(applicationName).build(); } catch (Exception e) { - LOG.error("Could not create Google Drive client.", e); + throw new RuntimeCamelException("Could not create Google Calendar client.", e); } - return null; } // Authorizes the installed application to access user's protected data. http://git-wip-us.apache.org/repos/asf/camel/blob/48fc9b29/components/camel-google-drive/pom.xml ---------------------------------------------------------------------- diff --git a/components/camel-google-drive/pom.xml b/components/camel-google-drive/pom.xml index 10f8363..1e500ae 100644 --- a/components/camel-google-drive/pom.xml +++ b/components/camel-google-drive/pom.xml @@ -284,7 +284,7 @@ <childDelegation>false</childDelegation> <useFile>true</useFile> <forkCount>1</forkCount> - <reuseForks>true</reuseForks> + <reuseForks>true</reuseForks> <forkedProcessTimeoutInSeconds>300</forkedProcessTimeoutInSeconds> <excludes> <exclude>**/*XXXTest.java</exclude> http://git-wip-us.apache.org/repos/asf/camel/blob/48fc9b29/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 index 9166aea..5248e23 100644 --- 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 @@ -24,6 +24,7 @@ import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.drive.Drive; +import org.apache.camel.RuntimeCamelException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -39,9 +40,11 @@ public class BatchGoogleDriveClientFactory implements GoogleDriveClientFactory { @Override public Drive makeClient(String clientId, String clientSecret, Collection<String> scopes, String applicationName, String refreshToken, String accessToken) { - Credential credential; + if (clientId == null || clientSecret == null) { + throw new IllegalArgumentException("clientId and clientSecret are required to create Google Drive client."); + } try { - credential = authorize(clientId, clientSecret, scopes); + Credential credential = authorize(clientId, clientSecret, scopes); if (refreshToken != null && !"".equals(refreshToken)) { credential.setRefreshToken(refreshToken); @@ -51,9 +54,8 @@ public class BatchGoogleDriveClientFactory implements GoogleDriveClientFactory { } return new Drive.Builder(transport, jsonFactory, credential).setApplicationName(applicationName).build(); } catch (Exception e) { - LOG.error("Could not create Google Drive client.", e); + throw new RuntimeCamelException("Could not create Google Drive client.", e); } - return null; } // Authorizes the installed application to access user's protected data. @@ -65,4 +67,4 @@ public class BatchGoogleDriveClientFactory implements GoogleDriveClientFactory { .setClientSecrets(clientId, clientSecret) .build(); } -} \ No newline at end of file +} http://git-wip-us.apache.org/repos/asf/camel/blob/48fc9b29/components/camel-google-mail/pom.xml ---------------------------------------------------------------------- diff --git a/components/camel-google-mail/pom.xml b/components/camel-google-mail/pom.xml index fb06e6a..5a2910d 100644 --- a/components/camel-google-mail/pom.xml +++ b/components/camel-google-mail/pom.xml @@ -296,7 +296,7 @@ <childDelegation>false</childDelegation> <useFile>true</useFile> <forkCount>1</forkCount> - <reuseForks>true</reuseForks> + <reuseForks>true</reuseForks> <forkedProcessTimeoutInSeconds>300</forkedProcessTimeoutInSeconds> <excludes> <exclude>**/*XXXTest.java</exclude> http://git-wip-us.apache.org/repos/asf/camel/blob/48fc9b29/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/BatchGoogleMailClientFactory.java ---------------------------------------------------------------------- diff --git a/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/BatchGoogleMailClientFactory.java b/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/BatchGoogleMailClientFactory.java index ea6b085..8ba7dbd 100644 --- a/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/BatchGoogleMailClientFactory.java +++ b/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/BatchGoogleMailClientFactory.java @@ -24,6 +24,7 @@ import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.gmail.Gmail; +import org.apache.camel.RuntimeCamelException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -39,9 +40,11 @@ public class BatchGoogleMailClientFactory implements GoogleMailClientFactory { @Override public Gmail makeClient(String clientId, String clientSecret, Collection<String> scopes, String applicationName, String refreshToken, String accessToken) { - Credential credential; + if (clientId == null || clientSecret == null) { + throw new IllegalArgumentException("clientId and clientSecret are required to create Gmail client."); + } try { - credential = authorize(clientId, clientSecret, scopes); + Credential credential = authorize(clientId, clientSecret, scopes); if (refreshToken != null && !"".equals(refreshToken)) { credential.setRefreshToken(refreshToken); @@ -51,9 +54,8 @@ public class BatchGoogleMailClientFactory implements GoogleMailClientFactory { } return new Gmail.Builder(transport, jsonFactory, credential).setApplicationName(applicationName).build(); } catch (Exception e) { - LOG.error("Could not create Google Drive client.", e); + throw new RuntimeCamelException("Could not create Gmail client.", e); } - return null; } // Authorizes the installed application to access user's protected data. @@ -61,4 +63,4 @@ public class BatchGoogleMailClientFactory implements GoogleMailClientFactory { // authorize return new GoogleCredential.Builder().setJsonFactory(jsonFactory).setTransport(transport).setClientSecrets(clientId, clientSecret).build(); } -} \ No newline at end of file +} http://git-wip-us.apache.org/repos/asf/camel/blob/48fc9b29/components/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/GmailConfigurationTest.java ---------------------------------------------------------------------- diff --git a/components/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/GmailConfigurationTest.java b/components/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/GmailConfigurationTest.java index cc53619..d7902b2 100644 --- a/components/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/GmailConfigurationTest.java +++ b/components/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/GmailConfigurationTest.java @@ -26,8 +26,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * Test class for {@link com.google.api.services.gmail.Gmail$Users$Messages} - * APIs. + * Test class for {@link GoogleMailConfiguration}. */ public class GmailConfigurationTest extends AbstractGoogleMailTestSupport {