james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rouaz...@apache.org
Subject [10/17] james-project git commit: JAMES-1818 Remove store usage in destruction processor by using managers
Date Mon, 29 Aug 2016 13:28:07 GMT
JAMES-1818 Remove store usage in destruction processor by using managers


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

Branch: refs/heads/master
Commit: 198f6cee83a9d023c259e0bc774d307a494e2753
Parents: b7fe5df
Author: Raphael Ouazana <raphael.ouazana@linagora.com>
Authored: Thu Aug 18 18:01:02 2016 +0200
Committer: Raphael Ouazana <raphael.ouazana@linagora.com>
Committed: Mon Aug 29 15:15:43 2016 +0200

----------------------------------------------------------------------
 .../SetMessagesDestructionProcessor.java        | 57 ++++++++------------
 .../org/apache/james/jmap/model/MessageId.java  |  5 ++
 2 files changed, 26 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/198f6cee/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesDestructionProcessor.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesDestructionProcessor.java
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesDestructionProcessor.java
index eac8707..a1d3d18 100644
--- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesDestructionProcessor.java
+++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesDestructionProcessor.java
@@ -19,70 +19,53 @@
 
 package org.apache.james.jmap.methods;
 
-import java.util.Iterator;
 import java.util.function.Function;
 
 import javax.inject.Inject;
+import javax.mail.Flags;
 
 import org.apache.james.jmap.exceptions.MessageNotFoundException;
 import org.apache.james.jmap.model.MessageId;
 import org.apache.james.jmap.model.SetError;
 import org.apache.james.jmap.model.SetMessagesRequest;
 import org.apache.james.jmap.model.SetMessagesResponse;
+import org.apache.james.mailbox.MailboxManager;
 import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MessageManager;
 import org.apache.james.mailbox.exception.MailboxException;
-import org.apache.james.mailbox.model.MessageRange;
-import org.apache.james.mailbox.store.MailboxSessionMapperFactory;
-import org.apache.james.mailbox.store.mail.MailboxMapperFactory;
-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.MailboxMessage;
+import org.apache.james.mailbox.model.FetchGroupImpl;
+import org.apache.james.mailbox.model.MessageResultIterator;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Throwables;
 
 public class SetMessagesDestructionProcessor implements SetMessagesProcessor {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(SetMessagesCreationProcessor.class);
-    private static final int LIMIT_BY_ONE = 1;
 
-    private final MailboxMapperFactory mailboxMapperFactory;
-    private final MailboxSessionMapperFactory mailboxSessionMapperFactory;
+    private final MailboxManager mailboxManager;
 
     @Inject
     @VisibleForTesting
-    SetMessagesDestructionProcessor(MailboxMapperFactory mailboxMapperFactory,
-                                           MailboxSessionMapperFactory mailboxSessionMapperFactory)
{
-        this.mailboxMapperFactory = mailboxMapperFactory;
-        this.mailboxSessionMapperFactory = mailboxSessionMapperFactory;
+    SetMessagesDestructionProcessor(MailboxManager mailboxManager) {
+        this.mailboxManager = mailboxManager;
     }
 
     @Override
     public SetMessagesResponse process(SetMessagesRequest request, MailboxSession mailboxSession)
{
-        MessageMapper messageMapper;
-        try {
-            messageMapper = mailboxSessionMapperFactory.createMessageMapper(mailboxSession);
-        } catch (MailboxException e) {
-            throw Throwables.propagate(e);
-        }
         return request.getDestroy().stream()
-                .map(delete(messageMapper, mailboxSession))
+                .map(delete(mailboxSession))
                 .reduce(SetMessagesResponse.builder(),  SetMessagesResponse.Builder::accumulator,
SetMessagesResponse.Builder::combiner)
                 .build();
     }
 
-    private Function<? super MessageId, SetMessagesResponse> delete(MessageMapper messageMapper,
MailboxSession mailboxSession) {
+    private Function<? super MessageId, SetMessagesResponse> delete(MailboxSession
mailboxSession) {
         return (messageId) -> {
             try {
-                Mailbox mailbox = mailboxMapperFactory
-                        .getMailboxMapper(mailboxSession)
-                        .findMailboxByPath(messageId.getMailboxPath());
-
-                MailboxMessage mailboxMessage = getMailboxMessage(messageMapper, messageId,
mailbox);
-
-                messageMapper.delete(mailbox, mailboxMessage);
+                MessageManager messageManager = mailboxManager.getMailbox(messageId.getMailboxPath(),
mailboxSession);
+                checkThatMessageExists(messageManager, messageId, mailboxSession);
+                removeMessage(messageManager, messageId, mailboxSession);
                 return SetMessagesResponse.builder().destroyed(messageId).build();
             } catch (MessageNotFoundException e) {
                 return SetMessagesResponse.builder().notDestroyed(messageId,
@@ -103,13 +86,15 @@ public class SetMessagesDestructionProcessor implements SetMessagesProcessor
{
         };
     }
 
-    private MailboxMessage getMailboxMessage(MessageMapper messageMapper, MessageId messageId,
Mailbox mailbox)
-            throws MailboxException, MessageNotFoundException {
-
-        Iterator<MailboxMessage> mailboxMessage = messageMapper.findInMailbox(mailbox,
MessageRange.one(messageId.getUid()), MessageMapper.FetchType.Metadata, LIMIT_BY_ONE);
-        if (!mailboxMessage.hasNext()) {
+    private void checkThatMessageExists(MessageManager messageManager, MessageId messageId,
MailboxSession mailboxSession) throws MailboxException, MessageNotFoundException {
+        MessageResultIterator messages = messageManager.getMessages(messageId.getUidAsRange(),
FetchGroupImpl.MINIMAL, mailboxSession);
+        if (!messages.hasNext()) {
             throw new MessageNotFoundException();
         }
-        return mailboxMessage.next();
+    }
+
+    private void removeMessage(MessageManager messageManager, MessageId messageId, MailboxSession
mailboxSession) throws MailboxException {
+        messageManager.setFlags(new Flags(Flags.Flag.DELETED), MessageManager.FlagsUpdateMode.ADD,
messageId.getUidAsRange(), mailboxSession);
+        messageManager.expunge(messageId.getUidAsRange(), mailboxSession);
     }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/198f6cee/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/MessageId.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/MessageId.java
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/MessageId.java
index 40e00f9..117124c 100644
--- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/MessageId.java
+++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/MessageId.java
@@ -23,6 +23,7 @@ import java.util.Objects;
 import org.apache.james.mailbox.MailboxSession.User;
 import org.apache.james.mailbox.model.MailboxConstants;
 import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.MessageRange;
 import org.javatuples.Triplet;
 
 import com.fasterxml.jackson.annotation.JsonCreator;
@@ -67,6 +68,10 @@ public class MessageId {
         return uid;
     }
     
+    public MessageRange getUidAsRange() {
+        return MessageRange.one(uid);
+    }
+    
     public MailboxPath getMailboxPath() {
         return new MailboxPath(MailboxConstants.USER_NAMESPACE, username, mailboxPath);
     }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message