james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [2/2] james-project git commit: JSIEVE-108 Rely on ActionDiscard instead of a hack
Date Wed, 07 Jun 2017 02:56:56 GMT
JSIEVE-108 Rely on ActionDiscard instead of a hack


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

Branch: refs/heads/master
Commit: 3a005e074ef93abbdbe48d21d160ef74f388dc84
Parents: ffd836d
Author: benwa <btellier@linagora.com>
Authored: Sat May 6 17:36:40 2017 +0700
Committer: benwa <btellier@linagora.com>
Committed: Wed Jun 7 09:50:36 2017 +0700

----------------------------------------------------------------------
 .../apache/james/transport/mailets/Sieve.java   | 50 +-----------------
 .../mailets/jsieve/ActionDispatcher.java        |  2 +
 .../transport/mailets/jsieve/DiscardAction.java | 54 ++++++++++++++++++++
 .../mailets/jsieve/RedirectAction.java          |  1 +
 .../mailets/delivery/SieveIntegrationTest.java  |  1 -
 5 files changed, 58 insertions(+), 50 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/3a005e07/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Sieve.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Sieve.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Sieve.java
index b17eea8..21225b0 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Sieve.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Sieve.java
@@ -84,56 +84,8 @@ public class Sieve extends GenericMailet {
 
     @Override
     public void service(Mail mail) throws MessagingException {
-        List<MailAddress> recipientsWithSuccessfulSieveExecution = executeRetrieveSuccess(mail);
-        mail.setRecipients(keepNonDiscardedRecipients(mail, recipientsWithSuccessfulSieveExecution));
-    }
-
-    private List<MailAddress> executeRetrieveSuccess(Mail mail) throws MessagingException
{
-        ImmutableList.Builder<MailAddress> recipientsWithSuccessfulSieveExecution =
ImmutableList.builder();
         for(MailAddress recipient: mail.getRecipients()) {
-            if (sieveExecutor.execute(recipient, mail)) {
-                recipientsWithSuccessfulSieveExecution.add(recipient);
-            }
-        }
-        return recipientsWithSuccessfulSieveExecution.build();
-    }
-
-    private ImmutableList<MailAddress> keepNonDiscardedRecipients(Mail mail, final
List<MailAddress> recipientsWithSuccessfulSieveExecution) {
-        final List<MailAddress> discardedRecipients = retrieveDiscardedRecipients(mail);
-        return FluentIterable.from(mail.getRecipients())
-            .filter(discardPredicate(discardedRecipients, recipientsWithSuccessfulSieveExecution))
-            .toList();
-    }
-
-    private Predicate<MailAddress> discardPredicate(final List<MailAddress> discardedAddressList,
final List<MailAddress> discardeableAddressList) {
-        return new Predicate<MailAddress>() {
-            @Override
-            public boolean apply(MailAddress input) {
-                return !discardeableAddressList.contains(input) || !discardedAddressList.contains(input);
-            }
-        };
-    }
-
-    private List<MailAddress> retrieveDiscardedRecipients(Mail mail) {
-        final List<MailAddress> discardedRecipients = new ArrayList<MailAddress>();
-        for(MailAddress recipient: mail.getRecipients()) {
-            if (isDiscarded(mail, recipient)) {
-                discardedRecipients.add(recipient);
-            }
-        }
-        return discardedRecipients;
-    }
-
-    private boolean isDiscarded(Mail mail, MailAddress recipient) {
-        return !(mail.getAttribute(MailStore.DELIVERY_PATH_PREFIX + retrieveUser(recipient))
instanceof String);
-    }
-
-    private String retrieveUser(MailAddress recipient) {
-        try {
-            return usersRepository.getUser(recipient);
-        } catch (UsersRepositoryException e) {
-            log("Can not retrieve username for mail address " + recipient.asPrettyString(),
e);
-            return recipient.asString();
+            sieveExecutor.execute(recipient, mail);
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/3a005e07/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/jsieve/ActionDispatcher.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/jsieve/ActionDispatcher.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/jsieve/ActionDispatcher.java
index f6e157c..a270f36 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/jsieve/ActionDispatcher.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/jsieve/ActionDispatcher.java
@@ -20,6 +20,7 @@
 package org.apache.james.transport.mailets.jsieve;
 
 import org.apache.jsieve.mail.Action;
+import org.apache.jsieve.mail.ActionDiscard;
 import org.apache.jsieve.mail.ActionFileInto;
 import org.apache.jsieve.mail.ActionKeep;
 import org.apache.jsieve.mail.ActionRedirect;
@@ -89,6 +90,7 @@ public class ActionDispatcher {
         actionMap.put(ActionRedirect.class, new RedirectAction());
         actionMap.put(ActionReject.class, new RejectAction());
         actionMap.put(ActionVacation.class, new VacationAction());
+        actionMap.put(ActionDiscard.class, new DiscardAction());
         return actionMap;
     }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/3a005e07/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/jsieve/DiscardAction.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/jsieve/DiscardAction.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/jsieve/DiscardAction.java
new file mode 100644
index 0000000..9964780
--- /dev/null
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/jsieve/DiscardAction.java
@@ -0,0 +1,54 @@
+/****************************************************************
+ * 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.jsieve;
+
+import javax.mail.MessagingException;
+
+import org.apache.jsieve.mail.Action;
+import org.apache.jsieve.mail.ActionDiscard;
+import org.apache.mailet.Mail;
+import org.apache.mailet.MailAddress;
+
+import com.google.common.base.Predicate;
+import com.google.common.collect.FluentIterable;
+
+public class DiscardAction extends FileIntoAction implements MailAction {
+
+    public void execute(Action action, Mail mail, final ActionContext context)
+            throws MessagingException {
+        if (action instanceof ActionDiscard) {
+            removeRecipient(mail, context);
+        }
+    }
+
+    public static void removeRecipient(Mail mail, ActionContext context) {
+        mail.setRecipients(FluentIterable.from(mail.getRecipients())
+            .filter(isNot(context.getRecipient()))
+            .toList());
+    }
+
+    private static Predicate<MailAddress> isNot(final MailAddress recipient) {
+        return new Predicate<MailAddress>() {
+            @Override
+            public boolean apply(MailAddress input) {
+                return !input.equals(recipient);
+            }
+        };
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/3a005e07/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/jsieve/RedirectAction.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/jsieve/RedirectAction.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/jsieve/RedirectAction.java
index 4096e34..e8931af 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/jsieve/RedirectAction.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/jsieve/RedirectAction.java
@@ -67,5 +67,6 @@ public class RedirectAction implements MailAction {
                 + aMail.getMessage().getMessageID() + " to \""
                 + anAction.getAddress() + "\"");
         }
+        DiscardAction.removeRecipient(aMail, context);
     }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/3a005e07/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/SieveIntegrationTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/SieveIntegrationTest.java
b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/SieveIntegrationTest.java
index 3d26130..f333e20 100644
--- a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/SieveIntegrationTest.java
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/SieveIntegrationTest.java
@@ -130,7 +130,6 @@ public class SieveIntegrationTest {
         FakeMail mail = createMail();
         testee.service(mail);
 
-
         assertThat(mail.getRecipients()).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