You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by ad...@apache.org on 2017/11/30 14:22:21 UTC

[1/9] james-project git commit: MAILBOX-321 Remove uneeded retrieveAttachment parameter

Repository: james-project
Updated Branches:
  refs/heads/master 4135794a1 -> a55693355


MAILBOX-321 Remove uneeded retrieveAttachment parameter


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

Branch: refs/heads/master
Commit: 58bd6d457eee3663477abb25eee52db817156c3d
Parents: 4135794
Author: benwa <bt...@linagora.com>
Authored: Tue Nov 28 10:08:36 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Thu Nov 30 16:17:31 2017 +0700

----------------------------------------------------------------------
 .../mailbox/store/mail/model/impl/MessageParser.java  | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/58bd6d45/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 529442e..67df235 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
@@ -35,7 +35,6 @@ import org.apache.james.mime4j.codec.DecodeMonitor;
 import org.apache.james.mime4j.dom.Body;
 import org.apache.james.mime4j.dom.Entity;
 import org.apache.james.mime4j.dom.Message;
-import org.apache.james.mime4j.dom.MessageWriter;
 import org.apache.james.mime4j.dom.Multipart;
 import org.apache.james.mime4j.dom.field.ContentDispositionField;
 import org.apache.james.mime4j.dom.field.ContentIdField;
@@ -88,7 +87,7 @@ public class MessageParser {
             Optional<ContentDispositionField> contentDisposition = readHeader(message, CONTENT_DISPOSITION, ContentDispositionField.class);
 
             if (isMessageWithOnlyOneAttachment(contentDisposition)) {
-                return ImmutableList.of(retrieveAttachment(new DefaultMessageWriter(), message));
+                return ImmutableList.of(retrieveAttachment(message));
             }
 
             if (body instanceof Multipart) {
@@ -108,14 +107,14 @@ public class MessageParser {
 
     private List<MessageAttachment> listAttachments(Multipart multipart, Context context) throws IOException {
         ImmutableList.Builder<MessageAttachment> attachments = ImmutableList.builder();
-        MessageWriter messageWriter = new DefaultMessageWriter();
+
         for (Entity entity : multipart.getBodyParts()) {
             if (isMultipart(entity)) {
                 attachments.addAll(listAttachments((Multipart) entity.getBody(), Context.fromEntity(entity)));
             } else {
                 if (isAttachment(entity, context)) {
                     try {
-                        attachments.add(retrieveAttachment(messageWriter, entity));
+                        attachments.add(retrieveAttachment(entity));
                     } catch (IllegalStateException e) {
                         LOGGER.error("The attachment is not well-formed: " + e.getCause());
                     } catch (IOException e) {
@@ -127,7 +126,7 @@ public class MessageParser {
         return attachments.build();
     }
 
-    private MessageAttachment retrieveAttachment(MessageWriter messageWriter, Entity entity) throws IOException {
+    private MessageAttachment retrieveAttachment(Entity entity) throws IOException {
         Optional<ContentTypeField> contentTypeField = getContentTypeField(entity);
         Optional<ContentDispositionField> contentDispositionField = getContentDispositionField(entity);
         Optional<String> contentType = contentType(contentTypeField);
@@ -137,7 +136,7 @@ public class MessageParser {
 
         return MessageAttachment.builder()
                 .attachment(Attachment.builder()
-                    .bytes(getBytes(messageWriter, entity.getBody()))
+                    .bytes(getBytes(entity.getBody()))
                     .type(contentType.orElse(DEFAULT_CONTENT_TYPE))
                     .build())
                 .name(name.orElse(null))
@@ -220,7 +219,8 @@ public class MessageParser {
         return false;
     }
 
-    private byte[] getBytes(MessageWriter messageWriter, Body body) throws IOException {
+    private byte[] getBytes(Body body) throws IOException {
+        DefaultMessageWriter messageWriter = new DefaultMessageWriter();
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         messageWriter.writeBody(body, out);
         return out.toByteArray();


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


[8/9] james-project git commit: MAILBOX-321 PGP signature parts SHOULD always be considered as attachment

Posted by ad...@apache.org.
MAILBOX-321 PGP signature parts SHOULD always be considered as attachment

A specific handling needs to be implemented using ContentType header as, as you can see in the added tests, ContentDisposition header is not specified on PGP signature.

The new code allow to add ContentTypes that should always be considered as attachment (even when no ContentDisposition is attached to them).


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

Branch: refs/heads/master
Commit: 9e3d5dce46e37d374b89cf9b2f01dd4dfd5d6a61
Parents: 1d05817
Author: benwa <bt...@linagora.com>
Authored: Tue Nov 28 13:13:59 2017 +0700
Committer: Antoine Duprat <ad...@linagora.com>
Committed: Thu Nov 30 14:55:36 2017 +0100

----------------------------------------------------------------------
 .../store/mail/model/impl/MessageParser.java    | 22 ++++-
 .../mail/model/impl/MessageParserTest.java      | 13 +++
 .../src/test/resources/eml/signedMessage.eml    | 84 ++++++++++++++++++++
 3 files changed, 115 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/9e3d5dce/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 cbde24a..a0273ba 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
@@ -68,6 +68,7 @@ public class MessageParser {
     private static final List<String> ATTACHMENT_CONTENT_DISPOSITIONS = ImmutableList.of(
             ContentDispositionField.DISPOSITION_TYPE_ATTACHMENT.toLowerCase(Locale.US),
             ContentDispositionField.DISPOSITION_TYPE_INLINE.toLowerCase(Locale.US));
+    private static final ImmutableList<String> ATTACHMENT_CONTENT_TYPES = ImmutableList.of("application/pgp-signature");
     private static final Logger LOGGER = LoggerFactory.getLogger(MessageParser.class);
 
     private final Cid.CidParser cidParser;
@@ -192,10 +193,7 @@ public class MessageParser {
         if (context == Context.BODY && isTextPart(part)) {
             return false;
         }
-        return Optional.ofNullable(part.getDispositionType())
-                .map(dispositionType -> ATTACHMENT_CONTENT_DISPOSITIONS.contains(
-                    dispositionType.toLowerCase(Locale.US)))
-            .orElse(false);
+        return attachmentDispositionCriterion(part) || attachmentContentTypeCriterion(part);
     }
 
     private boolean isTextPart(Entity part) {
@@ -205,6 +203,22 @@ public class MessageParser {
             .orElse(false);
     }
 
+    private Boolean attachmentContentTypeCriterion(Entity part) {
+        return getContentTypeField(part)
+            .map(ContentTypeField::getMimeType)
+            .map(dispositionType -> dispositionType.toLowerCase(Locale.US))
+            .map(ATTACHMENT_CONTENT_TYPES::contains)
+            .orElse(false);
+    }
+
+    private Boolean attachmentDispositionCriterion(Entity part) {
+        return getContentDispositionField(part)
+            .map(ContentDispositionField::getDispositionType)
+            .map(dispositionType -> dispositionType.toLowerCase(Locale.US))
+            .map(ATTACHMENT_CONTENT_DISPOSITIONS::contains)
+            .orElse(false);
+    }
+
     private byte[] getBytes(Body body) throws IOException {
         DefaultMessageWriter messageWriter = new DefaultMessageWriter();
         ByteArrayOutputStream out = new ByteArrayOutputStream();

http://git-wip-us.apache.org/repos/asf/james-project/blob/9e3d5dce/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 581a2df..61df5a9 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
@@ -20,6 +20,7 @@
 package org.apache.james.mailbox.store.mail.model.impl;
 
 import static org.assertj.core.api.Assertions.assertThat;
+
 import java.util.List;
 import java.util.Optional;
 
@@ -253,4 +254,16 @@ public class MessageParserTest {
 
         assertThat(attachments).hasSize(1);
     }
+
+    @Test
+    public void gpgSignatureShouldBeConsideredAsAnAttachment() throws Exception {
+        List<MessageAttachment> attachments = testee.retrieveAttachments(
+            ClassLoader.getSystemResourceAsStream("eml/signedMessage.eml"));
+
+        assertThat(attachments).hasSize(2)
+            .extracting(MessageAttachment::getName)
+            .allMatch(Optional::isPresent)
+            .extracting(Optional::get)
+            .containsOnly("message suivi", "signature.asc");
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/9e3d5dce/mailbox/store/src/test/resources/eml/signedMessage.eml
----------------------------------------------------------------------
diff --git a/mailbox/store/src/test/resources/eml/signedMessage.eml b/mailbox/store/src/test/resources/eml/signedMessage.eml
new file mode 100644
index 0000000..e8c33b8
--- /dev/null
+++ b/mailbox/store/src/test/resources/eml/signedMessage.eml
@@ -0,0 +1,84 @@
+Return-Path: <cl...@linagora.com>
+From: Important Client <cl...@linagora.com>
+To: Big Boss <bi...@linagora.com>, Big big boss <bi...@linagora.com>
+Subject: Fwd: Re: Deploying production instances of James
+Date: Fri, 17 Nov 2017 11:50:12 +0100
+Message-ID: <39...@domain.com>
+Organization: Linagora
+MIME-Version: 1.0
+Content-Type: multipart/signed; boundary="nextPart2399737.qbvXs1L0ps"; micalg="pgp-sha1"; protocol="application/pgp-signature"
+
+--nextPart2399737.qbvXs1L0ps
+Content-Type: multipart/mixed; boundary="nextPart3147505.khcI58SeaZ"
+Content-Transfer-Encoding: 7Bit
+
+This is a multi-part message in MIME format.
+
+--nextPart3147505.khcI58SeaZ
+Content-Transfer-Encoding: quoted-printable
+Content-Type: text/plain; charset="iso-8859-1"
+
+For your information,
+
+--nextPart3147505.khcI58SeaZ
+Content-Type: message/rfc822
+Content-Disposition: inline; filename="message suivi"
+Content-Description: Harold Goguelin <hg...@linagora.com>: Re: La Cerise - certificat =?UTF-8?B?RXhwaXLDqQ==?=
+
+Return-Path: <pr...@linagora.com>
+Subject: Re: Deploying production instances of James
+To: Important Client <cl...@linagora.com>
+From: Project manager <pr...@linagora.com>
+Organization: Linagora
+Message-ID: <c6...@linagora.com>
+Date: Fri, 17 Nov 2017 11:39:27 +0100
+MIME-Version: 1.0
+Content-Type: multipart/alternative; boundary="------------51EED611A8498A33106C3AAD"
+Content-Language: fr
+
+ This is a multi-part message in MIME format.
+--------------51EED611A8498A33106C3AAD
+Content-Type: text/plain; charset="utf-8"; format="flowed"
+Content-Transfer-Encoding: 8bit
+
+Message envoyé grâce à OBM <http://obm.org>, la Communication Libre par 
+Linagora <http://www.linagora.com/>
+
+--------------51EED611A8498A33106C3AAD
+Content-Type: text/html; charset="utf-8"
+Content-Transfer-Encoding: 8bit
+
+<html>
+  <head>
+    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+  </head>
+
+    <div><div>--</div><div>Message envoyé grâce à
+        <a href="http://obm.org">OBM</a>, la Communication Libre par
+        <a href="http://www.linagora.com/">Linagora</a></div></div>
+  </body>
+</html>
+
+--------------51EED611A8498A33106C3AAD--
+
+--nextPart3147505.khcI58SeaZ--
+
+--nextPart2399737.qbvXs1L0ps
+Content-Type: application/pgp-signature; name="signature.asc"
+Content-Description: This is a digitally signed message part.
+Content-Transfer-Encoding: 7Bit
+
+-----BEGIN PGP SIGNATURE-----
+Version: GnuPG v2
+
+iQEcBAABAgAGBQJaDr7mAAoJEG2idAxkrRbwU/AIAMdFxvcR0CkhBJcNJq3Y3acR
+hfrQEO25N7lKpqKgcOZ8q3oh6yUXoSTECSdXDZqOeFLg2lWcP30HxE+imU/7BMbq
+p0ODuTHGBm0jLlXHdgLSsmjgSsZ5QqLJNxuSlJ/S+eG1tREuxVMLw01vVvIOIH4f
+aUmaYmUbFrSzNV6AWbhvifgnzCiPfp6/Q/HorvTngE6LSvmwGUvcghxrlq5+98Ow
+AbWAdSeE1M1IArPKspUydVygD4WnfFnOurcITgka7jTS67MW/VYolwfiZYSX0ao+
+wOTkyd0HKWYU+WsCn/nXKa8+dlYxFdohjDx0UqoX9wagpZt9bgjdGpj7j6jhv/c=
+=rzKZ
+-----END PGP SIGNATURE-----
+
+--nextPart2399737.qbvXs1L0ps--
+


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


[9/9] james-project git commit: MAILBOX-321 Log only in warning when failing to retrieve an attachment

Posted by ad...@apache.org.
MAILBOX-321 Log only in warning when failing to retrieve an attachment


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

Branch: refs/heads/master
Commit: a55693355869a8573269065f684b9d243174725a
Parents: 9e3d5dc
Author: benwa <bt...@linagora.com>
Authored: Thu Nov 30 14:57:22 2017 +0700
Committer: Antoine Duprat <ad...@linagora.com>
Committed: Thu Nov 30 14:55:36 2017 +0100

----------------------------------------------------------------------
 .../james/mailbox/store/mail/model/impl/MessageParser.java       | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/a5569335/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 a0273ba..d447b20 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
@@ -116,9 +116,9 @@ public class MessageParser {
             try {
                 return Stream.of(retrieveAttachment(entity));
             } catch (IllegalStateException e) {
-                LOGGER.error("The attachment is not well-formed", e);
+                LOGGER.warn("The attachment is not well-formed", e);
             } catch (IOException e) {
-                LOGGER.error("There is an error when retrieving attachment", e);
+                LOGGER.warn("There is an error when retrieving attachment", e);
             }
         }
         return Stream.empty();


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


[7/9] james-project git commit: MAILBOX-321 Be more descriptive about attachment content

Posted by ad...@apache.org.
MAILBOX-321 Be more descriptive about attachment content


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

Branch: refs/heads/master
Commit: 1d05817405e054e1a7fef3615a4f34854092ded8
Parents: de10f8c
Author: benwa <bt...@linagora.com>
Authored: Tue Nov 28 13:13:15 2017 +0700
Committer: Antoine Duprat <ad...@linagora.com>
Committed: Thu Nov 30 14:55:35 2017 +0100

----------------------------------------------------------------------
 .../src/test/resources/cucumber/GetMessages.feature            | 6 ++++++
 1 file changed, 6 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/1d058174/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/resources/cucumber/GetMessages.feature
----------------------------------------------------------------------
diff --git a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/resources/cucumber/GetMessages.feature b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/resources/cucumber/GetMessages.feature
index fcf952f..abc5cc5 100644
--- a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/resources/cucumber/GetMessages.feature
+++ b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/resources/cucumber/GetMessages.feature
@@ -428,3 +428,9 @@ Feature: GetMessages method
     And the list should contain 1 message
     And the hasAttachment of the message is "true"
     And the list of attachments of the message contains 1 attachments
+    And the first attachment is:
+      |key      | value                        |
+      |type     |"application/octet-stream"    |
+      |cid      |null                          |
+      |name     |"encrypted.asc"               |
+      |isInline |true                          |


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


[4/9] james-project git commit: MAILBOX-321 Improve error handling: don't swallow stacktrace

Posted by ad...@apache.org.
MAILBOX-321 Improve error handling: don't swallow stacktrace


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

Branch: refs/heads/master
Commit: c034bc4d91142a3b1dff31a36f5f2f87d1f0253d
Parents: c2b04bc
Author: benwa <bt...@linagora.com>
Authored: Tue Nov 28 10:17:55 2017 +0700
Committer: Antoine Duprat <ad...@linagora.com>
Committed: Thu Nov 30 14:54:09 2017 +0100

----------------------------------------------------------------------
 .../james/mailbox/store/mail/model/impl/MessageParser.java       | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/c034bc4d/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 362f5ac..785c894 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
@@ -122,9 +122,9 @@ public class MessageParser {
             try {
                 return Stream.of(retrieveAttachment(entity));
             } catch (IllegalStateException e) {
-                LOGGER.error("The attachment is not well-formed: " + e.getCause());
+                LOGGER.error("The attachment is not well-formed", e);
             } catch (IOException e) {
-                LOGGER.error("There is error on retrieve attachment: " + e.getCause());
+                LOGGER.error("There is an error when retrieving attachment", e);
             }
         }
         return Stream.empty();


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


[2/9] james-project git commit: MAILBOX-321 Functional style for attachment parsing

Posted by ad...@apache.org.
MAILBOX-321 Functional style for attachment parsing


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

Branch: refs/heads/master
Commit: c2b04bc5ba0728993f45b37d3517065b72c71166
Parents: 58bd6d4
Author: benwa <bt...@linagora.com>
Authored: Tue Nov 28 10:17:10 2017 +0700
Committer: Antoine Duprat <ad...@linagora.com>
Committed: Thu Nov 30 14:54:06 2017 +0100

----------------------------------------------------------------------
 .../store/mail/model/impl/MessageParser.java    | 40 +++++++++++---------
 1 file changed, 22 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/c2b04bc5/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 67df235..362f5ac 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
@@ -26,6 +26,7 @@ import java.util.List;
 import java.util.Locale;
 import java.util.Optional;
 import java.util.function.Function;
+import java.util.stream.Stream;
 
 import org.apache.james.mailbox.model.Attachment;
 import org.apache.james.mailbox.model.Cid;
@@ -48,6 +49,7 @@ import org.apache.james.mime4j.util.MimeUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.github.steveash.guavate.Guavate;
 import com.google.common.collect.ImmutableList;
 
 public class MessageParser {
@@ -91,8 +93,9 @@ public class MessageParser {
             }
 
             if (body instanceof Multipart) {
-                Multipart multipartBody = (Multipart)body;
-                return listAttachments(multipartBody, Context.fromSubType(multipartBody.getSubType()));
+                Multipart multipartBody = (Multipart) body;
+                return listAttachments(multipartBody, Context.fromSubType(multipartBody.getSubType()))
+                    .collect(Guavate.toImmutableList());
             } else {
                 return ImmutableList.of();
             }
@@ -105,25 +108,26 @@ public class MessageParser {
         return contentDisposition.isPresent() && contentDisposition.get().isAttachment();
     }
 
-    private List<MessageAttachment> listAttachments(Multipart multipart, Context context) throws IOException {
-        ImmutableList.Builder<MessageAttachment> attachments = ImmutableList.builder();
+    private Stream<MessageAttachment> listAttachments(Multipart multipart, Context context) {
+        return multipart.getBodyParts()
+            .stream()
+            .flatMap(entity -> listAttachments(entity, context));
+    }
 
-        for (Entity entity : multipart.getBodyParts()) {
-            if (isMultipart(entity)) {
-                attachments.addAll(listAttachments((Multipart) entity.getBody(), Context.fromEntity(entity)));
-            } else {
-                if (isAttachment(entity, context)) {
-                    try {
-                        attachments.add(retrieveAttachment(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());
-                    }
-                }
+    private Stream<MessageAttachment> listAttachments(Entity entity, Context context) {
+        if (isMultipart(entity)) {
+            return listAttachments((Multipart) entity.getBody(), Context.fromEntity(entity));
+        }
+        if (isAttachment(entity, context)) {
+            try {
+                return Stream.of(retrieveAttachment(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());
             }
         }
-        return attachments.build();
+        return Stream.empty();
     }
 
     private MessageAttachment retrieveAttachment(Entity entity) throws IOException {


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


[6/9] james-project git commit: MAILBOX-321 Factorize single attachment handling

Posted by ad...@apache.org.
MAILBOX-321 Factorize single attachment handling

We can rely on `isAttachment(...)` for attachment detection.


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

Branch: refs/heads/master
Commit: de10f8c1df49374f3e284b66bdf4ff78f9b2ac7c
Parents: 4189dda
Author: benwa <bt...@linagora.com>
Authored: Tue Nov 28 11:15:03 2017 +0700
Committer: Antoine Duprat <ad...@linagora.com>
Committed: Thu Nov 30 14:55:33 2017 +0100

----------------------------------------------------------------------
 .../james/mailbox/store/mail/model/impl/MessageParser.java   | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/de10f8c1/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 5494e29..cbde24a 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
@@ -85,9 +85,7 @@ public class MessageParser {
         Message message = defaultMessageBuilder.parseMessage(fullContent);
         Body body = message.getBody();
         try {
-            Optional<ContentDispositionField> contentDisposition = readHeader(message, CONTENT_DISPOSITION, ContentDispositionField.class);
-
-            if (isMessageWithOnlyOneAttachment(contentDisposition)) {
+            if (isAttachment(message, Context.BODY)) {
                 return ImmutableList.of(retrieveAttachment(message));
             }
 
@@ -103,10 +101,6 @@ public class MessageParser {
         }
     }
 
-    private boolean isMessageWithOnlyOneAttachment(Optional<ContentDispositionField> contentDisposition) {
-        return contentDisposition.isPresent() && contentDisposition.get().isAttachment();
-    }
-
     private Stream<MessageAttachment> listAttachments(Multipart multipart, Context context) {
         return multipart.getBodyParts()
             .stream()


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


[3/9] james-project git commit: MAILBOX-321 Simplify CID handling

Posted by ad...@apache.org.
MAILBOX-321 Simplify CID handling


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

Branch: refs/heads/master
Commit: b292d37c36539bec9529f088a8f9f26e7e5bbc1b
Parents: c034bc4
Author: benwa <bt...@linagora.com>
Authored: Tue Nov 28 10:27:03 2017 +0700
Committer: Antoine Duprat <ad...@linagora.com>
Committed: Thu Nov 30 14:54:09 2017 +0100

----------------------------------------------------------------------
 .../mailbox/store/mail/model/impl/MessageParser.java    | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/b292d37c/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 785c894..c6b6b56 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
@@ -25,7 +25,6 @@ import java.io.InputStream;
 import java.util.List;
 import java.util.Locale;
 import java.util.Optional;
-import java.util.function.Function;
 import java.util.stream.Stream;
 
 import org.apache.james.mailbox.model.Attachment;
@@ -182,15 +181,8 @@ public class MessageParser {
     }
 
     private Optional<Cid> cid(Optional<ContentIdField> contentIdField) {
-        if (!contentIdField.isPresent()) {
-            return Optional.empty();
-        }
-        return contentIdField.map(toCid())
-            .get();
-    }
-
-    private Function<ContentIdField, Optional<Cid>> toCid() {
-        return contentIdField -> cidParser.parse(contentIdField.getId());
+        return contentIdField.map(ContentIdField::getId)
+            .flatMap(cidParser::parse);
     }
 
     private boolean isMultipart(Entity entity) {


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


[5/9] james-project git commit: MAILBOX-321 More functional code for MessageParser::isTextPart

Posted by ad...@apache.org.
MAILBOX-321 More functional code for MessageParser::isTextPart


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

Branch: refs/heads/master
Commit: 4189dda8eb9f7e340413b54e2d76ddfb77bff5d7
Parents: b292d37
Author: benwa <bt...@linagora.com>
Authored: Tue Nov 28 10:31:34 2017 +0700
Committer: Antoine Duprat <ad...@linagora.com>
Committed: Thu Nov 30 14:54:09 2017 +0100

----------------------------------------------------------------------
 .../mailbox/store/mail/model/impl/MessageParser.java    | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/4189dda8/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 c6b6b56..5494e29 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
@@ -205,14 +205,10 @@ public class MessageParser {
     }
 
     private boolean isTextPart(Entity part) {
-        Optional<ContentTypeField> contentTypeField = getContentTypeField(part);
-        if (contentTypeField.isPresent()) {
-            String mediaType = contentTypeField.get().getMediaType();
-            if (mediaType != null && mediaType.equals(TEXT_MEDIA_TYPE)) {
-                return true;
-            }
-        }
-        return false;
+        return getContentTypeField(part)
+            .map(ContentTypeField::getMediaType)
+            .map(TEXT_MEDIA_TYPE::equals)
+            .orElse(false);
     }
 
     private byte[] getBytes(Body body) throws IOException {


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