You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by bt...@apache.org on 2022/08/26 01:48:11 UTC

[james-project] branch master updated: JAMES-3775 Rspamd learnHam should be idempotent (#1155)

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


The following commit(s) were added to refs/heads/master by this push:
     new f214ffc043 JAMES-3775 Rspamd learnHam should be idempotent (#1155)
f214ffc043 is described below

commit f214ffc043fbcfee625eb62970e515e12ba5f9d0
Author: Benoit TELLIER <bt...@linagora.com>
AuthorDate: Fri Aug 26 08:48:07 2022 +0700

    JAMES-3775 Rspamd learnHam should be idempotent (#1155)
---
 .../apache/james/rspamd/client/RspamdHttpClient.java | 16 +++++++++++++++-
 .../james/rspamd/client/RspamdHttpClientTest.java    | 20 ++++++++++++++++++++
 2 files changed, 35 insertions(+), 1 deletion(-)

diff --git a/third-party/rspamd/src/main/java/org/apache/james/rspamd/client/RspamdHttpClient.java b/third-party/rspamd/src/main/java/org/apache/james/rspamd/client/RspamdHttpClient.java
index f9d66c5e09..7746c57d9a 100644
--- a/third-party/rspamd/src/main/java/org/apache/james/rspamd/client/RspamdHttpClient.java
+++ b/third-party/rspamd/src/main/java/org/apache/james/rspamd/client/RspamdHttpClient.java
@@ -36,6 +36,8 @@ import org.apache.james.server.core.MimeMessageInputStream;
 import org.apache.james.util.ReactorUtils;
 import org.apache.mailet.AttributeName;
 import org.apache.mailet.Mail;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
@@ -50,6 +52,8 @@ import reactor.netty.http.client.HttpClient;
 import reactor.netty.http.client.HttpClientResponse;
 
 public class RspamdHttpClient {
+    private static final Logger LOGGER = LoggerFactory.getLogger(RspamdHttpClient.class);
+
     public static final String CHECK_V2_ENDPOINT = "/checkV2";
     public static final String LEARN_SPAM_ENDPOINT = "/learnspam";
     public static final String LEARN_HAM_ENDPOINT = "/learnham";
@@ -163,7 +167,17 @@ public class RspamdHttpClient {
                     .flatMap(responseBody -> Mono.error(() -> new UnauthorizedException(responseBody)));
             default:
                 return byteBufMono.asString(StandardCharsets.UTF_8)
-                    .flatMap(responseBody -> Mono.error(() -> new RspamdUnexpectedException(responseBody)));
+                    .flatMap(responseBody -> {
+                        if (responseBody.contains(" has been already learned as ham, ignore it")) {
+                            LOGGER.debug(responseBody);
+                            return Mono.empty();
+                        }
+                        if (responseBody.contains(" has been already learned as spam, ignore it")) {
+                            LOGGER.debug(responseBody);
+                            return Mono.empty();
+                        }
+                        return Mono.error(() -> new RspamdUnexpectedException(responseBody));
+                    });
         }
     }
 
diff --git a/third-party/rspamd/src/test/java/org/apache/james/rspamd/client/RspamdHttpClientTest.java b/third-party/rspamd/src/test/java/org/apache/james/rspamd/client/RspamdHttpClientTest.java
index bbe0ac0bae..25f84da4cc 100644
--- a/third-party/rspamd/src/test/java/org/apache/james/rspamd/client/RspamdHttpClientTest.java
+++ b/third-party/rspamd/src/test/java/org/apache/james/rspamd/client/RspamdHttpClientTest.java
@@ -163,6 +163,26 @@ class RspamdHttpClientTest {
             .doesNotThrowAnyException();
     }
 
+    @Test
+    void learnHamMShouldBeIdempotent() {
+        RspamdClientConfiguration configuration = new RspamdClientConfiguration(rspamdExtension.getBaseUrl(), PASSWORD, Optional.empty());
+        RspamdHttpClient client = new RspamdHttpClient(configuration);
+
+        client.reportAsHam(new ByteArrayInputStream(hamMessage)).block();
+        assertThatCode(() -> client.reportAsHam(new ByteArrayInputStream(hamMessage)).block())
+            .doesNotThrowAnyException();
+    }
+
+    @Test
+    void learnSpamMShouldBeIdempotent() {
+        RspamdClientConfiguration configuration = new RspamdClientConfiguration(rspamdExtension.getBaseUrl(), PASSWORD, Optional.empty());
+        RspamdHttpClient client = new RspamdHttpClient(configuration);
+
+        client.reportAsSpam(new ByteArrayInputStream(spamMessage)).block();
+        assertThatCode(() -> client.reportAsSpam(new ByteArrayInputStream(spamMessage)).block())
+            .doesNotThrowAnyException();
+    }
+
     @Test
     void checkVirusMailUsingRspamdClientWithExactPasswordShouldReturnHasVirus() {
         RspamdClientConfiguration configuration = new RspamdClientConfiguration(rspamdExtension.getBaseUrl(), PASSWORD, Optional.empty());


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