You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2019/03/14 02:04:57 UTC
[james-project] 05/07: MAILBOX-379 Trigger pre-deletion hooks upon
mailbox delete
This is an automated email from the ASF dual-hosted git repository.
btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 3e97afc17df4ae50f829e151da8ea8ed46403c12
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Thu Mar 7 10:16:40 2019 +0700
MAILBOX-379 Trigger pre-deletion hooks upon mailbox delete
---
.../apache/james/mailbox/MailboxManagerTest.java | 21 ++++++++++++++++++++
.../james/mailbox/store/StoreMailboxManager.java | 23 +++++++++++++++++++---
2 files changed, 41 insertions(+), 3 deletions(-)
diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerTest.java b/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerTest.java
index 4e675b0..aba8e3a 100644
--- a/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerTest.java
+++ b/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerTest.java
@@ -1381,6 +1381,27 @@ public abstract class MailboxManagerTest<T extends MailboxManager> {
}
@Test
+ void deleteMailboxShouldCallAllPreDeletionHooks() throws Exception {
+ ComposedMessageId composeId = inboxManager.appendMessage(AppendCommand.builder()
+ .withFlags(new Flags(Flags.Flag.DELETED))
+ .build(message), session);
+ mailboxManager.deleteMailbox(inbox, session);
+
+ ArgumentCaptor<PreDeletionHook.DeleteOperation> preDeleteCaptor1 = ArgumentCaptor.forClass(PreDeletionHook.DeleteOperation.class);
+ ArgumentCaptor<PreDeletionHook.DeleteOperation> preDeleteCaptor2 = ArgumentCaptor.forClass(PreDeletionHook.DeleteOperation.class);
+ verify(preDeletionHook1, times(1)).notifyDelete(preDeleteCaptor1.capture());
+ verify(preDeletionHook2, times(1)).notifyDelete(preDeleteCaptor2.capture());
+
+ assertThat(preDeleteCaptor1.getValue().getDeletionMetadataList())
+ .hasSize(1)
+ .hasSameElementsAs(preDeleteCaptor2.getValue().getDeletionMetadataList())
+ .allSatisfy(deleteMetadata -> SoftAssertions.assertSoftly(softy -> {
+ softy.assertThat(deleteMetadata.getMailboxId()).isEqualTo(inboxId);
+ softy.assertThat(deleteMetadata.getMessageMetaData().getMessageId()).isEqualTo(composeId.getMessageId());
+ }));
+ }
+
+ @Test
void expungeShouldCallAllPreDeletionHooksOnEachMessageDeletionCall() throws Exception {
ComposedMessageId composeId1 = inboxManager.appendMessage(AppendCommand.builder()
.withFlags(new Flags(Flags.Flag.DELETED))
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java
index 09ca79f..a0d795d 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java
@@ -39,6 +39,7 @@ import org.apache.james.mailbox.MailboxPathLocker;
import org.apache.james.mailbox.MailboxPathLocker.LockAwareExecution;
import org.apache.james.mailbox.MailboxSession;
import org.apache.james.mailbox.MessageManager;
+import org.apache.james.mailbox.MetadataWithMailboxId;
import org.apache.james.mailbox.StandardMailboxMetaDataComparator;
import org.apache.james.mailbox.events.EventBus;
import org.apache.james.mailbox.events.MailboxIdRegistrationKey;
@@ -59,6 +60,7 @@ import org.apache.james.mailbox.model.MailboxMetaData.Selectability;
import org.apache.james.mailbox.model.MailboxPath;
import org.apache.james.mailbox.model.MessageId;
import org.apache.james.mailbox.model.MessageId.Factory;
+import org.apache.james.mailbox.model.MessageMetaData;
import org.apache.james.mailbox.model.MessageRange;
import org.apache.james.mailbox.model.MultimailboxesSearchQuery;
import org.apache.james.mailbox.model.QuotaRoot;
@@ -70,7 +72,6 @@ import org.apache.james.mailbox.store.event.EventFactory;
import org.apache.james.mailbox.store.mail.MailboxMapper;
import org.apache.james.mailbox.store.mail.MessageMapper;
import org.apache.james.mailbox.store.mail.model.Mailbox;
-import org.apache.james.mailbox.store.mail.model.Message;
import org.apache.james.mailbox.store.mail.model.impl.MessageParser;
import org.apache.james.mailbox.store.mail.model.impl.SimpleMailbox;
import org.apache.james.mailbox.store.quota.QuotaComponents;
@@ -87,6 +88,9 @@ import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
+import reactor.core.publisher.Flux;
+import reactor.core.scheduler.Schedulers;
+
/**
* This base class of an {@link MailboxManager} implementation provides a high-level api for writing your own
* {@link MailboxManager} implementation. If you plan to write your own {@link MailboxManager} its most times so easiest
@@ -395,10 +399,23 @@ public class StoreMailboxManager implements MailboxManager {
QuotaRoot quotaRoot = quotaRootResolver.getQuotaRoot(mailboxPath);
long messageCount = messageMapper.countMessagesInMailbox(mailbox);
- long totalSize = Iterators.toStream(messageMapper.findInMailbox(mailbox, MessageRange.all(), MessageMapper.FetchType.Metadata, UNLIMITED))
- .mapToLong(Message::getFullContentOctets)
+
+ List<MetadataWithMailboxId> metadata = Iterators.toStream(messageMapper.findInMailbox(mailbox, MessageRange.all(), MessageMapper.FetchType.Metadata, UNLIMITED))
+ .map(message -> MetadataWithMailboxId.from(message.metaData(), message.getMailboxId()))
+ .collect(Guavate.toImmutableList());
+
+ long totalSize = metadata.stream()
+ .map(MetadataWithMailboxId::getMessageMetaData)
+ .mapToLong(MessageMetaData::getSize)
.sum();
+ PreDeletionHook.DeleteOperation deleteOperation = PreDeletionHook.DeleteOperation.from(metadata);
+
+ Flux.fromIterable(preDeletionHooks)
+ .publishOn(Schedulers.elastic())
+ .flatMap(hook -> hook.notifyDelete(deleteOperation))
+ .blockLast();
+
// We need to create a copy of the mailbox as maybe we can not refer to the real
// mailbox once we remove it
SimpleMailbox m = new SimpleMailbox(mailbox);
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org