diff --git a/components/camel-google/camel-google-calendar/pom.xml b/components/camel-google/camel-google-calendar/pom.xml index 172e5d9b500bc..4ed04c787f5dd 100644 --- a/components/camel-google/camel-google-calendar/pom.xml +++ b/components/camel-google/camel-google-calendar/pom.xml @@ -41,6 +41,10 @@ + + org.apache.camel + camel-google-common + org.apache.camel camel-support diff --git a/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/BatchGoogleCalendarClientFactory.java b/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/BatchGoogleCalendarClientFactory.java index d815b9a55841c..461267497b08b 100644 --- a/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/BatchGoogleCalendarClientFactory.java +++ b/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/BatchGoogleCalendarClientFactory.java @@ -17,7 +17,6 @@ package org.apache.camel.component.google.calendar; import java.io.File; -import java.io.IOException; import java.util.Collection; import com.google.api.client.auth.oauth2.Credential; @@ -29,7 +28,7 @@ import com.google.api.services.calendar.Calendar; 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 BatchGoogleCalendarClientFactory implements GoogleCalendarClientFactory { private NetHttpTransport transport; @@ -46,7 +45,7 @@ public Calendar makeClient( String accessToken, String emailAddress, String p12FileName, String user) { // if emailAddress and p12FileName values are present, assume Google - // Service Account + // Service Account with P12 file (legacy) boolean serviceAccount = null != emailAddress && !emailAddress.isEmpty() && null != p12FileName && !p12FileName.isEmpty(); @@ -57,15 +56,17 @@ public Calendar makeClient( try { Credential credential; if (serviceAccount) { - credential = authorizeServiceAccount(emailAddress, p12FileName, scopes, user); + // Legacy P12 file authentication - keep as is since GoogleCredentialsHelper doesn't support P12 + credential = authorizeServiceAccountWithP12(emailAddress, p12FileName, scopes, user); } else { - credential = authorize(clientId, clientSecret); - if (refreshToken != null && !refreshToken.isEmpty()) { - credential.setRefreshToken(refreshToken); - } - if (accessToken != null && !accessToken.isEmpty()) { - credential.setAccessToken(accessToken); - } + // Use GoogleCredentialsHelper for OAuth credentials + GoogleCalendarConfiguration tempConfig = new GoogleCalendarConfiguration(); + tempConfig.setClientId(clientId); + tempConfig.setClientSecret(clientSecret); + tempConfig.setRefreshToken(refreshToken); + tempConfig.setAccessToken(accessToken); + + credential = GoogleCredentialsHelper.getOAuthCredential(null, tempConfig, scopes, transport, jsonFactory); } return new Calendar.Builder(transport, jsonFactory, credential).setApplicationName(applicationName).build(); } catch (Exception e) { @@ -73,18 +74,9 @@ public Calendar makeClient( } } - // 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(); - } - - // authorize with P12-Certificate file - private Credential authorizeServiceAccount(String emailAddress, String p12FileName, Collection scopes, String user) + // authorize with P12-Certificate file (legacy method - kept for backward compatibility) + private Credential authorizeServiceAccountWithP12( + String emailAddress, String p12FileName, Collection scopes, String user) throws Exception { HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); // set the service account user when provided @@ -106,27 +98,16 @@ public Calendar makeClient( throw new IllegalArgumentException("serviceAccountKey is required to create Google Calendar client."); } try { - Credential credential = authorizeServiceAccount(camelContext, serviceAccountKey, delegate, scopes); + // Use GoogleCredentialsHelper for service account JSON key + GoogleCalendarConfiguration tempConfig = new GoogleCalendarConfiguration(); + tempConfig.setServiceAccountKey(serviceAccountKey); + tempConfig.setDelegate(delegate); + + Credential credential + = GoogleCredentialsHelper.getOAuthCredential(camelContext, tempConfig, scopes, transport, jsonFactory); return new Calendar.Builder(transport, jsonFactory, credential).setApplicationName(applicationName).build(); } catch (Exception e) { throw new RuntimeCamelException("Could not create Google Calendar client.", e); } } - - // authorize with JSON-Credentials - private Credential authorizeServiceAccount( - CamelContext camelContext, String serviceAccountKey, String delegate, Collection 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-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarConfiguration.java b/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarConfiguration.java index ee56a80f4d6e6..1ac849eb24047 100644 --- a/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarConfiguration.java +++ b/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarConfiguration.java @@ -21,6 +21,7 @@ import com.google.api.services.calendar.CalendarScopes; import org.apache.camel.component.google.calendar.internal.GoogleCalendarApiName; +import org.apache.camel.component.google.common.GoogleCommonConfiguration; import org.apache.camel.spi.Configurer; import org.apache.camel.spi.Metadata; import org.apache.camel.spi.UriParam; @@ -32,7 +33,7 @@ */ @UriParams @Configurer(extended = true) -public class GoogleCalendarConfiguration { +public class GoogleCalendarConfiguration implements GoogleCommonConfiguration { @UriPath @Metadata(required = true) @@ -86,6 +87,7 @@ public void setMethodName(String methodName) { this.methodName = methodName; } + @Override public String getClientId() { return clientId; } @@ -108,6 +110,7 @@ public void setEmailAddress(String emailAddress) { this.emailAddress = emailAddress; } + @Override public String getClientSecret() { return clientSecret; } @@ -119,6 +122,7 @@ public void setClientSecret(String clientSecret) { this.clientSecret = clientSecret; } + @Override public String getAccessToken() { return accessToken; } @@ -130,6 +134,7 @@ public void setAccessToken(String accessToken) { this.accessToken = accessToken; } + @Override public String getRefreshToken() { return refreshToken; } @@ -157,6 +162,7 @@ public String getScopes() { return scopes; } + @Override public Collection getScopesAsList() { if (scopes != null) { return List.of(scopes.split(",")); @@ -198,6 +204,7 @@ public void setUser(String user) { this.user = user; } + @Override public String getServiceAccountKey() { return serviceAccountKey; } @@ -212,6 +219,7 @@ public void setServiceAccountKey(String serviceAccountKey) { this.serviceAccountKey = serviceAccountKey; } + @Override public String getDelegate() { return delegate; } diff --git a/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/stream/GoogleCalendarStreamConfiguration.java b/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/stream/GoogleCalendarStreamConfiguration.java index b546bd700c711..c1059fec54840 100644 --- a/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/stream/GoogleCalendarStreamConfiguration.java +++ b/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/stream/GoogleCalendarStreamConfiguration.java @@ -21,6 +21,7 @@ import com.google.api.services.calendar.CalendarScopes; 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; @@ -30,7 +31,7 @@ * Component configuration for GoogleCalendar stream component. */ @UriParams -public class GoogleCalendarStreamConfiguration implements Cloneable { +public class GoogleCalendarStreamConfiguration implements Cloneable, GoogleCommonConfiguration { private static final String DEFAULT_SCOPES = CalendarScopes.CALENDAR; @UriPath @@ -72,6 +73,7 @@ public class GoogleCalendarStreamConfiguration implements Cloneable { @UriParam private String delegate; + @Override public String getClientId() { return clientId; } @@ -94,6 +96,7 @@ public void setEmailAddress(String emailAddress) { this.emailAddress = emailAddress; } + @Override public String getClientSecret() { return clientSecret; } @@ -105,6 +108,7 @@ public void setClientSecret(String clientSecret) { this.clientSecret = clientSecret; } + @Override public String getAccessToken() { return accessToken; } @@ -116,6 +120,7 @@ public void setAccessToken(String accessToken) { this.accessToken = accessToken; } + @Override public String getRefreshToken() { return refreshToken; } @@ -154,6 +159,7 @@ public void setScopes(String scopes) { this.scopes = scopes; } + @Override public Collection getScopesAsList() { if (scopes != null) { return List.of(scopes.split(",")); @@ -263,6 +269,7 @@ public void setConsiderLastUpdate(boolean considerLastUpdate) { this.considerLastUpdate = considerLastUpdate; } + @Override public String getServiceAccountKey() { return serviceAccountKey; } @@ -277,6 +284,7 @@ public void setServiceAccountKey(String serviceAccountKey) { this.serviceAccountKey = serviceAccountKey; } + @Override public String getDelegate() { return delegate; }