james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adup...@apache.org
Subject [04/14] james-project git commit: JAMES-2540 MailDTO should be able to return the corresponding email
Date Mon, 17 Sep 2018 06:33:14 GMT
JAMES-2540 MailDTO should be able to return the corresponding email

Note that storing the MimeMessage is not the responsibility of the DTO
thus the caller has to supply it.


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

Branch: refs/heads/master
Commit: 0303205ee5a3f4b6b9a3db058427694dab52fa32
Parents: 994f5b1
Author: Benoit Tellier <btellier@linagora.com>
Authored: Tue Sep 11 10:14:57 2018 +0700
Committer: Benoit Tellier <btellier@linagora.com>
Committed: Fri Sep 14 11:05:43 2018 +0700

----------------------------------------------------------------------
 .../apache/james/queue/rabbitmq/Dequeuer.java   | 46 +-------------------
 .../apache/james/queue/rabbitmq/MailDTO.java    | 42 ++++++++++++++++++
 2 files changed, 43 insertions(+), 45 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/0303205e/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/Dequeuer.java
----------------------------------------------------------------------
diff --git a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/Dequeuer.java
b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/Dequeuer.java
index f5b690d..4cc11b5 100644
--- a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/Dequeuer.java
+++ b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/Dequeuer.java
@@ -22,31 +22,21 @@ package org.apache.james.queue.rabbitmq;
 import static org.apache.james.queue.api.MailQueue.DEQUEUED_METRIC_NAME_PREFIX;
 
 import java.io.IOException;
-import java.io.Serializable;
-import java.time.Instant;
-import java.util.Date;
 import java.util.concurrent.Executors;
 
 import javax.mail.MessagingException;
 import javax.mail.internet.AddressException;
 import javax.mail.internet.MimeMessage;
 
-import org.apache.commons.lang3.tuple.Pair;
 import org.apache.james.blob.api.BlobId;
 import org.apache.james.blob.api.Store;
 import org.apache.james.blob.mail.MimeMessagePartsId;
-import org.apache.james.core.MailAddress;
 import org.apache.james.metrics.api.Metric;
 import org.apache.james.metrics.api.MetricFactory;
 import org.apache.james.queue.api.MailQueue;
-import org.apache.james.server.core.MailImpl;
-import org.apache.james.util.SerializationUtil;
 import org.apache.mailet.Mail;
-import org.apache.mailet.PerRecipientHeaders;
 
 import com.fasterxml.jackson.databind.ObjectMapper;
-import com.github.fge.lambdas.Throwing;
-import com.github.steveash.guavate.Guavate;
 import com.nurkiewicz.asyncretry.AsyncRetryExecutor;
 import com.rabbitmq.client.GetResponse;
 
@@ -138,45 +128,11 @@ class Dequeuer {
                     .build())
                 .join();
 
-            MailImpl mail = new MailImpl(
-                dto.getName(),
-                dto.getSender().map(MailAddress::getMailSender).orElse(null),
-                dto.getRecipients()
-                    .stream()
-                    .map(Throwing.<String, MailAddress>function(MailAddress::new).sneakyThrow())
-                    .collect(Guavate.toImmutableList()),
-                mimeMessage);
-
-            mail.setErrorMessage(dto.getErrorMessage());
-            mail.setRemoteAddr(dto.getRemoteAddr());
-            mail.setRemoteHost(dto.getRemoteHost());
-            mail.setState(dto.getState());
-            dto.getLastUpdated()
-                .map(Instant::toEpochMilli)
-                .map(Date::new)
-                .ifPresent(mail::setLastUpdated);
-
-            dto.getAttributes()
-                .forEach((name, value) -> mail.setAttribute(name, SerializationUtil.<Serializable>deserialize(value)));
-
-            mail.addAllSpecificHeaderForRecipient(retrievePerRecipientHeaders(dto));
-
-            return mail;
+            return dto.toMailWithMimeMessage(mimeMessage);
         } catch (AddressException e) {
             throw new MailQueue.MailQueueException("Failed to parse mail address", e);
         } catch (MessagingException e) {
             throw new MailQueue.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;
-    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/0303205e/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 c4b9d28..628d76f 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
@@ -19,6 +19,7 @@
 
 package org.apache.james.queue.rabbitmq;
 
+import java.io.Serializable;
 import java.time.Instant;
 import java.util.Collection;
 import java.util.Date;
@@ -27,8 +28,13 @@ import java.util.Objects;
 import java.util.Optional;
 import java.util.stream.Stream;
 
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeMessage;
+
+import org.apache.commons.lang3.tuple.Pair;
 import org.apache.james.blob.mail.MimeMessagePartsId;
 import org.apache.james.core.MailAddress;
+import org.apache.james.server.core.MailImpl;
 import org.apache.james.util.SerializationUtil;
 import org.apache.james.util.streams.Iterators;
 import org.apache.mailet.Mail;
@@ -36,6 +42,7 @@ import org.apache.mailet.PerRecipientHeaders;
 
 import com.fasterxml.jackson.annotation.JsonCreator;
 import com.fasterxml.jackson.annotation.JsonProperty;
+import com.github.fge.lambdas.Throwing;
 import com.github.steveash.guavate.Guavate;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
@@ -178,6 +185,41 @@ class MailDTO {
         return bodyBlobId;
     }
 
+    MailImpl toMailWithMimeMessage(MimeMessage mimeMessage) throws MessagingException {
+        MailImpl mail = new MailImpl(name,
+            sender.map(MailAddress::getMailSender).orElse(null),
+            recipients.stream()
+                .map(Throwing.<String, MailAddress>function(MailAddress::new).sneakyThrow())
+                .collect(Guavate.toImmutableList()),
+            mimeMessage);
+
+        mail.setErrorMessage(errorMessage);
+        mail.setRemoteAddr(remoteAddr);
+        mail.setRemoteHost(remoteHost);
+        mail.setState(state);
+        lastUpdated
+            .map(Instant::toEpochMilli)
+            .map(Date::new)
+            .ifPresent(mail::setLastUpdated);
+
+        attributes
+            .forEach((name, value) -> mail.setAttribute(name, SerializationUtil.<Serializable>deserialize(value)));
+
+        mail.addAllSpecificHeaderForRecipient(retrievePerRecipientHeaders());
+
+        return mail;
+    }
+
+    private PerRecipientHeaders retrievePerRecipientHeaders() {
+        PerRecipientHeaders perRecipientHeaders = new PerRecipientHeaders();
+        this.perRecipientHeaders.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;
+    }
+
     @Override
     public final boolean equals(Object o) {
         if (o instanceof MailDTO) {


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