james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [james-project] 01/23: JAMES-2865 Decoralate SMTP behaviour from each other
Date Fri, 06 Sep 2019 02:01:52 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 20972ed12bd17c50832b29b07382e600d381a1c3
Author: Benoit Tellier <btellier@linagora.com>
AuthorDate: Fri Aug 30 13:13:31 2019 +0700

    JAMES-2865 Decoralate SMTP behaviour from each other
    
    Each SMTP behaviour needs knowledge about other ones, which can be
    abstracted away. Leading to cleaner code, and use of only static classes.
---
 .../james/mock/smtp/server/MockMessageHandler.java | 39 +++++++++++++++-------
 1 file changed, 27 insertions(+), 12 deletions(-)

diff --git a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/MockMessageHandler.java
b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/MockMessageHandler.java
index 70b01ee..668d37f 100644
--- a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/MockMessageHandler.java
+++ b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/MockMessageHandler.java
@@ -42,12 +42,11 @@ import org.subethamail.smtp.TooMuchDataException;
 public class MockMessageHandler implements MessageHandler {
 
     @FunctionalInterface
-    interface Behavior<T> {
+    private interface Behavior<T> {
         void behave(T input) throws RejectException;
     }
 
-    class MockBehavior<T> implements Behavior<T> {
-
+    private static class MockBehavior<T> implements Behavior<T> {
         private final MockSMTPBehavior behavior;
 
         MockBehavior(MockSMTPBehavior behavior) {
@@ -64,22 +63,37 @@ public class MockMessageHandler implements MessageHandler {
         }
     }
 
-    class SMTPBehaviorRepositoryUpdater<T> implements Behavior<T> {
+    private static class ComposedBehavior<T> implements Behavior<T> {
+        private static class Builder<U> {
+            private final Behavior<U> behavior1;
+
+            private Builder(Behavior<U> behavior1) {
+                this.behavior1 = behavior1;
+            }
+
+            ComposedBehavior<U> andThen(Behavior<U> behavior2) {
+                return new ComposedBehavior<>(behavior1, behavior2);
+            }
+        }
+
+        private static <V> Builder<V> startWith(Behavior<V> behavior1)
{
+            return new Builder<>(behavior1);
+        }
 
-        private final SMTPBehaviorRepository behaviorRepository;
-        private final MockBehavior<T> actualBehavior;
+        private final Behavior<T> behavior1;
+        private final Behavior<T> behavior2;
 
-        SMTPBehaviorRepositoryUpdater(SMTPBehaviorRepository behaviorRepository, MockSMTPBehavior
behavior) {
-            this.behaviorRepository = behaviorRepository;
-            this.actualBehavior = new MockBehavior<>(behavior);
+        private ComposedBehavior(Behavior<T> behavior1, Behavior<T> behavior2)
{
+            this.behavior1 = behavior1;
+            this.behavior2 = behavior2;
         }
 
         @Override
         public void behave(T input) throws RejectException {
             try {
-                actualBehavior.behave(input);
+                behavior1.behave(input);
             } finally {
-                behaviorRepository.decreaseRemainingAnswers(actualBehavior.behavior);
+                behavior2.behave(input);
             }
         }
     }
@@ -130,7 +144,8 @@ public class MockMessageHandler implements MessageHandler {
             .filter(behavior -> behavior.getCommand().equals(data))
             .filter(behavior -> behavior.getCondition().matches(dataLine))
             .findFirst()
-            .map(mockBehavior -> new SMTPBehaviorRepositoryUpdater<>(behaviorRepository,
mockBehavior));
+            .map(behavior -> ComposedBehavior.<T>startWith(new MockBehavior<>(behavior))
+                .andThen(any -> behaviorRepository.decreaseRemainingAnswers(behavior)));
     }
 
     @Override


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