JAMES-2143 Introduce Blob API
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/6487016d Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/6487016d Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/6487016d Branch: refs/heads/master Commit: 6487016d05923440b9309945d0e32594c7fe9e03 Parents: b3c308e Author: benwa <btell...@linagora.com> Authored: Mon Sep 11 15:38:16 2017 +0700 Committer: Antoine Duprat <adup...@linagora.com> Committed: Wed Sep 13 10:19:53 2017 +0200 ---------------------------------------------------------------------- .../org/apache/james/mailbox/BlobManager.java | 29 +++++ .../exception/BlobNotFoundException.java | 36 ++++++ .../apache/james/mailbox/model/Attachment.java | 8 ++ .../org/apache/james/mailbox/model/Blob.java | 112 +++++++++++++++++++ .../org/apache/james/mailbox/model/BlobId.java | 66 +++++++++++ .../james/mailbox/model/AttachmentTest.java | 22 +++- .../apache/james/mailbox/model/BlobIdTest.java | 55 +++++++++ .../apache/james/mailbox/model/BlobTest.java | 84 ++++++++++++++ 8 files changed, 409 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/6487016d/mailbox/api/src/main/java/org/apache/james/mailbox/BlobManager.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/BlobManager.java b/mailbox/api/src/main/java/org/apache/james/mailbox/BlobManager.java new file mode 100644 index 0000000..ad453b7 --- /dev/null +++ b/mailbox/api/src/main/java/org/apache/james/mailbox/BlobManager.java @@ -0,0 +1,29 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.mailbox; + +import org.apache.james.mailbox.exception.BlobNotFoundException; +import org.apache.james.mailbox.exception.MailboxException; +import org.apache.james.mailbox.model.Blob; +import org.apache.james.mailbox.model.BlobId; + +public interface BlobManager { + Blob retrieve(BlobId blobId, MailboxSession mailboxSession) throws MailboxException, BlobNotFoundException; +} http://git-wip-us.apache.org/repos/asf/james-project/blob/6487016d/mailbox/api/src/main/java/org/apache/james/mailbox/exception/BlobNotFoundException.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/exception/BlobNotFoundException.java b/mailbox/api/src/main/java/org/apache/james/mailbox/exception/BlobNotFoundException.java new file mode 100644 index 0000000..f46a4d6 --- /dev/null +++ b/mailbox/api/src/main/java/org/apache/james/mailbox/exception/BlobNotFoundException.java @@ -0,0 +1,36 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.mailbox.exception; + +import org.apache.james.mailbox.model.BlobId; + +public class BlobNotFoundException extends RuntimeException { + + private final BlobId blobId; + + public BlobNotFoundException(BlobId blobId) { + super("Could not retrieve " + blobId.asString()); + this.blobId = blobId; + } + + public BlobId getBlobId() { + return blobId; + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/6487016d/mailbox/api/src/main/java/org/apache/james/mailbox/model/Attachment.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/model/Attachment.java b/mailbox/api/src/main/java/org/apache/james/mailbox/model/Attachment.java index e0f6ac2..a1321d9 100644 --- a/mailbox/api/src/main/java/org/apache/james/mailbox/model/Attachment.java +++ b/mailbox/api/src/main/java/org/apache/james/mailbox/model/Attachment.java @@ -116,6 +116,14 @@ public class Attachment { return bytes; } + public Blob toBlob() { + return Blob.builder() + .id(BlobId.fromString(attachmentId.getId())) + .payload(bytes) + .contentType(type) + .build(); + } + @Override public boolean equals(Object obj) { if (obj instanceof Attachment) { http://git-wip-us.apache.org/repos/asf/james-project/blob/6487016d/mailbox/api/src/main/java/org/apache/james/mailbox/model/Blob.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/model/Blob.java b/mailbox/api/src/main/java/org/apache/james/mailbox/model/Blob.java new file mode 100644 index 0000000..16a372e --- /dev/null +++ b/mailbox/api/src/main/java/org/apache/james/mailbox/model/Blob.java @@ -0,0 +1,112 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.mailbox.model; + +import java.util.Objects; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.MoreObjects; +import com.google.common.base.Preconditions; + +public class Blob { + + public static class Builder { + private BlobId blobId; + private byte[] payload; + private String contentType; + + private Builder() { + } + + public Builder id(BlobId id) { + this.blobId = id; + return this; + } + + public Builder payload(byte[] payload) { + this.payload = payload; + return this; + } + + public Builder contentType(String contentType) { + this.contentType = contentType; + return this; + } + + public Blob build() { + Preconditions.checkState(blobId != null, "id can not be empty"); + Preconditions.checkState(payload != null, "payload can not be empty"); + Preconditions.checkState(contentType != null, "contentType can not be empty"); + + return new Blob(blobId, payload, contentType); + } + } + + public static Builder builder() { + return new Builder(); + } + + private final BlobId blobId; + private final byte[] payload; + private final String contentType; + + @VisibleForTesting + Blob(BlobId blobId, byte[] payload, String contentType) { + this.blobId = blobId; + this.payload = payload; + this.contentType = contentType; + } + + public BlobId getBlobId() { + return blobId; + } + + public byte[] getPayload() { + return payload; + } + + public String getContentType() { + return contentType; + } + + @Override + public final boolean equals(Object o) { + if (o instanceof Blob) { + Blob blob = (Blob) o; + + return Objects.equals(this.blobId, blob.blobId) + && Objects.equals(this.contentType, blob.contentType); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hash(blobId, contentType); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("blobId", blobId) + .add("contentType", contentType) + .toString(); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/6487016d/mailbox/api/src/main/java/org/apache/james/mailbox/model/BlobId.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/model/BlobId.java b/mailbox/api/src/main/java/org/apache/james/mailbox/model/BlobId.java new file mode 100644 index 0000000..476eca2 --- /dev/null +++ b/mailbox/api/src/main/java/org/apache/james/mailbox/model/BlobId.java @@ -0,0 +1,66 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.mailbox.model; + +import java.util.Objects; + +import com.google.common.base.MoreObjects; +import com.google.common.base.Preconditions; +import com.google.common.base.Strings; + +public class BlobId { + public static BlobId fromString(String raw) { + Preconditions.checkArgument(!Strings.isNullOrEmpty(raw)); + return new BlobId(raw); + } + + private final String id; + + private BlobId(String id) { + this.id = id; + } + + public String asString() { + return id; + } + + @Override + public final boolean equals(Object o) { + if (o instanceof BlobId) { + BlobId blobId = (BlobId) o; + + return Objects.equals(this.id, blobId.id); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hash(id); + } + + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("id", id) + .toString(); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/6487016d/mailbox/api/src/test/java/org/apache/james/mailbox/model/AttachmentTest.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/model/AttachmentTest.java b/mailbox/api/src/test/java/org/apache/james/mailbox/model/AttachmentTest.java index 8edbdae..3235a7c 100644 --- a/mailbox/api/src/test/java/org/apache/james/mailbox/model/AttachmentTest.java +++ b/mailbox/api/src/test/java/org/apache/james/mailbox/model/AttachmentTest.java @@ -25,12 +25,11 @@ import static org.assertj.core.api.Assertions.assertThat; import java.io.InputStream; import java.nio.charset.Charset; -import com.google.common.base.Charsets; import org.apache.commons.io.IOUtils; -import org.apache.james.mailbox.model.Attachment; - import org.junit.Test; +import com.google.common.base.Charsets; + public class AttachmentTest { private static Charset CHARSET = Charsets.UTF_8; @@ -140,4 +139,21 @@ public class AttachmentTest { assertThat(attachment.getSize()).isEqualTo(input.getBytes(CHARSET).length); } + + @Test + public void toBlobShouldGenerateTheAttachmentBlob() throws Exception { + byte[] bytes = "mystream".getBytes(CHARSET); + String content = "content"; + Attachment attachment = Attachment.builder() + .bytes(bytes) + .type(content) + .build(); + Blob expected = Blob.builder() + .id(BlobId.fromString(attachment.getAttachmentId().getId())) + .contentType(content) + .payload(bytes) + .build(); + + assertThat(attachment.toBlob()).isEqualTo(expected); + } } http://git-wip-us.apache.org/repos/asf/james-project/blob/6487016d/mailbox/api/src/test/java/org/apache/james/mailbox/model/BlobIdTest.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/model/BlobIdTest.java b/mailbox/api/src/test/java/org/apache/james/mailbox/model/BlobIdTest.java new file mode 100644 index 0000000..fefeed0 --- /dev/null +++ b/mailbox/api/src/test/java/org/apache/james/mailbox/model/BlobIdTest.java @@ -0,0 +1,55 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.mailbox.model; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.junit.Test; + +import nl.jqno.equalsverifier.EqualsVerifier; + +public class BlobIdTest { + + @Test + public void shouldMatchBeanContact() { + EqualsVerifier.forClass(BlobId.class) + .allFieldsShouldBeUsed() + .verify(); + } + + @Test + public void fromStringShouldThrowOnNull() { + assertThatThrownBy(() -> BlobId.fromString(null)) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test + public void fromStringShouldThrowOnEmpty() { + assertThatThrownBy(() -> BlobId.fromString("")) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test + public void asStringShouldReturnUnderlyingId() { + assertThat(BlobId.fromString("abc").asString()) + .isEqualTo("abc"); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/6487016d/mailbox/api/src/test/java/org/apache/james/mailbox/model/BlobTest.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/model/BlobTest.java b/mailbox/api/src/test/java/org/apache/james/mailbox/model/BlobTest.java new file mode 100644 index 0000000..ed35795 --- /dev/null +++ b/mailbox/api/src/test/java/org/apache/james/mailbox/model/BlobTest.java @@ -0,0 +1,84 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.mailbox.model; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.nio.charset.StandardCharsets; + +import org.junit.Test; + +import nl.jqno.equalsverifier.EqualsVerifier; + +public class BlobTest { + + public static final BlobId ID = BlobId.fromString("123"); + public static final String CONTENT_TYPE = "text/plain"; + public static final byte[] PAYLOAD = "abc".getBytes(StandardCharsets.UTF_8); + + @Test + public void shouldMatchBeanContract() { + EqualsVerifier.forClass(Blob.class) + .verify(); + } + + @Test + public void buildShouldConstructValidBlob() { + assertThat( + Blob.builder() + .id(ID) + .contentType(CONTENT_TYPE) + .payload(PAYLOAD) + .build()) + .isEqualTo( + new Blob(ID, PAYLOAD, CONTENT_TYPE)); + } + + @Test + public void buildShouldThrowOnMissingBlobId() { + assertThatThrownBy(() -> + Blob.builder() + .contentType(CONTENT_TYPE) + .payload(PAYLOAD) + .build()) + .isInstanceOf(IllegalStateException.class); + } + + @Test + public void buildShouldThrowOnMissingContentType() { + assertThatThrownBy(() -> + Blob.builder() + .id(ID) + .payload(PAYLOAD) + .build()) + .isInstanceOf(IllegalStateException.class); + } + + @Test + public void buildShouldThrowOnMissingPayload() { + assertThatThrownBy(() -> + Blob.builder() + .id(ID) + .contentType(CONTENT_TYPE) + .build()) + .isInstanceOf(IllegalStateException.class); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org