oscerd commented on a change in pull request #6726: URL: https://github.com/apache/camel/pull/6726#discussion_r783913669
########## File path: components/camel-google/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/BatchGoogleMailClientFactory.java ########## @@ -1,65 +1,99 @@ -/* - * 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 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.RuntimeCamelException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class BatchGoogleMailClientFactory implements GoogleMailClientFactory { - private static final Logger LOG = LoggerFactory.getLogger(BatchGoogleMailClientFactory.class); - private NetHttpTransport transport; - private JacksonFactory jsonFactory; - - public BatchGoogleMailClientFactory() { - this.transport = new NetHttpTransport(); - this.jsonFactory = new JacksonFactory(); - } - - @Override - public Gmail makeClient( - String clientId, String clientSecret, String applicationName, String refreshToken, String accessToken) { - if (clientId == null || clientSecret == null) { - throw new IllegalArgumentException("clientId and clientSecret are required to create Gmail client."); - } - try { - Credential credential = authorize(clientId, clientSecret); - - if (refreshToken != null && !"".equals(refreshToken)) { - credential.setRefreshToken(refreshToken); - } - if (accessToken != null && !"".equals(accessToken)) { - credential.setAccessToken(accessToken); - } - 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(); - } -} +/* + * 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.io.IOException; +import java.util.List; + +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.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class BatchGoogleMailClientFactory implements GoogleMailClientFactory { + private static final Logger LOG = LoggerFactory.getLogger(BatchGoogleMailClientFactory.class); + private NetHttpTransport transport; + private JacksonFactory jsonFactory; + + public BatchGoogleMailClientFactory() { + this.transport = new NetHttpTransport(); + this.jsonFactory = new JacksonFactory(); + } + + @Override + public Gmail makeClient( + String clientId, String clientSecret, String applicationName, String refreshToken, String accessToken) { + if (clientId == null || clientSecret == null) { + throw new IllegalArgumentException("clientId and clientSecret are required to create Gmail client."); + } + try { + Credential credential = authorize(clientId, clientSecret); + + if (refreshToken != null && !"".equals(refreshToken)) { + credential.setRefreshToken(refreshToken); + } + if (accessToken != null && !"".equals(accessToken)) { + credential.setAccessToken(accessToken); + } + 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 keyResource, String applicationName, String delegate, List<String> gmailScopes) { + if (keyResource == null) { + throw new IllegalArgumentException("keyResource is required to create Gmail client."); + } + try { + Credential credential = authorizeServiceAccount(camelContext, keyResource, delegate, gmailScopes); + 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 keyResource, String delegate, List<String> gmailScopes) { + // authorize + try { + GoogleCredential cred = GoogleCredential + .fromStream(ResourceHelper.resolveMandatoryResourceAsInputStream(camelContext, keyResource)) Review comment: My fault, I didn't notice you were doing the manipulation here. Looks good then. Thanks a bunch. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@camel.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org