MAILET-115 Extract message generation for Notify mailets from AbstractRedirect
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/c3045dab Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/c3045dab Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/c3045dab Branch: refs/heads/master Commit: c3045dab994d3947909a13f48265de4a74aea928 Parents: 724935d Author: Antoine Duprat <adup...@linagora.com> Authored: Tue Sep 13 10:55:13 2016 +0200 Committer: Benoit Tellier <btell...@linagora.com> Committed: Wed Jan 11 10:03:27 2017 +0700 ---------------------------------------------------------------------- .../mailets/redirect/AbstractRedirect.java | 67 +----- .../mailets/redirect/NotifyMailetsMessage.java | 110 ++++++++++ .../redirect/NotifyMailetsMessageTest.java | 214 +++++++++++++++++++ 3 files changed, 325 insertions(+), 66 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/c3045dab/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/AbstractRedirect.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/AbstractRedirect.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/AbstractRedirect.java index bb140c3..24efdfe 100644 --- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/AbstractRedirect.java +++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/AbstractRedirect.java @@ -345,76 +345,11 @@ public abstract class AbstractRedirect extends GenericMailet { */ protected String getMessage(Mail originalMail) throws MessagingException { if (isNotifyMailet()) { - return getMessageForNotifyMailets(originalMail); + return new NotifyMailetsMessage().generateMessage(getMessage(), originalMail); } return (isStatic()) ? this.messageText : getMessage(); } - private String getMessageForNotifyMailets(Mail originalMail) throws MessagingException { - MimeMessage message = originalMail.getMessage(); - StringBuffer buffer = new StringBuffer(); - - buffer.append(getMessage()).append(LINE_BREAK); - if (originalMail.getErrorMessage() != null) { - buffer.append(LINE_BREAK) - .append("Error message below:") - .append(LINE_BREAK) - .append(originalMail.getErrorMessage()) - .append(LINE_BREAK); - } - buffer.append(LINE_BREAK) - .append("Message details:") - .append(LINE_BREAK); - - if (message.getSubject() != null) { - buffer.append(" Subject: " + message.getSubject()) - .append(LINE_BREAK); - } - if (message.getSentDate() != null) { - buffer.append(" Sent date: " + message.getSentDate()) - .append(LINE_BREAK); - } - buffer.append(" MAIL FROM: " + originalMail.getSender()) - .append(LINE_BREAK); - - boolean firstRecipient = true; - for (MailAddress recipient : originalMail.getRecipients()) { - if (firstRecipient) { - buffer.append(" RCPT TO: " + recipient) - .append(LINE_BREAK); - firstRecipient = false; - } else { - buffer.append(" " + recipient) - .append(LINE_BREAK); - } - } - - appendAddresses(buffer, "From", message.getHeader(RFC2822Headers.FROM)); - appendAddresses(buffer, "To", message.getHeader(RFC2822Headers.TO)); - appendAddresses(buffer, "CC", message.getHeader(RFC2822Headers.CC)); - - buffer.append(" Size (in bytes): " + message.getSize()) - .append(LINE_BREAK); - if (message.getLineCount() >= 0) { - buffer.append(" Number of lines: " + message.getLineCount()) - .append(LINE_BREAK); - } - - return buffer.toString(); - } - - private void appendAddresses(StringBuffer buffer, String title, String[] addresses) { - if (addresses != null) { - buffer.append(" " + title + ": ") - .append(LINE_BREAK); - for (String address : addresses) { - buffer.append(address + " ") - .append(LINE_BREAK); - } - buffer.append(LINE_BREAK); - } - } - /** * Gets the <code>recipients</code> property. Returns the collection of * recipients of the new message, or null if no change is requested. Is a http://git-wip-us.apache.org/repos/asf/james-project/blob/c3045dab/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/NotifyMailetsMessage.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/NotifyMailetsMessage.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/NotifyMailetsMessage.java new file mode 100644 index 0000000..dc3b048 --- /dev/null +++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/NotifyMailetsMessage.java @@ -0,0 +1,110 @@ +/**************************************************************** + * 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.james.transport.mailets.redirect; + +import java.util.List; + +import javax.mail.MessagingException; +import javax.mail.internet.MimeMessage; + +import org.apache.mailet.Mail; +import org.apache.mailet.MailAddress; +import org.apache.mailet.base.RFC2822Headers; + +import com.google.common.base.Splitter; +import com.google.common.collect.ImmutableList; + +public class NotifyMailetsMessage { + + private static final char LINE_BREAK = '\n'; + + public String generateMessage(String parameterMessage, Mail originalMail) throws MessagingException { + MimeMessage message = originalMail.getMessage(); + StringBuilder builder = new StringBuilder(); + + builder.append(parameterMessage).append(LINE_BREAK); + if (originalMail.getErrorMessage() != null) { + builder.append(LINE_BREAK) + .append("Error message below:") + .append(LINE_BREAK) + .append(originalMail.getErrorMessage()) + .append(LINE_BREAK); + } + builder.append(LINE_BREAK) + .append("Message details:") + .append(LINE_BREAK); + + if (message.getSubject() != null) { + builder.append(" Subject: " + message.getSubject()) + .append(LINE_BREAK); + } + if (message.getSentDate() != null) { + builder.append(" Sent date: " + message.getSentDate()) + .append(LINE_BREAK); + } + builder.append(" MAIL FROM: " + originalMail.getSender()) + .append(LINE_BREAK); + + boolean firstRecipient = true; + for (MailAddress recipient : originalMail.getRecipients()) { + if (firstRecipient) { + builder.append(" RCPT TO: " + recipient) + .append(LINE_BREAK); + firstRecipient = false; + } else { + builder.append(" " + recipient) + .append(LINE_BREAK); + } + } + + appendAddresses(builder, "From", message.getHeader(RFC2822Headers.FROM)); + appendAddresses(builder, "To", message.getHeader(RFC2822Headers.TO)); + appendAddresses(builder, "CC", message.getHeader(RFC2822Headers.CC)); + + builder.append(" Size (in bytes): " + message.getSize()) + .append(LINE_BREAK); + if (message.getLineCount() >= 0) { + builder.append(" Number of lines: " + message.getLineCount()) + .append(LINE_BREAK); + } + + return builder.toString(); + } + + private void appendAddresses(StringBuilder builder, String title, String[] addresses) { + if (addresses != null) { + builder.append(" " + title + ": ") + .append(LINE_BREAK); + for (String address : flatten(addresses)) { + builder.append(address + " ") + .append(LINE_BREAK); + } + builder.append(LINE_BREAK); + } + } + + private List<String> flatten(String[] addresses) { + final ImmutableList.Builder<String> builder = ImmutableList.builder(); + for (String address : addresses) { + builder.addAll(Splitter.on(',').trimResults().split(address)); + } + return builder.build(); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/c3045dab/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/redirect/NotifyMailetsMessageTest.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/redirect/NotifyMailetsMessageTest.java b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/redirect/NotifyMailetsMessageTest.java new file mode 100644 index 0000000..0cdaca2 --- /dev/null +++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/redirect/NotifyMailetsMessageTest.java @@ -0,0 +1,214 @@ +/**************************************************************** + * 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.james.transport.mailets.redirect; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.ByteArrayInputStream; +import java.util.Properties; +import java.util.TimeZone; + +import javax.mail.Message.RecipientType; +import javax.mail.Session; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; + +import org.apache.mailet.MailAddress; +import org.apache.mailet.base.test.FakeMail; +import org.joda.time.DateTime; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.google.common.collect.ImmutableList; + +public class NotifyMailetsMessageTest { + + private TimeZone timeZone; + + @Before + public void setUp() throws Exception { + timeZone = TimeZone.getDefault(); + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + } + + @After + public void tearDown() { + TimeZone.setDefault(timeZone); + } + + @Test + public void generateMessageShouldReturnTheMessageWhenSimpleMimeMessage() throws Exception { + MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties())); + FakeMail mail = FakeMail.builder() + .mimeMessage(message) + .sender(new MailAddress("user", "james.org")) + .build(); + + String generateMessage = new NotifyMailetsMessage().generateMessage("my message", mail); + + assertThat(generateMessage).isEqualTo("my message\n" + + "\n" + + "Message details:\n" + + " MAIL FROM: u...@james.org\n" + + " Size (in bytes): -1\n"); + } + + @Test + public void generateMessageShouldAddErrorMessageWhenMimeMessageAsSome() throws Exception { + MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties())); + FakeMail mail = FakeMail.from(message); + mail.setErrorMessage("error message"); + + String generateMessage = new NotifyMailetsMessage().generateMessage("my message", mail); + + assertThat(generateMessage).isEqualTo("my message\n" + + "\n" + + "Error message below:\n" + + "error message\n" + + "\n" + + "Message details:\n" + + " MAIL FROM: null\n" + + " Size (in bytes): -1\n"); + } + + @Test + public void generateMessageShouldAddSubjectWhenMimeMessageAsSome() throws Exception { + MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties())); + message.setSubject("my subject"); + FakeMail mail = FakeMail.from(message); + + String generateMessage = new NotifyMailetsMessage().generateMessage("my message", mail); + + assertThat(generateMessage).isEqualTo("my message\n" + + "\n" + + "Message details:\n" + + " Subject: my subject\n" + + " MAIL FROM: null\n" + + " Size (in bytes): -1\n"); + } + + @Test + public void generateMessageShouldAddSentDateWhenMimeMessageAsSome() throws Exception { + MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties())); + message.setSentDate(DateTime.parse("2016-09-08T14:25:52.000Z").toDate()); + FakeMail mail = FakeMail.from(message); + + String generateMessage = new NotifyMailetsMessage().generateMessage("my message", mail); + + assertThat(generateMessage).isEqualTo("my message\n" + + "\n" + + "Message details:\n" + + " Sent date: Thu Sep 08 14:25:52 UTC 2016\n" + + " MAIL FROM: null\n" + + " Size (in bytes): -1\n"); + } + + @Test + public void generateMessageShouldAddRecipientsWhenMimeMessageAsSome() throws Exception { + MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties())); + FakeMail mail = FakeMail.from(message); + mail.setRecipients(ImmutableList.of(new MailAddress("user", "james.org"), new MailAddress("user2", "james.org"))); + + String generateMessage = new NotifyMailetsMessage().generateMessage("my message", mail); + + assertThat(generateMessage).isEqualTo("my message\n" + + "\n" + + "Message details:\n" + + " MAIL FROM: null\n" + + " RCPT TO: u...@james.org\n" + + " us...@james.org\n" + + " Size (in bytes): -1\n"); + } + + @Test + public void generateMessageShouldAddFromWhenMimeMessageAsSome() throws Exception { + MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties())); + message.setFrom(new InternetAddress("u...@james.org")); + FakeMail mail = FakeMail.from(message); + + String generateMessage = new NotifyMailetsMessage().generateMessage("my message", mail); + + assertThat(generateMessage).isEqualTo("my message\n" + + "\n" + + "Message details:\n" + + " MAIL FROM: null\n" + + " From: \n" + + "u...@james.org \n" + + "\n" + + " Size (in bytes): -1\n"); + } + + @Test + public void generateMessageShouldAddToWhenMimeMessageAsSome() throws Exception { + MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties())); + message.setRecipients(RecipientType.TO, new InternetAddress[] { new InternetAddress("u...@james.org"), new InternetAddress("us...@james.org") }); + FakeMail mail = FakeMail.from(message); + + String generateMessage = new NotifyMailetsMessage().generateMessage("my message", mail); + + assertThat(generateMessage).isEqualTo("my message\n" + + "\n" + + "Message details:\n" + + " MAIL FROM: null\n" + + " To: \n" + + "u...@james.org \n" + + "us...@james.org \n" + + "\n" + + " Size (in bytes): -1\n"); + } + + @Test + public void generateMessageShouldAddCCWhenMimeMessageAsSome() throws Exception { + MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties())); + message.setRecipients(RecipientType.CC, new InternetAddress[] { new InternetAddress("u...@james.org"), new InternetAddress("us...@james.org") }); + FakeMail mail = FakeMail.from(message); + + String generateMessage = new NotifyMailetsMessage().generateMessage("my message", mail); + + assertThat(generateMessage).isEqualTo("my message\n" + + "\n" + + "Message details:\n" + + " MAIL FROM: null\n" + + " CC: \n" + + "u...@james.org \n" + + "us...@james.org \n" + + "\n" + + " Size (in bytes): -1\n"); + } + + @Test + public void generateMessageShouldAddSizeWhenMimeMessageAsSome() throws Exception { + String content = "MIME-Version: 1.0\r\n" + + "Content-Type: text/plain; charset=utf-8\r\n" + + "\r\n" + + "test\r\n"; + MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()), new ByteArrayInputStream(content.getBytes())); + FakeMail mail = FakeMail.from(message); + + String generateMessage = new NotifyMailetsMessage().generateMessage("my message", mail); + + assertThat(generateMessage).isEqualTo("my message\n" + + "\n" + + "Message details:\n" + + " MAIL FROM: null\n" + + " Size (in bytes): 6\n"); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org