james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rouaz...@apache.org
Subject [17/50] [abbrv] james-project git commit: MAILET-115 Remove abstract getReversePath implementation
Date Wed, 11 Jan 2017 09:26:05 GMT
MAILET-115 Remove abstract getReversePath implementation


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

Branch: refs/heads/master
Commit: 95f1b56b2ab5f51cba8c9ff97378bf9647e87198
Parents: 6136d99
Author: Antoine Duprat <aduprat@apache.org>
Authored: Mon Nov 14 16:15:57 2016 +0100
Committer: Benoit Tellier <btellier@linagora.com>
Committed: Wed Jan 11 10:03:29 2017 +0700

----------------------------------------------------------------------
 .../apache/james/transport/mailets/Bounce.java  |   9 +-
 .../james/transport/mailets/DSNBounce.java      |   8 +-
 .../apache/james/transport/mailets/Forward.java |   2 +-
 .../transport/mailets/NotifyPostmaster.java     |   8 +-
 .../james/transport/mailets/NotifySender.java   |   8 +-
 .../james/transport/mailets/Redirect.java       |   2 +-
 .../apache/james/transport/mailets/Resend.java  |   8 +-
 .../mailets/redirect/AbstractRedirect.java      |  17 +--
 .../james/transport/util/ReversePathUtils.java  |  57 ++++++++
 .../transport/util/ReversePathUtilsTest.java    | 129 +++++++++++++++++++
 10 files changed, 225 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/95f1b56b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Bounce.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Bounce.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Bounce.java
index a161d08..e922d0e 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Bounce.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Bounce.java
@@ -30,6 +30,7 @@ import org.apache.james.transport.mailets.redirect.NotifyMailetInitParameters;
 import org.apache.james.transport.mailets.redirect.NotifyMailetsMessage;
 import org.apache.james.transport.mailets.redirect.SpecialAddress;
 import org.apache.james.transport.mailets.utils.MimeMessageModifier;
+import org.apache.james.transport.util.ReversePathUtils;
 import org.apache.mailet.Mail;
 import org.apache.mailet.MailAddress;
 
@@ -122,7 +123,7 @@ public class Bounce extends AbstractRedirect {
     }
 
     @Override
-    protected InitParameters getInitParameters() {
+    public InitParameters getInitParameters() {
         return NotifyMailetInitParameters.from(this);
     }
 
@@ -157,6 +158,11 @@ public class Bounce extends AbstractRedirect {
     }
 
     @Override
+    protected MailAddress getReversePath() throws MessagingException {
+        return ReversePathUtils.from(this).getReversePath();
+    }
+
+    @Override
     protected void setSubjectPrefix(Mail newMail, String subjectPrefix, Mail originalMail)
throws MessagingException {
         new MimeMessageModifier(originalMail.getMessage()).addSubjectPrefix(subjectPrefix);
     }
@@ -181,5 +187,4 @@ public class Bounce extends AbstractRedirect {
             originalMail.setState(Mail.GHOST);
         }
     }
-
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/95f1b56b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/DSNBounce.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/DSNBounce.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/DSNBounce.java
index ccdc43b..b9f10f5 100755
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/DSNBounce.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/DSNBounce.java
@@ -40,6 +40,7 @@ import org.apache.james.transport.mailets.redirect.SpecialAddress;
 import org.apache.james.transport.mailets.redirect.TypeCode;
 import org.apache.james.transport.mailets.utils.MimeMessageModifier;
 import org.apache.james.transport.util.Patterns;
+import org.apache.james.transport.util.ReversePathUtils;
 import org.apache.mailet.Mail;
 import org.apache.mailet.MailAddress;
 import org.apache.mailet.base.DateFormats;
@@ -114,7 +115,7 @@ public class DSNBounce extends AbstractRedirect {
     }
 
     @Override
-    protected InitParameters getInitParameters() {
+    public InitParameters getInitParameters() {
         return NotifyMailetInitParameters.from(this);
     }
 
@@ -144,6 +145,11 @@ public class DSNBounce extends AbstractRedirect {
     }
 
     @Override
+    protected MailAddress getReversePath() throws MessagingException {
+        return ReversePathUtils.from(this).getReversePath();
+    }
+
+    @Override
     protected MailAddress getReversePath(Mail originalMail) {
         return SpecialAddress.NULL;
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/95f1b56b/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 b61fa4e..a3fd04d 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
@@ -95,7 +95,7 @@ public class Forward extends AbstractRedirect {
     }
 
     @Override
-    protected InitParameters getInitParameters() {
+    public InitParameters getInitParameters() {
         return RedirectMailetInitParameters.from(this, Optional.of(TypeCode.NONE), Optional.<TypeCode>
absent());
     }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/95f1b56b/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 f00b6ac..fdc0fe8 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
@@ -31,6 +31,7 @@ import org.apache.james.transport.mailets.redirect.NotifyMailetInitParameters;
 import org.apache.james.transport.mailets.redirect.NotifyMailetsMessage;
 import org.apache.james.transport.mailets.redirect.SpecialAddress;
 import org.apache.james.transport.mailets.utils.MimeMessageModifier;
+import org.apache.james.transport.util.ReversePathUtils;
 import org.apache.mailet.Mail;
 import org.apache.mailet.MailAddress;
 import org.apache.mailet.MailetConfig;
@@ -130,7 +131,7 @@ public class NotifyPostmaster extends AbstractRedirect {
     }
 
     @Override
-    protected InitParameters getInitParameters() {
+    public InitParameters getInitParameters() {
         return NotifyMailetInitParameters.from(this);
     }
 
@@ -169,6 +170,11 @@ public class NotifyPostmaster extends AbstractRedirect {
     }
 
     @Override
+    protected MailAddress getReversePath() throws MessagingException {
+        return ReversePathUtils.from(this).getReversePath();
+    }
+
+    @Override
     protected MailAddress getReversePath(Mail originalMail) throws MessagingException {
         return getSender(originalMail);
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/95f1b56b/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 421475c..e54d5ba 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
@@ -31,6 +31,7 @@ import org.apache.james.transport.mailets.redirect.NotifyMailetInitParameters;
 import org.apache.james.transport.mailets.redirect.NotifyMailetsMessage;
 import org.apache.james.transport.mailets.redirect.SpecialAddress;
 import org.apache.james.transport.mailets.utils.MimeMessageModifier;
+import org.apache.james.transport.util.ReversePathUtils;
 import org.apache.mailet.Mail;
 import org.apache.mailet.MailAddress;
 import org.apache.mailet.MailetConfig;
@@ -130,7 +131,7 @@ public class NotifySender extends AbstractRedirect {
     }
 
     @Override
-    protected InitParameters getInitParameters() {
+    public InitParameters getInitParameters() {
         return NotifyMailetInitParameters.from(this);
     }
 
@@ -169,6 +170,11 @@ public class NotifySender extends AbstractRedirect {
     }
 
     @Override
+    protected MailAddress getReversePath() throws MessagingException {
+        return ReversePathUtils.from(this).getReversePath();
+    }
+
+    @Override
     protected MailAddress getReversePath(Mail originalMail) throws MessagingException {
         return getSender(originalMail);
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/95f1b56b/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 fc53c6b..d64b96c 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
@@ -302,7 +302,7 @@ public class Redirect extends AbstractRedirect {
     }
 
     @Override
-    protected InitParameters getInitParameters() {
+    public InitParameters getInitParameters() {
         return RedirectMailetInitParameters.from(this, Optional.<TypeCode> absent(),
Optional.of(TypeCode.BODY));
     }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/95f1b56b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Resend.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Resend.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Resend.java
index 2a0d735..64c3bda 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Resend.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Resend.java
@@ -30,6 +30,7 @@ import org.apache.james.transport.mailets.redirect.InitParameters;
 import org.apache.james.transport.mailets.redirect.RedirectMailetInitParameters;
 import org.apache.james.transport.mailets.utils.MimeMessageModifier;
 import org.apache.james.transport.util.MailAddressUtils;
+import org.apache.james.transport.util.ReversePathUtils;
 import org.apache.mailet.Mail;
 import org.apache.mailet.MailAddress;
 
@@ -294,7 +295,7 @@ public class Resend extends AbstractRedirect {
     }
 
     @Override
-    protected InitParameters getInitParameters() {
+    public InitParameters getInitParameters() {
         return RedirectMailetInitParameters.from(this);
     }
 
@@ -345,6 +346,11 @@ public class Resend extends AbstractRedirect {
       }
 
     @Override
+    protected MailAddress getReversePath() throws MessagingException {
+        return ReversePathUtils.from(this).getReversePath();
+    }
+
+    @Override
     protected MailAddress getReversePath(Mail originalMail) throws MessagingException {
         MailAddress reversePath = getReversePath();
         if (reversePath != null) {

http://git-wip-us.apache.org/repos/asf/james-project/blob/95f1b56b/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 ae7f5db..3aaa405 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
@@ -154,7 +154,7 @@ public abstract class AbstractRedirect extends GenericMailet {
 
     private static final char LINE_BREAK = '\n';
 
-    protected abstract InitParameters getInitParameters();
+    public abstract InitParameters getInitParameters();
 
     protected abstract String[] getAllowedInitParameters();
 
@@ -342,20 +342,7 @@ public abstract class AbstractRedirect extends GenericMailet {
      *         <code>SpecialAddress.UNALTERED</code> or <code>null</code>
if
      *         missing
      */
-    protected MailAddress getReversePath() throws MessagingException {
-        String reversePath = getInitParameters().getReversePath();
-        if (Strings.isNullOrEmpty(reversePath)) {
-            return null;
-        }
-
-        List<MailAddress> extractAddresses = AddressExtractor.withContext(getMailetContext())
-                .allowedSpecials(ImmutableList.of("postmaster", "sender", "null", "unaltered"))
-                .extract(reversePath);
-        if (extractAddresses.isEmpty()) {
-            return null;
-        }
-        return extractAddresses.get(0);
-    }
+    protected abstract MailAddress getReversePath() throws MessagingException; 
 
     /**
      * Gets the <code>reversePath</code> property, built dynamically using the

http://git-wip-us.apache.org/repos/asf/james-project/blob/95f1b56b/server/mailet/mailets/src/main/java/org/apache/james/transport/util/ReversePathUtils.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/util/ReversePathUtils.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/util/ReversePathUtils.java
new file mode 100644
index 0000000..651f8e1
--- /dev/null
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/util/ReversePathUtils.java
@@ -0,0 +1,57 @@
+/****************************************************************
+ * 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.util;
+
+import java.util.List;
+
+import javax.mail.MessagingException;
+
+import org.apache.james.transport.mailets.redirect.AbstractRedirect;
+import org.apache.james.transport.mailets.redirect.AddressExtractor;
+import org.apache.mailet.MailAddress;
+
+import com.google.common.base.Strings;
+import com.google.common.collect.FluentIterable;
+import com.google.common.collect.ImmutableList;
+
+public class ReversePathUtils {
+
+    public static ReversePathUtils from(AbstractRedirect mailet) {
+        return new ReversePathUtils(mailet);
+    }
+
+    private final AbstractRedirect mailet;
+
+    private ReversePathUtils(AbstractRedirect mailet) {
+        this.mailet = mailet;
+    }
+
+    public MailAddress getReversePath() throws MessagingException {
+        String reversePath = mailet.getInitParameters().getReversePath();
+        if (Strings.isNullOrEmpty(reversePath)) {
+            return null;
+        }
+
+        List<MailAddress> extractAddresses = AddressExtractor
+                .withContext(mailet.getMailetContext())
+                .allowedSpecials(ImmutableList.of("postmaster", "sender", "null", "unaltered"))
+                .extract(reversePath);
+        return FluentIterable.from(extractAddresses).first().orNull();
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/95f1b56b/server/mailet/mailets/src/test/java/org/apache/james/transport/util/ReversePathUtilsTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/util/ReversePathUtilsTest.java
b/server/mailet/mailets/src/test/java/org/apache/james/transport/util/ReversePathUtilsTest.java
new file mode 100644
index 0000000..4ceff65
--- /dev/null
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/util/ReversePathUtilsTest.java
@@ -0,0 +1,129 @@
+/****************************************************************
+ * 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.util;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import org.apache.james.transport.mailets.redirect.AbstractRedirect;
+import org.apache.james.transport.mailets.redirect.InitParameters;
+import org.apache.james.transport.mailets.redirect.SpecialAddress;
+import org.apache.mailet.MailAddress;
+import org.apache.mailet.MailetContext;
+import org.junit.Before;
+import org.junit.Test;
+
+public class ReversePathUtilsTest {
+
+    private InitParameters initParameters;
+    private MailAddress postmaster;
+    private ReversePathUtils testee;
+
+    @Before
+    public void setup() throws Exception {
+        AbstractRedirect mailet = mock(AbstractRedirect.class);
+        initParameters = mock(InitParameters.class);
+        when(mailet.getInitParameters())
+            .thenReturn(initParameters);
+
+        MailetContext mailetContext = mock(MailetContext.class);
+        postmaster = new MailAddress("postmaster@james.org");
+        when(mailetContext.getPostmaster())
+            .thenReturn(postmaster);
+        when(mailet.getMailetContext())
+            .thenReturn(mailetContext);
+
+        testee = ReversePathUtils.from(mailet);
+    }
+
+    @Test
+    public void getReversePathShouldReturnNullWhenReversePathInitParameterIsNull() throws
Exception {
+        when(initParameters.getReversePath())
+            .thenReturn(null);
+
+        MailAddress reversePath = testee.getReversePath();
+
+        assertThat(reversePath).isNull();
+    }
+
+    @Test
+    public void getReversePathShouldReturnNullWhenReversePathInitParameterIsEmpty() throws
Exception {
+        when(initParameters.getReversePath())
+            .thenReturn("");
+
+        MailAddress reversePath = testee.getReversePath();
+
+        assertThat(reversePath).isNull();
+    }
+
+    @Test
+    public void getReversePathShouldReturnFirstMailAddressWhenMultipleAddresses() throws
Exception {
+        when(initParameters.getReversePath())
+            .thenReturn("test@james.org, test2@james.org");
+
+        MailAddress reversePath = testee.getReversePath();
+
+        MailAddress expectedMailAddress = new MailAddress("test", "james.org");
+        assertThat(reversePath).isEqualTo(expectedMailAddress);
+    }
+
+    @Test
+    public void getReversePathShouldReturnPostmasterSpecialAddressWhenFirstAddressIsSpecialPostmaster()
throws Exception {
+        when(initParameters.getReversePath())
+            .thenReturn("postmaster, test2@james.org");
+
+        MailAddress reversePath = testee.getReversePath();
+
+        assertThat(reversePath).isEqualTo(postmaster);
+    }
+
+    @Test
+    public void getReversePathShouldReturnSenderSpecialAddressWhenFirstAddressIsSpecialSender()
throws Exception {
+        when(initParameters.getReversePath())
+            .thenReturn("sender, test2@james.org");
+
+        MailAddress reversePath = testee.getReversePath();
+
+        MailAddress expectedMailAddress = SpecialAddress.SENDER;
+        assertThat(reversePath).isEqualTo(expectedMailAddress);
+    }
+
+    @Test
+    public void getReversePathShouldReturnNullSpecialAddressWhenFirstAddressIsSpecialNull()
throws Exception {
+        when(initParameters.getReversePath())
+            .thenReturn("null, test2@james.org");
+
+        MailAddress reversePath = testee.getReversePath();
+
+        MailAddress expectedMailAddress = SpecialAddress.NULL;
+        assertThat(reversePath).isEqualTo(expectedMailAddress);
+    }
+
+    @Test
+    public void getReversePathShouldReturnUnalteredSpecialAddressWhenFirstAddressIsSpecialUnaltered()
throws Exception {
+        when(initParameters.getReversePath())
+            .thenReturn("unaltered, test2@james.org");
+
+        MailAddress reversePath = testee.getReversePath();
+
+        MailAddress expectedMailAddress = SpecialAddress.UNALTERED;
+        assertThat(reversePath).isEqualTo(expectedMailAddress);
+    }
+}


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