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 87e41356b548d429faf67185998c9bff4093abbd
Author: Benoit Tellier <[email protected]>
AuthorDate: Fri Jun 21 11:01:04 2019 +0700

    JAMES-2294 Upon unknown processor, default to Mail.ERROR
    
    This allows the admin to be aware of the issue and in-fine fix it. Note
    that the state of the mail needs to be overwritten...
    
    This behaviour prevents an infinite loop that is otherwise triggered upon
    reprocessing to an unknown processor - a typo/missconfiguration that could
    easily happen!
---
 .../mailets/MailReprocessingIntegrationTest.java   |  2 -
 .../lib/AbstractStateCompositeProcessor.java       | 63 ++++++++++++----------
 .../lib/AbstractStateCompositeProcessorTest.java   |  2 -
 3 files changed, 36 insertions(+), 31 deletions(-)

diff --git 
a/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/MailReprocessingIntegrationTest.java
 
b/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/MailReprocessingIntegrationTest.java
index f8de0d5..002f458 100644
--- 
a/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/MailReprocessingIntegrationTest.java
+++ 
b/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/MailReprocessingIntegrationTest.java
@@ -54,7 +54,6 @@ import 
org.apache.james.webadmin.authentication.NoAuthenticationFilter;
 import org.apache.mailet.base.test.FakeMail;
 import org.junit.After;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
@@ -190,7 +189,6 @@ public class MailReprocessingIntegrationTest {
         assertThat(containsExactlyOneMail(REPOSITORY_A)).isTrue();
     }
 
-    @Ignore("JAMES-2994 Reprocessing to an unknown processor triggers an 
infinite loop as the mail is nack")
     @Test
     public void reprocessingShouldProcessAsErrorWhenUnknownMailProcessor() 
throws Exception {
         // Given an incoming email
diff --git 
a/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/lib/AbstractStateCompositeProcessor.java
 
b/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/lib/AbstractStateCompositeProcessor.java
index 8c28422..0985721 100644
--- 
a/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/lib/AbstractStateCompositeProcessor.java
+++ 
b/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/lib/AbstractStateCompositeProcessor.java
@@ -23,6 +23,7 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 
 import javax.annotation.PostConstruct;
 import javax.annotation.PreDestroy;
@@ -66,7 +67,7 @@ public abstract class AbstractStateCompositeProcessor 
implements MailProcessor,
     }
 
     @Override
-    public void configure(HierarchicalConfiguration config) throws 
ConfigurationException {
+    public void configure(HierarchicalConfiguration config) {
         this.config = config;
         this.enableJmx = config.getBoolean("[@enableJmx]", true);
 
@@ -74,34 +75,42 @@ public abstract class AbstractStateCompositeProcessor 
implements MailProcessor,
 
     @Override
     public void service(Mail mail) throws MessagingException {
-        long start = System.currentTimeMillis();
+        handleWithProcessor(mail, getProcessorOrFallBackToError(mail));
+    }
+
+    private MailProcessor getProcessorOrFallBackToError(Mail mail) {
+        return Optional.ofNullable(getProcessor(mail.getState()))
+            .orElseGet(() -> {
+                mail.setErrorMessage("MailProcessor '" + mail.getState() + "' 
could not be found. Processing to 'error' instead");
+                LOGGER.error("MailProcessor '{}' could not be found. 
Processing {} to 'error' instead", mail.getState(), mail.getName());
+                mail.setState(Mail.ERROR);
+                return getProcessor(Mail.ERROR);
+            });
+    }
+
+    private void handleWithProcessor(Mail mail, MailProcessor processor) 
throws MessagingException {
         MessagingException ex = null;
-        MailProcessor processor = getProcessor(mail.getState());
-
-        if (processor != null) {
-            LOGGER.debug("Call MailProcessor {}", mail.getState());
-            try {
-                processor.service(mail);
-
-                if (Mail.GHOST.equals(mail.getState())) {
-                    LifecycleUtil.dispose(mail);
-                }
-                /*
-                 * // check the mail needs further processing if
-                 * (Mail.GHOST.equalsIgnoreCase(mail.getState()) == false) {
-                 * service(mail); } else { LifecycleUtil.dispose(mail); }
-                 */
-            } catch (MessagingException e) {
-                ex = e;
-                throw e;
-            } finally {
-                long end = System.currentTimeMillis() - start;
-                for (CompositeProcessorListener listener : listeners) {
-                    listener.afterProcessor(processor, mail.getName(), end, 
ex);
-                }
+        long start = System.currentTimeMillis();
+        LOGGER.debug("Call MailProcessor {}", mail.getState());
+        try {
+            processor.service(mail);
+
+            if (Mail.GHOST.equals(mail.getState())) {
+                LifecycleUtil.dispose(mail);
+            }
+            /*
+             * // check the mail needs further processing if
+             * (Mail.GHOST.equalsIgnoreCase(mail.getState()) == false) {
+             * service(mail); } else { LifecycleUtil.dispose(mail); }
+             */
+        } catch (MessagingException e) {
+            ex = e;
+            throw e;
+        } finally {
+            long end = System.currentTimeMillis() - start;
+            for (CompositeProcessorListener listener : listeners) {
+                listener.afterProcessor(processor, mail.getName(), end, ex);
             }
-        } else {
-            throw new MessagingException("No processor found for mail " + 
mail.getName() + " with state " + mail.getState());
         }
     }
 
diff --git 
a/server/mailet/mailetcontainer-camel/src/test/java/org/apache/james/mailetcontainer/lib/AbstractStateCompositeProcessorTest.java
 
b/server/mailet/mailetcontainer-camel/src/test/java/org/apache/james/mailetcontainer/lib/AbstractStateCompositeProcessorTest.java
index 781d84b..8b585fd 100644
--- 
a/server/mailet/mailetcontainer-camel/src/test/java/org/apache/james/mailetcontainer/lib/AbstractStateCompositeProcessorTest.java
+++ 
b/server/mailet/mailetcontainer-camel/src/test/java/org/apache/james/mailetcontainer/lib/AbstractStateCompositeProcessorTest.java
@@ -74,8 +74,6 @@ public abstract class AbstractStateCompositeProcessorTest {
             processor.service(mail2);
             processor.service(mail3);
 
-            expectedException.expect(MessagingException.class);
-
             processor.service(mail4);
         } finally {
             processor.dispose();


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to