CAMEL-8110 - google mail component
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/3c6769db Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/3c6769db Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/3c6769db Branch: refs/heads/master Commit: 3c6769dbd287d4ec919104649947b84eafc3050c Parents: 7ffb70e Author: Jonathan Anstey <jans...@gmail.com> Authored: Tue Dec 2 17:13:30 2014 -0330 Committer: Jonathan Anstey <jans...@gmail.com> Committed: Tue Dec 2 17:16:54 2014 -0330 ---------------------------------------------------------------------- components/camel-google-mail/pom.xml | 311 +++++++++++++++++++ .../mail/BatchGoogleMailClientFactory.java | 68 ++++ .../google/mail/GoogleMailClientFactory.java | 27 ++ .../google/mail/GoogleMailComponent.java | 75 +++++ .../google/mail/GoogleMailConfiguration.java | 100 ++++++ .../google/mail/GoogleMailConsumer.java | 58 ++++ .../google/mail/GoogleMailEndpoint.java | 111 +++++++ .../google/mail/GoogleMailProducer.java | 58 ++++ .../mail/internal/GoogleMailConstants.java | 29 ++ .../internal/GoogleMailPropertiesHelper.java | 39 +++ .../org/apache/camel/component/google-mail | 1 + .../mail/AbstractGoogleMailTestSupport.java | 88 ++++++ .../google/mail/GmailUsersIntegrationTest.java | 60 ++++ .../mail/GmailUsersLabelsIntegrationTest.java | 119 +++++++ .../mail/GmailUsersMessagesIntegrationTest.java | 222 +++++++++++++ .../mail/GmailUsersThreadsIntegrationTest.java | 136 ++++++++ .../src/test/resources/log4j.properties | 30 ++ .../src/test/resources/test-options.properties | 26 ++ 18 files changed, 1558 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/3c6769db/components/camel-google-mail/pom.xml ---------------------------------------------------------------------- diff --git a/components/camel-google-mail/pom.xml b/components/camel-google-mail/pom.xml new file mode 100644 index 0000000..f7c0272 --- /dev/null +++ b/components/camel-google-mail/pom.xml @@ -0,0 +1,311 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.camel</groupId> + <artifactId>components</artifactId> + <version>2.15-SNAPSHOT</version> + </parent> + + <artifactId>camel-google-mail</artifactId> + <packaging>bundle</packaging> + <name>Camel GoogleMail Component</name> + <description>Camel Component for GoogleMail</description> + + <properties> + <schemeName>google-mail</schemeName> + <componentName>GoogleMail</componentName> + <componentPackage>org.apache.camel.component.google.mail</componentPackage> + <outPackage>org.apache.camel.component.google.mail.internal</outPackage> + <camel.osgi.private.pkg>org.apache.camel.component.google.mail.internal</camel.osgi.private.pkg> + <camel.osgi.export.pkg>org.apache.camel.component.google.mail</camel.osgi.export.pkg> + <camel.osgi.export.service> + org.apache.camel.spi.ComponentResolver;component=google-mail + </camel.osgi.export.service> + </properties> + + <dependencies> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-core</artifactId> + </dependency> + <dependency> + <groupId>com.google.api-client</groupId> + <artifactId>google-api-client</artifactId> + <version>${google-api-client-version}</version> + </dependency> + <dependency> + <groupId>com.google.oauth-client</groupId> + <artifactId>google-oauth-client-jetty</artifactId> + <version>${google-api-client-version}</version> + </dependency> + <dependency> + <groupId>com.google.http-client</groupId> + <artifactId>google-http-client-jackson2</artifactId> + <version>${google-api-client-version}</version> + </dependency> + <dependency> + <groupId>com.google.apis</groupId> + <artifactId>google-api-services-gmail</artifactId> + <version>${google-api-services-mail-version}</version> + </dependency> + + <dependency> + <groupId>javax.mail</groupId> + <artifactId>mail</artifactId> + <version>${javax-mail-version}</version> + <exclusions> + <!-- javax activation is part of the JDK now --> + <exclusion> + <groupId>javax.activation</groupId> + <artifactId>activation</artifactId> + </exclusion> + </exclusions> + </dependency> + + <!-- Camel annotations in provided scope to avoid compile errors in IDEs --> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>spi-annotations</artifactId> + <version>${project.version}</version> + <scope>provided</scope> + </dependency> + + <!-- Component API javadoc in provided scope to read API signatures --> + <dependency> + <groupId>com.google.apis</groupId> + <artifactId>google-api-services-gmail</artifactId> + <version>${google-api-services-mail-version}</version> + <classifier>javadoc</classifier> + <scope>provided</scope> + </dependency> + + <!-- logging --> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-log4j12</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>log4j</groupId> + <artifactId>log4j</artifactId> + <scope>test</scope> + </dependency> + + <!-- testing --> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + + <build> + <defaultGoal>install</defaultGoal> + + <plugins> + + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>2.5.1</version> + <configuration> + <source>1.6</source> + <target>1.6</target> + </configuration> + </plugin> + + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-resources-plugin</artifactId> + <version>2.6</version> + <configuration> + <encoding>UTF-8</encoding> + </configuration> + </plugin> + + <!-- generate Component source and test source --> + <plugin> + <groupId>org.apache.camel</groupId> + <artifactId>camel-api-component-maven-plugin</artifactId> + <executions> + <execution> + <id>generate-test-component-classes</id> + <goals> + <goal>fromApis</goal> + </goals> + <configuration> + <apis> + <api> + <apiName>threads</apiName> + <proxyClass>com.google.api.services.gmail.Gmail$Users$Threads</proxyClass> + <fromJavadoc /> + </api> + <api> + <apiName>messages</apiName> + <proxyClass>com.google.api.services.gmail.Gmail$Users$Messages</proxyClass> + <fromJavadoc /> + </api> + <api> + <apiName>attachments</apiName> + <proxyClass>com.google.api.services.gmail.Gmail$Users$Messages$Attachments</proxyClass> + <fromJavadoc /> + </api> + <api> + <apiName>labels</apiName> + <proxyClass>com.google.api.services.gmail.Gmail$Users$Labels</proxyClass> + <fromJavadoc /> + </api> + <api> + <apiName>history</apiName> + <proxyClass>com.google.api.services.gmail.Gmail$Users$History</proxyClass> + <fromJavadoc /> + </api> + <api> + <apiName>drafts</apiName> + <proxyClass>com.google.api.services.gmail.Gmail$Users$Drafts</proxyClass> + <fromJavadoc /> + </api> + <api> + <apiName>users</apiName> + <proxyClass>com.google.api.services.gmail.Gmail$Users</proxyClass> + <fromJavadoc> + <includeMethods>getProfile</includeMethods> + </fromJavadoc> + </api> + </apis> + <substitutions> + <substitution> + <method>^.+$</method> + <argName>content</argName> + <argType>com.google.api.services.gmail.model.Channel</argType> + <replacement>contentChannel</replacement> + </substitution> + <substitution> + <method>^.+$</method> + <argName>content</argName> + <argType>com.google.api.services.gmail.model.ModifyMessageRequest</argType> + <replacement>modifyMessageRequest</replacement> + </substitution> + </substitutions> + </configuration> + </execution> + </executions> + </plugin> + + <!-- add generated source and test source to build --> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>build-helper-maven-plugin</artifactId> + <version>1.8</version> + <executions> + <execution> + <id>add-generated-sources</id> + <goals> + <goal>add-source</goal> + </goals> + <configuration> + <sources> + <source>${project.build.directory}/generated-sources/camel-component</source> + </sources> + </configuration> + </execution> + <execution> + <id>add-generated-test-sources</id> + <goals> + <goal>add-test-source</goal> + </goals> + <configuration> + <sources> + <source>${project.build.directory}/generated-test-sources/camel-component</source> + </sources> + </configuration> + </execution> + </executions> + </plugin> + + </plugins> + + <pluginManagement> + <plugins> + <plugin> + <groupId>org.apache.camel</groupId> + <artifactId>camel-api-component-maven-plugin</artifactId> + <version>${project.version}</version> + <configuration> + <scheme>${schemeName}</scheme> + <componentName>${componentName}</componentName> + <componentPackage>${componentPackage}</componentPackage> + <outPackage>${outPackage}</outPackage> + </configuration> + </plugin> + </plugins> + </pluginManagement> + + </build> + + <reporting> + <plugins> + <plugin> + <groupId>org.apache.camel</groupId> + <artifactId>camel-api-component-maven-plugin</artifactId> + <version>${project.version}</version> + <configuration> + <scheme>${schemeName}</scheme> + <componentName>${componentName}</componentName> + <componentPackage>${componentPackage}</componentPackage> + <outPackage>${outPackage}</outPackage> + </configuration> + </plugin> + </plugins> + </reporting> + + <profiles> + <profile> + <id>google-mail-test</id> + <build> + <plugins> + <plugin> + <artifactId>maven-surefire-plugin</artifactId> + <configuration> + <childDelegation>false</childDelegation> + <useFile>true</useFile> + <forkCount>1</forkCount> + <reuseForks>true</reuseForks> + <forkedProcessTimeoutInSeconds>300</forkedProcessTimeoutInSeconds> + <excludes> + <exclude>**/*XXXTest.java</exclude> + </excludes> + <includes> + <include>**/*Test.java</include> + </includes> + </configuration> + </plugin> + </plugins> + </build> + </profile> + </profiles> + +</project> http://git-wip-us.apache.org/repos/asf/camel/blob/3c6769db/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/BatchGoogleMailClientFactory.java ---------------------------------------------------------------------- diff --git a/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/BatchGoogleMailClientFactory.java b/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/BatchGoogleMailClientFactory.java new file mode 100644 index 0000000..626d232 --- /dev/null +++ b/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/BatchGoogleMailClientFactory.java @@ -0,0 +1,68 @@ +/** + * 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 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.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, Collection<String> scopes, String applicationName, String refreshToken, String accessToken) { + Credential credential; + try { + credential = authorize(clientId, clientSecret, scopes); + + 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) { + LOG.error("Could not create Google Drive client.", e); + } + return null; + } + + // Authorizes the installed application to access user's protected data. + private Credential authorize(String clientId, String clientSecret, Collection<String> scopes) throws Exception { + // authorize + return new GoogleCredential.Builder() + .setJsonFactory(jsonFactory) + .setTransport(transport) + .setClientSecrets(clientId, clientSecret) + .build(); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/3c6769db/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailClientFactory.java ---------------------------------------------------------------------- diff --git a/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailClientFactory.java b/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailClientFactory.java new file mode 100644 index 0000000..d21e121 --- /dev/null +++ b/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailClientFactory.java @@ -0,0 +1,27 @@ +/** + * 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 com.google.api.services.gmail.Gmail; + +public interface GoogleMailClientFactory { + + Gmail makeClient(String clientId, String clientSecret, Collection<String> scopes, String applicationName, String refreshToken, String accessToken); + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/3c6769db/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailComponent.java b/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailComponent.java new file mode 100644 index 0000000..4d88110 --- /dev/null +++ b/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailComponent.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.mail; + + +import com.google.api.services.gmail.Gmail; + +import org.apache.camel.CamelContext; +import org.apache.camel.Endpoint; +import org.apache.camel.spi.UriEndpoint; +import org.apache.camel.util.component.AbstractApiComponent; +import org.apache.camel.component.google.mail.internal.GoogleMailApiCollection; +import org.apache.camel.component.google.mail.internal.GoogleMailApiName; + +/** + * Represents the component that manages {@link GoogleMailEndpoint}. + */ +@UriEndpoint(scheme = "google-mail", consumerClass = GoogleMailConsumer.class, consumerPrefix = "consumer") +public class GoogleMailComponent extends AbstractApiComponent<GoogleMailApiName, GoogleMailConfiguration, GoogleMailApiCollection> { + + private Gmail client; + private GoogleMailClientFactory clientFactory; + + public GoogleMailComponent() { + super(GoogleMailEndpoint.class, GoogleMailApiName.class, GoogleMailApiCollection.getCollection()); + } + + public GoogleMailComponent(CamelContext context) { + super(context, GoogleMailEndpoint.class, GoogleMailApiName.class, GoogleMailApiCollection.getCollection()); + } + + @Override + protected GoogleMailApiName getApiName(String apiNameStr) throws IllegalArgumentException { + return GoogleMailApiName.fromValue(apiNameStr); + } + + public Gmail getClient() { + if (client == null) { + client = getClientFactory().makeClient(configuration.getClientId(), configuration.getClientSecret(), configuration.getScopes(), + configuration.getApplicationName(), configuration.getRefreshToken(), configuration.getAccessToken()); + } + return client; + } + + public GoogleMailClientFactory getClientFactory() { + if (clientFactory == null) { + clientFactory = new BatchGoogleMailClientFactory(); + } + return clientFactory; + } + + public void setClientFactory(GoogleMailClientFactory clientFactory) { + this.clientFactory = clientFactory; + } + + @Override + protected Endpoint createEndpoint(String uri, String methodName, GoogleMailApiName apiName, + GoogleMailConfiguration endpointConfiguration) { + return new GoogleMailEndpoint(uri, this, apiName, methodName, endpointConfiguration); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/3c6769db/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailConfiguration.java ---------------------------------------------------------------------- diff --git a/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailConfiguration.java b/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailConfiguration.java new file mode 100644 index 0000000..66786a7 --- /dev/null +++ b/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailConfiguration.java @@ -0,0 +1,100 @@ +/** + * 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.Arrays; +import java.util.List; + +import com.google.api.services.gmail.GmailScopes; + +import org.apache.camel.spi.UriParam; +import org.apache.camel.spi.UriParams; + +/** + * Component configuration for GoogleMail component. + */ +@UriParams +public class GoogleMailConfiguration { + private static final List<String> DEFAULT_SCOPES = Arrays.asList(GmailScopes.GMAIL_COMPOSE, GmailScopes.GMAIL_MODIFY, GmailScopes.MAIL_GOOGLE_COM); + + @UriParam + private List<String> scopes = DEFAULT_SCOPES; + + @UriParam + private String clientId; + + @UriParam + private String clientSecret; + + @UriParam + private String accessToken; + + @UriParam + private String refreshToken; + + @UriParam + private String applicationName; + + public String getClientId() { + return clientId; + } + + public void setClientId(String clientId) { + this.clientId = clientId; + } + + public String getClientSecret() { + return clientSecret; + } + + public void setClientSecret(String clientSecret) { + this.clientSecret = clientSecret; + } + + public String getAccessToken() { + return accessToken; + } + + public void setAccessToken(String accessToken) { + this.accessToken = accessToken; + } + + public String getRefreshToken() { + return refreshToken; + } + + public void setRefreshToken(String refreshToken) { + this.refreshToken = refreshToken; + } + + public String getApplicationName() { + return applicationName; + } + + public void setApplicationName(String applicationName) { + this.applicationName = applicationName; + } + + public List<String> getScopes() { + return scopes; + } + + public void setScopes(List<String> scopes) { + this.scopes = scopes; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/3c6769db/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailConsumer.java ---------------------------------------------------------------------- diff --git a/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailConsumer.java b/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailConsumer.java new file mode 100644 index 0000000..65dbc84 --- /dev/null +++ b/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailConsumer.java @@ -0,0 +1,58 @@ +/** + * 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.Map; + +import com.google.api.client.googleapis.services.AbstractGoogleClientRequest; + +import org.apache.camel.Processor; +import org.apache.camel.RuntimeCamelException; +import org.apache.camel.util.IntrospectionSupport; +import org.apache.camel.util.component.AbstractApiConsumer; +import org.apache.camel.component.google.mail.internal.GoogleMailApiName; + +/** + * The GoogleMail consumer. + */ +public class GoogleMailConsumer extends AbstractApiConsumer<GoogleMailApiName, GoogleMailConfiguration> { + + public GoogleMailConsumer(GoogleMailEndpoint endpoint, Processor processor) { + super(endpoint, processor); + } + + @Override + protected Object doInvokeMethod(Map<String, Object> properties) throws RuntimeCamelException { + AbstractGoogleClientRequest request = (AbstractGoogleClientRequest) super.doInvokeMethod(properties); + try { + setProperty(properties, request, "q"); + setProperty(properties, request, "maxResults"); + setProperty(properties, request, "pageToken"); + setProperty(properties, request, "format"); + return request.execute(); + } catch (Exception e) { + throw new RuntimeCamelException(e); + } + } + + private void setProperty(Map<String, Object> properties, AbstractGoogleClientRequest request, String key) throws Exception { + if (properties.containsKey(key)) { + IntrospectionSupport.setProperty(getEndpoint().getCamelContext().getTypeConverter(), request, key, properties.get(key)); + } + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/3c6769db/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailEndpoint.java b/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailEndpoint.java new file mode 100644 index 0000000..516ce1b --- /dev/null +++ b/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailEndpoint.java @@ -0,0 +1,111 @@ +/** + * 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.Map; + +import com.google.api.services.gmail.Gmail; + +import org.apache.camel.Consumer; +import org.apache.camel.Processor; +import org.apache.camel.Producer; +import org.apache.camel.spi.UriEndpoint; +import org.apache.camel.util.component.AbstractApiEndpoint; +import org.apache.camel.util.component.ApiMethod; +import org.apache.camel.util.component.ApiMethodPropertiesHelper; +import org.apache.camel.component.google.mail.internal.GoogleMailApiCollection; +import org.apache.camel.component.google.mail.internal.GoogleMailApiName; +import org.apache.camel.component.google.mail.internal.GoogleMailConstants; +import org.apache.camel.component.google.mail.internal.GoogleMailPropertiesHelper; + +/** + * Represents a GoogleMail endpoint. + */ +@UriEndpoint(scheme = "google-mail", consumerClass = GoogleMailConsumer.class, consumerPrefix = "consumer") +public class GoogleMailEndpoint extends AbstractApiEndpoint<GoogleMailApiName, GoogleMailConfiguration> { + + // TODO create and manage API proxy + private Object apiProxy; + + public GoogleMailEndpoint(String uri, GoogleMailComponent component, + GoogleMailApiName apiName, String methodName, GoogleMailConfiguration endpointConfiguration) { + super(uri, component, apiName, methodName, GoogleMailApiCollection.getCollection().getHelper(apiName), endpointConfiguration); + + } + + public Producer createProducer() throws Exception { + return new GoogleMailProducer(this); + } + + public Consumer createConsumer(Processor processor) throws Exception { + // make sure inBody is not set for consumers + if (inBody != null) { + throw new IllegalArgumentException("Option inBody is not supported for consumer endpoint"); + } + final GoogleMailConsumer consumer = new GoogleMailConsumer(this, processor); + // also set consumer.* properties + configureConsumer(consumer); + return consumer; + } + + @Override + protected ApiMethodPropertiesHelper<GoogleMailConfiguration> getPropertiesHelper() { + return GoogleMailPropertiesHelper.getHelper(); + } + + protected String getThreadProfileName() { + return GoogleMailConstants.THREAD_PROFILE_NAME; + } + + @Override + protected void afterConfigureProperties() { + switch (apiName) { + case ATTACHMENTS: + apiProxy = getClient().users().messages().attachments(); + break; + case DRAFTS: + apiProxy = getClient().users().drafts(); + break; + case HISTORY: + apiProxy = getClient().users().history(); + break; + case LABELS: + apiProxy = getClient().users().labels(); + break; + case MESSAGES: + apiProxy = getClient().users().messages(); + break; + case THREADS: + apiProxy = getClient().users().threads(); + break; + case USERS: + apiProxy = getClient().users(); + break; + default: + throw new IllegalArgumentException("Invalid API name " + apiName); + } + } + + public Gmail getClient() { + return ((GoogleMailComponent)getComponent()).getClient(); + } + + @Override + public Object getApiProxy(ApiMethod method, Map<String, Object> args) { + return apiProxy; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/3c6769db/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailProducer.java b/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailProducer.java new file mode 100644 index 0000000..763ca17 --- /dev/null +++ b/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailProducer.java @@ -0,0 +1,58 @@ +/** + * 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.Map; + +import com.google.api.client.googleapis.services.AbstractGoogleClientRequest; + +import org.apache.camel.RuntimeCamelException; +import org.apache.camel.util.IntrospectionSupport; +import org.apache.camel.util.component.AbstractApiProducer; +import org.apache.camel.util.component.ApiMethod; +import org.apache.camel.component.google.mail.internal.GoogleMailApiName; +import org.apache.camel.component.google.mail.internal.GoogleMailPropertiesHelper; + +/** + * The GoogleMail producer. + */ +public class GoogleMailProducer extends AbstractApiProducer<GoogleMailApiName, GoogleMailConfiguration> { + + public GoogleMailProducer(GoogleMailEndpoint endpoint) { + super(endpoint, GoogleMailPropertiesHelper.getHelper()); + } + + @Override + protected Object doInvokeMethod(ApiMethod method, Map<String, Object> properties) throws RuntimeCamelException { + AbstractGoogleClientRequest request = (AbstractGoogleClientRequest) super.doInvokeMethod(method, properties); + try { + setProperty(properties, request, "q"); + setProperty(properties, request, "maxResults"); + setProperty(properties, request, "pageToken"); + setProperty(properties, request, "format"); + return request.execute(); + } catch (Exception e) { + throw new RuntimeCamelException(e); + } + } + + private void setProperty(Map<String, Object> properties, AbstractGoogleClientRequest request, String key) throws Exception { + if (properties.containsKey(key)) { + IntrospectionSupport.setProperty(getEndpoint().getCamelContext().getTypeConverter(), request, key, properties.get(key)); + } + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/3c6769db/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/internal/GoogleMailConstants.java ---------------------------------------------------------------------- diff --git a/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/internal/GoogleMailConstants.java b/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/internal/GoogleMailConstants.java new file mode 100644 index 0000000..f788ff3 --- /dev/null +++ b/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/internal/GoogleMailConstants.java @@ -0,0 +1,29 @@ +/** + * 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.internal; + +/** + * Constants for GoogleMail component. + */ +public interface GoogleMailConstants { + + // suffix for parameters when passed as exchange header properties + String PROPERTY_PREFIX = "CamelGoogleMail."; + + // thread profile name for this component + String THREAD_PROFILE_NAME = "CamelGoogleMail"; +} http://git-wip-us.apache.org/repos/asf/camel/blob/3c6769db/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/internal/GoogleMailPropertiesHelper.java ---------------------------------------------------------------------- diff --git a/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/internal/GoogleMailPropertiesHelper.java b/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/internal/GoogleMailPropertiesHelper.java new file mode 100644 index 0000000..17c2125 --- /dev/null +++ b/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/internal/GoogleMailPropertiesHelper.java @@ -0,0 +1,39 @@ +/** + * 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.internal; + +import org.apache.camel.component.google.mail.GoogleMailConfiguration; +import org.apache.camel.util.component.ApiMethodPropertiesHelper; + +/** + * Singleton {@link ApiMethodPropertiesHelper} for GoogleMail component. + */ +public final class GoogleMailPropertiesHelper extends ApiMethodPropertiesHelper<GoogleMailConfiguration> { + + private static GoogleMailPropertiesHelper helper; + + private GoogleMailPropertiesHelper() { + super(GoogleMailConfiguration.class, GoogleMailConstants.PROPERTY_PREFIX); + } + + public static synchronized GoogleMailPropertiesHelper getHelper() { + if (helper == null) { + helper = new GoogleMailPropertiesHelper(); + } + return helper; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/3c6769db/components/camel-google-mail/src/main/resources/META-INF/services/org/apache/camel/component/google-mail ---------------------------------------------------------------------- diff --git a/components/camel-google-mail/src/main/resources/META-INF/services/org/apache/camel/component/google-mail b/components/camel-google-mail/src/main/resources/META-INF/services/org/apache/camel/component/google-mail new file mode 100644 index 0000000..75513b1 --- /dev/null +++ b/components/camel-google-mail/src/main/resources/META-INF/services/org/apache/camel/component/google-mail @@ -0,0 +1 @@ +class=org.apache.camel.component.google.mail.GoogleMailComponent http://git-wip-us.apache.org/repos/asf/camel/blob/3c6769db/components/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/AbstractGoogleMailTestSupport.java ---------------------------------------------------------------------- diff --git a/components/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/AbstractGoogleMailTestSupport.java b/components/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/AbstractGoogleMailTestSupport.java new file mode 100644 index 0000000..77f5e35 --- /dev/null +++ b/components/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/AbstractGoogleMailTestSupport.java @@ -0,0 +1,88 @@ +/** + * 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.io.InputStream; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +import org.apache.camel.CamelContext; +import org.apache.camel.CamelExecutionException; +import org.apache.camel.component.google.mail.GoogleMailComponent; +import org.apache.camel.component.google.mail.GoogleMailConfiguration; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.apache.camel.util.IntrospectionSupport; + +/** + * Abstract base class for GoogleMail Integration tests generated by Camel API component maven plugin. + */ +public class AbstractGoogleMailTestSupport extends CamelTestSupport { + + private static final String TEST_OPTIONS_PROPERTIES = "/test-options.properties"; + // userid of the currently authenticated user + public static final String CURRENT_USERID = "me"; + + + @Override + protected CamelContext createCamelContext() throws Exception { + + final CamelContext context = super.createCamelContext(); + + // read GoogleMail component configuration from TEST_OPTIONS_PROPERTIES + final Properties properties = new Properties(); + try { + properties.load(getClass().getResourceAsStream(TEST_OPTIONS_PROPERTIES)); + } catch (Exception e) { + throw new IOException(String.format("%s could not be loaded: %s", TEST_OPTIONS_PROPERTIES, e.getMessage()), + e); + } + + Map<String, Object> options = new HashMap<String, Object>(); + for (Map.Entry<Object, Object> entry : properties.entrySet()) { + options.put(entry.getKey().toString(), entry.getValue()); + } + + final GoogleMailConfiguration configuration = new GoogleMailConfiguration(); + IntrospectionSupport.setProperties(configuration, options); + + // add GoogleMailComponent to Camel context + final GoogleMailComponent component = new GoogleMailComponent(context); + component.setConfiguration(configuration); + context.addComponent("google-mail", component); + + return context; + } + + @Override + public boolean isCreateCamelContextPerClass() { + // only create the context once for this class + return true; + } + + @SuppressWarnings("unchecked") + protected <T> T requestBodyAndHeaders(String endpointUri, Object body, Map<String, Object> headers) + throws CamelExecutionException { + return (T) template().requestBodyAndHeaders(endpointUri, body, headers); + } + + @SuppressWarnings("unchecked") + protected <T> T requestBody(String endpoint, Object body) throws CamelExecutionException { + return (T) template().requestBody(endpoint, body); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/3c6769db/components/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/GmailUsersIntegrationTest.java ---------------------------------------------------------------------- diff --git a/components/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/GmailUsersIntegrationTest.java b/components/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/GmailUsersIntegrationTest.java new file mode 100644 index 0000000..3e49fac --- /dev/null +++ b/components/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/GmailUsersIntegrationTest.java @@ -0,0 +1,60 @@ +/** + * 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.HashMap; +import java.util.Map; + +import org.apache.camel.builder.RouteBuilder; +import org.junit.Ignore; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.camel.component.google.mail.internal.GoogleMailApiCollection; +import org.apache.camel.component.google.mail.internal.GmailUsersApiMethod; + +/** + * Test class for {@link com.google.api.services.gmail.Gmail$Users} APIs. + */ +public class GmailUsersIntegrationTest extends AbstractGoogleMailTestSupport { + + private static final Logger LOG = LoggerFactory.getLogger(GmailUsersIntegrationTest.class); + private static final String PATH_PREFIX = GoogleMailApiCollection.getCollection().getApiName(GmailUsersApiMethod.class).getName(); + + @Test + public void testGetProfile() throws Exception { + // using String message body for single parameter "userId" + final com.google.api.services.gmail.model.Profile result = requestBody("direct://GETPROFILE", CURRENT_USERID); + + assertNotNull("getProfile result", result); + assertNotNull("Should be email address associated with current account", result.getEmailAddress()); + LOG.debug("getProfile: " + result); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() { + // test route for getProfile + from("direct://GETPROFILE") + .to("google-mail://" + PATH_PREFIX + "/getProfile?inBody=userId"); + + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/3c6769db/components/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/GmailUsersLabelsIntegrationTest.java ---------------------------------------------------------------------- diff --git a/components/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/GmailUsersLabelsIntegrationTest.java b/components/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/GmailUsersLabelsIntegrationTest.java new file mode 100644 index 0000000..2773924 --- /dev/null +++ b/components/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/GmailUsersLabelsIntegrationTest.java @@ -0,0 +1,119 @@ +/** + * 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.HashMap; +import java.util.Map; + +import com.google.api.services.gmail.model.Label; + +import org.apache.camel.builder.RouteBuilder; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.camel.component.google.mail.internal.GoogleMailApiCollection; +import org.apache.camel.component.google.mail.internal.GmailUsersLabelsApiMethod; + +/** + * Test class for {@link com.google.api.services.gmail.Gmail$Users$Labels} APIs. + */ +public class GmailUsersLabelsIntegrationTest extends AbstractGoogleMailTestSupport { + + private static final String CAMEL_TEST_LABEL = "CamelTestLabel"; + private static final Logger LOG = LoggerFactory.getLogger(GmailUsersLabelsIntegrationTest.class); + private static final String PATH_PREFIX = GoogleMailApiCollection.getCollection().getApiName(GmailUsersLabelsApiMethod.class).getName(); + + @Test + public void testLabels() throws Exception { + // using String message body for single parameter "userId" + com.google.api.services.gmail.model.ListLabelsResponse labels = requestBody("direct://LIST", CURRENT_USERID); + + String labelId = null; + if (getTestLabel(labels) == null) { + Map<String, Object> headers = new HashMap<String, Object>(); + // parameter type is String + headers.put("CamelGoogleMail.userId", CURRENT_USERID); + Label label = new Label().setName(CAMEL_TEST_LABEL).setMessageListVisibility("show").setLabelListVisibility("labelShow"); + // parameter type is com.google.api.services.gmail.model.Label + headers.put("CamelGoogleMail.content", label); + + com.google.api.services.gmail.model.Label result = requestBodyAndHeaders("direct://CREATE", null, headers); + + assertNotNull("create result", result); + labelId = result.getId(); + } else { + labelId = getTestLabel(labels).getId(); + } + + // using String message body for single parameter "userId" + labels = requestBody("direct://LIST", CURRENT_USERID); + assertTrue(getTestLabel(labels) != null); + + Map<String, Object> headers = new HashMap<String, Object>(); + // parameter type is String + headers.put("CamelGoogleMail.userId", CURRENT_USERID); + // parameter type is String + headers.put("CamelGoogleMail.id", labelId); + + requestBodyAndHeaders("direct://DELETE", null, headers); + + // using String message body for single parameter "userId" + labels = requestBody("direct://LIST", CURRENT_USERID); + assertTrue(getTestLabel(labels) == null); + } + + private Label getTestLabel(com.google.api.services.gmail.model.ListLabelsResponse labels) { + for (Label label : labels.getLabels()) { + if (CAMEL_TEST_LABEL.equals(label.getName())) { + return label; + } + } + return null; + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() { + // test route for create + from("direct://CREATE") + .to("google-mail://" + PATH_PREFIX + "/create"); + + // test route for delete + from("direct://DELETE") + .to("google-mail://" + PATH_PREFIX + "/delete"); + + // test route for get + from("direct://GET") + .to("google-mail://" + PATH_PREFIX + "/get"); + + // test route for list + from("direct://LIST") + .to("google-mail://" + PATH_PREFIX + "/list?inBody=userId"); + + // test route for patch + from("direct://PATCH") + .to("google-mail://" + PATH_PREFIX + "/patch"); + + // test route for update + from("direct://UPDATE") + .to("google-mail://" + PATH_PREFIX + "/update"); + + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/3c6769db/components/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/GmailUsersMessagesIntegrationTest.java ---------------------------------------------------------------------- diff --git a/components/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/GmailUsersMessagesIntegrationTest.java b/components/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/GmailUsersMessagesIntegrationTest.java new file mode 100644 index 0000000..b682ae0 --- /dev/null +++ b/components/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/GmailUsersMessagesIntegrationTest.java @@ -0,0 +1,222 @@ +/** + * 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.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +import javax.mail.MessagingException; +import javax.mail.Session; +import javax.mail.internet.MimeMessage; +import javax.mail.internet.MimeMessage.RecipientType; + +import com.google.api.client.repackaged.org.apache.commons.codec.binary.Base64; +import com.google.api.services.gmail.model.ListMessagesResponse; +import com.google.api.services.gmail.model.Message; + +import org.apache.camel.builder.RouteBuilder; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.camel.component.google.mail.internal.GoogleMailApiCollection; +import org.apache.camel.component.google.mail.internal.GmailUsersMessagesApiMethod; + +/** + * Test class for {@link com.google.api.services.gmail.Gmail$Users$Messages} APIs. + */ +public class GmailUsersMessagesIntegrationTest extends AbstractGoogleMailTestSupport { + + // userid of the currently authenticated user + public static final String CURRENT_USERID = "me"; + private static final Logger LOG = LoggerFactory.getLogger(GmailUsersMessagesIntegrationTest.class); + private static final String PATH_PREFIX = GoogleMailApiCollection.getCollection().getApiName(GmailUsersMessagesApiMethod.class).getName(); + + @Test + public void testMessages() throws Exception { + + // ==== Send test email ==== + Message testEmail = createTestEmail(); + Map<String, Object> headers = new HashMap<String, Object>(); + // parameter type is String + headers.put("CamelGoogleMail.userId", CURRENT_USERID); + // parameter type is com.google.api.services.gmail.model.Message + headers.put("CamelGoogleMail.content", testEmail); + + com.google.api.services.gmail.model.Message result = requestBodyAndHeaders("direct://SEND", null, headers); + assertNotNull("send result", result); + String testEmailId = result.getId(); + + // ==== Search for message we just sent ==== + headers = new HashMap<String, Object>(); + headers.put("CamelGoogleMail.q", "subject:\"Hello from camel-google-mail\""); + // using String message body for single parameter "userId" + ListMessagesResponse listOfMessages = requestBody("direct://LIST", CURRENT_USERID); + assertTrue(idInList(testEmailId, listOfMessages)); + + // ===== trash it ==== + headers = new HashMap<String, Object>(); + // parameter type is String + headers.put("CamelGoogleMail.userId", CURRENT_USERID); + // parameter type is String + headers.put("CamelGoogleMail.id", testEmailId); + requestBodyAndHeaders("direct://TRASH", null, headers); + + // ==== Search for message we just trashed ==== + headers = new HashMap<String, Object>(); + headers.put("CamelGoogleMail.q", "subject:\"Hello from camel-google-mail\""); + // using String message body for single parameter "userId" + listOfMessages = requestBody("direct://LIST", CURRENT_USERID); + assertFalse(idInList(testEmailId, listOfMessages)); + + // ===== untrash it ==== + headers = new HashMap<String, Object>(); + // parameter type is String + headers.put("CamelGoogleMail.userId", CURRENT_USERID); + // parameter type is String + headers.put("CamelGoogleMail.id", testEmailId); + requestBodyAndHeaders("direct://UNTRASH", null, headers); + + // ==== Search for message we just trashed ==== + headers = new HashMap<String, Object>(); + headers.put("CamelGoogleMail.q", "subject:\"Hello from camel-google-mail\""); + // using String message body for single parameter "userId" + listOfMessages = requestBody("direct://LIST", CURRENT_USERID); + assertTrue(idInList(testEmailId, listOfMessages)); + + // ===== permanently delete it ==== + headers = new HashMap<String, Object>(); + // parameter type is String + headers.put("CamelGoogleMail.userId", CURRENT_USERID); + // parameter type is String + headers.put("CamelGoogleMail.id", testEmailId); + requestBodyAndHeaders("direct://DELETE", null, headers); + + // ==== Search for message we just deleted ==== + headers = new HashMap<String, Object>(); + headers.put("CamelGoogleMail.q", "subject:\"Hello from camel-google-mail\""); + // using String message body for single parameter "userId" + listOfMessages = requestBody("direct://LIST", CURRENT_USERID); + assertFalse(idInList(testEmailId, listOfMessages)); + } + + private boolean idInList(String testEmailId, ListMessagesResponse listOfMessages) { + assertNotNull("list result", listOfMessages); + assertTrue(!listOfMessages.getMessages().isEmpty()); + boolean foundMessage = false; + for (Message m : listOfMessages.getMessages()) { + if (testEmailId.equals(m.getId())) { + return true; + } + } + return false; + } + + private Message createTestEmail() throws MessagingException, IOException { + com.google.api.services.gmail.model.Profile profile = requestBody("google-mail://users/getProfile?inBody=userId", CURRENT_USERID); + Properties props = new Properties(); + Session session = Session.getDefaultInstance(props, null); + MimeMessage mm = new MimeMessage(session); + mm.addRecipients(RecipientType.TO, profile.getEmailAddress()); + mm.setSubject("Hello from camel-google-mail"); + mm.setContent("Camel rocks!", "text/plain"); + Message createMessageWithEmail = createMessageWithEmail(mm); + return createMessageWithEmail; + } + + private MimeMessage toMimeMessage(Message message) throws MessagingException { + byte[] emailBytes = Base64.decodeBase64(message.getRaw()); + + Properties props = new Properties(); + Session session = Session.getDefaultInstance(props, null); + + return new MimeMessage(session, new ByteArrayInputStream(emailBytes)); + } + + private Message createMessageWithEmail(MimeMessage email) + throws MessagingException, IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + email.writeTo(baos); + String encodedEmail = Base64.encodeBase64URLSafeString(baos.toByteArray()); + Message message = new Message(); + message.setRaw(encodedEmail); + return message; + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() { + // test route for attachments + from("direct://ATTACHMENTS") + .to("google-mail://" + PATH_PREFIX + "/attachments"); + + // test route for delete + from("direct://DELETE") + .to("google-mail://" + PATH_PREFIX + "/delete"); + + // test route for get + from("direct://GET") + .to("google-mail://" + PATH_PREFIX + "/get"); + + // test route for gmailImport + from("direct://GMAILIMPORT") + .to("google-mail://" + PATH_PREFIX + "/gmailImport"); + + // test route for gmailImport + from("direct://GMAILIMPORT_1") + .to("google-mail://" + PATH_PREFIX + "/gmailImport"); + + // test route for insert + from("direct://INSERT") + .to("google-mail://" + PATH_PREFIX + "/insert"); + + // test route for insert + from("direct://INSERT_1") + .to("google-mail://" + PATH_PREFIX + "/insert"); + + // test route for list + from("direct://LIST") + .to("google-mail://" + PATH_PREFIX + "/list?inBody=userId"); + + // test route for modify + from("direct://MODIFY") + .to("google-mail://" + PATH_PREFIX + "/modify"); + + // test route for send + from("direct://SEND") + .to("google-mail://" + PATH_PREFIX + "/send"); + + // test route for send + from("direct://SEND_1") + .to("google-mail://" + PATH_PREFIX + "/send"); + + // test route for trash + from("direct://TRASH") + .to("google-mail://" + PATH_PREFIX + "/trash"); + + // test route for untrash + from("direct://UNTRASH") + .to("google-mail://" + PATH_PREFIX + "/untrash"); + + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/3c6769db/components/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/GmailUsersThreadsIntegrationTest.java ---------------------------------------------------------------------- diff --git a/components/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/GmailUsersThreadsIntegrationTest.java b/components/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/GmailUsersThreadsIntegrationTest.java new file mode 100644 index 0000000..023be8c --- /dev/null +++ b/components/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/GmailUsersThreadsIntegrationTest.java @@ -0,0 +1,136 @@ +/** + * 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.ByteArrayOutputStream; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +import javax.mail.MessagingException; +import javax.mail.Session; +import javax.mail.internet.MimeMessage; +import javax.mail.internet.MimeMessage.RecipientType; + +import com.google.api.client.repackaged.org.apache.commons.codec.binary.Base64; +import com.google.api.services.gmail.model.Message; + +import org.apache.camel.builder.RouteBuilder; +import org.junit.Ignore; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.camel.component.google.mail.internal.GoogleMailApiCollection; +import org.apache.camel.component.google.mail.internal.GmailUsersThreadsApiMethod; + +/** + * Test class for {@link com.google.api.services.gmail.Gmail$Users$Threads} APIs. + */ +public class GmailUsersThreadsIntegrationTest extends AbstractGoogleMailTestSupport { + + private static final Logger LOG = LoggerFactory.getLogger(GmailUsersThreadsIntegrationTest.class); + private static final String PATH_PREFIX = GoogleMailApiCollection.getCollection().getApiName(GmailUsersThreadsApiMethod.class).getName(); + + private Message createThreadedTestEmail(String previousThreadId) throws MessagingException, IOException { + com.google.api.services.gmail.model.Profile profile = requestBody("google-mail://users/getProfile?inBody=userId", CURRENT_USERID); + Properties props = new Properties(); + Session session = Session.getDefaultInstance(props, null); + MimeMessage mm = new MimeMessage(session); + mm.addRecipients(RecipientType.TO, profile.getEmailAddress()); + mm.setSubject("Hello from camel-google-mail"); + mm.setContent("Camel rocks!", "text/plain"); + Message createMessageWithEmail = createMessageWithEmail(mm); + if (previousThreadId != null) { + createMessageWithEmail.setThreadId(previousThreadId); + } + + Map<String, Object> headers = new HashMap<String, Object>(); + // parameter type is String + headers.put("CamelGoogleMail.userId", CURRENT_USERID); + // parameter type is com.google.api.services.gmail.model.Message + headers.put("CamelGoogleMail.content", createMessageWithEmail); + + return requestBodyAndHeaders("google-mail://messages/send", null, headers); + } + + private Message createMessageWithEmail(MimeMessage email) + throws MessagingException, IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + email.writeTo(baos); + String encodedEmail = Base64.encodeBase64URLSafeString(baos.toByteArray()); + Message message = new Message(); + message.setRaw(encodedEmail); + return message; + } + + @Test + public void testList() throws Exception { + Message m1 = createThreadedTestEmail(null); + Message m2 = createThreadedTestEmail(m1.getThreadId()); + + Map<String, Object> headers = new HashMap<String, Object>(); + headers.put("CamelGoogleMail.q", "subject:\"Hello from camel-google-mail\""); + + // using String message body for single parameter "userId" + com.google.api.services.gmail.model.ListThreadsResponse result = requestBodyAndHeaders("direct://LIST", CURRENT_USERID, headers); + + assertNotNull("list result", result); + assertTrue(result.getThreads().size() > 0); + LOG.debug("list: " + result); + + headers = new HashMap<String, Object>(); + // parameter type is String + headers.put("CamelGoogleMail.userId", CURRENT_USERID); + // parameter type is String + headers.put("CamelGoogleMail.id", m1.getThreadId()); + + requestBodyAndHeaders("direct://DELETE", null, headers); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() { + // test route for delete + from("direct://DELETE") + .to("google-mail://" + PATH_PREFIX + "/delete"); + + // test route for get + from("direct://GET") + .to("google-mail://" + PATH_PREFIX + "/get"); + + // test route for list + from("direct://LIST") + .to("google-mail://" + PATH_PREFIX + "/list?inBody=userId"); + + // test route for modify + from("direct://MODIFY") + .to("google-mail://" + PATH_PREFIX + "/modify"); + + // test route for trash + from("direct://TRASH") + .to("google-mail://" + PATH_PREFIX + "/trash"); + + // test route for untrash + from("direct://UNTRASH") + .to("google-mail://" + PATH_PREFIX + "/untrash"); + + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/3c6769db/components/camel-google-mail/src/test/resources/log4j.properties ---------------------------------------------------------------------- diff --git a/components/camel-google-mail/src/test/resources/log4j.properties b/components/camel-google-mail/src/test/resources/log4j.properties new file mode 100644 index 0000000..a937abd --- /dev/null +++ b/components/camel-google-mail/src/test/resources/log4j.properties @@ -0,0 +1,30 @@ +## ------------------------------------------------------------------------ +## 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. +## ------------------------------------------------------------------------ +# +# The logging properties used +# +log4j.rootLogger=INFO, out + +# uncomment the following line to turn on Camel debugging +#log4j.logger.org.apache.camel=DEBUG + +# CONSOLE appender not used by default +log4j.appender.out=org.apache.log4j.ConsoleAppender +log4j.appender.out.layout=org.apache.log4j.PatternLayout +log4j.appender.out.layout.ConversionPattern=[%30.30t] %-30.30c{1} %-5p %m%n +#log4j.appender.out.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n + http://git-wip-us.apache.org/repos/asf/camel/blob/3c6769db/components/camel-google-mail/src/test/resources/test-options.properties ---------------------------------------------------------------------- diff --git a/components/camel-google-mail/src/test/resources/test-options.properties b/components/camel-google-mail/src/test/resources/test-options.properties new file mode 100644 index 0000000..0c10369 --- /dev/null +++ b/components/camel-google-mail/src/test/resources/test-options.properties @@ -0,0 +1,26 @@ +# +# 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. +# + +##################################### +## Login properties for Google Mail Component +##################################### +## Application client id and secret +clientId= +clientSecret= +applicationName=camel-google-mail/1.0 +accessToken= +refreshToken=