JAMES-2340 MailboxMessage used in event may be read multiple times
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/72dc3d12 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/72dc3d12 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/72dc3d12 Branch: refs/heads/master Commit: 72dc3d121d479d43cfd0edd011538f6b5888100e Parents: 0072b36 Author: Antoine Duprat <adup...@linagora.com> Authored: Fri Mar 23 14:52:45 2018 +0100 Committer: benwa <btell...@linagora.com> Committed: Tue Mar 27 15:17:38 2018 +0700 ---------------------------------------------------------------------- .../mailbox/store/ImmutableMailboxMessage.java | 30 +++-- .../store/ImmutableMailboxMessageTest.java | 113 +++++++++++++++++++ 2 files changed, 126 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/72dc3d12/mailbox/store/src/main/java/org/apache/james/mailbox/store/ImmutableMailboxMessage.java ---------------------------------------------------------------------- diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/ImmutableMailboxMessage.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/ImmutableMailboxMessage.java index 957ce3b..15330c7 100644 --- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/ImmutableMailboxMessage.java +++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/ImmutableMailboxMessage.java @@ -18,13 +18,14 @@ ****************************************************************/ package org.apache.james.mailbox.store; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; +import java.io.SequenceInputStream; import java.util.Date; import java.util.List; import javax.mail.Flags; -import javax.mail.util.SharedByteArrayInputStream; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.NotImplementedException; @@ -56,15 +57,14 @@ public class ImmutableMailboxMessage implements MailboxMessage { try { return new ImmutableMailboxMessage(message.getMessageId(), message.getInternalDate(), - copy(message.getBodyContent()), + IOUtils.toByteArray(message.getBodyContent()), message.getMediaType(), message.getSubType(), message.getBodyOctets(), message.getFullContentOctets(), message.getFullContentOctets() - message.getBodyOctets(), message.getTextualLineCount(), - copy(message.getHeaderContent()), - copy(message.getFullContent()), + IOUtils.toByteArray(message.getHeaderContent()), ImmutableList.copyOf(message.getProperties()), attachments(message), mailboxId, @@ -88,23 +88,18 @@ public class ImmutableMailboxMessage implements MailboxMessage { } return ImmutableList.of(); } - - private static SharedByteArrayInputStream copy(InputStream inputStream) throws IOException { - return new SharedByteArrayInputStream(IOUtils.toByteArray(inputStream)); - } } private final MessageId messageId; private final Date internalDate; - private final InputStream bodyContent; + private final byte[] bodyContent; private final String mediaType; private final String subType; private final long bodyOctets; private final long fullContentOctets; private final long headerOctets; private final Long textualLineCount; - private final InputStream headerContent; - private final InputStream fullContent; + private final byte[] headerContent; private final List<Property> properties; private final List<MessageAttachment> attachments; private final MailboxId mailboxId; @@ -118,8 +113,8 @@ public class ImmutableMailboxMessage implements MailboxMessage { private final boolean seen; private final String[] userFlags; - private ImmutableMailboxMessage(MessageId messageId, Date internalDate, InputStream bodyContent, String mediaType, String subType, long bodyOctets, long fullContentOctets, long headerOctets, Long textualLineCount, InputStream headerContent, - InputStream fullContent, List<Property> properties, List<MessageAttachment> attachments, MailboxId mailboxId, MessageUid uid, long modSeq, boolean answered, boolean deleted, boolean draft, boolean flagged, boolean recent, + private ImmutableMailboxMessage(MessageId messageId, Date internalDate, byte[] bodyContent, String mediaType, String subType, long bodyOctets, long fullContentOctets, long headerOctets, Long textualLineCount, byte[] headerContent, + List<Property> properties, List<MessageAttachment> attachments, MailboxId mailboxId, MessageUid uid, long modSeq, boolean answered, boolean deleted, boolean draft, boolean flagged, boolean recent, boolean seen, String[] userFlags) { this.messageId = messageId; this.internalDate = internalDate; @@ -131,7 +126,6 @@ public class ImmutableMailboxMessage implements MailboxMessage { this.headerOctets = headerOctets; this.textualLineCount = textualLineCount; this.headerContent = headerContent; - this.fullContent = fullContent; this.properties = properties; this.attachments = attachments; this.mailboxId = mailboxId; @@ -167,7 +161,7 @@ public class ImmutableMailboxMessage implements MailboxMessage { @Override public InputStream getBodyContent() { - return bodyContent; + return new ByteArrayInputStream(bodyContent); } @Override @@ -202,12 +196,14 @@ public class ImmutableMailboxMessage implements MailboxMessage { @Override public InputStream getHeaderContent() { - return headerContent; + return new ByteArrayInputStream(headerContent); } @Override public InputStream getFullContent() { - return fullContent; + return new SequenceInputStream( + new ByteArrayInputStream(headerContent), + new ByteArrayInputStream(bodyContent)); } @Override http://git-wip-us.apache.org/repos/asf/james-project/blob/72dc3d12/mailbox/store/src/test/java/org/apache/james/mailbox/store/ImmutableMailboxMessageTest.java ---------------------------------------------------------------------- diff --git a/mailbox/store/src/test/java/org/apache/james/mailbox/store/ImmutableMailboxMessageTest.java b/mailbox/store/src/test/java/org/apache/james/mailbox/store/ImmutableMailboxMessageTest.java new file mode 100644 index 0000000..8a48b14 --- /dev/null +++ b/mailbox/store/src/test/java/org/apache/james/mailbox/store/ImmutableMailboxMessageTest.java @@ -0,0 +1,113 @@ +/**************************************************************** + * 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.store; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.nio.charset.StandardCharsets; +import java.util.Date; +import java.util.EnumSet; + +import javax.mail.Flags; +import javax.mail.util.SharedByteArrayInputStream; + +import org.apache.commons.io.IOUtils; +import org.apache.james.mailbox.MailboxManager; +import org.apache.james.mailbox.MailboxManager.MessageCapabilities; +import org.apache.james.mailbox.model.TestId; +import org.apache.james.mailbox.store.mail.model.DefaultMessageId; +import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder; +import org.apache.james.mailbox.store.mail.model.impl.SimpleMailboxMessage; +import org.assertj.core.api.JUnitSoftAssertions; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +public class ImmutableMailboxMessageTest { + + private ImmutableMailboxMessage.Factory messageFactory; + + @Rule + public JUnitSoftAssertions softly = new JUnitSoftAssertions(); + + @Before + public void setup() { + MailboxManager mailboxManager = mock(MailboxManager.class); + when(mailboxManager.getSupportedMessageCapabilities()).thenReturn(EnumSet.noneOf(MessageCapabilities.class)); + + messageFactory = new ImmutableMailboxMessage.Factory(mailboxManager); + } + + @Test + public void fullContentMayBeReadMultipleTimes() throws Exception { + String fullContent = "Subject: Test1 \n\nBody1\n.\n"; + int bodyStartOctet = 16; + SimpleMailboxMessage simpleMailboxMessage = new SimpleMailboxMessage(new DefaultMessageId(), + new Date(), + fullContent.length(), + bodyStartOctet, + new SharedByteArrayInputStream(fullContent.getBytes(StandardCharsets.UTF_8)), + new Flags(), + new PropertyBuilder(), TestId.of(1)); + + ImmutableMailboxMessage message = messageFactory.from(TestId.of(1), simpleMailboxMessage); + + softly.assertThat(IOUtils.toString(message.getFullContent(), StandardCharsets.UTF_8)).isEqualTo(fullContent); + softly.assertThat(IOUtils.toString(message.getFullContent(), StandardCharsets.UTF_8)).isEqualTo(fullContent); + } + + @Test + public void headersMayBeReadMultipleTimes() throws Exception { + String fullContent = "Subject: Test1 \n\nBody1\n.\n"; + int bodyStartOctet = 16; + SimpleMailboxMessage simpleMailboxMessage = new SimpleMailboxMessage(new DefaultMessageId(), + new Date(), + fullContent.length(), + bodyStartOctet, + new SharedByteArrayInputStream(fullContent.getBytes(StandardCharsets.UTF_8)), + new Flags(), + new PropertyBuilder(), TestId.of(1)); + + ImmutableMailboxMessage message = messageFactory.from(TestId.of(1), simpleMailboxMessage); + + String expectedHeaders = "Subject: Test1 \n"; + softly.assertThat(IOUtils.toString(message.getHeaderContent(), StandardCharsets.UTF_8)).isEqualTo(expectedHeaders); + softly.assertThat(IOUtils.toString(message.getHeaderContent(), StandardCharsets.UTF_8)).isEqualTo(expectedHeaders); + } + + @Test + public void bodyMayBeReadMultipleTimes() throws Exception { + String fullContent = "Subject: Test1 \n\nBody1\n.\n"; + int bodyStartOctet = 16; + SimpleMailboxMessage simpleMailboxMessage = new SimpleMailboxMessage(new DefaultMessageId(), + new Date(), + fullContent.length(), + bodyStartOctet, + new SharedByteArrayInputStream(fullContent.getBytes(StandardCharsets.UTF_8)), + new Flags(), + new PropertyBuilder(), TestId.of(1)); + + ImmutableMailboxMessage message = messageFactory.from(TestId.of(1), simpleMailboxMessage); + + String expectedBody = "\nBody1\n.\n"; + softly.assertThat(IOUtils.toString(message.getBodyContent(), StandardCharsets.UTF_8)).isEqualTo(expectedBody); + softly.assertThat(IOUtils.toString(message.getBodyContent(), StandardCharsets.UTF_8)).isEqualTo(expectedBody); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org