You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2024/06/25 09:10:36 UTC

(camel) branch msc created (now 42ebca7c135)

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a change to branch msc
in repository https://gitbox.apache.org/repos/asf/camel.git


      at 42ebca7c135 CAMEL-20907: camel-mail - PollEnrich should work with stream caching to ignore if folder is not open.

This branch includes the following new commits:

     new 67693e28296 CAMEL-20907: camel-mail - Added unit test with pollEnrich and attachments.
     new 42ebca7c135 CAMEL-20907: camel-mail - PollEnrich should work with stream caching to ignore if folder is not open.

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(camel) 01/02: CAMEL-20907: camel-mail - Added unit test with pollEnrich and attachments.

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch msc
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 67693e28296d8cea5162384344e6aa157b762787
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Jun 25 10:57:03 2024 +0200

    CAMEL-20907: camel-mail - Added unit test with pollEnrich and attachments.
---
 .../mail/MailAttachmentPollEnrichTest.java         | 122 +++++++++++++++++++++
 1 file changed, 122 insertions(+)

diff --git a/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailAttachmentPollEnrichTest.java b/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailAttachmentPollEnrichTest.java
new file mode 100644
index 00000000000..ae81d4f7eb9
--- /dev/null
+++ b/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailAttachmentPollEnrichTest.java
@@ -0,0 +1,122 @@
+/*
+ * 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.mail;
+
+import java.util.Map;
+
+import jakarta.activation.DataHandler;
+import jakarta.activation.FileDataSource;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.Producer;
+import org.apache.camel.attachment.Attachment;
+import org.apache.camel.attachment.AttachmentMessage;
+import org.apache.camel.attachment.DefaultAttachment;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mail.Mailbox.MailboxUser;
+import org.apache.camel.component.mail.Mailbox.Protocol;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Unit test for Camel attachments and Mail attachments.
+ */
+public class MailAttachmentPollEnrichTest extends CamelTestSupport {
+    private static final MailboxUser james = Mailbox.getOrCreateUser("james", "secret");
+
+    @Test
+    public void testPollEnrichMailWithAttachments() throws Exception {
+        // clear mailbox
+        Mailbox.clearAll();
+
+        // START SNIPPET: e1
+
+        // create an exchange with a normal body and attachment to be produced as email
+        Endpoint endpoint = context.getEndpoint(james.uriPrefix(Protocol.smtp));
+
+        // create the exchange with the mail message that is multipart with a file and a Hello World text/plain message.
+        Exchange exchange = endpoint.createExchange();
+        AttachmentMessage in = exchange.getIn(AttachmentMessage.class);
+        in.setBody("Hello World");
+        DefaultAttachment att = new DefaultAttachment(new FileDataSource("src/test/data/logo.jpeg"));
+        att.addHeader("Content-Description", "some sample content");
+        in.addAttachmentObject("logo.jpeg", att);
+
+        // create a producer that can produce the exchange (= send the mail)
+        Producer producer = endpoint.createProducer();
+        // start the producer
+        producer.start();
+        // and let it go (processes the exchange by sending the email)
+        producer.process(exchange);
+
+        // END SNIPPET: e1
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+
+        template.sendBody("direct:start", "Trigger");
+
+        mock.assertIsSatisfied();
+        Exchange out = mock.assertExchangeReceived(0);
+
+        // plain text
+        assertEquals("Hello World", out.getIn().getBody(String.class));
+
+        // attachment
+        Map<String, Attachment> attachments = out.getIn(AttachmentMessage.class).getAttachmentObjects();
+        assertNotNull(attachments, "Should have attachments");
+        assertEquals(1, attachments.size());
+
+        Attachment attachment = out.getIn(AttachmentMessage.class).getAttachmentObject("logo.jpeg");
+        DataHandler handler = attachment.getDataHandler();
+        assertNotNull(handler, "The logo should be there");
+
+        // content type should match
+        boolean match1 = "image/jpeg; name=logo.jpeg".equals(handler.getContentType());
+        boolean match2 = "application/octet-stream; name=logo.jpeg".equals(handler.getContentType());
+        assertTrue(match1 || match2, "Should match 1 or 2");
+
+        assertEquals("logo.jpeg", handler.getName(), "Handler name should be the file name");
+
+        assertEquals("some sample content", attachment.getHeader("content-description"));
+
+        producer.stop();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                from("direct:start").streamCache("false")
+                        .pollEnrich(james.uriPrefix(Protocol.pop3) + "&initialDelay=100&delay=100", 5000)
+                        .process(e -> {
+                            AttachmentMessage attachmentMessage = e.getIn(AttachmentMessage.class);
+                            Map<String, DataHandler> attachments = attachmentMessage.getAttachments();
+                            assertEquals(1, attachments.size());
+                            assertEquals("logo.jpeg", attachments.keySet().iterator().next());
+                        })
+                        .to("mock:result");
+            }
+        };
+    }
+}


(camel) 02/02: CAMEL-20907: camel-mail - PollEnrich should work with stream caching to ignore if folder is not open.

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch msc
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 42ebca7c135009da18fcca259709dcf9e0de5e38
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Jun 25 11:10:14 2024 +0200

    CAMEL-20907: camel-mail - PollEnrich should work with stream caching to ignore if folder is not open.
---
 .../camel/component/mail/MailConverters.java       | 35 ++++++++++++++--------
 .../mail/MailAttachmentPollEnrichTest.java         |  8 ++++-
 2 files changed, 29 insertions(+), 14 deletions(-)

diff --git a/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConverters.java b/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConverters.java
index 0abdeadd5ba..846c8f18168 100644
--- a/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConverters.java
+++ b/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConverters.java
@@ -80,21 +80,30 @@ public final class MailConverters {
      */
     @Converter
     public static String toString(Multipart multipart) throws MessagingException, IOException {
-        int size = multipart.getCount();
-        for (int i = 0; i < size; i++) {
-            BodyPart part = multipart.getBodyPart(i);
-            Object content = part.getContent();
-            while (content instanceof MimeMultipart) {
-                if (multipart.getCount() < 1) {
-                    break;
+        try {
+            int size = multipart.getCount();
+            for (int i = 0; i < size; i++) {
+                BodyPart part = multipart.getBodyPart(i);
+                Object content = part.getContent();
+                while (content instanceof MimeMultipart) {
+                    if (multipart.getCount() < 1) {
+                        break;
+                    }
+                    part = ((MimeMultipart) content).getBodyPart(0);
+                    content = part.getContent();
+                }
+                // Perform a case-insensitive "startsWith" check that works for different locales
+                String prefix = "text";
+                if (part.getContentType().regionMatches(true, 0, prefix, 0, prefix.length())) {
+                    return part.getContent().toString();
                 }
-                part = ((MimeMultipart) content).getBodyPart(0);
-                content = part.getContent();
             }
-            // Perform a case insensitive "startsWith" check that works for different locales
-            String prefix = "text";
-            if (part.getContentType().regionMatches(true, 0, prefix, 0, prefix.length())) {
-                return part.getContent().toString();
+        } catch (MessagingException e) {
+            Throwable cause = e.getCause();
+            if (cause != null && "Folder is not Open".equals(cause.getMessage())) {
+                // ignore if folder is not open and we cannot read the mail
+            } else {
+                throw e;
             }
         }
         return null;
diff --git a/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailAttachmentPollEnrichTest.java b/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailAttachmentPollEnrichTest.java
index ae81d4f7eb9..d6f93411dda 100644
--- a/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailAttachmentPollEnrichTest.java
+++ b/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailAttachmentPollEnrichTest.java
@@ -107,13 +107,19 @@ public class MailAttachmentPollEnrichTest extends CamelTestSupport {
     protected RouteBuilder createRouteBuilder() {
         return new RouteBuilder() {
             public void configure() {
-                from("direct:start").streamCache("false")
+                from("direct:start")
                         .pollEnrich(james.uriPrefix(Protocol.pop3) + "&initialDelay=100&delay=100", 5000)
                         .process(e -> {
                             AttachmentMessage attachmentMessage = e.getIn(AttachmentMessage.class);
                             Map<String, DataHandler> attachments = attachmentMessage.getAttachments();
                             assertEquals(1, attachments.size());
                             assertEquals("logo.jpeg", attachments.keySet().iterator().next());
+
+                            DataHandler dh = attachments.values().iterator().next();
+                            byte[] data = context.getTypeConverter().convertTo(byte[].class, e, dh.getInputStream());
+                            assertNotNull(data);
+                            // should be logo that are 10.000 bytes or longer
+                            assertTrue(data.length > 10000, "Should be 10000 bytes or more");
                         })
                         .to("mock:result");
             }