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 2024/09/19 02:10:21 UTC

(james-project) branch master updated (ed9b72bb96 -> 48d22287d3)

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

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


    from ed9b72bb96 JAMES-4070 Implement query param to return 503 code if degraded - fixup document
     new 59e9d8fcdc AmqpForwardAttribute mailet supports declaring exchange types.
     new 48d22287d3 AmqpForwardAttribute mailet supports declaring exchange types - integration test

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../servers/partials/AmqpForwardAttribute.adoc     |  1 +
 .../transport/mailets/AmqpForwardAttribute.java    | 20 +++++++++++++-
 .../mailets/AmqpForwardAttributeTest.java          | 32 ++++++++++++++++++++++
 ...buteWithFanoutExchangeTypeIntegrationTest.java} |  9 ++++--
 .../transport/mailets/amqp/AmqpExtension.java      | 10 +++++--
 5 files changed, 66 insertions(+), 6 deletions(-)
 copy server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/{ContactExtractorTest.java => AmqpForwardAttributeWithFanoutExchangeTypeIntegrationTest.java} (95%)


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


(james-project) 02/02: AmqpForwardAttribute mailet supports declaring exchange types - integration test

Posted by rc...@apache.org.
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 48d22287d3e68343cd7c96839991507bd452c424
Author: Tung Tran <vt...@linagora.com>
AuthorDate: Wed Sep 18 16:42:44 2024 +0700

    AmqpForwardAttribute mailet supports declaring exchange types - integration test
---
 ...ibuteWithFanoutExchangeTypeIntegrationTest.java | 148 +++++++++++++++++++++
 .../transport/mailets/amqp/AmqpExtension.java      |  10 +-
 2 files changed, 156 insertions(+), 2 deletions(-)

diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/AmqpForwardAttributeWithFanoutExchangeTypeIntegrationTest.java b/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/AmqpForwardAttributeWithFanoutExchangeTypeIntegrationTest.java
new file mode 100644
index 0000000000..9ec3a3c8a0
--- /dev/null
+++ b/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/AmqpForwardAttributeWithFanoutExchangeTypeIntegrationTest.java
@@ -0,0 +1,148 @@
+/****************************************************************
+ * 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;
+
+import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
+import static org.apache.james.mailets.configuration.Constants.DEFAULT_DOMAIN;
+import static org.apache.james.mailets.configuration.Constants.LOCALHOST_IP;
+import static org.apache.james.mailets.configuration.Constants.PASSWORD;
+import static org.apache.james.mailets.configuration.Constants.awaitAtMostOneMinute;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.File;
+import java.util.Optional;
+
+import org.apache.james.MemoryJamesServerMain;
+import org.apache.james.core.builder.MimeMessageBuilder;
+import org.apache.james.mailets.TemporaryJamesServer;
+import org.apache.james.mailets.configuration.CommonProcessors;
+import org.apache.james.mailets.configuration.MailetConfiguration;
+import org.apache.james.mailets.configuration.MailetContainer;
+import org.apache.james.mailets.configuration.ProcessorConfiguration;
+import org.apache.james.modules.protocols.ImapGuiceProbe;
+import org.apache.james.modules.protocols.SmtpGuiceProbe;
+import org.apache.james.transport.mailets.amqp.AmqpExtension;
+import org.apache.james.transport.matchers.All;
+import org.apache.james.transport.matchers.SMTPAuthSuccessful;
+import org.apache.james.utils.DataProbeImpl;
+import org.apache.james.utils.SMTPMessageSender;
+import org.apache.james.utils.TestIMAPClient;
+import org.apache.mailet.base.test.FakeMail;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.junit.jupiter.api.io.TempDir;
+
+import com.rabbitmq.client.BuiltinExchangeType;
+
+class AmqpForwardAttributeWithFanoutExchangeTypeIntegrationTest {
+    public static final String SENDER = "sender@" + DEFAULT_DOMAIN;
+    public static final String TO = "to@" + DEFAULT_DOMAIN;
+    public static final String TO2 = "to2@" + DEFAULT_DOMAIN;
+    public static final String CC = "cc@" + DEFAULT_DOMAIN;
+    public static final String CC2 = "cc2@" + DEFAULT_DOMAIN;
+    public static final String BCC = "bcc@" + DEFAULT_DOMAIN;
+    public static final String BCC2 = "bcc2@" + DEFAULT_DOMAIN;
+    public static final String EXCHANGE = "collector:email";
+    public static final String ROUTING_KEY = "routing1";
+
+    @RegisterExtension
+    public static AmqpExtension amqpExtension = new AmqpExtension(EXCHANGE, ROUTING_KEY, BuiltinExchangeType.FANOUT);
+    @RegisterExtension
+    public TestIMAPClient testIMAPClient = new TestIMAPClient();
+    @RegisterExtension
+    public SMTPMessageSender messageSender = new SMTPMessageSender(DEFAULT_DOMAIN);
+
+    private TemporaryJamesServer jamesServer;
+
+    @BeforeEach
+    void setup(@TempDir File temporaryFolder) throws Exception {
+        String attribute = "ExtractedContacts";
+        MailetContainer.Builder mailets = TemporaryJamesServer.defaultMailetContainerConfiguration()
+            .postmaster(SENDER)
+            .putProcessor(
+                ProcessorConfiguration.transport()
+                    .addMailet(MailetConfiguration.builder()
+                        .matcher(SMTPAuthSuccessful.class)
+                        .mailet(ContactExtractor.class)
+                        .addProperty(ContactExtractor.Configuration.ATTRIBUTE, attribute))
+                    .addMailet(MailetConfiguration.builder()
+                        .matcher(All.class)
+                        .mailet(AmqpForwardAttribute.class)
+                        .addProperty(AmqpForwardAttribute.URI_PARAMETER_NAME, amqpExtension.getAmqpUri())
+                        .addProperty(AmqpForwardAttribute.EXCHANGE_PARAMETER_NAME, EXCHANGE)
+                        .addProperty(AmqpForwardAttribute.EXCHANGE_TYPE_PARAMETER_NAME, BuiltinExchangeType.FANOUT.getType())
+                        .addProperty(AmqpForwardAttribute.ATTRIBUTE_PARAMETER_NAME, attribute))
+                    .addMailetsFrom(CommonProcessors.deliverOnlyTransport()));
+
+        jamesServer = TemporaryJamesServer.builder()
+            .withBase(MemoryJamesServerMain.SMTP_AND_IMAP_MODULE)
+            .withMailetContainer(mailets)
+            .build(temporaryFolder);
+        jamesServer.start();
+
+        jamesServer.getProbe(DataProbeImpl.class)
+            .fluent()
+            .addDomain(DEFAULT_DOMAIN)
+            .addUser(SENDER, PASSWORD)
+            .addUser(TO, PASSWORD)
+            .addUser(TO2, PASSWORD)
+            .addUser(CC, PASSWORD)
+            .addUser(CC2, PASSWORD)
+            .addUser(BCC, PASSWORD)
+            .addUser(BCC2, PASSWORD);
+    }
+
+    @AfterEach
+    void tearDown() {
+        jamesServer.shutdown();
+    }
+
+    @Test
+    void recipientsShouldBePublishedToAmqpWhenSendingEmail() throws Exception {
+        MimeMessageBuilder message = MimeMessageBuilder.mimeMessageBuilder()
+            .setSender(SENDER)
+            .addToRecipient(TO, "John To2 <" + TO2 + ">")
+            .addCcRecipient(CC, "John Cc2 <" + CC2 + ">")
+            .addBccRecipient(BCC, "John Bcc2 <" + BCC2 + ">")
+            .setSubject("Contact collection Rocks")
+            .setText("This is my email");
+        messageSender.connect(LOCALHOST_IP, jamesServer.getProbe(SmtpGuiceProbe.class).getSmtpPort())
+            .authenticate(SENDER, PASSWORD)
+            .sendMessage(FakeMail.builder()
+                .name("name")
+                .mimeMessage(message)
+                .sender(SENDER)
+                .recipients(TO, TO2, CC, CC2, BCC, BCC2));
+
+        testIMAPClient.connect(LOCALHOST_IP, jamesServer.getProbe(ImapGuiceProbe.class).getImapPort())
+            .login(TO, PASSWORD)
+            .select(TestIMAPClient.INBOX)
+            .awaitMessage(awaitAtMostOneMinute);
+
+        Optional<String> actual = amqpExtension.readContent();
+        assertThat(actual).isNotEmpty();
+        assertThatJson(actual.get()).isEqualTo("{"
+            + "\"userEmail\" : \"sender@james.org\", "
+            + "\"emails\" : [ \"to@james.org\", \"John To2 <to...@james.org>\", \"cc@james.org\", \"John Cc2 <cc...@james.org>\", \"bcc@james.org\", \"John Bcc2 <bc...@james.org>\" ]"
+            + "}");
+    }
+
+}
diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/amqp/AmqpExtension.java b/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/amqp/AmqpExtension.java
index e7a1fbca37..7e25a7ced7 100644
--- a/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/amqp/AmqpExtension.java
+++ b/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/amqp/AmqpExtension.java
@@ -50,13 +50,14 @@ public class AmqpExtension implements BeforeAllCallback, AfterAllCallback, After
 
     private final DockerContainer rabbitMqContainer;
     private final String exchangeName;
+    private final BuiltinExchangeType exchangeType;
     private final String routingKey;
     private Channel channel;
     private String queueName;
     private Connection connection;
     private String amqpUri;
 
-    public AmqpExtension(String exchangeName, String routingKey) {
+    public AmqpExtension(String exchangeName, String routingKey, BuiltinExchangeType exchangeType) {
         this.rabbitMqContainer = DockerContainer.fromName(Images.RABBITMQ)
             .withAffinityToContainer()
             .waitingFor(new HostPortWaitStrategy()
@@ -64,6 +65,11 @@ public class AmqpExtension implements BeforeAllCallback, AfterAllCallback, After
                 .withLogConsumer(AmqpExtension::displayDockerLog);
         this.exchangeName = exchangeName;
         this.routingKey = routingKey;
+        this.exchangeType = exchangeType;
+    }
+
+    public AmqpExtension(String exchangeName, String routingKey) {
+        this(exchangeName, routingKey, BuiltinExchangeType.DIRECT);
     }
 
     private static void displayDockerLog(OutputFrame outputFrame) {
@@ -79,7 +85,7 @@ public class AmqpExtension implements BeforeAllCallback, AfterAllCallback, After
         waitingForRabbitToBeReady(factory);
         connection = factory.newConnection();
         channel = connection.createChannel();
-        channel.exchangeDeclare(exchangeName, BuiltinExchangeType.DIRECT);
+        channel.exchangeDeclare(exchangeName, exchangeType);
         queueName = channel.queueDeclare().getQueue();
         channel.queueBind(queueName, exchangeName, routingKey);
     }


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


(james-project) 01/02: AmqpForwardAttribute mailet supports declaring exchange types.

Posted by rc...@apache.org.
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 59e9d8fcdc3ad7d3c05d24ef2972ab3881466810
Author: Tung Tran <vt...@linagora.com>
AuthorDate: Wed Sep 18 16:24:33 2024 +0700

    AmqpForwardAttribute mailet supports declaring exchange types.
---
 .../servers/partials/AmqpForwardAttribute.adoc     |  1 +
 .../transport/mailets/AmqpForwardAttribute.java    | 20 +++++++++++++-
 .../mailets/AmqpForwardAttributeTest.java          | 32 ++++++++++++++++++++++
 3 files changed, 52 insertions(+), 1 deletion(-)

diff --git a/docs/modules/servers/partials/AmqpForwardAttribute.adoc b/docs/modules/servers/partials/AmqpForwardAttribute.adoc
index 8ce53b575a..46b4e79460 100644
--- a/docs/modules/servers/partials/AmqpForwardAttribute.adoc
+++ b/docs/modules/servers/partials/AmqpForwardAttribute.adoc
@@ -9,6 +9,7 @@ where the byte[] content is issued from a MimeBodyPart.
 It is typically generated from the StripAttachment mailet.
 * uri (mandatory): AMQP URI defining the server where to send the attachment.
 * exchange (mandatory): name of the AMQP exchange.
+* exchange_type (optional, default to "direct"): type of the exchange. Can be "direct", "fanout", "topic", "headers".
 * routing_key (optional, default to empty string): name of the routing key on this exchange.
 
 This mailet will sent the data attached to the mail as an attribute holding a map.
\ No newline at end of file
diff --git a/mailet/amqp/src/main/java/org/apache/james/transport/mailets/AmqpForwardAttribute.java b/mailet/amqp/src/main/java/org/apache/james/transport/mailets/AmqpForwardAttribute.java
index 1a8198dba2..5c0dc09a77 100644
--- a/mailet/amqp/src/main/java/org/apache/james/transport/mailets/AmqpForwardAttribute.java
+++ b/mailet/amqp/src/main/java/org/apache/james/transport/mailets/AmqpForwardAttribute.java
@@ -23,6 +23,7 @@ import java.net.URI;
 import java.net.URISyntaxException;
 import java.nio.charset.StandardCharsets;
 import java.time.Duration;
+import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
@@ -31,6 +32,7 @@ import java.util.stream.Stream;
 import jakarta.annotation.PreDestroy;
 import jakarta.inject.Inject;
 
+import org.apache.commons.lang3.StringUtils;
 import org.apache.james.backends.rabbitmq.RabbitMQConfiguration;
 import org.apache.james.backends.rabbitmq.RabbitMQConnectionFactory;
 import org.apache.james.backends.rabbitmq.ReactorRabbitMQChannelPool;
@@ -55,6 +57,7 @@ import com.google.common.base.Splitter;
 import com.google.common.base.Strings;
 import com.google.common.collect.ImmutableList;
 import com.rabbitmq.client.AlreadyClosedException;
+import com.rabbitmq.client.BuiltinExchangeType;
 import com.rabbitmq.client.ConnectionFactory;
 
 import reactor.core.publisher.Flux;
@@ -72,6 +75,7 @@ import reactor.rabbitmq.Sender;
  * It is typically generated from the StripAttachment mailet.</li>
  * <li>uri (mandatory): AMQP URI defining the server where to send the attachment.</li>
  * <li>exchange (mandatory): name of the AMQP exchange.</li>
+ * <li>exchange_type (optional, default to "direct"): type of the exchange. Valid values are: direct, fanout, topic, headers.</li>
  * <li>routing_key (optional, default to empty string): name of the routing key on this exchange.</li>
  * </ul>
  *
@@ -90,11 +94,13 @@ public class AmqpForwardAttribute extends GenericMailet {
     private static final String DEFAULT_USER = "guest";
     private static final String DEFAULT_PASSWORD_STRING = "guest";
     private static final char[] DEFAULT_PASSWORD = DEFAULT_PASSWORD_STRING.toCharArray();
+    private static final List<String> VALIDATE_EXCHANGE_TYPES = Arrays.stream(BuiltinExchangeType.values()).map(BuiltinExchangeType::getType).toList();
     static final RabbitMQConfiguration.ManagementCredentials DEFAULT_MANAGEMENT_CREDENTIAL = new RabbitMQConfiguration.ManagementCredentials(DEFAULT_USER, DEFAULT_PASSWORD);
 
 
     public static final String URI_PARAMETER_NAME = "uri";
     public static final String EXCHANGE_PARAMETER_NAME = "exchange";
+    public static final String EXCHANGE_TYPE_PARAMETER_NAME = "exchange_type";
     public static final String ROUTING_KEY_PARAMETER_NAME = "routing_key";
     public static final String ATTRIBUTE_PARAMETER_NAME = "attribute";
 
@@ -103,6 +109,7 @@ public class AmqpForwardAttribute extends GenericMailet {
     private final MetricFactory metricFactory;
 
     private String exchange;
+    private String exchangeType;
     private AttributeName attribute;
     private ConnectionFactory connectionFactory;
     @VisibleForTesting String routingKey;
@@ -142,7 +149,12 @@ public class AmqpForwardAttribute extends GenericMailet {
                 metricFactory, new NoopGaugeRegistry());
             reactorRabbitMQChannelPool.start();
             sender = reactorRabbitMQChannelPool.getSender();
-            sender.declareExchange(ExchangeSpecification.exchange(exchange)).block();
+
+            ExchangeSpecification exchangeSpecification = Optional.ofNullable(exchangeType)
+                .map(type -> ExchangeSpecification.exchange(exchange).type(type))
+                .orElse(ExchangeSpecification.exchange(exchange));
+
+            sender.declareExchange(exchangeSpecification).block();
         } catch (URISyntaxException e) {
             throw new RuntimeException(e);
         }
@@ -183,6 +195,12 @@ public class AmqpForwardAttribute extends GenericMailet {
             throw new MailetException("No value for " + EXCHANGE_PARAMETER_NAME
                     + " parameter was provided.");
         }
+        exchangeType = mailetConfig.getInitParameter(EXCHANGE_TYPE_PARAMETER_NAME);
+        if (StringUtils.isNotEmpty(exchangeType) && !VALIDATE_EXCHANGE_TYPES.contains(exchangeType)) {
+            throw new MailetException("Invalid value for " + EXCHANGE_TYPE_PARAMETER_NAME
+                + " parameter was provided: " + exchangeType + ". Valid values are: " + VALIDATE_EXCHANGE_TYPES);
+        }
+
         routingKey = Optional.ofNullable(mailetConfig.getInitParameter(ROUTING_KEY_PARAMETER_NAME))
             .orElse(ROUTING_KEY_DEFAULT_VALUE);
         String rawAttribute = mailetConfig.getInitParameter(ATTRIBUTE_PARAMETER_NAME);
diff --git a/mailet/amqp/src/test/java/org/apache/james/transport/mailets/AmqpForwardAttributeTest.java b/mailet/amqp/src/test/java/org/apache/james/transport/mailets/AmqpForwardAttributeTest.java
index e3a6afaf24..3569fe5c5a 100644
--- a/mailet/amqp/src/test/java/org/apache/james/transport/mailets/AmqpForwardAttributeTest.java
+++ b/mailet/amqp/src/test/java/org/apache/james/transport/mailets/AmqpForwardAttributeTest.java
@@ -21,6 +21,7 @@ package org.apache.james.transport.mailets;
 
 import static org.apache.james.transport.mailets.AmqpForwardAttribute.DEFAULT_MANAGEMENT_CREDENTIAL;
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatCode;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
@@ -43,6 +44,8 @@ import org.apache.mailet.base.test.FakeMailetConfig;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Nested;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
 import org.slf4j.Logger;
 
 class AmqpForwardAttributeTest {
@@ -153,6 +156,35 @@ class AmqpForwardAttributeTest {
             .isInstanceOf(MailetException.class);
     }
 
+    @Test
+    void initShouldThrowWhenInvalidExchangeType() {
+        FakeMailetConfig customMailetConfig = FakeMailetConfig.builder()
+            .mailetName("Test")
+            .mailetContext(mailetContext)
+            .setProperty("uri", AMQP_URI)
+            .setProperty("exchange", EXCHANGE_NAME)
+            .setProperty("exchange_type", "invalid")
+            .setProperty("attribute", MAIL_ATTRIBUTE.asString())
+            .build();
+        assertThatThrownBy(() -> mailet.preInit(customMailetConfig))
+            .isInstanceOf(MailetException.class)
+            .hasMessageContaining("Invalid value for exchange_type parameter was provided: invalid");
+    }
+
+    @ParameterizedTest
+    @ValueSource(strings = {"direct", "fanout", "topic", "headers"})
+    void initShouldNotThrowWhenValidExchangeType(String exchangeType) {
+        assertThatCode(() -> mailet.preInit(FakeMailetConfig.builder()
+            .mailetName("Test")
+            .mailetContext(mailetContext)
+            .setProperty("uri", AMQP_URI)
+            .setProperty("exchange", EXCHANGE_NAME)
+            .setProperty("exchange_type", exchangeType)
+            .setProperty("attribute", MAIL_ATTRIBUTE.asString())
+            .build()))
+            .doesNotThrowAnyException();
+    }
+
     @Nested
     class CreadentialTests {
         @Test


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