james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [6/6] james-project git commit: JAMES-1717 Support plain text extraction from HTML text in Vacation notifications
Date Fri, 03 Jun 2016 12:38:38 GMT
JAMES-1717 Support plain text extraction from HTML text in Vacation notifications


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

Branch: refs/heads/master
Commit: a8dd5d0630712e7ad52cea0aa007d93aa5354cba
Parents: a8d4723
Author: Benoit Tellier <btellier@linagora.com>
Authored: Thu Jun 2 09:47:36 2016 +0700
Committer: Benoit Tellier <btellier@linagora.com>
Committed: Fri Jun 3 19:38:15 2016 +0700

----------------------------------------------------------------------
 server/protocols/jmap/pom.xml                   |  5 ++++
 .../apache/james/jmap/mailet/VacationReply.java | 30 ++++++++------------
 .../james/jmap/mailet/VacationReplyTest.java    | 21 ++++++++++++--
 3 files changed, 36 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/a8dd5d06/server/protocols/jmap/pom.xml
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/pom.xml b/server/protocols/jmap/pom.xml
index e088819..d800fa6 100644
--- a/server/protocols/jmap/pom.xml
+++ b/server/protocols/jmap/pom.xml
@@ -274,6 +274,11 @@
                     <artifactId>bcpkix-jdk15on</artifactId>
                 </dependency>
                 <dependency>
+                    <groupId>org.jsoup</groupId>
+                    <artifactId>jsoup</artifactId>
+                    <version>1.9.2</version>
+                </dependency>
+                <dependency>
                     <groupId>org.apache.james</groupId>
                     <artifactId>apache-james-mailbox-store</artifactId>
                     <scope>test</scope>

http://git-wip-us.apache.org/repos/asf/james-project/blob/a8dd5d06/server/protocols/jmap/src/main/java/org/apache/james/jmap/mailet/VacationReply.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/mailet/VacationReply.java
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/mailet/VacationReply.java
index 3b8ae2a..a848759 100644
--- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/mailet/VacationReply.java
+++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/mailet/VacationReply.java
@@ -21,7 +21,6 @@ package org.apache.james.jmap.mailet;
 
 import java.io.IOException;
 import java.util.List;
-import java.util.Optional;
 
 import javax.activation.DataHandler;
 import javax.mail.MessagingException;
@@ -36,6 +35,7 @@ import org.apache.james.mime4j.dom.field.ContentTypeField;
 import org.apache.mailet.Mail;
 import org.apache.mailet.MailAddress;
 import org.apache.mailet.base.AutomaticallySentMailDetector;
+import org.jsoup.Jsoup;
 
 import com.github.fge.lambdas.Throwing;
 import com.google.common.base.Preconditions;
@@ -78,9 +78,6 @@ public class VacationReply {
             Preconditions.checkState(mailRecipient != null, "Original recipient address should
not be null");
             Preconditions.checkState(originalMail.getSender() != null, "Original sender address
should not be null");
 
-            MimeMessage reply = (MimeMessage) originalMail.getMessage().reply(false);
-            reply.setContent(generateMultipart());
-
             return new VacationReply(mailRecipient, ImmutableList.of(originalMail.getSender()),
generateMimeMessage());
         }
 
@@ -94,35 +91,32 @@ public class VacationReply {
             return addBody(reply);
         }
 
+        @SuppressWarnings("OptionalGetWithoutIsPresent")
         private MimeMessage addBody(MimeMessage reply) throws MessagingException {
-            if (! vacation.getHtmlBody().isPresent()) {
-                reply.setText(vacation.getTextBody().get());
-            } else {
+            if (vacation.getHtmlBody().isPresent()) {
                 reply.setContent(generateMultipart());
+            } else {
+                reply.setText(vacation.getTextBody().get());
             }
             return reply;
         }
 
-
+        @SuppressWarnings("OptionalGetWithoutIsPresent")
         private Multipart generateMultipart() throws MessagingException {
             try {
                 Multipart multipart = new MimeMultipart(MIXED);
-                addPlainPart(multipart, vacation.getTextBody());
-                addHtmlPart(multipart, vacation.getHtmlBody());
+                addTextPart(multipart, vacation.getHtmlBody().get(), "text/html");
+                addTextPart(multipart, retrievePlainTextMessage(), ContentTypeField.TYPE_TEXT_PLAIN);
                 return multipart;
             } catch (IOException e) {
                 throw new MessagingException("Cannot read specified content", e);
             }
         }
 
-        private Multipart addPlainPart(Multipart multipart, Optional<String> textBody)
throws MessagingException, IOException {
-            textBody.ifPresent(Throwing.consumer(text -> addTextPart(multipart, text,
ContentTypeField.TYPE_TEXT_PLAIN)));
-            return multipart;
-        }
-
-        private Multipart addHtmlPart(Multipart multipart, Optional<String> htmlBody)
throws MessagingException, IOException {
-            htmlBody.ifPresent(Throwing.consumer(html -> addTextPart(multipart, html,
"text/html")));
-            return multipart;
+        @SuppressWarnings("OptionalGetWithoutIsPresent")
+        private String retrievePlainTextMessage() {
+            return vacation.getTextBody()
+                .orElseGet(() -> Jsoup.parse(vacation.getHtmlBody().get()).text());
         }
 
         private Multipart addTextPart(Multipart multipart, String text, String contentType)
throws MessagingException, IOException {

http://git-wip-us.apache.org/repos/asf/james-project/blob/a8dd5d06/server/protocols/jmap/src/test/java/org/apache/james/jmap/mailet/VacationReplyTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/mailet/VacationReplyTest.java
b/server/protocols/jmap/src/test/java/org/apache/james/jmap/mailet/VacationReplyTest.java
index b42c799..5bb2ddd 100644
--- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/mailet/VacationReplyTest.java
+++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/mailet/VacationReplyTest.java
@@ -36,8 +36,9 @@ import org.junit.Test;
 
 public class VacationReplyTest {
 
-    public static final String REASON = "I am in vacation dudes !";
-    public static final String HTML_REASON = "<p>I am in vacation dudes !</p>";
+    public static final String REASON = "I am in vacation dudes ! (plain text)";
+    public static final String HTML_REASON = "<b>I am in vacation dudes !</b>
(html text)";
+    public static final String HTML_EXTRACTED_REASON = "I am in vacation dudes ! (html text)";
     public static final String SUBJECT = "subject";
 
     private MailAddress originalSender;
@@ -73,6 +74,22 @@ public class VacationReplyTest {
     }
 
     @Test
+    public void vacationReplyShouldExtractPlainTextContentWhenOnlyHtmlBody() throws Exception
{
+        VacationReply vacationReply = VacationReply.builder(mail)
+            .vacation(Vacation.builder()
+                .enabled(true)
+                .htmlBody(HTML_REASON)
+                .build())
+            .receivedMailRecipient(originalRecipient)
+            .build();
+
+        assertThat(vacationReply.getRecipients()).containsExactly(originalSender);
+        assertThat(vacationReply.getSender()).isEqualTo(originalRecipient);
+        assertThat(IOUtils.toString(vacationReply.getMimeMessage().getInputStream())).contains(HTML_EXTRACTED_REASON);
+        assertThat(IOUtils.toString(vacationReply.getMimeMessage().getInputStream())).contains(HTML_REASON);
+    }
+
+    @Test
     public void vacationReplyShouldNotBeMultipartWhenVacationHaveNoHTML() throws Exception
{
         VacationReply vacationReply = VacationReply.builder(mail)
             .vacation(Vacation.builder()


---------------------------------------------------------------------
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