james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rouaz...@apache.org
Subject [04/10] james-project git commit: JAMES-1818 Use getMailboxById in MailboxUtils
Date Mon, 05 Sep 2016 12:44:00 GMT
JAMES-1818 Use getMailboxById in MailboxUtils


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

Branch: refs/heads/master
Commit: df3a8a0bb2927e8af394d0e8e1985a7d4d9d168c
Parents: 22ed8ef
Author: Raphael Ouazana <raphael.ouazana@linagora.com>
Authored: Tue Aug 30 17:14:06 2016 +0200
Committer: Raphael Ouazana <raphael.ouazana@linagora.com>
Committed: Mon Sep 5 14:42:00 2016 +0200

----------------------------------------------------------------------
 .../apache/james/jmap/utils/MailboxUtils.java   | 60 ++++++++++----------
 .../jmap/methods/GetMailboxesMethodTest.java    |  2 +-
 .../james/jmap/utils/MailboxUtilsTest.java      |  2 +-
 3 files changed, 33 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/df3a8a0b/server/protocols/jmap/src/main/java/org/apache/james/jmap/utils/MailboxUtils.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/utils/MailboxUtils.java
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/utils/MailboxUtils.java
index 8bb4ba3..bfb9acc 100644
--- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/utils/MailboxUtils.java
+++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/utils/MailboxUtils.java
@@ -31,13 +31,14 @@ 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.exception.MailboxNotFoundException;
 import org.apache.james.mailbox.model.MailboxId;
 import org.apache.james.mailbox.model.MailboxPath;
-import org.apache.james.mailbox.store.mail.MailboxMapperFactory;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.github.fge.lambdas.Throwing;
+import com.github.fge.lambdas.functions.ThrowingFunction;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Splitter;
@@ -46,38 +47,40 @@ public class MailboxUtils {
 
     private static final boolean DONT_RESET_RECENT = false;
     private static final Logger LOGGER = LoggerFactory.getLogger(MailboxUtils.class);
-    private static final String WILDCARD = "%";
 
     private final MailboxManager mailboxManager;
-    private final MailboxMapperFactory mailboxMapperFactory;
 
     @Inject
     @VisibleForTesting
-    public MailboxUtils(MailboxManager mailboxManager, MailboxMapperFactory mailboxMapperFactory)
{
+    public MailboxUtils(MailboxManager mailboxManager) {
         this.mailboxManager = mailboxManager;
-        this.mailboxMapperFactory = mailboxMapperFactory;
     }
 
     public Optional<Mailbox> mailboxFromMailboxPath(MailboxPath mailboxPath, MailboxSession
mailboxSession) {
         try {
-            Optional<Role> role = Role.from(mailboxPath.getName());
             MessageManager mailbox = mailboxManager.getMailbox(mailboxPath, mailboxSession);
-            MessageManager.MetaData mailboxMetaData = getMailboxMetaData(mailbox, mailboxSession);
-            return Optional.ofNullable(Mailbox.builder()
-                    .id(mailbox.getId())
-                    .name(getName(mailboxPath, mailboxSession))
-                    .parentId(getParentIdFromMailboxPath(mailboxPath, mailboxSession).orElse(null))
-                    .role(role)
-                    .unreadMessages(mailboxMetaData.getUnseenCount())
-                    .totalMessages(mailboxMetaData.getMessageCount())
-                    .sortOrder(SortOrder.getSortOrder(role))
-                    .build());
+            return mailboxFromMessageManager(mailbox, mailboxSession);
         } catch (MailboxException e) {
-            LOGGER.warn("Cannot find mailbox for :" + mailboxPath.getName(), e);
+            LOGGER.warn("Cannot find mailbox for: " + mailboxPath.getName(), e);
             return Optional.empty();
         }
     }
 
+    private Optional<Mailbox> mailboxFromMessageManager(MessageManager messageManager,
MailboxSession mailboxSession) throws MailboxException {
+        MailboxPath mailboxPath = messageManager.getMailboxPath();
+        Optional<Role> role = Role.from(mailboxPath.getName());
+        MessageManager.MetaData mailboxMetaData = getMailboxMetaData(messageManager, mailboxSession);
+        return Optional.ofNullable(Mailbox.builder()
+                .id(messageManager.getId())
+                .name(getName(mailboxPath, mailboxSession))
+                .parentId(getParentIdFromMailboxPath(mailboxPath, mailboxSession).orElse(null))
+                .role(role)
+                .unreadMessages(mailboxMetaData.getUnseenCount())
+                .totalMessages(mailboxMetaData.getMessageCount())
+                .sortOrder(SortOrder.getSortOrder(role))
+                .build());
+    }
+
     private MessageManager.MetaData getMailboxMetaData(MessageManager messageManager, MailboxSession
mailboxSession) throws MailboxException {
         return messageManager.getMetaData(DONT_RESET_RECENT, mailboxSession, MessageManager.MetaData.FetchGroup.UNSEEN_COUNT);
     }
@@ -98,15 +101,16 @@ public class MailboxUtils {
 
     public Optional<String> getMailboxNameFromId(MailboxId mailboxId, MailboxSession
mailboxSession) throws MailboxException {
         return getMailboxFromId(mailboxId, mailboxSession)
-                .map(org.apache.james.mailbox.store.mail.model.Mailbox::getName);
+                .map(Throwing.function(MessageManager::getMailboxPath).sneakyThrow())
+                .map(MailboxPath::getName);
     }
 
-    private Optional<org.apache.james.mailbox.store.mail.model.Mailbox> getMailboxFromId(MailboxId
mailboxId, MailboxSession mailboxSession) throws MailboxException {
-        return mailboxMapperFactory.getMailboxMapper(mailboxSession)
-                .findMailboxWithPathLike(new MailboxPath(mailboxSession.getPersonalSpace(),
mailboxSession.getUser().getUserName(), WILDCARD))
-                .stream()
-                .filter(mailbox -> mailbox.getMailboxId().equals(mailboxId))
-                .findFirst();
+    private Optional<MessageManager> getMailboxFromId(MailboxId mailboxId, MailboxSession
mailboxSession) throws MailboxException {
+        try {
+            return Optional.of(mailboxManager.getMailbox(mailboxId, mailboxSession));
+        } catch (MailboxNotFoundException e) {
+            return Optional.empty();
+        }
     }
 
     @VisibleForTesting Optional<MailboxId> getParentIdFromMailboxPath(MailboxPath mailboxPath,
MailboxSession mailboxSession) throws MailboxException {
@@ -119,12 +123,10 @@ public class MailboxUtils {
     }
 
     public Optional<Mailbox> mailboxFromMailboxId(MailboxId mailboxId, MailboxSession
mailboxSession) {
+        ThrowingFunction<MessageManager, Optional<Mailbox>> toMailbox = path
-> mailboxFromMessageManager(path, mailboxSession);
         try {
             return getMailboxFromId(mailboxId, mailboxSession)
-                .flatMap(jamesMailbox ->
-                    mailboxFromMailboxPath(new MailboxPath(jamesMailbox.getNamespace(), mailboxSession.getUser().getUserName(),
jamesMailbox.getName()), 
-                            mailboxSession)
-                );
+                .flatMap(Throwing.function(toMailbox).sneakyThrow());
         } catch (MailboxException e) {
             return Optional.empty();
         }
@@ -144,7 +146,7 @@ public class MailboxUtils {
 
     public boolean hasChildren(MailboxId mailboxId, MailboxSession mailboxSession) throws
MailboxException {
         return getMailboxFromId(mailboxId, mailboxSession)
-                .map(mailbox -> new MailboxPath(mailbox.getNamespace(), mailbox.getUser(),
mailbox.getName()))
+                .map(Throwing.function(MessageManager::getMailboxPath).sneakyThrow())
                 .map(Throwing.function(path -> mailboxManager.hasChildren(path, mailboxSession)))
                 .orElse(false);
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/df3a8a0b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/GetMailboxesMethodTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/GetMailboxesMethodTest.java
b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/GetMailboxesMethodTest.java
index e321085..e3461d4 100644
--- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/GetMailboxesMethodTest.java
+++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/GetMailboxesMethodTest.java
@@ -82,7 +82,7 @@ public class GetMailboxesMethodTest {
         MessageParser messageParser = new MessageParser();
         mailboxManager = new StoreMailboxManager(mailboxMapperFactory, new MockAuthenticator(),
aclResolver, groupMembershipResolver, messageParser);
         mailboxManager.init();
-        mailboxUtils = new MailboxUtils(mailboxManager, mailboxMapperFactory);
+        mailboxUtils = new MailboxUtils(mailboxManager);
 
         getMailboxesMethod = new GetMailboxesMethod(mailboxManager, mailboxUtils);
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/df3a8a0b/server/protocols/jmap/src/test/java/org/apache/james/jmap/utils/MailboxUtilsTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/utils/MailboxUtilsTest.java
b/server/protocols/jmap/src/test/java/org/apache/james/jmap/utils/MailboxUtilsTest.java
index 6c76622..ef0a93c 100644
--- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/utils/MailboxUtilsTest.java
+++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/utils/MailboxUtilsTest.java
@@ -54,7 +54,7 @@ public class MailboxUtilsTest {
         mailboxMapperFactory = new InMemoryMailboxSessionMapperFactory();
         user = "user@domain.org";
         mailboxSession = mailboxManager.login(user, "pass", LOGGER);
-        sut = new MailboxUtils(mailboxManager, mailboxMapperFactory);
+        sut = new MailboxUtils(mailboxManager);
     }
 
     @Test


---------------------------------------------------------------------
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