Skip to content

Commit

Permalink
JAMES-2214 Improve Emailer and envelope design
Browse files Browse the repository at this point in the history
  • Loading branch information
chibenwa committed Nov 15, 2017
1 parent 944643b commit 5df4371
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 55 deletions.
Expand Up @@ -22,17 +22,26 @@
import java.util.Objects; import java.util.Objects;
import java.util.Optional; 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.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.MoreObjects; import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;


@JsonDeserialize(builder = Emailer.Builder.class) @JsonDeserialize(builder = Emailer.Builder.class)
public class Emailer { public class Emailer {


private static final Logger LOGGER = LoggerFactory.getLogger(Emailer.class);

public static Builder builder() { public static Builder builder() {
return new Builder(); return new Builder();
} }
Expand Down Expand Up @@ -117,6 +126,17 @@ public Optional<String> getEmail() {
return email; 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 @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (o instanceof Emailer) { if (o instanceof Emailer) {
Expand Down
Expand Up @@ -22,78 +22,53 @@
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Stream;

import javax.mail.internet.AddressException;


import org.apache.james.core.MailAddress; import org.apache.james.core.MailAddress;
import org.slf4j.Logger; import org.apache.james.util.StreamUtils;
import org.slf4j.LoggerFactory;


import com.github.steveash.guavate.Guavate;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;


public class Envelope { public class Envelope {
private static final Logger LOGGER = LoggerFactory.getLogger(Envelope.class);


public static Envelope fromMessage(Message jmapMessage) { public static Envelope fromMessage(Message jmapMessage) {
MailAddress sender = jmapMessage.getFrom() MailAddress sender = jmapMessage.getFrom()
.map(Envelope::emailerToMailAddress) .map(Emailer::toMailAddress)
.orElseThrow(() -> new RuntimeException("Sender is mandatory")); .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() return emailers.stream()
.map(Envelope::emailerToMailAddress) .map(Emailer::toMailAddress);
.collect(Collectors.toSet());
} }


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 MailAddress from;
private final Set<MailAddress> to; private final Set<MailAddress> recipients;
private final Set<MailAddress> cc;
private final Set<MailAddress> bcc;


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(from);
Preconditions.checkNotNull(to); Preconditions.checkNotNull(recipients);
Preconditions.checkNotNull(cc);
Preconditions.checkNotNull(bcc);


this.from = from; this.from = from;
this.to = to; this.recipients = recipients;
this.cc = cc;
this.bcc = bcc;
} }


public MailAddress getFrom() { public MailAddress getFrom() {
return from; return from;
} }


public Set<MailAddress> getTo() { public Set<MailAddress> getRecipients() {
return to; return recipients;
}

public Set<MailAddress> getCc() {
return cc;
}

public Set<MailAddress> getBcc() {
return bcc;
} }


@Override @Override
Expand All @@ -102,15 +77,13 @@ public final boolean equals(Object o) {
Envelope envelope = (Envelope) o; Envelope envelope = (Envelope) o;


return Objects.equals(this.from, envelope.from) return Objects.equals(this.from, envelope.from)
&& Objects.equals(this.to, envelope.to) && Objects.equals(this.recipients, envelope.recipients);
&& Objects.equals(this.cc, envelope.cc)
&& Objects.equals(this.bcc, envelope.bcc);
} }
return false; return false;
} }


@Override @Override
public final int hashCode() { public final int hashCode() {
return Objects.hash(from, to, cc, bcc); return Objects.hash(from, recipients);
} }
} }
Expand Up @@ -21,27 +21,23 @@


import javax.mail.MessagingException; import javax.mail.MessagingException;


import org.apache.james.core.MailAddress;
import org.apache.james.jmap.model.Envelope; import org.apache.james.jmap.model.Envelope;
import org.apache.james.jmap.model.MessageFactory.MetaDataWithContent; import org.apache.james.jmap.model.MessageFactory.MetaDataWithContent;
import org.apache.james.server.core.MailImpl; import org.apache.james.server.core.MailImpl;
import org.apache.mailet.Mail; import org.apache.mailet.Mail;


import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;


public class MailFactory { public class MailFactory {


@VisibleForTesting MailFactory() { @VisibleForTesting MailFactory() {
} }


public Mail build(MetaDataWithContent message, Envelope envelope) throws MessagingException { 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(), return new MailImpl(message.getMessageId().serialize(),
envelope.getFrom(), recipients, message.getContent()); envelope.getFrom(),
envelope.getRecipients(),
message.getContent());
} }


} }

0 comments on commit 5df4371

Please sign in to comment.