james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [james-project] 07/07: JAMES-2279 Write a WithPriority mailet and HasPriority matchers
Date Fri, 07 Jun 2019 10:59:15 GMT
This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit f9fe42c18456c31810d2827b7a3270762af40135
Author: ilker yıldırım <ilker.ceng@gmail.com>
AuthorDate: Sun Feb 3 03:22:45 2019 +0300

    JAMES-2279 Write a WithPriority mailet and HasPriority matchers
    
    Many thanks to ilker yıldırım for this nice contribution!
---
 server/app/src/main/resources/mailetcontainer.xml  | 11 ++-
 .../james/transport/mailets/WithPriority.java      | 16 ++--
 .../AbstractPriorityMatcher.java}                  | 65 ++++++++--------
 .../james/transport/matchers/AtLeastPriority.java  | 31 ++++++++
 .../james/transport/matchers/AtMostPriority.java   | 31 ++++++++
 .../james/transport/matchers/HasPriority.java      | 31 ++++++++
 .../transport/matchers/AtLeastPriorityTest.java    | 86 ++++++++++++++++++++++
 .../transport/matchers/AtMostPriorityTest.java     | 85 +++++++++++++++++++++
 .../james/transport/matchers/HasPriorityTest.java  | 77 +++++++++++++++++++
 9 files changed, 393 insertions(+), 40 deletions(-)

diff --git a/server/app/src/main/resources/mailetcontainer.xml b/server/app/src/main/resources/mailetcontainer.xml
index ae84957..b193bfc 100644
--- a/server/app/src/main/resources/mailetcontainer.xml
+++ b/server/app/src/main/resources/mailetcontainer.xml
@@ -56,7 +56,16 @@
        <!-- Important check to avoid looping -->
        <mailet match="RelayLimit=30" class="Null"/>
 
-       <!-- Check attachment extensions for possible viruses -->
+        <mailet matcher="All" class="WithPriority">
+            <value>8</value>
+        </mailet>
+
+        <mailet matcher="HasPriority=8" class="Null"/>
+        <mailet matcher="AtLeastPriority=8" class="Null"/>
+        <mailet matcher="AtMostPriority=8" class="Null"/>
+
+
+        <!-- Check attachment extensions for possible viruses -->
        <!-- The "-z" option requests the check to be non-recursively applied -->
        <!-- to the contents of any attached '*.zip' file. -->
        <!-- 
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/WithPriority.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/WithPriority.java
index 3138f4b..3fc9776 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/WithPriority.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/WithPriority.java
@@ -50,17 +50,17 @@ public class WithPriority extends GenericMailet {
     @Override
     public void init() throws MessagingException {
         Integer priorityRaw = Optional.ofNullable(getInitParameter("priority", null))
-            .map(Integer::valueOf)
-            .orElseThrow(() -> new IllegalArgumentException("'priority' init parameter
is compulsory"));
+                .map(Integer::valueOf)
+                .orElseThrow(() -> new IllegalArgumentException("'priority' init parameter
is compulsory"));
 
         if (priorityRaw < 0 || priorityRaw > 9) {
             throw new IllegalArgumentException("Invalid priority: Priority should be from
0 to 9");
+            }
+            priority = new Attribute(MailPrioritySupport.MAIL_PRIORITY, AttributeValue.of(priorityRaw));
         }
-        priority = new Attribute(MailPrioritySupport.MAIL_PRIORITY, AttributeValue.of(priorityRaw));
-    }
 
-    @Override
-    public void service(Mail mail) throws MessagingException {
-        mail.setAttribute(priority);
+        @Override
+        public void service(Mail mail) throws MessagingException {
+            mail.setAttribute(priority);
+        }
     }
-}
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/WithPriority.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/AbstractPriorityMatcher.java
similarity index 51%
copy from server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/WithPriority.java
copy to server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/AbstractPriorityMatcher.java
index 3138f4b..18170fd 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/WithPriority.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/AbstractPriorityMatcher.java
@@ -16,51 +16,54 @@
  * specific language governing permissions and limitations      *
  * under the License.                                           *
  ****************************************************************/
+package org.apache.james.transport.matchers;
 
-package org.apache.james.transport.mailets;
-
-import java.util.Optional;
-
+import java.util.Collection;
 import javax.mail.MessagingException;
 
+import org.apache.james.core.MailAddress;
 import org.apache.james.queue.api.MailPrioritySupport;
-import org.apache.mailet.Attribute;
-import org.apache.mailet.AttributeValue;
+import org.apache.mailet.AttributeUtils;
 import org.apache.mailet.Mail;
-import org.apache.mailet.base.GenericMailet;
+import org.apache.mailet.base.GenericMatcher;
+import org.apache.mailet.base.MailetUtil;
 
-/**
- * This mailet sets the priority of the incoming mail.
- *
- * Example configuration:
- *
- * <mailet match="All" class="WithPriority">
- *     <priority>7</priority>
- * </mailet>
- */
-public class WithPriority extends GenericMailet {
+import com.google.common.collect.ImmutableList;
 
-    private Attribute priority;
 
-    @Override
-    public String getMailetInfo() {
-        return "With Priority Mailet";
+public abstract class AbstractPriorityMatcher extends GenericMatcher {
+    private final String priorityMatcherName;
+    private Integer priority;
+
+    public AbstractPriorityMatcher(String priorityMatcherName) {
+        this.priorityMatcherName = priorityMatcherName;
     }
 
     @Override
     public void init() throws MessagingException {
-        Integer priorityRaw = Optional.ofNullable(getInitParameter("priority", null))
-            .map(Integer::valueOf)
-            .orElseThrow(() -> new IllegalArgumentException("'priority' init parameter
is compulsory"));
-
-        if (priorityRaw < 0 || priorityRaw > 9) {
-            throw new IllegalArgumentException("Invalid priority: Priority should be from
0 to 9");
-        }
-        priority = new Attribute(MailPrioritySupport.MAIL_PRIORITY, AttributeValue.of(priorityRaw));
+        Integer priority = MailetUtil.getInitParameterAsStrictlyPositiveInteger(getCondition());
+        this.setPriority(priority);
     }
 
     @Override
-    public void service(Mail mail) throws MessagingException {
-        mail.setAttribute(priority);
+    public Collection<MailAddress> match(Mail mail) throws MessagingException {
+        return AttributeUtils.getValueAndCastFromMail(mail, MailPrioritySupport.MAIL_PRIORITY,
Integer.class)
+                .filter(this::priorityMatch)
+                .map(any -> mail.getRecipients())
+                .orElse(ImmutableList.of());
+    }
+
+    public abstract boolean priorityMatch(Integer mailPriorityValue);
+
+    public Integer getPriority() {
+        return priority;
+    }
+
+    public void setPriority(Integer priority) {
+        this.priority = priority;
+    }
+
+    public String getPriorityMatcherName() {
+        return priorityMatcherName;
     }
 }
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/AtLeastPriority.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/AtLeastPriority.java
new file mode 100644
index 0000000..0dc93be
--- /dev/null
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/AtLeastPriority.java
@@ -0,0 +1,31 @@
+/****************************************************************
+ * 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.matchers;
+
+public class AtLeastPriority extends AbstractPriorityMatcher {
+    public AtLeastPriority() {
+        super("AtLeastPriority");
+    }
+
+    @Override
+    public boolean priorityMatch(Integer mailPriorityValue) {
+        return this.getPriority() <= mailPriorityValue;
+    }
+
+}
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/AtMostPriority.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/AtMostPriority.java
new file mode 100644
index 0000000..0faf109
--- /dev/null
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/AtMostPriority.java
@@ -0,0 +1,31 @@
+/****************************************************************
+ * 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.matchers;
+
+public class AtMostPriority extends AbstractPriorityMatcher {
+    public AtMostPriority() {
+        super("AtMostPriority");
+    }
+
+    @Override
+    public boolean priorityMatch(Integer mailPriorityValue) {
+        return this.getPriority() >= mailPriorityValue;
+    }
+
+}
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/HasPriority.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/HasPriority.java
new file mode 100644
index 0000000..ec3197f
--- /dev/null
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/HasPriority.java
@@ -0,0 +1,31 @@
+/****************************************************************
+ * 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.matchers;
+
+public class HasPriority extends AbstractPriorityMatcher {
+    public HasPriority() {
+        super("HasPriority");
+    }
+
+    @Override
+    public boolean priorityMatch(Integer mailPriorityValue) {
+        return this.getPriority() == mailPriorityValue;
+    }
+
+}
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/AtLeastPriorityTest.java
b/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/AtLeastPriorityTest.java
new file mode 100644
index 0000000..0b3d916
--- /dev/null
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/AtLeastPriorityTest.java
@@ -0,0 +1,86 @@
+/****************************************************************
+ * 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.matchers;
+
+import java.util.Collection;
+import javax.mail.MessagingException;
+import static org.assertj.core.api.Assertions.assertThat;
+import org.junit.Before;
+import org.junit.Test;
+
+import org.apache.james.core.MailAddress;
+import org.apache.james.queue.api.MailPrioritySupport;
+import org.apache.mailet.Attribute;
+import org.apache.mailet.AttributeValue;
+import org.apache.mailet.base.test.FakeMail;
+import org.apache.mailet.base.test.FakeMatcherConfig;
+
+public class AtLeastPriorityTest {
+    protected AtLeastPriority matcher;
+    protected MailAddress testRecipient;
+    private final String condition = "5";
+
+    private FakeMail createFakeMail(Integer priority) throws MessagingException {
+        FakeMail fakeMail = FakeMail.builder().name("test-message")
+                .recipient(testRecipient)
+                .attribute(new Attribute(MailPrioritySupport.MAIL_PRIORITY, AttributeValue.of(priority)))
+                .build();
+        return fakeMail;
+    }
+
+    @Before
+    public void setup() throws MessagingException {
+        this.matcher = new AtLeastPriority();
+        FakeMatcherConfig matcherConfig = FakeMatcherConfig.builder()
+                .matcherName(matcher.getPriorityMatcherName())
+                .condition(condition)
+                .build();
+
+        matcher.init(matcherConfig);
+        testRecipient = new MailAddress("test@james.apache.org");
+    }
+
+    @Test
+    public void shouldNotMatchWhenPriorityDoesNotMatch() throws MessagingException {
+        FakeMail fakeMail = createFakeMail(3);
+
+        Collection<MailAddress> actual = matcher.match(fakeMail);
+
+        assertThat(actual).isEmpty();
+    }
+
+    @Test
+    public void shouldMatchWhenPriorityMatch() throws MessagingException {
+        FakeMail fakeMail = createFakeMail(5);
+
+        Collection<MailAddress> actual = matcher.match(fakeMail);
+
+        assertThat(actual).containsOnly(testRecipient);
+    }
+
+    @Test
+    public void shouldMatchWhenMailHasHigherPriority() throws MessagingException {
+        FakeMail fakeMail = createFakeMail(7);
+
+        Collection<MailAddress> actual = matcher.match(fakeMail);
+
+        assertThat(actual).containsOnly(testRecipient);
+    }
+
+}
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/AtMostPriorityTest.java
b/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/AtMostPriorityTest.java
new file mode 100644
index 0000000..cb9de9b
--- /dev/null
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/AtMostPriorityTest.java
@@ -0,0 +1,85 @@
+/****************************************************************
+ * 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.matchers;
+
+import java.util.Collection;
+import javax.mail.MessagingException;
+import org.apache.james.core.MailAddress;
+import org.apache.james.queue.api.MailPrioritySupport;
+import org.apache.mailet.Attribute;
+import org.apache.mailet.AttributeValue;
+import org.apache.mailet.base.test.FakeMail;
+import org.apache.mailet.base.test.FakeMatcherConfig;
+import static org.assertj.core.api.Assertions.assertThat;
+import org.junit.Before;
+import org.junit.Test;
+
+public class AtMostPriorityTest{
+    protected AtMostPriority matcher;
+    protected MailAddress testRecipient;
+    private final String condition = "5";
+
+    private FakeMail createFakeMail(Integer priority) throws MessagingException {
+        FakeMail fakeMail = FakeMail.builder().name("test-message")
+                .recipient(testRecipient)
+                .attribute(new Attribute(MailPrioritySupport.MAIL_PRIORITY, AttributeValue.of(priority)))
+                .build();
+        return fakeMail;
+    }
+
+    @Before
+    public void setup() throws MessagingException {
+        this.matcher = new AtMostPriority();
+        FakeMatcherConfig matcherConfig = FakeMatcherConfig.builder()
+                .matcherName(matcher.getPriorityMatcherName())
+                .condition(condition)
+                .build();
+
+        matcher.init(matcherConfig);
+        testRecipient = new MailAddress("test@james.apache.org");
+    }
+
+    @Test
+    public void shouldMatchWhenMailHasLowerPriority() throws MessagingException {
+        FakeMail fakeMail = this.createFakeMail(3);
+
+        Collection<MailAddress> actual = matcher.match(fakeMail);
+
+        assertThat(actual).containsOnly(testRecipient);
+    }
+
+    @Test
+    public void shouldMatchWhenPriorityMatch() throws MessagingException {
+        FakeMail fakeMail = this.createFakeMail(5);
+
+        Collection<MailAddress> actual = matcher.match(fakeMail);
+
+        assertThat(actual).containsOnly(testRecipient);
+    }
+
+    @Test
+    public void shouldNotMatchWhenPriorityDoesNotMatch() throws MessagingException {
+        FakeMail fakeMail = this.createFakeMail(7);
+
+        Collection<MailAddress> actual = matcher.match(fakeMail);
+
+        assertThat(actual).isEmpty();
+    }
+
+}
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/HasPriorityTest.java
b/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/HasPriorityTest.java
new file mode 100644
index 0000000..d521db3
--- /dev/null
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/HasPriorityTest.java
@@ -0,0 +1,77 @@
+/****************************************************************
+ * 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.matchers;
+
+import org.apache.james.core.MailAddress;
+import org.apache.james.queue.api.MailPrioritySupport;
+import org.apache.mailet.Attribute;
+import org.apache.mailet.AttributeValue;
+import org.apache.mailet.base.test.FakeMail;
+import org.apache.mailet.base.test.FakeMatcherConfig;
+
+import java.util.Collection;
+import javax.mail.MessagingException;
+import org.junit.Before;
+import org.junit.Test;
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class HasPriorityTest {
+    protected HasPriority matcher;
+    protected MailAddress testRecipient;
+    private final String condition = "5";
+
+    private FakeMail createFakeMail(Integer priority) throws MessagingException {
+        FakeMail fakeMail = FakeMail.builder().name("test-message")
+                .recipient(testRecipient)
+                .attribute(new Attribute(MailPrioritySupport.MAIL_PRIORITY, AttributeValue.of(priority)))
+                .build();
+        return fakeMail;
+    }
+
+    @Before
+    public void setup() throws MessagingException {
+        this.matcher = new HasPriority();
+        FakeMatcherConfig matcherConfig = FakeMatcherConfig.builder()
+                .matcherName(matcher.getPriorityMatcherName())
+                .condition(condition)
+                .build();
+
+        matcher.init(matcherConfig);
+        testRecipient = new MailAddress("test@james.apache.org");
+    }
+
+    @Test
+    public void shouldMatchWhenPriorityMatch() throws MessagingException {
+        FakeMail fakeMail = this.createFakeMail(5);
+
+        Collection<MailAddress> actual = matcher.match(fakeMail);
+
+        assertThat(actual).containsOnly(testRecipient);
+    }
+
+    @Test
+    public void shouldNotMatchWhenPriorityDoesNotMatch() throws MessagingException {
+        FakeMail fakeMail = this.createFakeMail(7);
+
+        Collection<MailAddress> actual = matcher.match(fakeMail);
+
+        assertThat(actual).isEmpty();
+    }
+
+}


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