Skip to content

Commit

Permalink
JAMES-2082 Introduce SimpleMailboxMessage builder
Browse files Browse the repository at this point in the history
  • Loading branch information
chibenwa authored and aduprat committed Jul 10, 2017
1 parent d27cfb1 commit 15e77ea
Show file tree
Hide file tree
Showing 5 changed files with 376 additions and 28 deletions.
Expand Up @@ -37,20 +37,20 @@ public class MessageWithoutAttachment {
private final MessageId messageId; private final MessageId messageId;
private final Date internalDate; private final Date internalDate;
private final Long size; private final Long size;
private final Integer boduSize; private final Integer bodySize;
private final SharedByteArrayInputStream content; private final SharedByteArrayInputStream content;
private final Flags flags; private final Flags flags;
private final PropertyBuilder propertyBuilder; private final PropertyBuilder propertyBuilder;
private final MailboxId mailboxId; private final MailboxId mailboxId;
private final MessageUid messageUid; private final MessageUid messageUid;
private final long modSeq; private final long modSeq;


public MessageWithoutAttachment(MessageId messageId, Date internalDate, Long size, Integer boduSize, SharedByteArrayInputStream content, public MessageWithoutAttachment(MessageId messageId, Date internalDate, Long size, Integer bodySize, SharedByteArrayInputStream content,
Flags flags, PropertyBuilder propertyBuilder, MailboxId mailboxId, MessageUid messageUid, long modSeq) { Flags flags, PropertyBuilder propertyBuilder, MailboxId mailboxId, MessageUid messageUid, long modSeq) {
this.messageId = messageId; this.messageId = messageId;
this.internalDate = internalDate; this.internalDate = internalDate;
this.size = size; this.size = size;
this.boduSize = boduSize; this.bodySize = bodySize;
this.content = content; this.content = content;
this.flags = flags; this.flags = flags;
this.propertyBuilder = propertyBuilder; this.propertyBuilder = propertyBuilder;
Expand All @@ -60,11 +60,19 @@ public MessageWithoutAttachment(MessageId messageId, Date internalDate, Long siz
} }


public SimpleMailboxMessage toMailboxMessage(List<MessageAttachment> attachments) { public SimpleMailboxMessage toMailboxMessage(List<MessageAttachment> attachments) {
SimpleMailboxMessage simpleMailboxMessage = new SimpleMailboxMessage(messageId, internalDate, size, boduSize, return SimpleMailboxMessage.builder()
content, flags, propertyBuilder, mailboxId, attachments); .messageId(messageId)
simpleMailboxMessage.setUid(messageUid); .mailboxId(mailboxId)
simpleMailboxMessage.setModSeq(modSeq); .uid(messageUid)
return simpleMailboxMessage; .modseq(modSeq)
.internalDate(internalDate)
.bodyStartOctet(bodySize)
.size(size)
.content(content)
.flags(flags)
.propertyBuilder(propertyBuilder)
.addAttachments(attachments)
.build();
} }


public MailboxId getMailboxId() { public MailboxId getMailboxId() {
Expand Down
Expand Up @@ -43,6 +43,7 @@
import org.apache.james.mailbox.store.mail.model.impl.SimpleMailboxMessage; import org.apache.james.mailbox.store.mail.model.impl.SimpleMailboxMessage;


import com.github.steveash.guavate.Guavate; import com.github.steveash.guavate.Guavate;
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;


import org.junit.After; import org.junit.After;
Expand Down Expand Up @@ -142,22 +143,24 @@ public void saveShouldStoreMessageWithAttachmentButNoCid() throws Exception {
} }


private SimpleMailboxMessage createMessage( private SimpleMailboxMessage createMessage(
MessageId messageId, MessageId messageId,
String content, String content,
int bodyStart, int bodyStart,
PropertyBuilder propertyBuilder, PropertyBuilder propertyBuilder,
List<MessageAttachment> attachments List<MessageAttachment> attachments) {
) {
return new SimpleMailboxMessage( return SimpleMailboxMessage.builder()
messageId, .messageId(messageId)
new Date(), .mailboxId(MAILBOX_ID)
content.length(), .uid(messageUid)
bodyStart, .internalDate(new Date())
new SharedByteArrayInputStream(content.getBytes()), .bodyStartOctet(bodyStart)
new Flags(), .size(content.length())
propertyBuilder, .content(new SharedByteArrayInputStream(content.getBytes(Charsets.UTF_8)))
MAILBOX_ID, .flags(new Flags())
attachments); .propertyBuilder(propertyBuilder)
.addAttachments(attachments)
.build();
} }


} }
Expand Up @@ -167,9 +167,17 @@ public void saveShouldStoreMessageWithHeaderContent() throws Exception {
} }


private SimpleMailboxMessage createMessage(MessageId messageId, String content, int bodyStart, PropertyBuilder propertyBuilder) { private SimpleMailboxMessage createMessage(MessageId messageId, String content, int bodyStart, PropertyBuilder propertyBuilder) {
return new SimpleMailboxMessage(messageId, new Date(), content.length(), bodyStart, return SimpleMailboxMessage.builder()
new SharedByteArrayInputStream(content.getBytes(Charsets.UTF_8)), new Flags(), .messageId(messageId)
propertyBuilder, MAILBOX_ID); .mailboxId(MAILBOX_ID)
.uid(messageUid)
.internalDate(new Date())
.bodyStartOctet(bodyStart)
.size(content.length())
.content(new SharedByteArrayInputStream(content.getBytes(Charsets.UTF_8)))
.flags(new Flags())
.propertyBuilder(propertyBuilder)
.build();
} }


private MessageWithoutAttachment toMessage(CompletableFuture<Stream<CassandraMessageDAOV2.MessageResult>> readOptional) throws InterruptedException, java.util.concurrent.ExecutionException { private MessageWithoutAttachment toMessage(CompletableFuture<Stream<CassandraMessageDAOV2.MessageResult>> readOptional) throws InterruptedException, java.util.concurrent.ExecutionException {
Expand Down
Expand Up @@ -20,6 +20,7 @@
package org.apache.james.mailbox.store.mail.model.impl; package org.apache.james.mailbox.store.mail.model.impl;


import java.io.IOException; import java.io.IOException;
import java.util.Collection;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;


Expand All @@ -38,11 +39,112 @@


import com.google.common.base.MoreObjects; import com.google.common.base.MoreObjects;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.primitives.Ints; import com.google.common.primitives.Ints;


public class SimpleMailboxMessage extends DelegatingMailboxMessage { public class SimpleMailboxMessage extends DelegatingMailboxMessage {


public static Builder builder() {
return new Builder();
}

public static class Builder {
private MessageId messageId;
private Date internalDate;
private Long size;
private Integer bodyStartOctet;
private SharedInputStream content;
private Flags flags;
private PropertyBuilder propertyBuilder;
private MailboxId mailboxId;
private Optional<MessageUid> uid = Optional.absent();
private Optional<Long> modseq = Optional.absent();
private ImmutableList.Builder<MessageAttachment> attachments = ImmutableList.builder();

public Builder messageId(MessageId messageId) {
this.messageId = messageId;
return this;
}

public Builder uid(MessageUid messageUid) {
this.uid = Optional.of(messageUid);
return this;
}

public Builder modseq(long modseq) {
Preconditions.checkArgument(modseq >= 0, "modseq can not be negative");
this.modseq = Optional.of(modseq);
return this;
}

public Builder internalDate(Date internalDate) {
this.internalDate = internalDate;
return this;
}

public Builder size(long size) {
Preconditions.checkArgument(size >= 0, "size can not be negative");
this.size = size;
return this;
}

public Builder bodyStartOctet(int bodyStartOctet) {
Preconditions.checkArgument(bodyStartOctet >= 0, "bodyStartOctet can not be negative");
this.bodyStartOctet = bodyStartOctet;
return this;
}

public Builder content(SharedInputStream content) {
this.content = content;
return this;
}

public Builder flags(Flags flags) {
this.flags = flags;
return this;
}

public Builder propertyBuilder(PropertyBuilder propertyBuilder) {
this.propertyBuilder = propertyBuilder;
return this;
}

public Builder mailboxId(MailboxId mailboxId) {
this.mailboxId = mailboxId;
return this;
}

public Builder addAttachments(Collection<MessageAttachment> attachments) {
this.attachments.addAll(attachments);
return this;
}

public SimpleMailboxMessage build() {
Preconditions.checkNotNull(messageId, "messageId is required");
Preconditions.checkNotNull(internalDate, "internalDate is required");
Preconditions.checkNotNull(size, "size is required");
Preconditions.checkNotNull(bodyStartOctet, "bodyStartOctet is required");
Preconditions.checkNotNull(content, "content is required");
Preconditions.checkNotNull(flags, "flags is required");
Preconditions.checkNotNull(propertyBuilder, "propertyBuilder is required");
Preconditions.checkNotNull(mailboxId, "mailboxId is required");

SimpleMailboxMessage simpleMailboxMessage = new SimpleMailboxMessage(messageId, internalDate, size,
bodyStartOctet, content, flags, propertyBuilder, mailboxId, attachments.build());

if (uid.isPresent()) {
simpleMailboxMessage.setUid(uid.get());
}

if (modseq.isPresent()) {
simpleMailboxMessage.setModSeq(modseq.get());
}
return simpleMailboxMessage;
}
}

public static SimpleMailboxMessage copy(MailboxId mailboxId, MailboxMessage original) throws MailboxException { public static SimpleMailboxMessage copy(MailboxId mailboxId, MailboxMessage original) throws MailboxException {
return copy(mailboxId, original, original.getAttachments()); return copy(mailboxId, original, original.getAttachments());
} }
Expand Down

0 comments on commit 15e77ea

Please sign in to comment.