JAMES-1777 Add name in Attachment table
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/9d0b3b41 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/9d0b3b41 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/9d0b3b41 Branch: refs/heads/master Commit: 9d0b3b41954f08831e9feab6a0028adda1e98bb6 Parents: f643c18 Author: Antoine Duprat <[email protected]> Authored: Thu Jun 23 21:47:28 2016 +0200 Committer: Antoine Duprat <[email protected]> Committed: Fri Jul 1 16:32:41 2016 +0200 ---------------------------------------------------------------------- .../mail/CassandraAttachmentMapper.java | 5 +- .../modules/CassandraAttachmentModule.java | 1 + .../table/CassandraAttachmentTable.java | 3 +- .../mailbox/store/mail/model/Attachment.java | 47 +++++++++++------ .../store/mail/model/impl/MessageParser.java | 5 +- .../store/mail/model/AttachmentMapperTest.java | 18 +++++-- .../store/mail/model/AttachmentTest.java | 54 ++++++++++++++------ .../james/jmap/model/MailboxMessageTest.java | 1 - 8 files changed, 94 insertions(+), 40 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/9d0b3b41/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentMapper.java ---------------------------------------------------------------------- diff --git a/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentMapper.java b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentMapper.java index c36d824..fcbfec8 100644 --- a/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentMapper.java +++ b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentMapper.java @@ -24,6 +24,7 @@ import static com.datastax.driver.core.querybuilder.QueryBuilder.insertInto; import static com.datastax.driver.core.querybuilder.QueryBuilder.select; import static org.apache.james.mailbox.cassandra.table.CassandraAttachmentTable.FIELDS; import static org.apache.james.mailbox.cassandra.table.CassandraAttachmentTable.ID; +import static org.apache.james.mailbox.cassandra.table.CassandraAttachmentTable.NAME; import static org.apache.james.mailbox.cassandra.table.CassandraAttachmentTable.PAYLOAD; import static org.apache.james.mailbox.cassandra.table.CassandraAttachmentTable.SIZE; import static org.apache.james.mailbox.cassandra.table.CassandraAttachmentTable.TABLE_NAME; @@ -46,6 +47,7 @@ import com.datastax.driver.core.Row; import com.datastax.driver.core.Session; import com.github.fge.lambdas.Throwing; import com.github.fge.lambdas.ThrownByLambdaException; +import com.google.common.base.Optional; import com.google.common.base.Preconditions; public class CassandraAttachmentMapper implements AttachmentMapper { @@ -82,7 +84,7 @@ public class CassandraAttachmentMapper implements AttachmentMapper { .attachmentId(AttachmentId.from(row.getString(ID))) .bytes(row.getBytes(PAYLOAD).array()) .type(row.getString(TYPE)) - .size(row.getLong(SIZE)) + .name(Optional.fromNullable(row.getString(NAME))) .build(); } @@ -101,6 +103,7 @@ public class CassandraAttachmentMapper implements AttachmentMapper { .value(ID, attachment.getAttachmentId().getId()) .value(PAYLOAD, ByteBuffer.wrap(IOUtils.toByteArray(attachment.getStream()))) .value(TYPE, attachment.getType()) + .value(NAME, attachment.getName().orNull()) .value(SIZE, attachment.getSize()) ); } http://git-wip-us.apache.org/repos/asf/james-project/blob/9d0b3b41/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/modules/CassandraAttachmentModule.java ---------------------------------------------------------------------- diff --git a/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/modules/CassandraAttachmentModule.java b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/modules/CassandraAttachmentModule.java index 0108161..b26fe71 100644 --- a/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/modules/CassandraAttachmentModule.java +++ b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/modules/CassandraAttachmentModule.java @@ -48,6 +48,7 @@ public class CassandraAttachmentModule implements CassandraModule { .addPartitionKey(CassandraAttachmentTable.ID, text()) .addColumn(CassandraAttachmentTable.PAYLOAD, blob()) .addColumn(CassandraAttachmentTable.TYPE, text()) + .addColumn(CassandraAttachmentTable.NAME, text()) .addColumn(CassandraAttachmentTable.SIZE, bigint()))); index = Collections.emptyList(); types = Collections.emptyList(); http://git-wip-us.apache.org/repos/asf/james-project/blob/9d0b3b41/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/table/CassandraAttachmentTable.java ---------------------------------------------------------------------- diff --git a/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/table/CassandraAttachmentTable.java b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/table/CassandraAttachmentTable.java index a7115eb..17158d1 100644 --- a/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/table/CassandraAttachmentTable.java +++ b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/table/CassandraAttachmentTable.java @@ -25,7 +25,8 @@ public interface CassandraAttachmentTable { String ID = "id"; String PAYLOAD = "payload"; String TYPE = "type"; + String NAME = "name"; String SIZE = "size"; - String[] FIELDS = { ID, PAYLOAD, TYPE, SIZE }; + String[] FIELDS = { ID, PAYLOAD, TYPE, NAME, SIZE }; } http://git-wip-us.apache.org/repos/asf/james-project/blob/9d0b3b41/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/model/Attachment.java ---------------------------------------------------------------------- diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/model/Attachment.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/model/Attachment.java index 5af2b28..6205647 100644 --- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/model/Attachment.java +++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/model/Attachment.java @@ -26,6 +26,7 @@ import java.util.Arrays; import com.google.common.base.MoreObjects; import com.google.common.base.Objects; +import com.google.common.base.Optional; import com.google.common.base.Preconditions; import com.google.common.base.Strings; @@ -35,23 +36,15 @@ public class Attachment { return new Builder(); } - public static Attachment from(byte[] bytes, String type) { - return builder() - .attachmentId(AttachmentId.forPayload(bytes)) - .bytes(bytes) - .type(type) - .size(bytes.length) - .build(); - } - public static class Builder { private AttachmentId attachmentId; private byte[] bytes; private String type; - private Long size; + private Optional<String> name; private Builder() { + name = Optional.absent(); } public Builder attachmentId(AttachmentId attachmentId) { @@ -72,29 +65,43 @@ public class Attachment { return this; } - public Builder size(long size) { - this.size = size; + public Builder name(Optional<String> name) { + Preconditions.checkArgument(name != null); + this.name = name; return this; } public Attachment build() { - Preconditions.checkState(attachmentId != null, "'attachmentId' is mandatory"); Preconditions.checkState(bytes != null, "'bytes' is mandatory"); + AttachmentId builtAttachmentId = attachmentId(); + Preconditions.checkState(builtAttachmentId != null, "'attachmentId' is mandatory"); Preconditions.checkState(type != null, "'type' is mandatory"); - Preconditions.checkState(size != null, "'size' is mandatory"); - return new Attachment(bytes, attachmentId, type, size); + return new Attachment(bytes, builtAttachmentId, type, name, size()); + } + + private AttachmentId attachmentId() { + if (attachmentId != null) { + return attachmentId; + } + return AttachmentId.forPayload(bytes); + } + + private long size() { + return bytes.length; } } private final byte[] bytes; private final AttachmentId attachmentId; private final String type; + private final Optional<String> name; private final long size; - private Attachment(byte[] bytes, AttachmentId attachmentId, String type, long size) { + private Attachment(byte[] bytes, AttachmentId attachmentId, String type, Optional<String> name, long size) { this.bytes = bytes; this.attachmentId = attachmentId; this.type = type; + this.name = name; this.size = size; } @@ -106,6 +113,10 @@ public class Attachment { return type; } + public Optional<String> getName() { + return name; + } + public long getSize() { return size; } @@ -121,6 +132,7 @@ public class Attachment { return Objects.equal(attachmentId, other.attachmentId) && Arrays.equals(bytes, other.bytes) && Objects.equal(type, other.type) + && Objects.equal(name, other.name) && Objects.equal(size, other.size); } return false; @@ -128,7 +140,7 @@ public class Attachment { @Override public int hashCode() { - return Objects.hashCode(attachmentId, bytes, type, size); + return Objects.hashCode(attachmentId, bytes, type, name, size); } @Override @@ -138,6 +150,7 @@ public class Attachment { .add("attachmentId", attachmentId) .add("bytes", bytes) .add("type", type) + .add("name", name) .add("size", size) .toString(); } http://git-wip-us.apache.org/repos/asf/james-project/blob/9d0b3b41/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/model/impl/MessageParser.java ---------------------------------------------------------------------- diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/model/impl/MessageParser.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/model/impl/MessageParser.java index c784474..be342d8 100644 --- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/model/impl/MessageParser.java +++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/model/impl/MessageParser.java @@ -96,6 +96,9 @@ public class MessageParser { ByteArrayOutputStream out = new ByteArrayOutputStream(); messageWriter.writeBody(body, out); byte[] bytes = out.toByteArray(); - return Attachment.from(bytes, contentType); + return Attachment.builder() + .bytes(bytes) + .type(contentType) + .build(); } } http://git-wip-us.apache.org/repos/asf/james-project/blob/9d0b3b41/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/AttachmentMapperTest.java ---------------------------------------------------------------------- diff --git a/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/AttachmentMapperTest.java b/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/AttachmentMapperTest.java index 0fbfe1a..39b26e6 100644 --- a/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/AttachmentMapperTest.java +++ b/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/AttachmentMapperTest.java @@ -31,6 +31,7 @@ import org.xenei.junit.contract.Contract; import org.xenei.junit.contract.ContractTest; import org.xenei.junit.contract.IProducer; +import com.google.common.base.Charsets; import com.google.common.collect.ImmutableList; @Contract(MapperProvider.class) @@ -62,13 +63,16 @@ public class AttachmentMapperTest<T extends MapperProvider> { @ContractTest public void getAttachmentShouldThrowWhenNonReferencedAttachmentId() throws Exception { expected.expect(AttachmentNotFoundException.class); - attachmentMapper.getAttachment(AttachmentId.forPayload("unknown".getBytes())); + attachmentMapper.getAttachment(AttachmentId.forPayload("unknown".getBytes(Charsets.UTF_8))); } @ContractTest public void getAttachmentShouldReturnTheAttachmentWhenReferenced() throws Exception { //Given - Attachment expected = Attachment.from("payload".getBytes(), "content"); + Attachment expected = Attachment.builder() + .bytes("payload".getBytes(Charsets.UTF_8)) + .type("content") + .build(); AttachmentId attachmentId = expected.getAttachmentId(); attachmentMapper.storeAttachment(expected); //When @@ -80,8 +84,14 @@ public class AttachmentMapperTest<T extends MapperProvider> { @ContractTest public void getAttachmentShouldReturnTheAttachmentsWhenMultipleStored() throws Exception { //Given - Attachment expected1 = Attachment.from("payload1".getBytes(), "content1"); - Attachment expected2 = Attachment.from("payload2".getBytes(), "content2"); + Attachment expected1 = Attachment.builder() + .bytes("payload1".getBytes(Charsets.UTF_8)) + .type("content1") + .build(); + Attachment expected2 = Attachment.builder() + .bytes("payload2".getBytes(Charsets.UTF_8)) + .type("content2") + .build(); AttachmentId attachmentId1 = expected1.getAttachmentId(); AttachmentId attachmentId2 = expected2.getAttachmentId(); //When http://git-wip-us.apache.org/repos/asf/james-project/blob/9d0b3b41/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/AttachmentTest.java ---------------------------------------------------------------------- diff --git a/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/AttachmentTest.java b/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/AttachmentTest.java index 5e96c7d..f3505f4 100644 --- a/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/AttachmentTest.java +++ b/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/AttachmentTest.java @@ -27,12 +27,17 @@ import java.io.InputStream; import org.apache.commons.io.IOUtils; import org.junit.Test; +import com.google.common.base.Optional; + public class AttachmentTest { @Test public void streamShouldBeConsumedOneTime() throws Exception { String input = "mystream"; - Attachment attachment = Attachment.from(input.getBytes(), "content"); + Attachment attachment = Attachment.builder() + .bytes(input.getBytes()) + .type("content") + .build(); InputStream stream = attachment.getStream(); assertThat(stream).isNotNull(); @@ -42,7 +47,10 @@ public class AttachmentTest { @Test public void streamShouldBeConsumedMoreThanOneTime() throws Exception { String input = "mystream"; - Attachment attachment = Attachment.from(input.getBytes(), "content"); + Attachment attachment = Attachment.builder() + .bytes(input.getBytes()) + .type("content") + .build(); attachment.getStream(); InputStream stream = attachment.getStream(); @@ -74,6 +82,12 @@ public class AttachmentTest { .type(""); } + @Test (expected = IllegalArgumentException.class) + public void builderShouldThrowWhenNameIsNull() { + Attachment.builder() + .name(null); + } + @Test (expected = IllegalStateException.class) public void buildShouldThrowWhenAttachmentIdIsNotProvided() { Attachment.builder().build(); @@ -94,29 +108,39 @@ public class AttachmentTest { .build(); } - @Test (expected = IllegalStateException.class) - public void buildShouldThrowWhenSizeIsNotProvided() { - Attachment.builder() - .attachmentId(AttachmentId.forPayload("mystream".getBytes())) - .bytes("mystream".getBytes()) - .type("content") - .build(); - } - @Test - public void fromShouldSetTheAttachmentId() throws Exception { + public void buildShouldSetTheAttachmentId() throws Exception { byte[] bytes = "mystream".getBytes(); - Attachment attachment = Attachment.from(bytes, "content"); + Attachment attachment = Attachment.builder() + .bytes(bytes) + .type("content") + .build(); AttachmentId expected = AttachmentId.forPayload(bytes); assertThat(attachment.getAttachmentId()).isEqualTo(expected); } @Test - public void fromShouldSetTheSize() throws Exception { + public void buildShouldSetTheSize() throws Exception { String input = "mystream"; - Attachment attachment = Attachment.from(input.getBytes(), "content"); + Attachment attachment = Attachment.builder() + .bytes(input.getBytes()) + .type("content") + .build(); assertThat(attachment.getSize()).isEqualTo(input.getBytes().length); } + + @Test + public void buildShouldSetTheName() throws Exception { + String input = "mystream"; + Optional<String> expectedName = Optional.of("myName"); + Attachment attachment = Attachment.builder() + .bytes(input.getBytes()) + .type("content") + .name(expectedName) + .build(); + + assertThat(attachment.getName()).isEqualTo(expectedName); + } } http://git-wip-us.apache.org/repos/asf/james-project/blob/9d0b3b41/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/MailboxMessageTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/MailboxMessageTest.java b/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/MailboxMessageTest.java index f356460..eae0c49 100644 --- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/MailboxMessageTest.java +++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/MailboxMessageTest.java @@ -422,7 +422,6 @@ public class MailboxMessageTest { .attachmentId(AttachmentId.from(blodId)) .bytes(payload.getBytes()) .type(type) - .size(payload.length()) .build()), x -> MessageId.of("user|box|" + x)); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
