james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rouaz...@apache.org
Subject [02/50] [abbrv] james-project git commit: MAILET-115 Extract subject prefixing from AbstractRedirect
Date Wed, 11 Jan 2017 09:25:50 GMT
MAILET-115 Extract subject prefixing 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/875c39ba
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/875c39ba
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/875c39ba

Branch: refs/heads/master
Commit: 875c39ba3b890e2c5dd9b4f3603b4f81de6f03eb
Parents: c3045da
Author: Antoine Duprat <aduprat@linagora.com>
Authored: Tue Sep 13 11:00:05 2016 +0200
Committer: Benoit Tellier <btellier@linagora.com>
Committed: Wed Jan 11 10:03:27 2017 +0700

----------------------------------------------------------------------
 .../transport/mailets/AddSubjectPrefix.java     | 30 ++---------
 .../mailets/utils/MimeMessageModifier.java      | 55 ++++++++++++++++++++
 .../mailets/utils/MimeMessageModifierTest.java  | 51 ++++++++++++++++++
 .../apache/james/transport/mailets/Forward.java |  1 +
 .../transport/mailets/NotifyPostmaster.java     |  1 +
 .../james/transport/mailets/NotifySender.java   |  1 +
 .../james/transport/mailets/Redirect.java       |  1 +
 .../mailets/redirect/AbstractRedirect.java      |  8 +--
 8 files changed, 117 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/875c39ba/mailet/standard/src/main/java/org/apache/james/transport/mailets/AddSubjectPrefix.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/main/java/org/apache/james/transport/mailets/AddSubjectPrefix.java
b/mailet/standard/src/main/java/org/apache/james/transport/mailets/AddSubjectPrefix.java
index e923aaf..b808126 100644
--- a/mailet/standard/src/main/java/org/apache/james/transport/mailets/AddSubjectPrefix.java
+++ b/mailet/standard/src/main/java/org/apache/james/transport/mailets/AddSubjectPrefix.java
@@ -20,16 +20,14 @@
 
 package org.apache.james.transport.mailets;
 
+import javax.mail.MessagingException;
+
+import org.apache.james.transport.mailets.utils.MimeMessageModifier;
 import org.apache.mailet.Mail;
 import org.apache.mailet.base.GenericMailet;
 
-import com.google.common.base.Charsets;
-import com.google.common.base.Joiner;
 import com.google.common.base.Strings;
 
-import javax.mail.MessagingException;
-import javax.mail.internet.MimeMessage;
-
 /**
  * Add an prefix (tag) to the subject of a message <br>
  * <br>
@@ -55,29 +53,11 @@ public class AddSubjectPrefix extends GenericMailet {
 
     @Override
     public void service(Mail mail) throws MessagingException {
-        MimeMessage m = mail.getMessage();
-        String newSubject = prefixSubject(m);
-        replaceSubject(m, newSubject);
-    }
-
-    private void replaceSubject(MimeMessage m, String newSubject) throws MessagingException
{
-        m.setSubject(null);
-        m.setSubject(newSubject, Charsets.UTF_8.displayName());
-    }
-
-    private String prefixSubject(MimeMessage m) throws MessagingException {
-        String subject = m.getSubject();
-
-        if (subject != null) {
-            return Joiner.on(' ').join(subjectPrefix, subject);
-        } else {
-            return subjectPrefix;
-        }
+        new MimeMessageModifier(mail.getMessage()).addSubjectPrefix(subjectPrefix);
     }
 
+    @Override
     public String getMailetInfo() {
         return "AddSubjectPrefix Mailet";
     }
-
-
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/875c39ba/mailet/standard/src/main/java/org/apache/james/transport/mailets/utils/MimeMessageModifier.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/main/java/org/apache/james/transport/mailets/utils/MimeMessageModifier.java
b/mailet/standard/src/main/java/org/apache/james/transport/mailets/utils/MimeMessageModifier.java
new file mode 100644
index 0000000..efaf596
--- /dev/null
+++ b/mailet/standard/src/main/java/org/apache/james/transport/mailets/utils/MimeMessageModifier.java
@@ -0,0 +1,55 @@
+/****************************************************************
+ * 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.utils;
+
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeMessage;
+
+import com.google.common.base.Charsets;
+import com.google.common.base.Joiner;
+
+public class MimeMessageModifier {
+
+    private final MimeMessage message;
+
+    public MimeMessageModifier(MimeMessage message) {
+        this.message = message;
+    }
+
+    public void addSubjectPrefix(String subjectPrefix) throws MessagingException {
+        String newSubject = prefixSubject(message, subjectPrefix);
+        replaceSubject(message, newSubject);
+    }
+    
+    private void replaceSubject(MimeMessage message, String newSubject) throws MessagingException
{
+        message.setSubject(null);
+        message.setSubject(newSubject, Charsets.UTF_8.displayName());
+    }
+    
+    private String prefixSubject(MimeMessage message, String subjectPrefix) throws MessagingException
{
+        String subject = message.getSubject();
+    
+        if (subject != null) {
+            return Joiner.on(' ').join(subjectPrefix, subject);
+        } else {
+            return subjectPrefix;
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/875c39ba/mailet/standard/src/test/java/org/apache/james/transport/mailets/utils/MimeMessageModifierTest.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/test/java/org/apache/james/transport/mailets/utils/MimeMessageModifierTest.java
b/mailet/standard/src/test/java/org/apache/james/transport/mailets/utils/MimeMessageModifierTest.java
new file mode 100644
index 0000000..5b17964
--- /dev/null
+++ b/mailet/standard/src/test/java/org/apache/james/transport/mailets/utils/MimeMessageModifierTest.java
@@ -0,0 +1,51 @@
+/****************************************************************
+ * 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.utils;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.Properties;
+
+import javax.mail.Session;
+import javax.mail.internet.MimeMessage;
+
+import org.junit.Test;
+
+public class MimeMessageModifierTest {
+
+    @Test
+    public void addSubjectPrefixShouldAddPrefixToSubjectWhenSubjectIsPresent() throws Exception
{
+        MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()));
+        message.setSubject("subject");
+
+        new MimeMessageModifier(message).addSubjectPrefix("my");
+
+        assertThat(message.getSubject()).isEqualTo("my subject");
+    }
+
+    @Test
+    public void addSubjectPrefixShouldSetPrefixAsSubjectWhenSubjectIsAbsent() throws Exception
{
+        MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()));
+
+        new MimeMessageModifier(message).addSubjectPrefix("my");
+
+        assertThat(message.getSubject()).isEqualTo("my");
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/875c39ba/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Forward.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Forward.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Forward.java
index 0bc9256..bca2c43 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Forward.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Forward.java
@@ -27,6 +27,7 @@ import javax.mail.internet.AddressException;
 import javax.mail.internet.InternetAddress;
 
 import org.apache.james.transport.mailets.redirect.AbstractRedirect;
+import org.apache.james.transport.mailets.redirect.AddressExtractor;
 import org.apache.james.transport.mailets.redirect.InitParameters;
 import org.apache.james.transport.mailets.redirect.RedirectMailetInitParameters;
 import org.apache.james.transport.mailets.redirect.TypeCode;

http://git-wip-us.apache.org/repos/asf/james-project/blob/875c39ba/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/NotifyPostmaster.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/NotifyPostmaster.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/NotifyPostmaster.java
index 6de5b7d..a71bede 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/NotifyPostmaster.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/NotifyPostmaster.java
@@ -26,6 +26,7 @@ import javax.mail.MessagingException;
 import javax.mail.internet.InternetAddress;
 
 import org.apache.james.transport.mailets.redirect.AbstractRedirect;
+import org.apache.james.transport.mailets.redirect.AddressExtractor;
 import org.apache.james.transport.mailets.redirect.InitParameters;
 import org.apache.james.transport.mailets.redirect.NotifyMailetInitParameters;
 import org.apache.mailet.MailAddress;

http://git-wip-us.apache.org/repos/asf/james-project/blob/875c39ba/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/NotifySender.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/NotifySender.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/NotifySender.java
index 08207d5..d93cc9d 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/NotifySender.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/NotifySender.java
@@ -27,6 +27,7 @@ import javax.mail.MessagingException;
 import javax.mail.internet.InternetAddress;
 
 import org.apache.james.transport.mailets.redirect.AbstractRedirect;
+import org.apache.james.transport.mailets.redirect.AddressExtractor;
 import org.apache.james.transport.mailets.redirect.InitParameters;
 import org.apache.james.transport.mailets.redirect.NotifyMailetInitParameters;
 import org.apache.james.transport.mailets.redirect.SpecialAddress;

http://git-wip-us.apache.org/repos/asf/james-project/blob/875c39ba/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Redirect.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Redirect.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Redirect.java
index f018f0c..d16cc41 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Redirect.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Redirect.java
@@ -26,6 +26,7 @@ import javax.mail.MessagingException;
 import javax.mail.internet.InternetAddress;
 
 import org.apache.james.transport.mailets.redirect.AbstractRedirect;
+import org.apache.james.transport.mailets.redirect.AddressExtractor;
 import org.apache.james.transport.mailets.redirect.InitParameters;
 import org.apache.james.transport.mailets.redirect.RedirectMailetInitParameters;
 import org.apache.james.transport.mailets.redirect.TypeCode;

http://git-wip-us.apache.org/repos/asf/james-project/blob/875c39ba/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 24efdfe..dcdce85 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
@@ -42,6 +42,7 @@ import org.apache.james.core.MailImpl;
 import org.apache.james.core.MimeMessageUtil;
 import org.apache.james.dnsservice.api.DNSService;
 import org.apache.james.transport.mailets.Redirect;
+import org.apache.james.transport.mailets.utils.MimeMessageModifier;
 import org.apache.mailet.Mail;
 import org.apache.mailet.MailAddress;
 import org.apache.mailet.base.DateFormats;
@@ -733,12 +734,7 @@ public abstract class AbstractRedirect extends GenericMailet {
      */
     protected void setSubjectPrefix(Mail newMail, String subjectPrefix, Mail originalMail)
throws MessagingException {
         if (isNotifyMailet()) {
-            String subject = Strings.nullToEmpty(originalMail.getMessage().getSubject());
-            if (subjectPrefix == null || !subject.contains(subjectPrefix)) {
-                newMail.getMessage().setSubject(subject);
-            } else {
-                newMail.getMessage().setSubject(subjectPrefix + subject);
-            }
+            new MimeMessageModifier(originalMail.getMessage()).addSubjectPrefix(subjectPrefix);
         }
 
         String subject = getSubject(originalMail);


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