james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [13/19] james-project git commit: JAMES-2541 Rely on JSON for Per RCPT headers serialization
Date Mon, 10 Sep 2018 10:34:16 GMT
JAMES-2541 Rely on JSON for Per RCPT headers serialization


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/d8b8527e
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/d8b8527e
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/d8b8527e

Branch: refs/heads/master
Commit: d8b8527ea3bae21059112c1cf5bd4208888d8c19
Parents: 8ed6400
Author: Benoit Tellier <btellier@linagora.com>
Authored: Fri Sep 7 16:38:14 2018 +0700
Committer: Benoit Tellier <btellier@linagora.com>
Committed: Mon Sep 10 17:19:38 2018 +0700

----------------------------------------------------------------------
 .../apache/james/queue/rabbitmq/HeadersDto.java | 62 ++++++++++++++++++++
 .../apache/james/queue/rabbitmq/MailDTO.java    | 19 ++++--
 .../james/queue/rabbitmq/RabbitMQMailQueue.java | 15 ++++-
 3 files changed, 89 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/d8b8527e/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/HeadersDto.java
----------------------------------------------------------------------
diff --git a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/HeadersDto.java
b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/HeadersDto.java
new file mode 100644
index 0000000..5d6061b
--- /dev/null
+++ b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/HeadersDto.java
@@ -0,0 +1,62 @@
+/****************************************************************
+ * 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.queue.rabbitmq;
+
+import java.util.Collection;
+
+import org.apache.mailet.PerRecipientHeaders;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableListMultimap;
+import com.google.common.collect.Multimap;
+
+public class HeadersDto {
+
+    public static HeadersDto from(Collection<PerRecipientHeaders.Header> headers) {
+        return new HeadersDto(headers.stream()
+            .collect(ImmutableListMultimap.toImmutableListMultimap(
+                PerRecipientHeaders.Header::getName,
+                PerRecipientHeaders.Header::getValue)));
+    }
+
+    private final Multimap<String, String> headers;
+
+    @JsonCreator
+    private HeadersDto(@JsonProperty("header") Multimap<String, String> headers) {
+        this.headers = headers;
+    }
+
+    @JsonProperty("header")
+    public Multimap<String, String> getHeaders() {
+        return headers;
+    }
+
+    public Collection<PerRecipientHeaders.Header> toHeaders() {
+        return headers.entries()
+            .stream()
+            .map(entry -> PerRecipientHeaders.Header.builder()
+                .name(entry.getKey())
+                .value(entry.getValue())
+                .build())
+            .collect(ImmutableList.toImmutableList());
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/d8b8527e/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/MailDTO.java
----------------------------------------------------------------------
diff --git a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/MailDTO.java
b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/MailDTO.java
index 76ed232..d760f18 100644
--- a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/MailDTO.java
+++ b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/MailDTO.java
@@ -29,6 +29,7 @@ import org.apache.james.core.MailAddress;
 import org.apache.james.util.SerializationUtil;
 import org.apache.james.util.streams.Iterators;
 import org.apache.mailet.Mail;
+import org.apache.mailet.PerRecipientHeaders;
 
 import com.fasterxml.jackson.annotation.JsonCreator;
 import com.fasterxml.jackson.annotation.JsonProperty;
@@ -51,11 +52,21 @@ class MailDTO {
             serializedAttributes(mail),
             mail.getRemoteAddr(),
             mail.getRemoteHost(),
-            SerializationUtil.serialize(mail.getPerRecipientSpecificHeaders()),
+            fromPerRecipientHeaders(mail.getPerRecipientSpecificHeaders()),
             partsId.getHeaderBlobId().asString(),
             partsId.getBodyBlobId().asString());
     }
 
+    private static Map<String, HeadersDto> fromPerRecipientHeaders(PerRecipientHeaders
perRecipientHeaders) {
+        return perRecipientHeaders.getHeadersByRecipient()
+            .asMap()
+            .entrySet()
+            .stream()
+            .collect(ImmutableMap.toImmutableMap(
+                entry -> entry.getKey().asString(),
+                entry -> HeadersDto.from(entry.getValue())));
+    }
+
     private static ImmutableMap<String, String> serializedAttributes(Mail mail) {
         return Iterators.toStream(mail.getAttributeNames())
             .collect(Guavate.toImmutableMap(
@@ -72,7 +83,7 @@ class MailDTO {
     private final ImmutableMap<String, String> attributes;
     private final String remoteAddr;
     private final String remoteHost;
-    private final String perRecipientHeaders;
+    private final Map<String, HeadersDto> perRecipientHeaders;
     private final String headerBlobId;
     private final String bodyBlobId;
 
@@ -86,7 +97,7 @@ class MailDTO {
                     @JsonProperty("attributes") ImmutableMap<String, String> attributes,
                     @JsonProperty("remoteAddr") String remoteAddr,
                     @JsonProperty("remoteHost") String remoteHost,
-                    @JsonProperty("perRecipientHeaders") String perRecipientHeaders,
+                    @JsonProperty("perRecipientHeaders") Map<String, HeadersDto>  perRecipientHeaders,
                     @JsonProperty("headerBlobId") String headerBlobId,
                     @JsonProperty("bodyBlobId") String bodyBlobId) {
         this.recipients = recipients;
@@ -149,7 +160,7 @@ class MailDTO {
     }
 
     @JsonProperty("perRecipientHeaders")
-    String getPerRecipientHeaders() {
+    Map<String, HeadersDto>  getPerRecipientHeaders() {
         return perRecipientHeaders;
     }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/d8b8527e/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueue.java
----------------------------------------------------------------------
diff --git a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueue.java
b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueue.java
index c40fe6b..10300cd 100644
--- a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueue.java
+++ b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueue.java
@@ -22,7 +22,6 @@ package org.apache.james.queue.rabbitmq;
 import java.io.IOException;
 import java.io.Serializable;
 import java.util.Date;
-import java.util.Optional;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.Executors;
 import java.util.concurrent.TimeUnit;
@@ -220,8 +219,7 @@ public class RabbitMQMailQueue implements MailQueue {
             dto.getAttributes()
                 .forEach((name, value) -> mail.setAttribute(name, SerializationUtil.<Serializable>deserialize(value)));
 
-            Optional.ofNullable(SerializationUtil.<PerRecipientHeaders>deserialize(dto.getPerRecipientHeaders()))
-                .ifPresent(mail::addAllSpecificHeaderForRecipient);
+            mail.addAllSpecificHeaderForRecipient(retrievePerRecipientHeaders(dto));
 
             return mail;
         } catch (AddressException e) {
@@ -230,4 +228,15 @@ public class RabbitMQMailQueue implements MailQueue {
             throw new MailQueueException("Failed to generate mime message", e);
         }
     }
+
+    private PerRecipientHeaders retrievePerRecipientHeaders(MailDTO dto) {
+        PerRecipientHeaders perRecipientHeaders = new PerRecipientHeaders();
+        dto.getPerRecipientHeaders()
+            .entrySet()
+            .stream()
+            .flatMap(entry -> entry.getValue().toHeaders().stream()
+                .map(Throwing.function(header -> Pair.of(new MailAddress(entry.getKey()),
header))))
+            .forEach(pair -> perRecipientHeaders.addHeaderForRecipient(pair.getValue(),
pair.getKey()));
+        return perRecipientHeaders;
+    }
 }
\ No newline at end of file


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