james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [6/7] james-project git commit: MAILBOX-284 Error handling for Attachment parser, should not throw when getting attachments which contain non cid
Date Tue, 14 Mar 2017 09:01:46 GMT
MAILBOX-284 Error handling for Attachment parser, should not throw when getting attachments
which contain non cid


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/fb0c9653
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/fb0c9653
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/fb0c9653

Branch: refs/heads/master
Commit: fb0c9653b83bfe5c91c23560ceb36f94d7466cf1
Parents: 1fda029
Author: Quynh Nguyen <qnguyen@linagora.com>
Authored: Mon Mar 13 10:08:57 2017 +0700
Committer: benwa <btellier@linagora.com>
Committed: Tue Mar 14 16:00:52 2017 +0700

----------------------------------------------------------------------
 .../store/mail/model/impl/MessageParser.java    | 11 ++-
 .../mail/model/impl/MessageParserTest.java      |  7 ++
 ...AttachmentsWithOneWrongInlinedAttachment.eml | 73 ++++++++++++++++++++
 .../integration/SetMessagesMethodTest.java      | 62 +++++++++++++++++
 .../methods/SetMessagesCreationProcessor.java   | 15 ++--
 5 files changed, 162 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/fb0c9653/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/model/impl/MessageParser.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/model/impl/MessageParser.java
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/model/impl/MessageParser.java
index a3cb3af..4b0ef37 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/model/impl/MessageParser.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/model/impl/MessageParser.java
@@ -42,6 +42,8 @@ import org.apache.james.mime4j.message.DefaultMessageBuilder;
 import org.apache.james.mime4j.message.DefaultMessageWriter;
 import org.apache.james.mime4j.stream.Field;
 import org.apache.james.mime4j.stream.MimeConfig;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import com.google.common.base.Function;
 import com.google.common.base.Optional;
@@ -64,6 +66,7 @@ public class MessageParser {
     private static final List<String> ATTACHMENT_CONTENT_DISPOSITIONS = ImmutableList.of(
             ContentDispositionField.DISPOSITION_TYPE_ATTACHMENT.toLowerCase(),
             ContentDispositionField.DISPOSITION_TYPE_INLINE.toLowerCase());
+    private static final Logger LOGGER = LoggerFactory.getLogger(MessageParser.class);
 
     public List<MessageAttachment> retrieveAttachments(InputStream fullContent) throws
MimeException, IOException {
         DefaultMessageBuilder defaultMessageBuilder = new DefaultMessageBuilder();
@@ -91,7 +94,13 @@ public class MessageParser {
                 attachments.addAll(listAttachments((Multipart) entity.getBody(), Context.fromEntity(entity)));
             } else {
                 if (isAttachment(entity, context)) {
-                    attachments.add(retrieveAttachment(messageWriter, entity));
+                    try {
+                        attachments.add(retrieveAttachment(messageWriter, entity));
+                    } catch (IllegalStateException e) {
+                        LOGGER.error("The attachment is not well-formed: " + e.getCause());
+                    } catch (IOException e) {
+                        LOGGER.error("There is error on retrieve attachment: " + e.getCause());
+                    }
                 }
             }
         }

http://git-wip-us.apache.org/repos/asf/james-project/blob/fb0c9653/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/impl/MessageParserTest.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/impl/MessageParserTest.java
b/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/impl/MessageParserTest.java
index 855235b..7e7d3a5 100644
--- a/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/impl/MessageParserTest.java
+++ b/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/impl/MessageParserTest.java
@@ -198,4 +198,11 @@ public class MessageParserTest {
 
         assertThat(attachments).hasSize(0);
     }
+
+    @Test
+    public void getAttachmentsShouldRetrieveAttachmentsWhenOneFailBecauseOfNonCIDForInlined()
throws Exception {
+        List<MessageAttachment> attachments = testee.retrieveAttachments(ClassLoader.getSystemResourceAsStream("eml/multiAttachmentsWithOneWrongInlinedAttachment.eml"));
+
+        assertThat(attachments).hasSize(1);
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/fb0c9653/mailbox/store/src/test/resources/eml/multiAttachmentsWithOneWrongInlinedAttachment.eml
----------------------------------------------------------------------
diff --git a/mailbox/store/src/test/resources/eml/multiAttachmentsWithOneWrongInlinedAttachment.eml
b/mailbox/store/src/test/resources/eml/multiAttachmentsWithOneWrongInlinedAttachment.eml
new file mode 100644
index 0000000..c7cd548
--- /dev/null
+++ b/mailbox/store/src/test/resources/eml/multiAttachmentsWithOneWrongInlinedAttachment.eml
@@ -0,0 +1,73 @@
+Return-Path: <from@linagora.com>
+Received: from alderaan.linagora.com (smtp.linagora.dc1 [172.16.18.53])
+	 by imap (Cyrus v2.2.13-Debian-2.2.13-19+squeeze3) with LMTPA;
+	 Mon, 27 Feb 2017 05:24:55 +0100
+X-Sieve: CMU Sieve 2.2
+Received: from [192.168.12.138] (unknown [1.55.245.4])
+	(using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits))
+	(No client certificate requested)
+	by alderaan.linagora.com (Postfix) with ESMTPSA id 688BF7A9
+	for <from@linagora.com>; Mon, 27 Feb 2017 05:24:54 +0100 (CET)
+To: to@linagora.com
+From: Lina <from@linagora.com>
+Subject: MultiAttachment
+Message-ID: <13d4375e-a4a9-f613-06a1-7e8cb1e0ea93@linagora.com>
+Date: Mon, 27 Feb 2017 11:24:48 +0700
+User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
+ Thunderbird/45.2.0
+MIME-Version: 1.0
+Content-Type: multipart/mixed;
+ boundary="------------64D8D789FC30153D6ED18258"
+
+This is a multi-part message in MIME format.
+--------------64D8D789FC30153D6ED18258
+Content-Type: text/plain; charset=utf-8; format=flowed
+Content-Transfer-Encoding: 7bit
+
+Send
+
+
+--------------64D8D789FC30153D6ED18258
+Content-Type: text/plain; charset=UTF-8;
+ name="text1"
+Content-Transfer-Encoding: base64
+Content-Disposition: wrongDisposition;
+ filename="text1"
+
+LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLQpNSUlFb2dJQkFBS0NBUUVBeDdQRzAr
+RS8vRU1wbTdJZ0k1UTlUTURTRnlhLzFoRSt2dlRKcmswaUdGbGxQZUhMCkE1L1ZsVE0wWVdn
+RzZYNTBxaU1mRTNWTGF6ZjJjMTlpWHJUMG1xLzIxUFoxd0Zub2d2NHp4VU5haWgrQm5nNjIK
+RjBTeXJ1RS9PL05qcXhoL0NjcTZLL2UwNVRWNFQ2NDNVU3hBZUcwS3BwbVlXOXg4SEEvR3ZW
+ODMyYXBadXhrVgppNk5Wa0RCcmZ6YVVDd3U0ekgrSHdPdi9wSTg3RTdLY2NIWUMrK0JpYWoz
+Cg==
+--------------64D8D789FC30153D6ED18258
+Content-Type: application/vnd.ms-publisher;
+ name="text2"
+Content-Transfer-Encoding: base64
+Content-Disposition: inline;
+ filename="text2"
+
+c3NoLXJzYSBBQUFBQjNOemFDMXljMkVBQUFBREFRQUJBQUFCQVFESHM4YlQ0VC84UXltYnNp
+QWpsRDFNd05JWEpyL1dFVDYrOU1tdVRTSVlXV1U5NGNzRG45V1ZNelJoYUFicGZuU3FJeDhU
+ZFV0ck4vWnpYMkpldFBTYXIvYlU5blhBV2VpQy9qUEZRMXFLSDRHZURyWVhSTEt1NFQ4Nzgy
+T3JHSDhKeXJvcjk3VGxOWGhQcmpkUkxFQjRiUXFtbVpoYjNId2NEOGE5WHpmWnFsbTdHUldM
+bzFXUU1HdC9OcFFMQzdqTWY0ZkE2LytranpzVHNweHdkZ0w3NEdKcVBmT1hPaXdnTEhYOENa
+Ni81UnlUcWhUNnBEM01rdFNOV2F6L3pJSFBORXFmNUJZOUNCTTFURlI1dys2TURIbzBnbWlJ
+c1hGRUpUUG5maEJ2SERoU2pCMVJJMEt4VUNseVlySjRmQmxVVmVLZm5hd29WY3U3WXZDcUY0
+RjUgcXV5bmhubkBsaW5hZ29yYQo=
+--------------64D8D789FC30153D6ED18258
+Content-Type: text/plain; charset=UTF-8;
+ name="text3"
+Content-Transfer-Encoding: base64
+Content-Disposition: attachment;
+ filename="text3"
+
+fDF8b1M3NU9nTDN2RjJHZGw5OUNKRGJFcGFKM3lFPXxJTkdxbGpDVzFYTWY0Z2dPUW0yNi9C
+Tm5LR2M9IHNzaC1yc2EgQUFBQUIzTnphQzF5YzJFQUFBQUJJd0FBQVFFQXEyQTdoUkdtZG5t
+OXRVRGJPOUlEU3dCSzZUYlFhK1BYWVBDUHk2cmJUclR0dzdQSGtjY0tycHAweVZocDVIZEVJ
+Y0tyNnBMbFZEQmZPTFg5UVVzeUNPVjB3emZqSUpObEdFWXNkbExKaXpIaGJuMm1VanZTQUhR
+cVpFVFlQODFlRnpMUU5uUEh0NEVWVlVoN1ZmREVTVTg0S2V6bUQ1UWxXcFhMbXZVMzEveU1m
+K1NlOHhoSFR2S1NDWklGSW1Xd29HNm1iVW9XZjluenBJb2FTakIrd2VxcVVVbXBhYWFzWFZh
+bDcySitVWDJCKzJSUFczUmNUMGVPelFncWxKTDNSS3JUSnZkc2pFM0pFQXZHcTNsR0hTWlh5
+TjZtNVU0aHBwaDl1T3Y1NGFIYzRYcjhqaEFhL1NYNU1KCg==
+--------------64D8D789FC30153D6ED18258--

http://git-wip-us.apache.org/repos/asf/james-project/blob/fb0c9653/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetMessagesMethodTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetMessagesMethodTest.java
b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetMessagesMethodTest.java
index 7d62fe2..b5ef729 100644
--- a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetMessagesMethodTest.java
+++ b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetMessagesMethodTest.java
@@ -3126,4 +3126,66 @@ public abstract class SetMessagesMethodTest {
             .body(NAME, equalTo("messageList"))
             .body(ARGUMENTS + ".messageIds", hasItem(messageId));
     }
+
+    @Test
+    public void setMessagesShouldReturnAttachmentsWhenMessageHasWrongInlinedAttachment()
throws Exception {
+        Attachment attachment = Attachment.builder()
+            .bytes("attachment".getBytes(Charsets.UTF_8))
+            .type("application/octet-stream")
+            .build();
+        uploadAttachment(attachment);
+        Attachment attachment2 = Attachment.builder()
+            .bytes("attachment2".getBytes(Charsets.UTF_8))
+            .type("application/octet-stream")
+            .build();
+        uploadAttachment(attachment2);
+
+        String messageCreationId = "creationId";
+        String fromAddress = USERNAME;
+        String outboxId = getOutboxId(accessToken);
+        String requestBody = "[" +
+            "  [" +
+            "    \"setMessages\","+
+            "    {" +
+            "      \"create\": { \"" + messageCreationId  + "\" : {" +
+            "        \"from\": { \"name\": \"Me\", \"email\": \"" + fromAddress + "\"},"
+
+            "        \"to\": [{ \"name\": \"BOB\", \"email\": \"someone@example.com\"}],"
+
+            "        \"subject\": \"Message with two attachments\"," +
+            "        \"textBody\": \"Test body\"," +
+            "        \"mailboxIds\": [\"" + outboxId + "\"], " +
+            "        \"attachments\": [" +
+            "               {\"blobId\" : \"" + attachment.getAttachmentId().getId() + "\",
" +
+            "               \"type\" : \"" + attachment.getType() + "\", " +
+            "               \"size\" : " + attachment.getSize() + "}," +
+            "               {\"blobId\" : \"" + attachment2.getAttachmentId().getId() + "\",
" +
+            "               \"type\" : \"" + attachment2.getType() + "\", " +
+            "               \"size\" : " + attachment2.getSize() + ", " +
+            "               \"isInline\" : true }" +
+            "           ]" +
+            "      }}" +
+            "    }," +
+            "    \"#0\"" +
+            "  ]" +
+            "]";
+
+        String createdPath = ARGUMENTS + ".created[\""+messageCreationId+"\"]";
+        String firstAttachment = createdPath + ".attachments[0]";
+
+        given()
+            .header("Authorization", accessToken.serialize())
+            .body(requestBody)
+        .when()
+            .post("/jmap")
+        .then()
+            .statusCode(200)
+            .body(NAME, equalTo("messagesSet"))
+            .body(ARGUMENTS + ".notCreated", aMapWithSize(0))
+            .body(ARGUMENTS + ".created", aMapWithSize(1))
+            .body(createdPath + ".attachments", hasSize(1))
+            .body(firstAttachment + ".blobId", equalTo(attachment.getAttachmentId().getId()))
+            .body(firstAttachment + ".type", equalTo("application/octet-stream; charset=UTF-8"))
+            .body(firstAttachment + ".size", equalTo((int) attachment.getSize()))
+            .body(firstAttachment + ".cid", nullValue())
+            .body(firstAttachment + ".isInline", equalTo(false));
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/fb0c9653/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesCreationProcessor.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesCreationProcessor.java
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesCreationProcessor.java
index 4d2b748..c489bc1 100644
--- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesCreationProcessor.java
+++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesCreationProcessor.java
@@ -68,6 +68,7 @@ import org.apache.james.mailbox.model.ComposedMessageId;
 import org.apache.james.mailbox.model.MessageAttachment;
 import org.apache.james.metrics.api.MetricFactory;
 import org.apache.james.metrics.api.TimeMetric;
+import org.apache.james.util.OptionalConverter;
 import org.apache.mailet.Mail;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -304,24 +305,28 @@ public class SetMessagesCreationProcessor implements SetMessagesProcessor
{
     }
 
     private ImmutableList<MessageAttachment> getMessageAttachments(MailboxSession session,
ImmutableList<Attachment> attachments) throws MailboxException {
-        ThrowingFunction<Attachment, MessageAttachment> toMessageAttachment = att ->
messageAttachment(session, att);
+        ThrowingFunction<Attachment, Optional<MessageAttachment>> toMessageAttachment
= att -> messageAttachment(session, att);
         return attachments.stream()
             .map(Throwing.function(toMessageAttachment).sneakyThrow())
+            .flatMap(OptionalConverter::toStream)
             .collect(Guavate.toImmutableList());
     }
 
-    private MessageAttachment messageAttachment(MailboxSession session, Attachment attachment)
throws MailboxException {
+    private Optional<MessageAttachment> messageAttachment(MailboxSession session, Attachment
attachment) throws MailboxException {
         try {
-            return MessageAttachment.builder()
+            return Optional.of(MessageAttachment.builder()
                     .attachment(attachmentManager.getAttachment(AttachmentId.from(attachment.getBlobId().getRawValue()),
session))
                     .name(attachment.getName().orElse(null))
                     .cid(attachment.getCid().map(Cid::from).orElse(null))
                     .isInline(attachment.isIsInline())
-                    .build();
+                    .build());
         } catch (AttachmentNotFoundException e) {
             // should not happen (checked before)
             LOG.error(String.format("Attachment %s not found", attachment.getBlobId()), e);
-            return null;
+            return Optional.empty();
+        } catch (IllegalStateException e) {
+            LOG.error(String.format("Attachment %s is not well-formed", attachment.getBlobId()),
e);
+            return Optional.empty();
         }
     }
 


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message