This is an automated email from the ASF dual-hosted git repository. acosentino pushed a commit to branch 23040 in repository https://gitbox.apache.org/repos/asf/camel.git
commit e300084dbd36d56f26073f11e853e6c682a58c00 Author: Andrea Cosentino <[email protected]> AuthorDate: Wed Feb 25 10:35:00 2026 +0100 CAMEL-23040 - Generalize Google services authentication with common module - Google Mail Signed-off-by: Andrea Cosentino <[email protected]> --- components/camel-google/camel-google-mail/pom.xml | 4 ++ .../google/mail/BatchGoogleMailClientFactory.java | 51 +++++++--------------- .../google/mail/GoogleMailConfiguration.java | 10 ++++- .../mail/stream/GoogleMailStreamConfiguration.java | 10 ++++- 4 files changed, 38 insertions(+), 37 deletions(-) diff --git a/components/camel-google/camel-google-mail/pom.xml b/components/camel-google/camel-google-mail/pom.xml index 87d690386a78..08a202cab843 100644 --- a/components/camel-google/camel-google-mail/pom.xml +++ b/components/camel-google/camel-google-mail/pom.xml @@ -41,6 +41,10 @@ </properties> <dependencies> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-google-common</artifactId> + </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-support</artifactId> diff --git a/components/camel-google/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/BatchGoogleMailClientFactory.java b/components/camel-google/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/BatchGoogleMailClientFactory.java index 5ad8ec1c5051..078b53f6f24f 100644 --- a/components/camel-google/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/BatchGoogleMailClientFactory.java +++ b/components/camel-google/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/BatchGoogleMailClientFactory.java @@ -16,17 +16,15 @@ */ package org.apache.camel.component.google.mail; -import java.io.IOException; 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.gmail.Gmail; import org.apache.camel.CamelContext; import org.apache.camel.RuntimeCamelException; -import org.apache.camel.support.ResourceHelper; +import org.apache.camel.component.google.common.GoogleCredentialsHelper; public class BatchGoogleMailClientFactory implements GoogleMailClientFactory { private NetHttpTransport transport; @@ -45,27 +43,21 @@ public class BatchGoogleMailClientFactory implements GoogleMailClientFactory { throw new IllegalArgumentException("clientId and clientSecret are required to create Gmail client."); } try { - Credential credential = authorize(clientId, clientSecret); + // Use GoogleCredentialsHelper for OAuth credentials + GoogleMailConfiguration tempConfig = new GoogleMailConfiguration(); + tempConfig.setClientId(clientId); + tempConfig.setClientSecret(clientSecret); + tempConfig.setRefreshToken(refreshToken); + tempConfig.setAccessToken(accessToken); - if (refreshToken != null && !refreshToken.isEmpty()) { - credential.setRefreshToken(refreshToken); - } - if (accessToken != null && !accessToken.isEmpty()) { - credential.setAccessToken(accessToken); - } + Credential credential + = GoogleCredentialsHelper.getOAuthCredential(null, tempConfig, scopes, transport, jsonFactory); return new Gmail.Builder(transport, jsonFactory, credential).setApplicationName(applicationName).build(); } catch (Exception e) { throw new RuntimeCamelException("Could not create Gmail client.", e); } } - // Authorizes the installed application to access user's protected data. - private Credential authorize(String clientId, String clientSecret) { - // authorize - return new GoogleCredential.Builder().setJsonFactory(jsonFactory).setTransport(transport) - .setClientSecrets(clientId, clientSecret).build(); - } - @Override public Gmail makeClient( CamelContext camelContext, String serviceAccountKey, Collection<String> scopes, String applicationName, @@ -74,27 +66,16 @@ public class BatchGoogleMailClientFactory implements GoogleMailClientFactory { throw new IllegalArgumentException("serviceAccountKey is required to create Gmail client."); } try { - Credential credential = authorizeServiceAccount(camelContext, serviceAccountKey, delegate, scopes); + // Use GoogleCredentialsHelper for service account credentials + GoogleMailConfiguration tempConfig = new GoogleMailConfiguration(); + tempConfig.setServiceAccountKey(serviceAccountKey); + tempConfig.setDelegate(delegate); + + Credential credential + = GoogleCredentialsHelper.getOAuthCredential(camelContext, tempConfig, scopes, transport, jsonFactory); return new Gmail.Builder(transport, jsonFactory, credential).setApplicationName(applicationName).build(); } catch (Exception e) { throw new RuntimeCamelException("Could not create Gmail client.", e); } } - - private Credential authorizeServiceAccount( - CamelContext camelContext, String serviceAccountKey, String delegate, Collection<String> scopes) { - // authorize - try { - GoogleCredential cred = GoogleCredential - .fromStream(ResourceHelper.resolveMandatoryResourceAsInputStream(camelContext, serviceAccountKey), - transport, - jsonFactory) - .createScoped(scopes != null && !scopes.isEmpty() ? scopes : null) - .createDelegated(delegate); - cred.refreshToken(); - return cred; - } catch (IOException e) { - throw new RuntimeException(e); - } - } } diff --git a/components/camel-google/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailConfiguration.java b/components/camel-google/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailConfiguration.java index a6c5b092dae0..5daaeb750f0b 100644 --- a/components/camel-google/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailConfiguration.java +++ b/components/camel-google/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailConfiguration.java @@ -19,6 +19,7 @@ package org.apache.camel.component.google.mail; import java.util.Collection; import java.util.List; +import org.apache.camel.component.google.common.GoogleCommonConfiguration; import org.apache.camel.component.google.mail.internal.GoogleMailApiName; import org.apache.camel.spi.Configurer; import org.apache.camel.spi.Metadata; @@ -31,7 +32,7 @@ import org.apache.camel.spi.UriPath; */ @UriParams @Configurer(extended = true) -public class GoogleMailConfiguration { +public class GoogleMailConfiguration implements GoogleCommonConfiguration { @UriPath @Metadata(required = true) private GoogleMailApiName apiName; @@ -78,6 +79,7 @@ public class GoogleMailConfiguration { this.methodName = methodName; } + @Override public String getClientId() { return clientId; } @@ -89,6 +91,7 @@ public class GoogleMailConfiguration { this.clientId = clientId; } + @Override public String getClientSecret() { return clientSecret; } @@ -100,6 +103,7 @@ public class GoogleMailConfiguration { this.clientSecret = clientSecret; } + @Override public String getAccessToken() { return accessToken; } @@ -111,6 +115,7 @@ public class GoogleMailConfiguration { this.accessToken = accessToken; } + @Override public String getRefreshToken() { return refreshToken; } @@ -134,6 +139,7 @@ public class GoogleMailConfiguration { this.applicationName = applicationName; } + @Override public String getServiceAccountKey() { return serviceAccountKey; } @@ -148,6 +154,7 @@ public class GoogleMailConfiguration { this.serviceAccountKey = serviceAccountKey; } + @Override public String getDelegate() { return delegate; } @@ -163,6 +170,7 @@ public class GoogleMailConfiguration { return scopes; } + @Override public Collection<String> getScopesAsList() { if (scopes != null) { return List.of(scopes.split(",")); diff --git a/components/camel-google/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/stream/GoogleMailStreamConfiguration.java b/components/camel-google/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/stream/GoogleMailStreamConfiguration.java index 3dcffa0da54e..fe647fc38a8d 100644 --- a/components/camel-google/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/stream/GoogleMailStreamConfiguration.java +++ b/components/camel-google/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/stream/GoogleMailStreamConfiguration.java @@ -20,6 +20,7 @@ import java.util.Collection; import java.util.List; import org.apache.camel.RuntimeCamelException; +import org.apache.camel.component.google.common.GoogleCommonConfiguration; import org.apache.camel.spi.Metadata; import org.apache.camel.spi.UriParam; import org.apache.camel.spi.UriParams; @@ -29,7 +30,7 @@ import org.apache.camel.spi.UriPath; * Component configuration for GoogleMail stream component. */ @UriParams -public class GoogleMailStreamConfiguration implements Cloneable { +public class GoogleMailStreamConfiguration implements Cloneable, GoogleCommonConfiguration { @UriPath @Metadata(required = true) private String index; @@ -61,6 +62,7 @@ public class GoogleMailStreamConfiguration implements Cloneable { @UriParam private String scopes; + @Override public String getClientId() { return clientId; } @@ -72,6 +74,7 @@ public class GoogleMailStreamConfiguration implements Cloneable { this.clientId = clientId; } + @Override public String getClientSecret() { return clientSecret; } @@ -83,6 +86,7 @@ public class GoogleMailStreamConfiguration implements Cloneable { this.clientSecret = clientSecret; } + @Override public String getAccessToken() { return accessToken; } @@ -94,6 +98,7 @@ public class GoogleMailStreamConfiguration implements Cloneable { this.accessToken = accessToken; } + @Override public String getRefreshToken() { return refreshToken; } @@ -184,6 +189,7 @@ public class GoogleMailStreamConfiguration implements Cloneable { this.markAsRead = markAsRead; } + @Override public String getServiceAccountKey() { return serviceAccountKey; } @@ -197,6 +203,7 @@ public class GoogleMailStreamConfiguration implements Cloneable { this.serviceAccountKey = serviceAccountKey; } + @Override public String getDelegate() { return delegate; } @@ -212,6 +219,7 @@ public class GoogleMailStreamConfiguration implements Cloneable { return scopes; } + @Override public Collection<String> getScopesAsList() { if (scopes != null) { return List.of(scopes.split(","));
