Repository: james-project
Updated Branches:
  refs/heads/master 55dc6ff94 -> 8c26272df


JAMES-2266 Allow moving messages when their mailbox no more exist

I introduced a separated helper to avoid leaking that "feature" too much. 
However I mainly based that behaviour on StoreMessageIdManager to limit code 
duplication.


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/c964a64d
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/c964a64d
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/c964a64d

Branch: refs/heads/master
Commit: c964a64df71553481cbbe1e07f54c27208780496
Parents: 55dc6ff
Author: benwa <[email protected]>
Authored: Wed Dec 20 11:40:02 2017 +0700
Committer: benwa <[email protected]>
Committed: Fri Jan 5 16:06:04 2018 +0700

----------------------------------------------------------------------
 .../mailbox/store/StoreMessageIdManager.java    | 31 +++++++++++++++-----
 1 file changed, 23 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/c964a64d/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMessageIdManager.java
----------------------------------------------------------------------
diff --git 
a/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMessageIdManager.java
 
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMessageIdManager.java
index 14a0126..fb8b45f 100644
--- 
a/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMessageIdManager.java
+++ 
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMessageIdManager.java
@@ -72,7 +72,7 @@ import com.google.common.collect.Sets;
 
 public class StoreMessageIdManager implements MessageIdManager {
 
-    private static class MessageMoves {
+    public static class MessageMoves {
         private final ImmutableSet<MailboxId> previousMailboxIds;
         private final ImmutableSet<MailboxId> targetMailboxIds;
 
@@ -108,6 +108,13 @@ public class StoreMessageIdManager implements 
MessageIdManager {
         return new MetadataWithMailboxId(new SimpleMessageMetaData(message), 
message.getMailboxId());
     }
 
+    public static ImmutableSet<MailboxId> toMailboxIds(List<MailboxMessage> 
mailboxMessages) {
+        return mailboxMessages
+            .stream()
+            .map(MailboxMessage::getMailboxId)
+            .collect(Guavate.toImmutableSet());
+    }
+
     private static final Logger LOGGER = 
LoggerFactory.getLogger(StoreMessageIdManager.class);
 
     private final MailboxManager mailboxManager;
@@ -207,6 +214,17 @@ public class StoreMessageIdManager implements 
MessageIdManager {
         }
     }
 
+    public void setInMailboxesNoCheck(MessageId messageId, MailboxId 
targetMailboxId, MailboxSession mailboxSession) throws MailboxException {
+        MessageIdMapper messageIdMapper = 
mailboxSessionMapperFactory.getMessageIdMapper(mailboxSession);
+        List<MailboxMessage> currentMailboxMessages = 
messageIdMapper.find(ImmutableList.of(messageId), MessageMapper.FetchType.Full);
+
+        MessageMoves messageMoves = new 
MessageMoves(toMailboxIds(currentMailboxMessages), 
ImmutableList.of(targetMailboxId));
+
+        if (messageMoves.isChange()) {
+            applyMessageMoveNoMailboxChecks(mailboxSession, 
currentMailboxMessages, messageMoves);
+        }
+    }
+
     private List<MailboxMessage> findRelatedMailboxMessages(MessageId 
messageId, MailboxSession mailboxSession) throws MailboxException {
         MessageIdMapper messageIdMapper = 
mailboxSessionMapperFactory.getMessageIdMapper(mailboxSession);
         return messageIdMapper.find(ImmutableList.of(messageId), 
MessageMapper.FetchType.Full)
@@ -219,6 +237,10 @@ public class StoreMessageIdManager implements 
MessageIdManager {
         assertRightsOnMailboxes(messageMoves.addedMailboxIds(), 
mailboxSession, Right.Insert);
         assertRightsOnMailboxes(messageMoves.removedMailboxIds(), 
mailboxSession, Right.DeleteMessages);
 
+        applyMessageMoveNoMailboxChecks(mailboxSession, 
currentMailboxMessages, messageMoves);
+    }
+
+    private void applyMessageMoveNoMailboxChecks(MailboxSession 
mailboxSession, List<MailboxMessage> currentMailboxMessages, MessageMoves 
messageMoves) throws MailboxException {
         MailboxMessage mailboxMessage = 
currentMailboxMessages.stream().findAny().orElseThrow(() -> new 
MailboxNotFoundException("can't load message"));
 
         validateQuota(messageMoves, mailboxSession, mailboxMessage);
@@ -238,13 +260,6 @@ public class StoreMessageIdManager implements 
MessageIdManager {
         }
     }
 
-    private ImmutableSet<MailboxId> toMailboxIds(List<MailboxMessage> 
mailboxMessages) {
-        return mailboxMessages
-            .stream()
-            .map(MailboxMessage::getMailboxId)
-            .collect(Guavate.toImmutableSet());
-    }
-
     protected MailboxMessage createMessage(Date internalDate, int size, int 
bodyStartOctet, SharedInputStream content, Flags flags, PropertyBuilder 
propertyBuilder, List<MessageAttachment> attachments, MailboxId mailboxId) 
throws MailboxException {
         return new SimpleMailboxMessage(messageIdFactory.generate(), 
internalDate, size, bodyStartOctet, content, flags, propertyBuilder, mailboxId, 
attachments);
     }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to