This is an automated email from the ASF dual-hosted git repository. rouazana pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit c3e9b50aa93028032be50d01c14d456a5b5a1d6d Author: RĂ©mi Kowalski <rkowal...@linagora.com> AuthorDate: Tue Mar 12 10:13:04 2019 +0100 JAMES-2681 change archive api to accept MessageResults in place of MailboxMessages --- .../james/mailbox/backup/ArchiveService.java | 6 ++--- .../{ArchiveService.java => MailboxBackup.java} | 14 +++++----- .../org/apache/james/mailbox/backup/Zipper.java | 30 ++++++++++++++++------ .../apache/james/mailbox/backup/ZipperTest.java | 18 ++++++++++--- 4 files changed, 46 insertions(+), 22 deletions(-) diff --git a/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/ArchiveService.java b/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/ArchiveService.java index 2789691..c284c14 100644 --- a/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/ArchiveService.java +++ b/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/ArchiveService.java @@ -23,14 +23,14 @@ import java.io.OutputStream; import java.util.List; import java.util.stream.Stream; -import org.apache.james.mailbox.store.mail.model.MailboxMessage; +import org.apache.james.mailbox.model.MessageResult; public interface ArchiveService { /** * @param mailboxes list of mailboxes and their annotations to be stored in the archive - * @param messages a stream of MailboxMessages that will be consumed + * @param messages a stream of MessageResult that will be consumed * @param destination an OutputStream in which the zip will be written */ - void archive(List<MailboxWithAnnotations> mailboxes, Stream<MailboxMessage> messages, OutputStream destination) throws IOException; + void archive(List<MailboxWithAnnotations> mailboxes, Stream<MessageResult> messages, OutputStream destination) throws IOException; } diff --git a/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/ArchiveService.java b/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/MailboxBackup.java similarity index 71% copy from mailbox/backup/src/main/java/org/apache/james/mailbox/backup/ArchiveService.java copy to mailbox/backup/src/main/java/org/apache/james/mailbox/backup/MailboxBackup.java index 2789691..1b3151e 100644 --- a/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/ArchiveService.java +++ b/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/MailboxBackup.java @@ -20,17 +20,15 @@ package org.apache.james.mailbox.backup; import java.io.IOException; import java.io.OutputStream; -import java.util.List; -import java.util.stream.Stream; -import org.apache.james.mailbox.store.mail.model.MailboxMessage; +import org.apache.james.core.User; +import org.apache.james.mailbox.exception.MailboxException; -public interface ArchiveService { +public interface MailboxBackup { /** - * @param mailboxes list of mailboxes and their annotations to be stored in the archive - * @param messages a stream of MailboxMessages that will be consumed - * @param destination an OutputStream in which the zip will be written + * @param user the user account to export */ - void archive(List<MailboxWithAnnotations> mailboxes, Stream<MailboxMessage> messages, OutputStream destination) throws IOException; + void backupAccount(User user, OutputStream destination) throws IOException, MailboxException; + } diff --git a/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/Zipper.java b/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/Zipper.java index 370dbb4..bf003b6 100644 --- a/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/Zipper.java +++ b/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/Zipper.java @@ -20,6 +20,7 @@ package org.apache.james.mailbox.backup; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; @@ -30,9 +31,13 @@ import org.apache.commons.compress.archivers.zip.ExtraFieldUtils; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import org.apache.commons.io.IOUtils; +import org.apache.james.mailbox.exception.MailboxException; +import org.apache.james.mailbox.model.Content; import org.apache.james.mailbox.model.MailboxAnnotation; +import org.apache.james.mailbox.model.MessageResult; import org.apache.james.mailbox.store.mail.model.Mailbox; -import org.apache.james.mailbox.store.mail.model.MailboxMessage; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import com.github.fge.lambdas.Throwing; import com.google.common.base.Charsets; @@ -41,6 +46,7 @@ public class Zipper implements ArchiveService { private static final String ANNOTATION_DIRECTORY = "annotations"; private static final boolean AUTO_FLUSH = true; + private static final Logger LOGGER = LoggerFactory.getLogger(Zipper.class); public Zipper() { ExtraFieldUtils.register(SizeExtraField.class); @@ -53,7 +59,7 @@ public class Zipper implements ArchiveService { } @Override - public void archive(List<MailboxWithAnnotations> mailboxes, Stream<MailboxMessage> messages, OutputStream destination) throws IOException { + public void archive(List<MailboxWithAnnotations> mailboxes, Stream<MessageResult> messages, OutputStream destination) throws IOException { try (ZipArchiveOutputStream archiveOutputStream = new ZipArchiveOutputStream(destination)) { storeMailboxes(mailboxes, archiveOutputStream); storeMessages(messages, archiveOutputStream); @@ -67,8 +73,8 @@ public class Zipper implements ArchiveService { ).sneakyThrow()); } - private void storeMessages(Stream<MailboxMessage> messages, ZipArchiveOutputStream archiveOutputStream) throws IOException { - messages.forEach(Throwing.<MailboxMessage>consumer(message -> + private void storeMessages(Stream<MessageResult> messages, ZipArchiveOutputStream archiveOutputStream) throws IOException { + messages.forEach(Throwing.<MessageResult>consumer(message -> storeInArchive(message, archiveOutputStream) ).sneakyThrow()); } @@ -115,19 +121,27 @@ public class Zipper implements ArchiveService { archiveOutputStream.closeArchiveEntry(); } - private void storeInArchive(MailboxMessage message, ZipArchiveOutputStream archiveOutputStream) throws IOException { + private void storeInArchive(MessageResult message, ZipArchiveOutputStream archiveOutputStream) throws IOException { String entryId = message.getMessageId().serialize(); ZipArchiveEntry archiveEntry = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new File(entryId), entryId); - archiveEntry.addExtraField(new SizeExtraField(message.getFullContentOctets())); + archiveEntry.addExtraField(new SizeExtraField(message.getSize())); archiveEntry.addExtraField(new UidExtraField(message.getUid().asLong())); archiveEntry.addExtraField(new MessageIdExtraField(message.getMessageId().serialize())); archiveEntry.addExtraField(new MailboxIdExtraField(message.getMailboxId().serialize())); archiveEntry.addExtraField(new InternalDateExtraField(message.getInternalDate())); - archiveEntry.addExtraField(new FlagsExtraField(message.createFlags())); + archiveEntry.addExtraField(new FlagsExtraField(message.getFlags())); archiveOutputStream.putArchiveEntry(archiveEntry); - IOUtils.copy(message.getFullContent(), archiveOutputStream); + try { + Content content = message.getFullContent(); + try (InputStream stream = content.getInputStream()) { + IOUtils.copy(stream, archiveOutputStream); + } + } catch (MailboxException e) { + LOGGER.error("Error while storing message in archive", e); + } + archiveOutputStream.closeArchiveEntry(); } } diff --git a/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipperTest.java b/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipperTest.java index a7c4ea0..699769a 100644 --- a/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipperTest.java +++ b/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipperTest.java @@ -46,6 +46,9 @@ import java.io.ByteArrayOutputStream; import java.util.List; import java.util.stream.Stream; +import org.apache.james.mailbox.model.MessageResult; +import org.apache.james.mailbox.store.MessageResultImpl; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -60,6 +63,15 @@ class ZipperTest { private Zipper testee; private ByteArrayOutputStream output; + private static MessageResult MESSAGE_RESULT_1; + private static MessageResult MESSAGE_RESULT_2; + + @BeforeAll + static void beforeAll() throws Exception { + MESSAGE_RESULT_1 = new MessageResultImpl(MESSAGE_1); + MESSAGE_RESULT_2 = new MessageResultImpl(MESSAGE_2); + } + @BeforeEach void beforeEach() { testee = new Zipper(); @@ -76,7 +88,7 @@ class ZipperTest { @Test void archiveShouldWriteOneMessageWhenOne() throws Exception { - testee.archive(NO_MAILBOXES, Stream.of(MESSAGE_1), output); + testee.archive(NO_MAILBOXES, Stream.of(new MessageResultImpl(MESSAGE_1)), output); try (ZipAssert zipAssert = assertThatZip(output)) { zipAssert.containsOnlyEntriesMatching( @@ -87,7 +99,7 @@ class ZipperTest { @Test void archiveShouldWriteTwoMessagesWhenTwo() throws Exception { - testee.archive(NO_MAILBOXES, Stream.of(MESSAGE_1, MESSAGE_2), output); + testee.archive(NO_MAILBOXES, Stream.of(MESSAGE_RESULT_1, MESSAGE_RESULT_2), output); try (ZipAssert zipAssert = assertThatZip(output)) { zipAssert.containsOnlyEntriesMatching( @@ -100,7 +112,7 @@ class ZipperTest { @Test void archiveShouldWriteMetadata() throws Exception { - testee.archive(NO_MAILBOXES, Stream.of(MESSAGE_1), output); + testee.archive(NO_MAILBOXES, Stream.of(MESSAGE_RESULT_1), output); try (ZipAssert zipAssert = assertThatZip(output)) { zipAssert.containsOnlyEntriesMatching( --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org