This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch CAMEL-27226-4.22.x in repository https://gitbox.apache.org/repos/asf/camel.git
commit aba561db18212f7866af536969579ec60aef4272 Author: Claus Ibsen <[email protected]> AuthorDate: Thu Nov 19 13:15:36 2026 +0100 CAMEL-27226: camel-google - the OAuth access token is sent only in the Authorization header The producers and consumers of google-sheets, google-drive, google-mail and google-calendar set the options and headers of the exchange on the request of the Google API client. The credential options (clientId, clientSecret, accessToken, refreshToken, serviceAccountKey, delegate, and p12FileName for google-calendar) are no longer set on the request, matched ignoring case, so an accessToken option or header does not become the access_token query parameter. The client already sends the token in the Authorization header. The google-sheets part is CAMEL-27220 (only its credential handling; the change of the x-struct transformer is not part of this backport). (backport of #30364 and #30369, adapted for camel-4.22.x) Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../google/calendar/GoogleCalendarConsumer.java | 10 +- .../google/calendar/GoogleCalendarProducer.java | 10 +- .../GoogleCalendarRequestCredentialTest.java | 145 +++++++++++++++++++ .../google/common/GoogleApiRequestHelper.java | 70 +++++++++ .../google/common/GoogleApiRequestHelperTest.java | 75 ++++++++++ .../google/drive/GoogleDriveConsumer.java | 10 +- .../google/drive/GoogleDriveProducer.java | 10 +- .../drive/GoogleDriveRequestCredentialTest.java | 159 +++++++++++++++++++++ .../component/google/mail/GoogleMailConsumer.java | 10 +- .../component/google/mail/GoogleMailProducer.java | 10 +- .../mail/GoogleMailRequestCredentialTest.java | 145 +++++++++++++++++++ .../google/sheets/GoogleSheetsConsumer.java | 9 +- .../google/sheets/GoogleSheetsEndpoint.java | 34 +++++ .../google/sheets/GoogleSheetsProducer.java | 9 +- .../sheets/GoogleSheetsRequestCredentialTest.java | 116 +++++++++++++++ 15 files changed, 758 insertions(+), 64 deletions(-) diff --git a/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarConsumer.java b/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarConsumer.java index 231b076c0b07..5823c28a0cf0 100644 --- a/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarConsumer.java +++ b/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarConsumer.java @@ -17,14 +17,12 @@ package org.apache.camel.component.google.calendar; import java.util.Map; -import java.util.Map.Entry; import com.google.api.client.googleapis.services.AbstractGoogleClientRequest; import org.apache.camel.Processor; import org.apache.camel.RuntimeCamelException; import org.apache.camel.component.google.calendar.internal.GoogleCalendarApiName; -import org.apache.camel.spi.BeanIntrospection; -import org.apache.camel.support.PluginHelper; +import org.apache.camel.component.google.common.GoogleApiRequestHelper; import org.apache.camel.support.component.AbstractApiConsumer; /** @@ -40,11 +38,7 @@ public class GoogleCalendarConsumer extends AbstractApiConsumer<GoogleCalendarAp protected Object doInvokeMethod(Map<String, Object> properties) throws RuntimeCamelException { AbstractGoogleClientRequest request = (AbstractGoogleClientRequest) super.doInvokeMethod(properties); try { - BeanIntrospection beanIntrospection - = PluginHelper.getBeanIntrospection(getEndpoint().getCamelContext()); - for (Entry<String, Object> p : properties.entrySet()) { - beanIntrospection.setProperty(getEndpoint().getCamelContext(), request, p.getKey(), p.getValue()); - } + GoogleApiRequestHelper.configureRequest(getEndpoint().getCamelContext(), request, properties); return request.execute(); } catch (Exception e) { throw new RuntimeCamelException(e); diff --git a/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarProducer.java b/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarProducer.java index b415066b3402..c0b94a47cc18 100644 --- a/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarProducer.java +++ b/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarProducer.java @@ -17,14 +17,12 @@ package org.apache.camel.component.google.calendar; import java.util.Map; -import java.util.Map.Entry; import com.google.api.client.googleapis.services.AbstractGoogleClientRequest; import org.apache.camel.RuntimeCamelException; import org.apache.camel.component.google.calendar.internal.GoogleCalendarApiName; import org.apache.camel.component.google.calendar.internal.GoogleCalendarPropertiesHelper; -import org.apache.camel.spi.BeanIntrospection; -import org.apache.camel.support.PluginHelper; +import org.apache.camel.component.google.common.GoogleApiRequestHelper; import org.apache.camel.support.component.AbstractApiProducer; import org.apache.camel.support.component.ApiMethod; @@ -42,11 +40,7 @@ public class GoogleCalendarProducer extends AbstractApiProducer<GoogleCalendarAp throws RuntimeCamelException { AbstractGoogleClientRequest request = (AbstractGoogleClientRequest) super.doInvokeMethod(method, properties); try { - BeanIntrospection beanIntrospection - = PluginHelper.getBeanIntrospection(getEndpoint().getCamelContext()); - for (Entry<String, Object> p : properties.entrySet()) { - beanIntrospection.setProperty(getEndpoint().getCamelContext(), request, p.getKey(), p.getValue()); - } + GoogleApiRequestHelper.configureRequest(getEndpoint().getCamelContext(), request, properties); return request.execute(); } catch (Exception e) { throw new RuntimeCamelException(e); diff --git a/components/camel-google/camel-google-calendar/src/test/java/org/apache/camel/component/google/calendar/GoogleCalendarRequestCredentialTest.java b/components/camel-google/camel-google-calendar/src/test/java/org/apache/camel/component/google/calendar/GoogleCalendarRequestCredentialTest.java new file mode 100644 index 000000000000..77eda740a3f8 --- /dev/null +++ b/components/camel-google/camel-google-calendar/src/test/java/org/apache/camel/component/google/calendar/GoogleCalendarRequestCredentialTest.java @@ -0,0 +1,145 @@ +/* + * 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.calendar; + +import java.util.Collection; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + +import com.google.api.client.auth.oauth2.Credential; +import com.google.api.client.json.Json; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.gson.GsonFactory; +import com.google.api.client.testing.http.MockHttpTransport; +import com.google.api.client.testing.http.MockLowLevelHttpRequest; +import com.google.api.client.testing.http.MockLowLevelHttpResponse; +import com.google.api.services.calendar.Calendar; +import org.apache.camel.CamelContext; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.google.common.GoogleCredentialsHelper; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.impl.DefaultCamelContext; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +/** + * The client sends the OAuth access token in the Authorization header; the request URL does not carry it. + */ +class GoogleCalendarRequestCredentialTest { + + private static final String OAUTH = "clientId=id&clientSecret=secret&accessToken=atoken"; + + private final RecordingTransport transport = new RecordingTransport(); + + @Test + void shouldSendTheAccessTokenOfTheProducerOnlyInTheAuthorizationHeader() throws Exception { + try (CamelContext context = createContext()) { + Object body = context.createProducerTemplate() + .requestBody("google-calendar://calendars/get?calendarId=primary&" + OAUTH, (Object) null); + + assertNotNull(body); + assertSentWithTheTokenInTheHeaderOnly(transport.requests.get(0)); + } + } + + @Test + void shouldSendTheAccessTokenOfTheConsumerOnlyInTheAuthorizationHeader() throws Exception { + try (CamelContext context = createContext()) { + MockEndpoint mock = context.getEndpoint("mock:result", MockEndpoint.class); + mock.expectedMinimumMessageCount(1); + context.addRoutes(new RouteBuilder() { + @Override + public void configure() { + from("google-calendar://calendars/get?calendarId=primary&delay=100&" + OAUTH).to("mock:result"); + } + }); + + mock.assertIsSatisfied(); + assertSentWithTheTokenInTheHeaderOnly(transport.requests.get(0)); + } + } + + private static void assertSentWithTheTokenInTheHeaderOnly(MockLowLevelHttpRequest request) { + assertFalse(request.getUrl().contains("access_token"), request.getUrl()); + assertFalse(request.getUrl().contains("atoken"), request.getUrl()); + assertEquals("Bearer atoken", request.getFirstHeaderValue("Authorization")); + } + + private CamelContext createContext() { + CamelContext context = new DefaultCamelContext(); + context.getComponent("google-calendar", GoogleCalendarComponent.class) + .setClientFactory(new OAuthClientFactory(transport)); + context.start(); + return context; + } + + /** + * Creates the client with the OAuth credential as the client factory of the component does, on a transport that + * records the requests. + */ + private static final class OAuthClientFactory implements GoogleCalendarClientFactory { + + private final JsonFactory jsonFactory = GsonFactory.getDefaultInstance(); + private final MockHttpTransport transport; + + private OAuthClientFactory(MockHttpTransport transport) { + this.transport = transport; + } + + @Override + public Calendar makeClient( + String clientId, String clientSecret, Collection<String> scopes, String applicationName, String refreshToken, + String accessToken, String emailAddress, String p12FileName, String user) { + GoogleCalendarConfiguration configuration = new GoogleCalendarConfiguration(); + configuration.setClientId(clientId); + configuration.setClientSecret(clientSecret); + configuration.setRefreshToken(refreshToken); + configuration.setAccessToken(accessToken); + try { + Credential credential + = GoogleCredentialsHelper.getOAuthCredential(null, configuration, scopes, transport, jsonFactory); + return new Calendar.Builder(transport, jsonFactory, credential).setApplicationName("test").build(); + } catch (Exception e) { + throw new IllegalStateException(e); + } + } + + @Override + public Calendar makeClient( + CamelContext camelContext, String serviceAccountKey, Collection<String> scopes, String applicationName, + String delegate) { + throw new UnsupportedOperationException("OAuth only"); + } + } + + private static final class RecordingTransport extends MockHttpTransport { + + private final List<MockLowLevelHttpRequest> requests = new CopyOnWriteArrayList<>(); + + @Override + public MockLowLevelHttpRequest buildRequest(String method, String url) { + MockLowLevelHttpRequest request = new MockLowLevelHttpRequest(url); + request.setResponse(new MockLowLevelHttpResponse().setContentType(Json.MEDIA_TYPE) + .setContent("{\"id\":\"primary\"}")); + requests.add(request); + return request; + } + } +} diff --git a/components/camel-google/camel-google-common/src/main/java/org/apache/camel/component/google/common/GoogleApiRequestHelper.java b/components/camel-google/camel-google-common/src/main/java/org/apache/camel/component/google/common/GoogleApiRequestHelper.java new file mode 100644 index 000000000000..ac81de3c0fe9 --- /dev/null +++ b/components/camel-google/camel-google-common/src/main/java/org/apache/camel/component/google/common/GoogleApiRequestHelper.java @@ -0,0 +1,70 @@ +/* + * 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.common; + +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; + +import org.apache.camel.CamelContext; +import org.apache.camel.spi.BeanIntrospection; +import org.apache.camel.support.PluginHelper; + +/** + * Sets the options and headers of an exchange on the request that a method of a Google API client builds, for the + * components that are generated from a Google API client (Calendar, Drive, Mail). + */ +public final class GoogleApiRequestHelper { + + /** + * The options that make up the credential of the client. The client sends the credential in the Authorization + * header, so they are not set on the request, where accessToken would become the access_token query parameter. + * p12FileName is the key file of a service account (Calendar). The names are matched ignoring case, as a header + * such as CamelGoogleDrive.AccessToken sets the same property as accessToken. + */ + public static final Set<String> CREDENTIAL_OPTIONS; + + static { + Set<String> names = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); + names.addAll(List.of("clientId", "clientSecret", "accessToken", "refreshToken", "serviceAccountKey", "delegate", + "p12FileName")); + CREDENTIAL_OPTIONS = Collections.unmodifiableSet(names); + } + + private GoogleApiRequestHelper() { + } + + /** + * Sets the given properties on the request, such as the optional parameters of the method and the standard + * parameters of the Google API, except the options of the credential (see {@link #CREDENTIAL_OPTIONS}). + * + * @param camelContext the Camel context + * @param request the request built by the method of the API client + * @param properties the options of the endpoint and the headers of the exchange + */ + public static void configureRequest(CamelContext camelContext, Object request, Map<String, Object> properties) + throws Exception { + BeanIntrospection beanIntrospection = PluginHelper.getBeanIntrospection(camelContext); + for (Map.Entry<String, Object> p : properties.entrySet()) { + if (!CREDENTIAL_OPTIONS.contains(p.getKey())) { + beanIntrospection.setProperty(camelContext, request, p.getKey(), p.getValue()); + } + } + } +} diff --git a/components/camel-google/camel-google-common/src/test/java/org/apache/camel/component/google/common/GoogleApiRequestHelperTest.java b/components/camel-google/camel-google-common/src/test/java/org/apache/camel/component/google/common/GoogleApiRequestHelperTest.java new file mode 100644 index 000000000000..39fd3d318306 --- /dev/null +++ b/components/camel-google/camel-google-common/src/test/java/org/apache/camel/component/google/common/GoogleApiRequestHelperTest.java @@ -0,0 +1,75 @@ +/* + * 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.common; + +import java.util.Map; + +import org.apache.camel.CamelContext; +import org.apache.camel.impl.DefaultCamelContext; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +class GoogleApiRequestHelperTest { + + @Test + void shouldSetTheParametersButNotTheCredentialOptions() throws Exception { + try (CamelContext context = new DefaultCamelContext()) { + context.start(); + Request request = new Request(); + + GoogleApiRequestHelper.configureRequest(context, request, + Map.of("AccessToken", "atoken", "clientSecret", "secret", "fields", "id,name")); + + assertEquals("id,name", request.getFields()); + assertNull(request.getAccessToken()); + assertNull(request.getClientSecret()); + } + } + + public static class Request { + + private String accessToken; + private String clientSecret; + private String fields; + + public String getAccessToken() { + return accessToken; + } + + public void setAccessToken(String accessToken) { + this.accessToken = accessToken; + } + + public String getClientSecret() { + return clientSecret; + } + + public void setClientSecret(String clientSecret) { + this.clientSecret = clientSecret; + } + + public String getFields() { + return fields; + } + + public void setFields(String fields) { + this.fields = fields; + } + } +} diff --git a/components/camel-google/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveConsumer.java b/components/camel-google/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveConsumer.java index 62b0cebd74ef..cd286074a4d3 100644 --- a/components/camel-google/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveConsumer.java +++ b/components/camel-google/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveConsumer.java @@ -17,14 +17,12 @@ package org.apache.camel.component.google.drive; import java.util.Map; -import java.util.Map.Entry; import com.google.api.client.googleapis.services.AbstractGoogleClientRequest; import org.apache.camel.Processor; import org.apache.camel.RuntimeCamelException; +import org.apache.camel.component.google.common.GoogleApiRequestHelper; import org.apache.camel.component.google.drive.internal.GoogleDriveApiName; -import org.apache.camel.spi.BeanIntrospection; -import org.apache.camel.support.PluginHelper; import org.apache.camel.support.component.AbstractApiConsumer; /** @@ -39,11 +37,7 @@ public class GoogleDriveConsumer extends AbstractApiConsumer<GoogleDriveApiName, protected Object doInvokeMethod(Map<String, Object> properties) throws RuntimeCamelException { AbstractGoogleClientRequest request = (AbstractGoogleClientRequest) super.doInvokeMethod(properties); try { - BeanIntrospection beanIntrospection - = PluginHelper.getBeanIntrospection(getEndpoint().getCamelContext()); - for (Entry<String, Object> p : properties.entrySet()) { - beanIntrospection.setProperty(getEndpoint().getCamelContext(), request, p.getKey(), p.getValue()); - } + GoogleApiRequestHelper.configureRequest(getEndpoint().getCamelContext(), request, properties); return request.execute(); } catch (Exception e) { throw new RuntimeCamelException(e); diff --git a/components/camel-google/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveProducer.java b/components/camel-google/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveProducer.java index f689131a8586..dd4dd0c81ecb 100644 --- a/components/camel-google/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveProducer.java +++ b/components/camel-google/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveProducer.java @@ -17,15 +17,13 @@ package org.apache.camel.component.google.drive; import java.util.Map; -import java.util.Map.Entry; import com.google.api.client.googleapis.services.AbstractGoogleClientRequest; import org.apache.camel.RuntimeCamelException; +import org.apache.camel.component.google.common.GoogleApiRequestHelper; 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 org.apache.camel.spi.BeanIntrospection; -import org.apache.camel.support.PluginHelper; import org.apache.camel.support.component.AbstractApiProducer; import org.apache.camel.support.component.ApiMethod; @@ -43,11 +41,7 @@ public class GoogleDriveProducer extends AbstractApiProducer<GoogleDriveApiName, throws RuntimeCamelException { AbstractGoogleClientRequest request = (AbstractGoogleClientRequest) super.doInvokeMethod(method, properties); try { - BeanIntrospection beanIntrospection - = PluginHelper.getBeanIntrospection(getEndpoint().getCamelContext()); - for (Entry<String, Object> p : properties.entrySet()) { - beanIntrospection.setProperty(getEndpoint().getCamelContext(), request, p.getKey(), p.getValue()); - } + GoogleApiRequestHelper.configureRequest(getEndpoint().getCamelContext(), request, properties); return request.execute(); } catch (Exception e) { throw new RuntimeCamelException(e); diff --git a/components/camel-google/camel-google-drive/src/test/java/org/apache/camel/component/google/drive/GoogleDriveRequestCredentialTest.java b/components/camel-google/camel-google-drive/src/test/java/org/apache/camel/component/google/drive/GoogleDriveRequestCredentialTest.java new file mode 100644 index 000000000000..ed9a8d763737 --- /dev/null +++ b/components/camel-google/camel-google-drive/src/test/java/org/apache/camel/component/google/drive/GoogleDriveRequestCredentialTest.java @@ -0,0 +1,159 @@ +/* + * 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 java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + +import com.google.api.client.auth.oauth2.Credential; +import com.google.api.client.json.Json; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.gson.GsonFactory; +import com.google.api.client.testing.http.MockHttpTransport; +import com.google.api.client.testing.http.MockLowLevelHttpRequest; +import com.google.api.client.testing.http.MockLowLevelHttpResponse; +import com.google.api.services.drive.Drive; +import org.apache.camel.CamelContext; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.google.common.GoogleCredentialsHelper; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.impl.DefaultCamelContext; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +/** + * The client sends the OAuth access token in the Authorization header; the request URL does not carry it. + */ +class GoogleDriveRequestCredentialTest { + + private static final String OAUTH = "clientId=id&clientSecret=secret&accessToken=atoken"; + + private final RecordingTransport transport = new RecordingTransport(); + + @Test + void shouldSendTheAccessTokenOfTheProducerOnlyInTheAuthorizationHeader() throws Exception { + try (CamelContext context = createContext()) { + Object body + = context.createProducerTemplate().requestBody("google-drive://drive-files/list?" + OAUTH, (Object) null); + + assertNotNull(body); + assertSentWithTheTokenInTheHeaderOnly(transport.requests.get(0)); + } + } + + @Test + void shouldNotSendAnAccessTokenHeaderOnTheRequestWhateverItsCase() throws Exception { + for (String header : List.of("CamelGoogleDrive.accessToken", "CamelGoogleDrive.AccessToken")) { + transport.requests.clear(); + try (CamelContext context = createContext()) { + context.createProducerTemplate().requestBodyAndHeader( + "google-drive://drive-files/list?" + OAUTH, null, header, "htoken"); + + String url = transport.requests.get(0).getUrl(); + assertFalse(url.contains("access_token"), header + " -> " + url); + assertFalse(url.contains("htoken"), header + " -> " + url); + } + } + } + + @Test + void shouldSendTheAccessTokenOfTheConsumerOnlyInTheAuthorizationHeader() throws Exception { + try (CamelContext context = createContext()) { + MockEndpoint mock = context.getEndpoint("mock:result", MockEndpoint.class); + mock.expectedMinimumMessageCount(1); + context.addRoutes(new RouteBuilder() { + @Override + public void configure() { + from("google-drive://drive-files/list?delay=100&" + OAUTH).to("mock:result"); + } + }); + + mock.assertIsSatisfied(); + assertSentWithTheTokenInTheHeaderOnly(transport.requests.get(0)); + } + } + + private static void assertSentWithTheTokenInTheHeaderOnly(MockLowLevelHttpRequest request) { + assertFalse(request.getUrl().contains("access_token"), request.getUrl()); + assertFalse(request.getUrl().contains("atoken"), request.getUrl()); + assertEquals("Bearer atoken", request.getFirstHeaderValue("Authorization")); + } + + private CamelContext createContext() { + CamelContext context = new DefaultCamelContext(); + context.getComponent("google-drive", GoogleDriveComponent.class).setClientFactory(new OAuthClientFactory(transport)); + context.start(); + return context; + } + + /** + * Creates the client with the OAuth credential as the client factory of the component does, on a transport that + * records the requests. + */ + private static final class OAuthClientFactory implements GoogleDriveClientFactory { + + private final JsonFactory jsonFactory = GsonFactory.getDefaultInstance(); + private final MockHttpTransport transport; + + private OAuthClientFactory(MockHttpTransport transport) { + this.transport = transport; + } + + @Override + public Drive makeClient( + String clientId, String clientSecret, Collection<String> scopes, String applicationName, String refreshToken, + String accessToken) { + GoogleDriveConfiguration configuration = new GoogleDriveConfiguration(); + configuration.setClientId(clientId); + configuration.setClientSecret(clientSecret); + configuration.setRefreshToken(refreshToken); + configuration.setAccessToken(accessToken); + try { + Credential credential + = GoogleCredentialsHelper.getOAuthCredential(null, configuration, scopes, transport, jsonFactory); + return new Drive.Builder(transport, jsonFactory, credential).setApplicationName("test").build(); + } catch (Exception e) { + throw new IllegalStateException(e); + } + } + + @Override + public Drive makeClient( + CamelContext camelContext, String serviceAccountKey, Collection<String> scopes, String applicationName, + String delegate) { + throw new UnsupportedOperationException("OAuth only"); + } + } + + private static final class RecordingTransport extends MockHttpTransport { + + private final List<MockLowLevelHttpRequest> requests = new CopyOnWriteArrayList<>(); + + @Override + public MockLowLevelHttpRequest buildRequest(String method, String url) { + MockLowLevelHttpRequest request = new MockLowLevelHttpRequest(url); + request.setResponse(new MockLowLevelHttpResponse().setContentType(Json.MEDIA_TYPE) + .setContent("{\"files\":[]}")); + requests.add(request); + return request; + } + } +} diff --git a/components/camel-google/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailConsumer.java b/components/camel-google/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailConsumer.java index c66b5c783c0e..4a536de53ca3 100644 --- a/components/camel-google/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailConsumer.java +++ b/components/camel-google/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailConsumer.java @@ -17,14 +17,12 @@ package org.apache.camel.component.google.mail; import java.util.Map; -import java.util.Map.Entry; import com.google.api.client.googleapis.services.AbstractGoogleClientRequest; import org.apache.camel.Processor; import org.apache.camel.RuntimeCamelException; +import org.apache.camel.component.google.common.GoogleApiRequestHelper; import org.apache.camel.component.google.mail.internal.GoogleMailApiName; -import org.apache.camel.spi.BeanIntrospection; -import org.apache.camel.support.PluginHelper; import org.apache.camel.support.component.AbstractApiConsumer; /** @@ -40,11 +38,7 @@ public class GoogleMailConsumer extends AbstractApiConsumer<GoogleMailApiName, G protected Object doInvokeMethod(Map<String, Object> properties) throws RuntimeCamelException { AbstractGoogleClientRequest request = (AbstractGoogleClientRequest) super.doInvokeMethod(properties); try { - BeanIntrospection beanIntrospection - = PluginHelper.getBeanIntrospection(getEndpoint().getCamelContext()); - for (Entry<String, Object> p : properties.entrySet()) { - beanIntrospection.setProperty(getEndpoint().getCamelContext(), request, p.getKey(), p.getValue()); - } + GoogleApiRequestHelper.configureRequest(getEndpoint().getCamelContext(), request, properties); return request.execute(); } catch (Exception e) { throw new RuntimeCamelException(e); diff --git a/components/camel-google/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailProducer.java b/components/camel-google/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailProducer.java index 447570d76d09..0b722ec599e8 100644 --- a/components/camel-google/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailProducer.java +++ b/components/camel-google/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailProducer.java @@ -17,14 +17,12 @@ package org.apache.camel.component.google.mail; import java.util.Map; -import java.util.Map.Entry; import com.google.api.client.googleapis.services.AbstractGoogleClientRequest; import org.apache.camel.RuntimeCamelException; +import org.apache.camel.component.google.common.GoogleApiRequestHelper; import org.apache.camel.component.google.mail.internal.GoogleMailApiName; import org.apache.camel.component.google.mail.internal.GoogleMailPropertiesHelper; -import org.apache.camel.spi.BeanIntrospection; -import org.apache.camel.support.PluginHelper; import org.apache.camel.support.component.AbstractApiProducer; import org.apache.camel.support.component.ApiMethod; @@ -42,11 +40,7 @@ public class GoogleMailProducer extends AbstractApiProducer<GoogleMailApiName, G throws RuntimeCamelException { AbstractGoogleClientRequest request = (AbstractGoogleClientRequest) super.doInvokeMethod(method, properties); try { - BeanIntrospection beanIntrospection - = PluginHelper.getBeanIntrospection(getEndpoint().getCamelContext()); - for (Entry<String, Object> p : properties.entrySet()) { - beanIntrospection.setProperty(getEndpoint().getCamelContext(), request, p.getKey(), p.getValue()); - } + GoogleApiRequestHelper.configureRequest(getEndpoint().getCamelContext(), request, properties); return request.execute(); } catch (Exception e) { throw new RuntimeCamelException(e); diff --git a/components/camel-google/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/GoogleMailRequestCredentialTest.java b/components/camel-google/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/GoogleMailRequestCredentialTest.java new file mode 100644 index 000000000000..be5c801ff5d9 --- /dev/null +++ b/components/camel-google/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/GoogleMailRequestCredentialTest.java @@ -0,0 +1,145 @@ +/* + * 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.mail; + +import java.util.Collection; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + +import com.google.api.client.auth.oauth2.Credential; +import com.google.api.client.json.Json; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.gson.GsonFactory; +import com.google.api.client.testing.http.MockHttpTransport; +import com.google.api.client.testing.http.MockLowLevelHttpRequest; +import com.google.api.client.testing.http.MockLowLevelHttpResponse; +import com.google.api.services.gmail.Gmail; +import org.apache.camel.CamelContext; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.google.common.GoogleCredentialsHelper; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.impl.DefaultCamelContext; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +/** + * The client sends the OAuth access token in the Authorization header; the request URL does not carry it. + */ +class GoogleMailRequestCredentialTest { + + private static final String OAUTH = "clientId=id&clientSecret=secret&accessToken=atoken"; + + private final RecordingTransport transport = new RecordingTransport(); + + @Test + void shouldSendTheAccessTokenOfTheProducerOnlyInTheAuthorizationHeader() throws Exception { + try (CamelContext context = createContext()) { + Object body + = context.createProducerTemplate().requestBody("google-mail://users/getProfile?userId=me&" + OAUTH, + (Object) null); + + assertNotNull(body); + assertSentWithTheTokenInTheHeaderOnly(transport.requests.get(0)); + } + } + + @Test + void shouldSendTheAccessTokenOfTheConsumerOnlyInTheAuthorizationHeader() throws Exception { + try (CamelContext context = createContext()) { + MockEndpoint mock = context.getEndpoint("mock:result", MockEndpoint.class); + mock.expectedMinimumMessageCount(1); + context.addRoutes(new RouteBuilder() { + @Override + public void configure() { + from("google-mail://users/getProfile?userId=me&delay=100&" + OAUTH).to("mock:result"); + } + }); + + mock.assertIsSatisfied(); + assertSentWithTheTokenInTheHeaderOnly(transport.requests.get(0)); + } + } + + private static void assertSentWithTheTokenInTheHeaderOnly(MockLowLevelHttpRequest request) { + assertFalse(request.getUrl().contains("access_token"), request.getUrl()); + assertFalse(request.getUrl().contains("atoken"), request.getUrl()); + assertEquals("Bearer atoken", request.getFirstHeaderValue("Authorization")); + } + + private CamelContext createContext() { + CamelContext context = new DefaultCamelContext(); + context.getComponent("google-mail", GoogleMailComponent.class).setClientFactory(new OAuthClientFactory(transport)); + context.start(); + return context; + } + + /** + * Creates the client with the OAuth credential as the client factory of the component does, on a transport that + * records the requests. + */ + private static final class OAuthClientFactory implements GoogleMailClientFactory { + + private final JsonFactory jsonFactory = GsonFactory.getDefaultInstance(); + private final MockHttpTransport transport; + + private OAuthClientFactory(MockHttpTransport transport) { + this.transport = transport; + } + + @Override + public Gmail makeClient( + String clientId, String clientSecret, Collection<String> scopes, String applicationName, String refreshToken, + String accessToken) { + GoogleMailConfiguration configuration = new GoogleMailConfiguration(); + configuration.setClientId(clientId); + configuration.setClientSecret(clientSecret); + configuration.setRefreshToken(refreshToken); + configuration.setAccessToken(accessToken); + try { + Credential credential + = GoogleCredentialsHelper.getOAuthCredential(null, configuration, scopes, transport, jsonFactory); + return new Gmail.Builder(transport, jsonFactory, credential).setApplicationName("test").build(); + } catch (Exception e) { + throw new IllegalStateException(e); + } + } + + @Override + public Gmail makeClient( + CamelContext camelContext, String serviceAccountKey, Collection<String> scopes, String applicationName, + String delegate) { + throw new UnsupportedOperationException("OAuth only"); + } + } + + private static final class RecordingTransport extends MockHttpTransport { + + private final List<MockLowLevelHttpRequest> requests = new CopyOnWriteArrayList<>(); + + @Override + public MockLowLevelHttpRequest buildRequest(String method, String url) { + MockLowLevelHttpRequest request = new MockLowLevelHttpRequest(url); + request.setResponse(new MockLowLevelHttpResponse().setContentType(Json.MEDIA_TYPE) + .setContent("{\"emailAddress\":\"[email protected]\"}")); + requests.add(request); + return request; + } + } +} diff --git a/components/camel-google/camel-google-sheets/src/main/java/org/apache/camel/component/google/sheets/GoogleSheetsConsumer.java b/components/camel-google/camel-google-sheets/src/main/java/org/apache/camel/component/google/sheets/GoogleSheetsConsumer.java index f175f3406398..0faebbfca783 100644 --- a/components/camel-google/camel-google-sheets/src/main/java/org/apache/camel/component/google/sheets/GoogleSheetsConsumer.java +++ b/components/camel-google/camel-google-sheets/src/main/java/org/apache/camel/component/google/sheets/GoogleSheetsConsumer.java @@ -17,14 +17,11 @@ package org.apache.camel.component.google.sheets; import java.util.Map; -import java.util.Map.Entry; import com.google.api.client.googleapis.services.AbstractGoogleClientRequest; import org.apache.camel.Processor; import org.apache.camel.RuntimeCamelException; import org.apache.camel.component.google.sheets.internal.GoogleSheetsApiName; -import org.apache.camel.spi.BeanIntrospection; -import org.apache.camel.support.PluginHelper; import org.apache.camel.support.component.AbstractApiConsumer; /** @@ -40,11 +37,7 @@ public class GoogleSheetsConsumer extends AbstractApiConsumer<GoogleSheetsApiNam protected Object doInvokeMethod(Map<String, Object> properties) throws RuntimeCamelException { AbstractGoogleClientRequest<?> request = (AbstractGoogleClientRequest) super.doInvokeMethod(properties); try { - BeanIntrospection beanIntrospection - = PluginHelper.getBeanIntrospection(getEndpoint().getCamelContext()); - for (Entry<String, Object> p : properties.entrySet()) { - beanIntrospection.setProperty(getEndpoint().getCamelContext(), request, p.getKey(), p.getValue()); - } + ((GoogleSheetsEndpoint) getEndpoint()).configureRequest(request, properties); return request.execute(); } catch (Exception e) { throw new RuntimeCamelException(e); diff --git a/components/camel-google/camel-google-sheets/src/main/java/org/apache/camel/component/google/sheets/GoogleSheetsEndpoint.java b/components/camel-google/camel-google-sheets/src/main/java/org/apache/camel/component/google/sheets/GoogleSheetsEndpoint.java index 334cce5fdcd8..3718dd157392 100644 --- a/components/camel-google/camel-google-sheets/src/main/java/org/apache/camel/component/google/sheets/GoogleSheetsEndpoint.java +++ b/components/camel-google/camel-google-sheets/src/main/java/org/apache/camel/component/google/sheets/GoogleSheetsEndpoint.java @@ -16,10 +16,15 @@ */ package org.apache.camel.component.google.sheets; +import java.util.Collections; +import java.util.List; import java.util.Map; +import java.util.Set; +import java.util.TreeSet; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; +import com.google.api.client.googleapis.services.AbstractGoogleClientRequest; import com.google.api.services.sheets.v4.Sheets; import org.apache.camel.Category; import org.apache.camel.Consumer; @@ -29,9 +34,11 @@ import org.apache.camel.component.google.sheets.internal.GoogleSheetsApiCollecti import org.apache.camel.component.google.sheets.internal.GoogleSheetsApiName; import org.apache.camel.component.google.sheets.internal.GoogleSheetsConstants; import org.apache.camel.component.google.sheets.internal.GoogleSheetsPropertiesHelper; +import org.apache.camel.spi.BeanIntrospection; import org.apache.camel.spi.EndpointServiceLocation; import org.apache.camel.spi.UriEndpoint; import org.apache.camel.spi.UriParam; +import org.apache.camel.support.PluginHelper; import org.apache.camel.support.component.AbstractApiEndpoint; import org.apache.camel.support.component.ApiMethod; import org.apache.camel.support.component.ApiMethodPropertiesHelper; @@ -46,6 +53,20 @@ import org.apache.camel.util.ObjectHelper; public class GoogleSheetsEndpoint extends AbstractApiEndpoint<GoogleSheetsApiName, GoogleSheetsConfiguration> implements EndpointServiceLocation { + /** + * The options that make up the credential of the client. The client sends the credential in the Authorization + * header, so they are not set on the request, where accessToken would become the access_token query parameter. The + * names are matched ignoring case, as a header such as CamelGoogleSheets.AccessToken sets the same property. + */ + private static final Set<String> CREDENTIAL_OPTIONS; + + static { + Set<String> options = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); + options.addAll( + List.of("clientId", "clientSecret", "accessToken", "refreshToken", "serviceAccountKey", "delegate")); + CREDENTIAL_OPTIONS = Collections.unmodifiableSet(options); + } + @UriParam private GoogleSheetsConfiguration configuration; private final Lock clientLock = new ReentrantLock(); @@ -120,6 +141,19 @@ public class GoogleSheetsEndpoint extends AbstractApiEndpoint<GoogleSheetsApiNam return answer; } + /** + * Sets the options and headers on the request of the method, such as the standard parameters of the Google API, + * except the options of the credential, which the client already sends in the Authorization header. + */ + void configureRequest(AbstractGoogleClientRequest<?> request, Map<String, Object> properties) throws Exception { + BeanIntrospection beanIntrospection = PluginHelper.getBeanIntrospection(getCamelContext()); + for (Map.Entry<String, Object> p : properties.entrySet()) { + if (!CREDENTIAL_OPTIONS.contains(p.getKey())) { + beanIntrospection.setProperty(getCamelContext(), request, p.getKey(), p.getValue()); + } + } + } + @Override public Object getApiProxy(ApiMethod method, Map<String, Object> args) { return apiProxy; diff --git a/components/camel-google/camel-google-sheets/src/main/java/org/apache/camel/component/google/sheets/GoogleSheetsProducer.java b/components/camel-google/camel-google-sheets/src/main/java/org/apache/camel/component/google/sheets/GoogleSheetsProducer.java index 15ed352b7fa3..6398ebd68357 100644 --- a/components/camel-google/camel-google-sheets/src/main/java/org/apache/camel/component/google/sheets/GoogleSheetsProducer.java +++ b/components/camel-google/camel-google-sheets/src/main/java/org/apache/camel/component/google/sheets/GoogleSheetsProducer.java @@ -17,14 +17,11 @@ package org.apache.camel.component.google.sheets; import java.util.Map; -import java.util.Map.Entry; import com.google.api.client.googleapis.services.AbstractGoogleClientRequest; import org.apache.camel.RuntimeCamelException; import org.apache.camel.component.google.sheets.internal.GoogleSheetsApiName; import org.apache.camel.component.google.sheets.internal.GoogleSheetsPropertiesHelper; -import org.apache.camel.spi.BeanIntrospection; -import org.apache.camel.support.PluginHelper; import org.apache.camel.support.component.AbstractApiProducer; import org.apache.camel.support.component.ApiMethod; @@ -43,11 +40,7 @@ public class GoogleSheetsProducer extends AbstractApiProducer<GoogleSheetsApiNam AbstractGoogleClientRequest<?> request = (AbstractGoogleClientRequest) super.doInvokeMethod(method, properties); try { - BeanIntrospection beanIntrospection - = PluginHelper.getBeanIntrospection(getEndpoint().getCamelContext()); - for (Entry<String, Object> p : properties.entrySet()) { - beanIntrospection.setProperty(getEndpoint().getCamelContext(), request, p.getKey(), p.getValue()); - } + ((GoogleSheetsEndpoint) getEndpoint()).configureRequest(request, properties); return request.execute(); } catch (Exception e) { throw new RuntimeCamelException(e); diff --git a/components/camel-google/camel-google-sheets/src/test/java/org/apache/camel/component/google/sheets/GoogleSheetsRequestCredentialTest.java b/components/camel-google/camel-google-sheets/src/test/java/org/apache/camel/component/google/sheets/GoogleSheetsRequestCredentialTest.java new file mode 100644 index 000000000000..ae6518173899 --- /dev/null +++ b/components/camel-google/camel-google-sheets/src/test/java/org/apache/camel/component/google/sheets/GoogleSheetsRequestCredentialTest.java @@ -0,0 +1,116 @@ +/* + * 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.sheets; + +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + +import com.google.api.client.json.Json; +import com.google.api.client.testing.http.MockHttpTransport; +import com.google.api.client.testing.http.MockLowLevelHttpRequest; +import com.google.api.client.testing.http.MockLowLevelHttpResponse; +import com.google.api.services.sheets.v4.model.ValueRange; +import org.apache.camel.CamelContext; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.impl.DefaultCamelContext; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; + +/** + * The client sends the OAuth access token in the Authorization header; the request URL does not carry it. + */ +class GoogleSheetsRequestCredentialTest { + + private static final String OAUTH = "clientId=id&clientSecret=secret&accessToken=atoken"; + + private final RecordingTransport transport = new RecordingTransport(); + + @Test + void shouldSendTheAccessTokenOfTheProducerOnlyInTheAuthorizationHeader() throws Exception { + try (CamelContext context = createContext()) { + ValueRange body = context.createProducerTemplate().requestBody( + "google-sheets://data/get?spreadsheetId=abc&range=A1:B1&" + OAUTH, null, ValueRange.class); + + assertEquals(List.of(List.of("a", "b")), body.getValues()); + assertSentWithTheTokenInTheHeaderOnly(transport.requests.get(0)); + } + } + + @Test + void shouldNotSendAnAccessTokenHeaderOnTheRequestWhateverItsCase() throws Exception { + for (String header : List.of("CamelGoogleSheets.accessToken", "CamelGoogleSheets.AccessToken")) { + transport.requests.clear(); + try (CamelContext context = createContext()) { + context.createProducerTemplate().requestBodyAndHeader( + "google-sheets://data/get?spreadsheetId=abc&range=A1:B1&" + OAUTH, null, header, "htoken"); + + String url = transport.requests.get(0).getUrl(); + assertFalse(url.contains("access_token"), header + " -> " + url); + assertFalse(url.contains("htoken"), header + " -> " + url); + } + } + } + + @Test + void shouldSendTheAccessTokenOfTheConsumerOnlyInTheAuthorizationHeader() throws Exception { + try (CamelContext context = createContext()) { + MockEndpoint mock = context.getEndpoint("mock:result", MockEndpoint.class); + mock.expectedMinimumMessageCount(1); + context.addRoutes(new RouteBuilder() { + @Override + public void configure() { + from("google-sheets://data/get?spreadsheetId=abc&range=A1:B1&delay=100&" + OAUTH) + .to("mock:result"); + } + }); + + mock.assertIsSatisfied(); + assertSentWithTheTokenInTheHeaderOnly(transport.requests.get(0)); + } + } + + private static void assertSentWithTheTokenInTheHeaderOnly(MockLowLevelHttpRequest request) { + assertFalse(request.getUrl().contains("access_token"), request.getUrl()); + assertFalse(request.getUrl().contains("atoken"), request.getUrl()); + assertEquals("Bearer atoken", request.getFirstHeaderValue("Authorization")); + } + + private CamelContext createContext() throws Exception { + CamelContext context = new DefaultCamelContext(); + context.getComponent("google-sheets", GoogleSheetsComponent.class) + .setClientFactory(new BatchGoogleSheetsClientFactory(transport)); + context.start(); + return context; + } + + private static final class RecordingTransport extends MockHttpTransport { + + private final List<MockLowLevelHttpRequest> requests = new CopyOnWriteArrayList<>(); + + @Override + public MockLowLevelHttpRequest buildRequest(String method, String url) { + MockLowLevelHttpRequest request = new MockLowLevelHttpRequest(url); + request.setResponse(new MockLowLevelHttpResponse().setContentType(Json.MEDIA_TYPE) + .setContent("{\"range\":\"Sheet1!A1:B1\",\"majorDimension\":\"ROWS\",\"values\":[[\"a\",\"b\"]]}")); + requests.add(request); + return request; + } + } +}
