Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions components/camel-google/camel-google-calendar/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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();

Expand All @@ -57,34 +56,27 @@ 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) {
throw new RuntimeCamelException("Could not create Google Calendar 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();
}

// authorize with P12-Certificate file
private Credential authorizeServiceAccount(String emailAddress, String p12FileName, Collection<String> scopes, String user)
// authorize with P12-Certificate file (legacy method - kept for backward compatibility)
private Credential authorizeServiceAccountWithP12(
String emailAddress, String p12FileName, Collection<String> scopes, String user)
throws Exception {
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
// set the service account user when provided
Expand All @@ -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<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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,7 +33,7 @@
*/
@UriParams
@Configurer(extended = true)
public class GoogleCalendarConfiguration {
public class GoogleCalendarConfiguration implements GoogleCommonConfiguration {

@UriPath
@Metadata(required = true)
Expand Down Expand Up @@ -86,6 +87,7 @@ public void setMethodName(String methodName) {
this.methodName = methodName;
}

@Override
public String getClientId() {
return clientId;
}
Expand All @@ -108,6 +110,7 @@ public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}

@Override
public String getClientSecret() {
return clientSecret;
}
Expand All @@ -119,6 +122,7 @@ public void setClientSecret(String clientSecret) {
this.clientSecret = clientSecret;
}

@Override
public String getAccessToken() {
return accessToken;
}
Expand All @@ -130,6 +134,7 @@ public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}

@Override
public String getRefreshToken() {
return refreshToken;
}
Expand Down Expand Up @@ -157,6 +162,7 @@ public String getScopes() {
return scopes;
}

@Override
public Collection<String> getScopesAsList() {
if (scopes != null) {
return List.of(scopes.split(","));
Expand Down Expand Up @@ -198,6 +204,7 @@ public void setUser(String user) {
this.user = user;
}

@Override
public String getServiceAccountKey() {
return serviceAccountKey;
}
Expand All @@ -212,6 +219,7 @@ public void setServiceAccountKey(String serviceAccountKey) {
this.serviceAccountKey = serviceAccountKey;
}

@Override
public String getDelegate() {
return delegate;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -72,6 +73,7 @@ public class GoogleCalendarStreamConfiguration implements Cloneable {
@UriParam
private String delegate;

@Override
public String getClientId() {
return clientId;
}
Expand All @@ -94,6 +96,7 @@ public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}

@Override
public String getClientSecret() {
return clientSecret;
}
Expand All @@ -105,6 +108,7 @@ public void setClientSecret(String clientSecret) {
this.clientSecret = clientSecret;
}

@Override
public String getAccessToken() {
return accessToken;
}
Expand All @@ -116,6 +120,7 @@ public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}

@Override
public String getRefreshToken() {
return refreshToken;
}
Expand Down Expand Up @@ -154,6 +159,7 @@ public void setScopes(String scopes) {
this.scopes = scopes;
}

@Override
public Collection<String> getScopesAsList() {
if (scopes != null) {
return List.of(scopes.split(","));
Expand Down Expand Up @@ -263,6 +269,7 @@ public void setConsiderLastUpdate(boolean considerLastUpdate) {
this.considerLastUpdate = considerLastUpdate;
}

@Override
public String getServiceAccountKey() {
return serviceAccountKey;
}
Expand All @@ -277,6 +284,7 @@ public void setServiceAccountKey(String serviceAccountKey) {
this.serviceAccountKey = serviceAccountKey;
}

@Override
public String getDelegate() {
return delegate;
}
Expand Down