Search in sources :

Example 1 with GoogleMailComponent

use of org.apache.camel.component.google.mail.GoogleMailComponent in project wildfly-camel by wildfly-extras.

the class GoogleMailIntegrationTest method threads.

@SuppressWarnings("serial")
@Test
public void threads() throws Exception {
    CamelContext camelctx = new DefaultCamelContext();
    GoogleMailComponent gMailComponent = camelctx.getComponent("google-mail", GoogleMailComponent.class);
    GoogleApiEnv.configure(gMailComponent.getConfiguration(), getClass(), LOG);
    camelctx.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            final String pathPrefix = "threads";
            // test route for delete
            from("direct://DELETE").to("google-mail://" + pathPrefix + "/delete");
            // test route for get
            from("direct://GET").to("google-mail://" + pathPrefix + "/get");
            // test route for list
            from("direct://LIST").to("google-mail://" + pathPrefix + "/list?inBody=userId");
            // test route for modify
            from("direct://MODIFY").to("google-mail://" + pathPrefix + "/modify");
            // test route for trash
            from("direct://TRASH").to("google-mail://" + pathPrefix + "/trash");
            // test route for untrash
            from("direct://UNTRASH").to("google-mail://" + pathPrefix + "/untrash");
        }
    });
    try {
        camelctx.start();
        final String subject = getClass().getSimpleName() + ".threads " + UUID.randomUUID().toString();
        ProducerTemplate template = camelctx.createProducerTemplate();
        Message m1 = createThreadedMessage(null, subject, template);
        final String threadId = m1.getThreadId();
        createThreadedMessage(threadId, subject, template);
        // using String message body for single parameter "userId"
        ListThreadsResponse result = template.requestBodyAndHeaders("direct://LIST", CURRENT_USERID, Collections.singletonMap("CamelGoogleMail.q", "subject:\"" + subject + "\""), ListThreadsResponse.class);
        Assert.assertNotNull("list result", result);
        Assert.assertTrue(result.getThreads().size() > 0);
        // ===== trash it ====
        template.requestBodyAndHeaders("direct://TRASH", null, new HashMap<String, Object>() {

            {
                put("CamelGoogleMail.userId", CURRENT_USERID);
                put("CamelGoogleMail.id", threadId);
            }
        });
        // ==== Search for message we just trashed ====
        result = template.requestBodyAndHeaders("direct://LIST", CURRENT_USERID, Collections.singletonMap("CamelGoogleMail.q", "subject:\"" + subject + "\""), ListThreadsResponse.class);
        Assert.assertNotNull("list result", result);
        Assert.assertTrue(result.getThreads() == null || result.getThreads().stream().noneMatch(t -> threadId.equals(t.getId())));
        /* For some reason the thread deletion often needs some delay to succeed */
        int attemptCount = 0;
        for (; ; ) {
            try {
                template.requestBodyAndHeaders("direct://DELETE", null, new HashMap<String, Object>() {

                    {
                        put("CamelGoogleMail.userId", CURRENT_USERID);
                        put("CamelGoogleMail.id", threadId);
                    }
                });
                break;
            /* success */
            } catch (Exception e) {
                if (attemptCount >= 5) {
                    throw e;
                /* too many attempts */
                } else {
                    /* retry */
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e1) {
                        Thread.currentThread().interrupt();
                    }
                }
                attemptCount++;
            }
        }
    } finally {
        camelctx.stop();
    }
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) GoogleMailComponent(org.apache.camel.component.google.mail.GoogleMailComponent) ProducerTemplate(org.apache.camel.ProducerTemplate) RouteBuilder(org.apache.camel.builder.RouteBuilder) Message(com.google.api.services.gmail.model.Message) MimeMessage(javax.mail.internet.MimeMessage) ListThreadsResponse(com.google.api.services.gmail.model.ListThreadsResponse) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with GoogleMailComponent

use of org.apache.camel.component.google.mail.GoogleMailComponent in project wildfly-camel by wildfly-extras.

the class GoogleMailIntegrationTest method testGoogleMailLabels.

@Test
public void testGoogleMailLabels() throws Exception {
    CamelContext camelctx = new DefaultCamelContext();
    GoogleMailComponent gMailComponent = camelctx.getComponent("google-mail", GoogleMailComponent.class);
    GoogleApiEnv.configure(gMailComponent.getConfiguration(), getClass(), LOG);
    camelctx.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            final String pathPrefix = "labels";
            // test route for create
            from("direct://CREATE").to("google-mail://" + pathPrefix + "/create");
            // test route for delete
            from("direct://DELETE").to("google-mail://" + pathPrefix + "/delete");
            // test route for get
            from("direct://GET").to("google-mail://" + pathPrefix + "/get");
            // test route for list
            from("direct://LIST").to("google-mail://" + pathPrefix + "/list?inBody=userId");
            // test route for patch
            from("direct://PATCH").to("google-mail://" + pathPrefix + "/patch");
            // test route for update
            from("direct://UPDATE").to("google-mail://" + pathPrefix + "/update");
        }
    });
    try {
        camelctx.start();
        ProducerTemplate template = camelctx.createProducerTemplate();
        // using String message body for single parameter "userId"
        ListLabelsResponse labels = template.requestBody("direct://LIST", CURRENT_USERID, ListLabelsResponse.class);
        final String testLabel = getClass().getSimpleName() + ".labels " + UUID.randomUUID().toString();
        String labelId;
        if (findLabel(labels, testLabel) == null) {
            Map<String, Object> headers = new HashMap<>();
            // parameter type is String
            headers.put("CamelGoogleMail.userId", CURRENT_USERID);
            Label label = new Label().setName(testLabel).setMessageListVisibility("show").setLabelListVisibility("labelShow");
            // parameter type is com.google.api.services.gmail.model.Label
            headers.put("CamelGoogleMail.content", label);
            Label result = template.requestBodyAndHeaders("direct://CREATE", null, headers, Label.class);
            Assert.assertNotNull("create result", result);
            labelId = result.getId();
        } else {
            labelId = findLabel(labels, testLabel).getId();
        }
        // using String message body for single parameter "userId"
        labels = template.requestBody("direct://LIST", CURRENT_USERID, ListLabelsResponse.class);
        Assert.assertTrue(findLabel(labels, testLabel) != null);
        Map<String, Object> headers = new HashMap<>();
        // parameter type is String
        headers.put("CamelGoogleMail.userId", CURRENT_USERID);
        // parameter type is String
        headers.put("CamelGoogleMail.id", labelId);
        template.requestBodyAndHeaders("direct://DELETE", null, headers);
        // using String message body for single parameter "userId"
        labels = template.requestBody("direct://LIST", CURRENT_USERID, ListLabelsResponse.class);
        Assert.assertTrue(findLabel(labels, testLabel) == null);
    } finally {
        camelctx.stop();
    }
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) GoogleMailComponent(org.apache.camel.component.google.mail.GoogleMailComponent) ProducerTemplate(org.apache.camel.ProducerTemplate) RouteBuilder(org.apache.camel.builder.RouteBuilder) HashMap(java.util.HashMap) Label(com.google.api.services.gmail.model.Label) ListLabelsResponse(com.google.api.services.gmail.model.ListLabelsResponse) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) Test(org.junit.Test)

Example 3 with GoogleMailComponent

use of org.apache.camel.component.google.mail.GoogleMailComponent in project camel by apache.

the class GoogleMailComponentAutoConfiguration method configureGoogleMailComponent.

@Lazy
@Bean(name = "google-mail-component")
@ConditionalOnClass(CamelContext.class)
@ConditionalOnMissingBean(GoogleMailComponent.class)
public GoogleMailComponent configureGoogleMailComponent(CamelContext camelContext, GoogleMailComponentConfiguration configuration) throws Exception {
    GoogleMailComponent component = new GoogleMailComponent();
    component.setCamelContext(camelContext);
    Map<String, Object> parameters = new HashMap<>();
    IntrospectionSupport.getProperties(configuration, parameters, null, false);
    for (Map.Entry<String, Object> entry : parameters.entrySet()) {
        Object value = entry.getValue();
        Class<?> paramClass = value.getClass();
        if (paramClass.getName().endsWith("NestedConfiguration")) {
            Class nestedClass = null;
            try {
                nestedClass = (Class) paramClass.getDeclaredField("CAMEL_NESTED_CLASS").get(null);
                HashMap<String, Object> nestedParameters = new HashMap<>();
                IntrospectionSupport.getProperties(value, nestedParameters, null, false);
                Object nestedProperty = nestedClass.newInstance();
                IntrospectionSupport.setProperties(camelContext, camelContext.getTypeConverter(), nestedProperty, nestedParameters);
                entry.setValue(nestedProperty);
            } catch (NoSuchFieldException e) {
            }
        }
    }
    IntrospectionSupport.setProperties(camelContext, camelContext.getTypeConverter(), component, parameters);
    return component;
}
Also used : GoogleMailComponent(org.apache.camel.component.google.mail.GoogleMailComponent) HashMap(java.util.HashMap) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) HashMap(java.util.HashMap) Map(java.util.Map) Lazy(org.springframework.context.annotation.Lazy) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 4 with GoogleMailComponent

use of org.apache.camel.component.google.mail.GoogleMailComponent in project wildfly-camel by wildfly-extras.

the class GoogleMailIntegrationTest method profile.

@Test
public void profile() throws Exception {
    CamelContext camelctx = new DefaultCamelContext();
    GoogleMailComponent gMailComponent = camelctx.getComponent("google-mail", GoogleMailComponent.class);
    GoogleApiEnv.configure(gMailComponent.getConfiguration(), getClass(), LOG);
    camelctx.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            final String pathPrefix = "users";
            // test route for attachments
            from("direct://GETPROFILE").to("google-mail://" + pathPrefix + "/getProfile?inBody=userId");
        }
    });
    try {
        camelctx.start();
        ProducerTemplate template = camelctx.createProducerTemplate();
        // using String message body for single parameter "userId"
        final Profile result = template.requestBody("direct://GETPROFILE", CURRENT_USERID, Profile.class);
        Assert.assertNotNull("getProfile result", result);
        Assert.assertNotNull("Should be email address associated with current account", result.getEmailAddress());
        System.out.println("getProfile: " + result);
    } finally {
        camelctx.stop();
    }
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) GoogleMailComponent(org.apache.camel.component.google.mail.GoogleMailComponent) ProducerTemplate(org.apache.camel.ProducerTemplate) RouteBuilder(org.apache.camel.builder.RouteBuilder) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) Profile(com.google.api.services.gmail.model.Profile) Test(org.junit.Test)

Example 5 with GoogleMailComponent

use of org.apache.camel.component.google.mail.GoogleMailComponent in project wildfly-camel by wildfly-extras.

the class GoogleMailIntegrationTest method messages.

@Test
public void messages() throws Exception {
    CamelContext camelctx = new DefaultCamelContext();
    GoogleMailComponent gMailComponent = camelctx.getComponent("google-mail", GoogleMailComponent.class);
    GoogleApiEnv.configure(gMailComponent.getConfiguration(), getClass(), LOG);
    camelctx.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            final String pathPrefix = "messages";
            // test route for attachments
            from("direct://ATTACHMENTS").to("google-mail://" + pathPrefix + "/attachments");
            // test route for delete
            from("direct://DELETE").to("google-mail://" + pathPrefix + "/delete");
            // test route for get
            from("direct://GET").to("google-mail://" + pathPrefix + "/get");
            // test route for gmailImport
            from("direct://GMAILIMPORT").to("google-mail://" + pathPrefix + "/gmailImport");
            // test route for gmailImport
            from("direct://GMAILIMPORT_1").to("google-mail://" + pathPrefix + "/gmailImport");
            // test route for insert
            from("direct://INSERT").to("google-mail://" + pathPrefix + "/insert");
            // test route for insert
            from("direct://INSERT_1").to("google-mail://" + pathPrefix + "/insert");
            // test route for list
            from("direct://LIST").to("google-mail://" + pathPrefix + "/list?inBody=userId");
            // test route for modify
            from("direct://MODIFY").to("google-mail://" + pathPrefix + "/modify");
            // test route for send
            from("direct://SEND").to("google-mail://" + pathPrefix + "/send");
            // test route for send
            from("direct://SEND_1").to("google-mail://" + pathPrefix + "/send");
            // test route for trash
            from("direct://TRASH").to("google-mail://" + pathPrefix + "/trash");
            // test route for untrash
            from("direct://UNTRASH").to("google-mail://" + pathPrefix + "/untrash");
        }
    });
    try {
        camelctx.start();
        ProducerTemplate template = camelctx.createProducerTemplate();
        // ==== Send test email ====
        final String subject = getClass().getSimpleName() + ".messages " + UUID.randomUUID().toString();
        Message testEmail = createMessage(template, subject);
        Map<String, Object> headers = new HashMap<>();
        // 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);
        Message result = template.requestBodyAndHeaders("direct://SEND", null, headers, Message.class);
        Assert.assertNotNull("send result", result);
        String testEmailId = result.getId();
        // ==== Search for message we just sent ====
        headers = new HashMap<>();
        headers.put("CamelGoogleMail.q", "subject:\"" + subject + "\"");
        // using String message body for single parameter "userId"
        ListMessagesResponse listOfMessages = template.requestBody("direct://LIST", CURRENT_USERID, ListMessagesResponse.class);
        Assert.assertTrue(idInList(testEmailId, listOfMessages));
        // ===== trash it ====
        headers = new HashMap<>();
        // parameter type is String
        headers.put("CamelGoogleMail.userId", CURRENT_USERID);
        // parameter type is String
        headers.put("CamelGoogleMail.id", testEmailId);
        template.requestBodyAndHeaders("direct://TRASH", null, headers);
        // ==== Search for message we just trashed ====
        headers = new HashMap<>();
        headers.put("CamelGoogleMail.q", "subject:\"" + subject + "\"");
        // using String message body for single parameter "userId"
        listOfMessages = template.requestBody("direct://LIST", CURRENT_USERID, ListMessagesResponse.class);
        Assert.assertFalse(idInList(testEmailId, listOfMessages));
        // ===== untrash it ====
        headers = new HashMap<>();
        // parameter type is String
        headers.put("CamelGoogleMail.userId", CURRENT_USERID);
        // parameter type is String
        headers.put("CamelGoogleMail.id", testEmailId);
        template.requestBodyAndHeaders("direct://UNTRASH", null, headers);
        // ==== Search for message we just untrashed ====
        headers = new HashMap<>();
        headers.put("CamelGoogleMail.q", "subject:\"" + subject + "\"");
        // using String message body for single parameter "userId"
        listOfMessages = template.requestBody("direct://LIST", CURRENT_USERID, ListMessagesResponse.class);
        Assert.assertTrue(idInList(testEmailId, listOfMessages));
        // ===== permanently delete it ====
        headers = new HashMap<>();
        // parameter type is String
        headers.put("CamelGoogleMail.userId", CURRENT_USERID);
        // parameter type is String
        headers.put("CamelGoogleMail.id", testEmailId);
        template.requestBodyAndHeaders("direct://DELETE", null, headers);
        // ==== Search for message we just deleted ====
        headers = new HashMap<>();
        headers.put("CamelGoogleMail.q", "subject:\"" + subject + "\"");
        // using String message body for single parameter "userId"
        listOfMessages = template.requestBody("direct://LIST", CURRENT_USERID, ListMessagesResponse.class);
        Assert.assertFalse(idInList(testEmailId, listOfMessages));
    } finally {
        camelctx.stop();
    }
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) GoogleMailComponent(org.apache.camel.component.google.mail.GoogleMailComponent) ProducerTemplate(org.apache.camel.ProducerTemplate) RouteBuilder(org.apache.camel.builder.RouteBuilder) Message(com.google.api.services.gmail.model.Message) MimeMessage(javax.mail.internet.MimeMessage) HashMap(java.util.HashMap) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) ListMessagesResponse(com.google.api.services.gmail.model.ListMessagesResponse) Test(org.junit.Test)

Aggregations

GoogleMailComponent (org.apache.camel.component.google.mail.GoogleMailComponent)5 IOException (java.io.IOException)4 MessagingException (javax.mail.MessagingException)4 CamelContext (org.apache.camel.CamelContext)4 ProducerTemplate (org.apache.camel.ProducerTemplate)4 RouteBuilder (org.apache.camel.builder.RouteBuilder)4 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)4 Test (org.junit.Test)4 HashMap (java.util.HashMap)3 Message (com.google.api.services.gmail.model.Message)2 MimeMessage (javax.mail.internet.MimeMessage)2 Label (com.google.api.services.gmail.model.Label)1 ListLabelsResponse (com.google.api.services.gmail.model.ListLabelsResponse)1 ListMessagesResponse (com.google.api.services.gmail.model.ListMessagesResponse)1 ListThreadsResponse (com.google.api.services.gmail.model.ListThreadsResponse)1 Profile (com.google.api.services.gmail.model.Profile)1 Map (java.util.Map)1 ConditionalOnBean (org.springframework.boot.autoconfigure.condition.ConditionalOnBean)1 ConditionalOnClass (org.springframework.boot.autoconfigure.condition.ConditionalOnClass)1 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1