You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by rc...@apache.org on 2020/12/30 03:35:19 UTC

[james-project] 07/29: JAMES-3431 MockSMTPServer: Capture RCPT TO ESMTP parameters

This is an automated email from the ASF dual-hosted git repository.

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

commit 8694c0cc96fedef1a61ef8f0feebd70f0511588f
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Thu Dec 24 09:14:06 2020 +0700

    JAMES-3431 MockSMTPServer: Capture RCPT TO ESMTP parameters
---
 .../mock/smtp/server/ExtendedMailFromCommand.java  | 25 +--------
 .../mock/smtp/server/ExtendedRcptToCommand.java    | 63 ++++++++++++++++++++++
 .../james/mock/smtp/server/MockMessageHandler.java | 13 ++++-
 .../james/mock/smtp/server/MockSMTPServer.java     |  1 +
 .../apache/james/mock/smtp/server/model/Mail.java  | 19 +++++++
 .../james/mock/smtp/server/MockSMTPServerTest.java | 52 +++++++++++++++++-
 6 files changed, 146 insertions(+), 27 deletions(-)

diff --git a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/ExtendedMailFromCommand.java b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/ExtendedMailFromCommand.java
index 89fe612..5e443c2 100644
--- a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/ExtendedMailFromCommand.java
+++ b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/ExtendedMailFromCommand.java
@@ -20,7 +20,6 @@
 package org.apache.james.mock.smtp.server;
 
 import java.io.IOException;
-import java.util.Collection;
 import java.util.Locale;
 
 import org.apache.james.mock.smtp.server.model.Mail;
@@ -30,10 +29,6 @@ import org.subethamail.smtp.server.BaseCommand;
 import org.subethamail.smtp.server.Session;
 import org.subethamail.smtp.util.EmailUtils;
 
-import com.github.steveash.guavate.Guavate;
-import com.google.common.base.Preconditions;
-import com.google.common.base.Splitter;
-
 public class ExtendedMailFromCommand extends BaseCommand {
     public ExtendedMailFromCommand() {
         super("MAIL", "Specifies the sender.", "FROM: <sender> [ <parameters> ]");
@@ -74,7 +69,7 @@ public class ExtendedMailFromCommand extends BaseCommand {
                 try {
                     sess.startMailTransaction();
                     MockMessageHandler messageHandler = (MockMessageHandler) sess.getMessageHandler();
-                    messageHandler.from(emailAddress, parameters(args));
+                    messageHandler.from(emailAddress, Mail.Parameter.fromArgLine(args));
                     sess.setDeclaredMessageSize(size);
                     sess.setHasMailFrom(true);
                     sess.sendResponse("250 Ok");
@@ -88,22 +83,4 @@ public class ExtendedMailFromCommand extends BaseCommand {
             }
         }
     }
-
-    private Collection<Mail.Parameter> parameters(String argLine) {
-        return Splitter.on(' ').splitToList(argLine)
-            .stream()
-            .filter(argString -> argString.contains("="))
-            .map(this::parameter)
-            .collect(Guavate.toImmutableList());
-    }
-
-    private Mail.Parameter parameter(String argString) {
-        Preconditions.checkArgument(argString.contains("="));
-        int index = argString.indexOf('=');
-
-        return Mail.Parameter.builder()
-            .name(argString.substring(0, index))
-            .value(argString.substring(index + 1))
-            .build();
-    }
 }
diff --git a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/ExtendedRcptToCommand.java b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/ExtendedRcptToCommand.java
new file mode 100644
index 0000000..e9108b5
--- /dev/null
+++ b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/ExtendedRcptToCommand.java
@@ -0,0 +1,63 @@
+/****************************************************************
+ * 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.mock.smtp.server;
+
+import java.io.IOException;
+import java.util.Locale;
+
+import org.apache.james.mock.smtp.server.model.Mail;
+import org.subethamail.smtp.DropConnectionException;
+import org.subethamail.smtp.RejectException;
+import org.subethamail.smtp.server.BaseCommand;
+import org.subethamail.smtp.server.Session;
+import org.subethamail.smtp.util.EmailUtils;
+
+public class ExtendedRcptToCommand extends BaseCommand {
+    public ExtendedRcptToCommand() {
+        super("RCPT", "Specifies the recipient. Can be used any number of times.", "TO: <recipient> [ <parameters> ]");
+    }
+
+    public void execute(String commandString, Session sess) throws IOException, DropConnectionException {
+        if (!sess.getHasMailFrom()) {
+            sess.sendResponse("503 Error: need MAIL command");
+        } else if (sess.getServer().getMaxRecipients() >= 0 && sess.getRecipientCount() >= sess.getServer().getMaxRecipients()) {
+            sess.sendResponse("452 Error: too many recipients");
+        } else {
+            String args = this.getArgPredicate(commandString);
+            if (!args.toUpperCase(Locale.ENGLISH).startsWith("TO:")) {
+                sess.sendResponse("501 Syntax: RCPT TO: <address>  Error in parameters: \"" + args + "\"");
+            } else {
+                String recipientAddress = EmailUtils.extractEmailAddress(args, 3);
+
+                try {
+                    MockMessageHandler messageHandler = (MockMessageHandler) sess.getMessageHandler();
+                    messageHandler.recipient(recipientAddress, Mail.Parameter.fromArgLine(args));
+                    sess.addRecipient(recipientAddress);
+                    sess.sendResponse("250 Ok");
+                } catch (DropConnectionException var6) {
+                    throw var6;
+                } catch (RejectException var7) {
+                    sess.sendResponse(var7.getErrorResponse());
+                }
+
+            }
+        }
+    }
+}
\ No newline at end of file
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 2cde815..1d50fc6 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
@@ -41,8 +41,6 @@ import org.slf4j.LoggerFactory;
 import org.subethamail.smtp.MessageHandler;
 import org.subethamail.smtp.RejectException;
 
-import com.google.common.collect.ImmutableMap;
-
 public class MockMessageHandler implements MessageHandler {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(MockMessageHandler.class);
@@ -140,6 +138,17 @@ public class MockMessageHandler implements MessageHandler {
             .behave(parse(recipient));
     }
 
+    public void recipient(String recipient, Collection<Parameter> parameters) throws RejectException {
+        Optional<Behavior<MailAddress>> recipientBehavior = firstMatchedBehavior(SMTPCommand.RCPT_TO, recipient);
+
+        recipientBehavior
+            .orElseGet(() -> address -> envelopeBuilder.addRecipient(Mail.Recipient.builder()
+                .parameters(parameters)
+                .address(address)
+                .build()))
+            .behave(parse(recipient));
+    }
+
     @Override
     public void data(InputStream data) throws RejectException {
         String dataString = readData(data);
diff --git a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/MockSMTPServer.java b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/MockSMTPServer.java
index d008ef4..3e2b5f6 100644
--- a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/MockSMTPServer.java
+++ b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/MockSMTPServer.java
@@ -40,6 +40,7 @@ class MockSMTPServer {
         this.server.setPort(port);
         this.server.getCommandHandler().addCommand(new ExtendedEhloCommand(behaviorRepository));
         this.server.getCommandHandler().addCommand(new ExtendedMailFromCommand());
+        this.server.getCommandHandler().addCommand(new ExtendedRcptToCommand());
     }
 
     void start() {
diff --git a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/model/Mail.java b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/model/Mail.java
index c2e5d3a..3e62964 100644
--- a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/model/Mail.java
+++ b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/model/Mail.java
@@ -33,6 +33,7 @@ import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
 import com.github.steveash.guavate.Guavate;
 import com.google.common.base.MoreObjects;
 import com.google.common.base.Preconditions;
+import com.google.common.base.Splitter;
 import com.google.common.collect.ImmutableList;
 
 @JsonDeserialize(builder = Mail.Builder.class)
@@ -70,6 +71,24 @@ public class Mail {
             return new Builder();
         }
 
+        public static Collection<Mail.Parameter> fromArgLine(String argLine) {
+            return Splitter.on(' ').splitToList(argLine)
+                .stream()
+                .filter(argString -> argString.contains("="))
+                .map(Parameter::fromString)
+                .collect(Guavate.toImmutableList());
+        }
+
+        public static Parameter fromString(String argString) {
+            Preconditions.checkArgument(argString.contains("="));
+            int index = argString.indexOf('=');
+
+            return Mail.Parameter.builder()
+                .name(argString.substring(0, index))
+                .value(argString.substring(index + 1))
+                .build();
+        }
+
         private final String name;
         private final String value;
 
diff --git a/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/MockSMTPServerTest.java b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/MockSMTPServerTest.java
index 3183837..1ce0e7d 100644
--- a/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/MockSMTPServerTest.java
+++ b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/MockSMTPServerTest.java
@@ -49,7 +49,6 @@ import org.apache.james.mock.smtp.server.model.MockSMTPBehavior;
 import org.apache.james.mock.smtp.server.model.Operator;
 import org.apache.james.mock.smtp.server.model.Response;
 import org.apache.james.mock.smtp.server.model.SMTPExtension;
-import org.apache.james.mock.smtp.server.model.SMTPExtensions;
 import org.apache.james.util.MimeMessageUtil;
 import org.apache.james.util.Port;
 import org.apache.james.utils.SMTPMessageSender;
@@ -196,6 +195,57 @@ class MockSMTPServerTest {
                             assertThat(assertedMail.getEnvelope()).isEqualTo(expectedEnvelope)));
                 });
         }
+
+        @Test
+        void rcptToParametersShouldBeRecognised() throws Exception {
+            AuthenticatingSMTPClient smtpClient = new AuthenticatingSMTPClient("TLS", "UTF-8");
+
+            try {
+                smtpClient.connect("localhost", mockServer.getPort().getValue());
+                smtpClient.ehlo("localhost");
+                smtpClient.mail("<bo...@james.org>");
+                smtpClient.rcpt("<al...@james.org> ORCPT=rfc822;alice@james.org NOTIFY=FAILURE,DELAY");
+                smtpClient.rcpt("<ja...@james.org> ORCPT=rfc822;jack@james.org NOTIFY=NEVER");
+                smtpClient.sendShortMessageData("A short message...");
+            } finally {
+                smtpClient.disconnect();
+            }
+
+            Mail.Envelope expectedEnvelope = Mail.Envelope.builder()
+                .from(new MailAddress(BOB))
+                .addRecipient(Mail.Recipient.builder()
+                    .addParameter(Mail.Parameter.builder()
+                        .name("ORCPT")
+                        .value("rfc822;alice@james.org")
+                        .build())
+                    .addParameter(Mail.Parameter.builder()
+                        .name("NOTIFY")
+                        .value("FAILURE,DELAY")
+                        .build())
+                    .address(new MailAddress(ALICE))
+                    .build())
+                .addRecipient(Mail.Recipient.builder()
+                    .addParameter(Mail.Parameter.builder()
+                        .name("ORCPT")
+                        .value("rfc822;jack@james.org")
+                        .build())
+                    .addParameter(Mail.Parameter.builder()
+                        .name("NOTIFY")
+                        .value("NEVER")
+                        .build())
+                    .address(new MailAddress(JACK))
+                    .build())
+                .build();
+
+            Awaitility.await().atMost(Duration.TEN_SECONDS)
+                .untilAsserted(() -> {
+                    List<Mail> mails = mailRepository.list();
+                    assertThat(mails)
+                        .hasSize(1)
+                        .allSatisfy(Throwing.consumer(assertedMail ->
+                            assertThat(assertedMail.getEnvelope()).isEqualTo(expectedEnvelope)));
+                });
+        }
     }
 
     @Nested


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org