JAMES-2214 Improve Emailer and envelope design
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/5df4371b Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/5df4371b Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/5df4371b Branch: refs/heads/master Commit: 5df4371bc064f7bdc764df65d7aaef538251afc3 Parents: 944643b Author: benwa <[email protected]> Authored: Tue Nov 14 16:49:10 2017 +0700 Committer: benwa <[email protected]> Committed: Wed Nov 15 18:05:46 2017 +0700 ---------------------------------------------------------------------- .../org/apache/james/jmap/model/Emailer.java | 20 ++++++ .../org/apache/james/jmap/model/Envelope.java | 69 ++++++-------------- .../org/apache/james/jmap/send/MailFactory.java | 10 +-- 3 files changed, 44 insertions(+), 55 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/5df4371b/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Emailer.java ---------------------------------------------------------------------- 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 db6a701..5789939 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 @@ package org.apache.james.jmap.model; 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.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; 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 class Emailer { 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) { http://git-wip-us.apache.org/repos/asf/james-project/blob/5df4371b/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Envelope.java ---------------------------------------------------------------------- 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 e823aca..c4d80f9 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 @@ package org.apache.james.jmap.model; 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<MailAddress> to = emailersToMailAddressSet(jmapMessage.getTo()); - Set<MailAddress> cc = emailersToMailAddressSet(jmapMessage.getCc()); - Set<MailAddress> bcc = emailersToMailAddressSet(jmapMessage.getBcc()); - return new Envelope(sender, to, cc, bcc); + Stream<MailAddress> to = emailersToMailAddresses(jmapMessage.getTo()); + Stream<MailAddress> cc = emailersToMailAddresses(jmapMessage.getCc()); + Stream<MailAddress> bcc = emailersToMailAddresses(jmapMessage.getBcc()); + + return new Envelope(sender, + StreamUtils.flatten(Stream.of(to, cc, bcc)) + .collect(Guavate.toImmutableSet())); } - private static Set<MailAddress> emailersToMailAddressSet(List<Emailer> emailers) { + private static Stream<MailAddress> emailersToMailAddresses(List<Emailer> 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<MailAddress> to; - private final Set<MailAddress> cc; - private final Set<MailAddress> bcc; + private final Set<MailAddress> recipients; - private Envelope(MailAddress from, Set<MailAddress> to, Set<MailAddress> cc, Set<MailAddress> bcc) { + private Envelope(MailAddress from, Set<MailAddress> 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<MailAddress> getTo() { - return to; - } - - public Set<MailAddress> getCc() { - return cc; - } - - public Set<MailAddress> getBcc() { - return bcc; + public Set<MailAddress> getRecipients() { + return recipients; } @Override @@ -102,15 +77,13 @@ public class Envelope { 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); } } http://git-wip-us.apache.org/repos/asf/james-project/blob/5df4371b/server/protocols/jmap/src/main/java/org/apache/james/jmap/send/MailFactory.java ---------------------------------------------------------------------- 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 405bab1..3d156d3 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 @@ package org.apache.james.jmap.send; 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<MailAddress> 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()); } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
