From 5df4371bc064f7bdc764df65d7aaef538251afc3 Mon Sep 17 00:00:00 2001 From: benwa Date: Tue, 14 Nov 2017 16:49:10 +0700 Subject: [PATCH] JAMES-2214 Improve Emailer and envelope design --- .../org/apache/james/jmap/model/Emailer.java | 20 ++++++ .../org/apache/james/jmap/model/Envelope.java | 69 ++++++------------- .../apache/james/jmap/send/MailFactory.java | 10 +-- 3 files changed, 44 insertions(+), 55 deletions(-) diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Emailer.java b/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Emailer.java index db6a70167b2..5789939b922 100644 --- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Emailer.java +++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Emailer.java @@ -22,6 +22,12 @@ import java.util.Objects; import java.util.Optional; +import javax.mail.internet.AddressException; + +import org.apache.james.core.MailAddress; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; @@ -29,10 +35,13 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.base.MoreObjects; import com.google.common.base.Preconditions; +import com.google.common.base.Throwables; @JsonDeserialize(builder = Emailer.Builder.class) public class Emailer { + private static final Logger LOGGER = LoggerFactory.getLogger(Emailer.class); + public static Builder builder() { return new Builder(); } @@ -117,6 +126,17 @@ public Optional getEmail() { return email; } + @JsonIgnore + public MailAddress toMailAddress() { + Preconditions.checkArgument(email.isPresent(), "eMailer mail address should be present when sending a mail using JMAP"); + try { + return new MailAddress(email.get()); + } catch (AddressException e) { + LOGGER.error("Invalid mail address", email); + throw Throwables.propagate(e); + } + } + @Override public boolean equals(Object o) { if (o instanceof Emailer) { diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Envelope.java b/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Envelope.java index e823aca82f4..c4d80f941cf 100644 --- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Envelope.java +++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Envelope.java @@ -22,78 +22,53 @@ import java.util.List; import java.util.Objects; import java.util.Set; -import java.util.stream.Collectors; - -import javax.mail.internet.AddressException; +import java.util.stream.Stream; import org.apache.james.core.MailAddress; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.apache.james.util.StreamUtils; +import com.github.steveash.guavate.Guavate; import com.google.common.base.Preconditions; -import com.google.common.base.Throwables; public class Envelope { - private static final Logger LOGGER = LoggerFactory.getLogger(Envelope.class); public static Envelope fromMessage(Message jmapMessage) { MailAddress sender = jmapMessage.getFrom() - .map(Envelope::emailerToMailAddress) + .map(Emailer::toMailAddress) .orElseThrow(() -> new RuntimeException("Sender is mandatory")); - Set to = emailersToMailAddressSet(jmapMessage.getTo()); - Set cc = emailersToMailAddressSet(jmapMessage.getCc()); - Set bcc = emailersToMailAddressSet(jmapMessage.getBcc()); - return new Envelope(sender, to, cc, bcc); + Stream to = emailersToMailAddresses(jmapMessage.getTo()); + Stream cc = emailersToMailAddresses(jmapMessage.getCc()); + Stream bcc = emailersToMailAddresses(jmapMessage.getBcc()); + + return new Envelope(sender, + StreamUtils.flatten(Stream.of(to, cc, bcc)) + .collect(Guavate.toImmutableSet())); } - private static Set emailersToMailAddressSet(List emailers) { + private static Stream emailersToMailAddresses(List emailers) { return emailers.stream() - .map(Envelope::emailerToMailAddress) - .collect(Collectors.toSet()); + .map(Emailer::toMailAddress); } - private static MailAddress emailerToMailAddress(Emailer emailer) { - Preconditions.checkArgument(emailer.getEmail().isPresent(), "eMailer mail address should be present when sending a mail using JMAP"); - try { - return new MailAddress(emailer.getEmail().get()); - } catch (AddressException e) { - LOGGER.error("Invalid mail address", emailer.getEmail()); - throw Throwables.propagate(e); - } - } private final MailAddress from; - private final Set to; - private final Set cc; - private final Set bcc; + private final Set recipients; - private Envelope(MailAddress from, Set to, Set cc, Set bcc) { + private Envelope(MailAddress from, Set recipients) { Preconditions.checkNotNull(from); - Preconditions.checkNotNull(to); - Preconditions.checkNotNull(cc); - Preconditions.checkNotNull(bcc); + Preconditions.checkNotNull(recipients); this.from = from; - this.to = to; - this.cc = cc; - this.bcc = bcc; + this.recipients = recipients; } public MailAddress getFrom() { return from; } - public Set getTo() { - return to; - } - - public Set getCc() { - return cc; - } - - public Set getBcc() { - return bcc; + public Set getRecipients() { + return recipients; } @Override @@ -102,15 +77,13 @@ public final boolean equals(Object o) { Envelope envelope = (Envelope) o; return Objects.equals(this.from, envelope.from) - && Objects.equals(this.to, envelope.to) - && Objects.equals(this.cc, envelope.cc) - && Objects.equals(this.bcc, envelope.bcc); + && Objects.equals(this.recipients, envelope.recipients); } return false; } @Override public final int hashCode() { - return Objects.hash(from, to, cc, bcc); + return Objects.hash(from, recipients); } } diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/send/MailFactory.java b/server/protocols/jmap/src/main/java/org/apache/james/jmap/send/MailFactory.java index 405bab1fa7c..3d156d35bbc 100644 --- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/send/MailFactory.java +++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/send/MailFactory.java @@ -21,15 +21,12 @@ import javax.mail.MessagingException; -import org.apache.james.core.MailAddress; import org.apache.james.jmap.model.Envelope; import org.apache.james.jmap.model.MessageFactory.MetaDataWithContent; import org.apache.james.server.core.MailImpl; import org.apache.mailet.Mail; import com.google.common.annotations.VisibleForTesting; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Sets; public class MailFactory { @@ -37,11 +34,10 @@ public class MailFactory { } public Mail build(MetaDataWithContent message, Envelope envelope) throws MessagingException { - ImmutableSet recipients = Sets.union( - Sets.union(envelope.getTo(), envelope.getCc()), - envelope.getBcc()).immutableCopy(); return new MailImpl(message.getMessageId().serialize(), - envelope.getFrom(), recipients, message.getContent()); + envelope.getFrom(), + envelope.getRecipients(), + message.getContent()); } }