james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rouaz...@apache.org
Subject [02/10] james-project git commit: JAMES-1818 Introduce getMailboxById and its implementations
Date Mon, 05 Sep 2016 12:43:58 GMT
JAMES-1818 Introduce getMailboxById and its implementations


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

Branch: refs/heads/master
Commit: 22ed8ef29d62d32060f1acd5cb528e5bdab557ce
Parents: ed7268e
Author: Raphael Ouazana <raphael.ouazana@linagora.com>
Authored: Wed Aug 17 14:48:59 2016 +0200
Committer: Raphael Ouazana <raphael.ouazana@linagora.com>
Committed: Mon Sep 5 14:42:00 2016 +0200

----------------------------------------------------------------------
 .../apache/james/mailbox/MailboxManager.java    | 17 +++-
 .../apache/james/mailbox/MailboxSession.java    |  2 +
 .../james/mailbox/mock/MockMailboxSession.java  |  8 ++
 .../mailbox/caching/CachingMailboxMapper.java   | 21 +++--
 .../cassandra/mail/CassandraMailboxMapper.java  | 12 +++
 .../mailbox/hbase/mail/HBaseMailboxMapper.java  | 27 +++++++
 .../hbase/mail/HBaseMailboxMapperTest.java      | 19 ++++-
 .../mailbox/jcr/mail/JCRMailboxMapper.java      | 13 ++++
 .../mailbox/jpa/mail/JPAMailboxMapper.java      | 13 ++++
 .../maildir/mail/MaildirMailboxMapper.java      |  7 ++
 .../inmemory/mail/InMemoryMailboxMapper.java    | 11 +++
 .../mailbox/store/SimpleMailboxSession.java     |  8 ++
 .../mailbox/store/StoreMailboxManager.java      | 39 +++++++++-
 .../james/mailbox/store/mail/MailboxMapper.java | 12 +++
 .../mailbox/store/StoreMailboxManagerTest.java  | 82 +++++++++++++++++---
 .../store/mail/model/MailboxMapperTest.java     | 17 ++++
 .../james/imap/processor/LSubProcessorTest.java |  5 ++
 .../base/MailboxEventAnalyserTest.java          |  5 +-
 .../FirstUserConnectionFilterThreadTest.java    |  5 ++
 .../jmap/methods/GetMessagesMethodTest.java     |  5 ++
 .../methods/SetVacationResponseMethodTest.java  |  5 ++
 21 files changed, 308 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/22ed8ef2/mailbox/api/src/main/java/org/apache/james/mailbox/MailboxManager.java
----------------------------------------------------------------------
diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/MailboxManager.java b/mailbox/api/src/main/java/org/apache/james/mailbox/MailboxManager.java
index d993c4e..450c4fa 100644
--- a/mailbox/api/src/main/java/org/apache/james/mailbox/MailboxManager.java
+++ b/mailbox/api/src/main/java/org/apache/james/mailbox/MailboxManager.java
@@ -115,13 +115,12 @@ public interface MailboxManager extends RequestAware, MailboxListenerSupport
{
     char getDelimiter();
 
     /**
-     * Gets an session suitable for IMAP.
+     * Gets an object managing the given mailbox.
      * 
      * @param mailboxPath
      *            the Path of the mailbox, not null
      * @param session
      *            the context for this call, not null
-     * @return <code>ImapMailboxSession</code>, not null
      * @throws MailboxException
      *             when the mailbox cannot be opened
      * @throws MailboxNotFoundException
@@ -130,6 +129,20 @@ public interface MailboxManager extends RequestAware, MailboxListenerSupport
{
     MessageManager getMailbox(MailboxPath mailboxPath, MailboxSession session) throws MailboxException;
 
     /**
+     * Gets an object managing the given mailbox.
+     * 
+     * @param mailboxId
+     *            the Id of the mailbox, not null
+     * @param session
+     *            the context for this call, not null
+     * @throws MailboxException
+     *             when the mailbox cannot be opened
+     * @throws MailboxNotFoundException
+     *             when the given mailbox does not exist
+     */
+    MessageManager getMailbox(MailboxId mailboxId, MailboxSession session) throws MailboxException;
+
+    /**
      * Creates a new mailbox. Any intermediary mailboxes missing from the
      * hierarchy should be created.
      * 

http://git-wip-us.apache.org/repos/asf/james-project/blob/22ed8ef2/mailbox/api/src/main/java/org/apache/james/mailbox/MailboxSession.java
----------------------------------------------------------------------
diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/MailboxSession.java b/mailbox/api/src/main/java/org/apache/james/mailbox/MailboxSession.java
index a9c66e1..55fbe88 100644
--- a/mailbox/api/src/main/java/org/apache/james/mailbox/MailboxSession.java
+++ b/mailbox/api/src/main/java/org/apache/james/mailbox/MailboxSession.java
@@ -115,6 +115,8 @@ public interface MailboxSession {
          * @return not null, when empty the default local should be used
          */
         List<Locale> getLocalePreferences();
+        
+        boolean isSameUser(String username);
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/james-project/blob/22ed8ef2/mailbox/api/src/test/java/org/apache/james/mailbox/mock/MockMailboxSession.java
----------------------------------------------------------------------
diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/mock/MockMailboxSession.java
b/mailbox/api/src/test/java/org/apache/james/mailbox/mock/MockMailboxSession.java
index a77f225..9a241a7 100644
--- a/mailbox/api/src/test/java/org/apache/james/mailbox/mock/MockMailboxSession.java
+++ b/mailbox/api/src/test/java/org/apache/james/mailbox/mock/MockMailboxSession.java
@@ -54,6 +54,14 @@ public class MockMailboxSession implements MailboxSession{
             public List<Locale> getLocalePreferences() {
                 return new ArrayList<Locale>();
             }
+
+            @Override
+            public boolean isSameUser(String other) {
+                if (username == null) {
+                    return other == null;
+                }
+                return username.equalsIgnoreCase(other);
+            }
         };
     }
     

http://git-wip-us.apache.org/repos/asf/james-project/blob/22ed8ef2/mailbox/caching/src/main/java/org/apache/james/mailbox/caching/CachingMailboxMapper.java
----------------------------------------------------------------------
diff --git a/mailbox/caching/src/main/java/org/apache/james/mailbox/caching/CachingMailboxMapper.java
b/mailbox/caching/src/main/java/org/apache/james/mailbox/caching/CachingMailboxMapper.java
index 4c1debe..c311b1d 100644
--- a/mailbox/caching/src/main/java/org/apache/james/mailbox/caching/CachingMailboxMapper.java
+++ b/mailbox/caching/src/main/java/org/apache/james/mailbox/caching/CachingMailboxMapper.java
@@ -23,6 +23,7 @@ import java.util.List;
 import org.apache.james.mailbox.exception.MailboxException;
 import org.apache.james.mailbox.exception.MailboxNotFoundException;
 import org.apache.james.mailbox.model.MailboxACL;
+import org.apache.james.mailbox.model.MailboxId;
 import org.apache.james.mailbox.model.MailboxPath;
 import org.apache.james.mailbox.store.mail.MailboxMapper;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
@@ -76,12 +77,20 @@ public class CachingMailboxMapper implements MailboxMapper {
 		}
 	}
 
-	@Override
-	public List<Mailbox> findMailboxWithPathLike(MailboxPath mailboxPath)
-			throws MailboxException {
-		// TODO possible to meaningfully cache it?
-		return underlying.findMailboxWithPathLike(mailboxPath);
-	}
+    @Override
+    public Mailbox findMailboxById(MailboxId mailboxId)
+            throws MailboxException {
+        // TODO possible to meaningfully cache it?
+        return underlying.findMailboxById(mailboxId);
+    }
+
+
+    @Override
+    public List<Mailbox> findMailboxWithPathLike(MailboxPath mailboxPath)
+            throws MailboxException {
+        // TODO possible to meaningfully cache it?
+        return underlying.findMailboxWithPathLike(mailboxPath);
+    }
 
 	@Override
 	public boolean hasChildren(Mailbox mailbox, char delimiter)

http://git-wip-us.apache.org/repos/asf/james-project/blob/22ed8ef2/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraMailboxMapper.java
----------------------------------------------------------------------
diff --git a/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraMailboxMapper.java
b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraMailboxMapper.java
index 853e19c..1e81978 100644
--- a/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraMailboxMapper.java
+++ b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraMailboxMapper.java
@@ -45,6 +45,7 @@ import org.apache.james.mailbox.cassandra.table.CassandraMailboxTable.MailboxBas
 import org.apache.james.mailbox.exception.MailboxException;
 import org.apache.james.mailbox.exception.MailboxNotFoundException;
 import org.apache.james.mailbox.model.MailboxACL;
+import org.apache.james.mailbox.model.MailboxId;
 import org.apache.james.mailbox.model.MailboxPath;
 import org.apache.james.mailbox.store.mail.MailboxMapper;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
@@ -92,6 +93,17 @@ public class CassandraMailboxMapper implements MailboxMapper {
     }
 
     @Override
+    public Mailbox findMailboxById(MailboxId id) throws MailboxException {
+        CassandraId mailboxId = (CassandraId) id;
+        ResultSet resultSet = session.execute(select(FIELDS).from(TABLE_NAME).where(eq(ID,
mailboxId.asUuid())));
+        if (resultSet.isExhausted()) {
+            throw new MailboxNotFoundException(id.serialize());
+        } else {
+            return mailbox(resultSet.one());
+        }
+    }
+
+    @Override
     public List<Mailbox> findMailboxWithPathLike(MailboxPath path) throws MailboxException
{
         Pattern regex = Pattern.compile(constructEscapedRegexForMailboxNameMatching(path));
         return getMailboxFilteredByNamespaceAndUserStream(path.getNamespace(), path.getUser())

http://git-wip-us.apache.org/repos/asf/james-project/blob/22ed8ef2/mailbox/hbase/src/main/java/org/apache/james/mailbox/hbase/mail/HBaseMailboxMapper.java
----------------------------------------------------------------------
diff --git a/mailbox/hbase/src/main/java/org/apache/james/mailbox/hbase/mail/HBaseMailboxMapper.java
b/mailbox/hbase/src/main/java/org/apache/james/mailbox/hbase/mail/HBaseMailboxMapper.java
index 628cd8c..8c89e9d 100644
--- a/mailbox/hbase/src/main/java/org/apache/james/mailbox/hbase/mail/HBaseMailboxMapper.java
+++ b/mailbox/hbase/src/main/java/org/apache/james/mailbox/hbase/mail/HBaseMailboxMapper.java
@@ -36,6 +36,7 @@ import java.util.List;
 
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.hbase.client.Delete;
+import org.apache.hadoop.hbase.client.Get;
 import org.apache.hadoop.hbase.client.HTable;
 import org.apache.hadoop.hbase.client.Put;
 import org.apache.hadoop.hbase.client.Result;
@@ -54,6 +55,7 @@ import org.apache.james.mailbox.hbase.HBaseId;
 import org.apache.james.mailbox.hbase.HBaseNonTransactionalMapper;
 import org.apache.james.mailbox.hbase.mail.model.HBaseMailbox;
 import org.apache.james.mailbox.model.MailboxACL;
+import org.apache.james.mailbox.model.MailboxId;
 import org.apache.james.mailbox.model.MailboxPath;
 import org.apache.james.mailbox.store.mail.MailboxMapper;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
@@ -123,6 +125,31 @@ public class HBaseMailboxMapper extends HBaseNonTransactionalMapper implements
M
             }
         }
     }
+
+    @Override
+    public Mailbox findMailboxById(MailboxId id) throws MailboxException, MailboxNotFoundException
{
+        HBaseId mailboxId = (HBaseId)id;
+        HTable mailboxes = null;
+        try {
+            mailboxes = new HTable(conf, MAILBOXES_TABLE);
+            Get get = new Get(mailboxId.toBytes());
+            Result result = mailboxes.get(get);
+            if (result == null) {
+                throw new MailboxNotFoundException(mailboxId.serialize());
+            }
+            return mailboxFromResult(result);
+        } catch (IOException ex) {
+            throw new MailboxException("IOException in HBase cluster during get()", ex);
+        } finally {
+            if (mailboxes != null) {
+                try {
+                    mailboxes.close();
+                } catch (IOException ex) {
+                    throw new MailboxException("Error closing table " + mailboxes, ex);
+                }
+            }
+        }
+    }
     
     @Override
     public List<Mailbox> findMailboxWithPathLike(MailboxPath mailboxPath) throws MailboxException
{

http://git-wip-us.apache.org/repos/asf/james-project/blob/22ed8ef2/mailbox/hbase/src/test/java/org/apache/james/mailbox/hbase/mail/HBaseMailboxMapperTest.java
----------------------------------------------------------------------
diff --git a/mailbox/hbase/src/test/java/org/apache/james/mailbox/hbase/mail/HBaseMailboxMapperTest.java
b/mailbox/hbase/src/test/java/org/apache/james/mailbox/hbase/mail/HBaseMailboxMapperTest.java
index 27b5c86..35dc9ff 100644
--- a/mailbox/hbase/src/test/java/org/apache/james/mailbox/hbase/mail/HBaseMailboxMapperTest.java
+++ b/mailbox/hbase/src/test/java/org/apache/james/mailbox/hbase/mail/HBaseMailboxMapperTest.java
@@ -47,9 +47,11 @@ import org.apache.hadoop.hbase.util.Bytes;
 import org.apache.james.mailbox.exception.MailboxException;
 import org.apache.james.mailbox.exception.MailboxNotFoundException;
 import org.apache.james.mailbox.hbase.HBaseClusterSingleton;
+import org.apache.james.mailbox.hbase.HBaseId;
 import org.apache.james.mailbox.hbase.io.ChunkInputStream;
 import org.apache.james.mailbox.hbase.io.ChunkOutputStream;
 import org.apache.james.mailbox.hbase.mail.model.HBaseMailbox;
+import org.apache.james.mailbox.model.MailboxId;
 import org.apache.james.mailbox.model.MailboxPath;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
 import org.junit.Before;
@@ -69,6 +71,7 @@ public class HBaseMailboxMapperTest {
     private static HBaseMailboxMapper mapper;
     private static List<HBaseMailbox> mailboxList;
     private static List<MailboxPath> pathsList;
+    private static List<HBaseId> idsList;
     private static final int NAMESPACES = 5;
     private static final int USERS = 5;
     private static final int MAILBOX_NO = 5;
@@ -108,6 +111,7 @@ public class HBaseMailboxMapperTest {
     @Test
     public void testMailboxMapperScenario() throws Exception {
         testFindMailboxByPath();
+        testFindMailboxById();
         testFindMailboxWithPathLike();
         testList();
         testSave();
@@ -131,6 +135,16 @@ public class HBaseMailboxMapperTest {
         }
     }
 
+    private void testFindMailboxById() throws Exception {
+        LOG.info("findMailboxById");
+        HBaseMailbox mailbox;
+        for (MailboxId id : idsList) {
+            LOG.info("Searching for " + id.serialize());
+            mailbox = (HBaseMailbox) mapper.findMailboxById(id);
+            assertEquals(mailbox.getMailboxId(), id);
+        }
+    }
+
     /**
      * Test of findMailboxWithPathLike method, of class HBaseMailboxMapper.
      */
@@ -301,6 +315,7 @@ public class HBaseMailboxMapperTest {
     private static void fillMailboxList() {
         mailboxList = new ArrayList<HBaseMailbox>();
         pathsList = new ArrayList<MailboxPath>();
+        idsList = new ArrayList<HBaseId>();
         MailboxPath path;
         String name;
         for (int i = 0; i < NAMESPACES; i++) {
@@ -313,7 +328,9 @@ public class HBaseMailboxMapperTest {
                     }
                     path = new MailboxPath("namespace" + i, "user" + j, name);
                     pathsList.add(path);
-                    mailboxList.add(new HBaseMailbox(path, 13));
+                    HBaseMailbox mailbox = new HBaseMailbox(path, 13);
+                    mailboxList.add(mailbox);
+                    idsList.add(mailbox.getMailboxId());
                 }
             }
         }

http://git-wip-us.apache.org/repos/asf/james-project/blob/22ed8ef2/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRMailboxMapper.java
----------------------------------------------------------------------
diff --git a/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRMailboxMapper.java
b/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRMailboxMapper.java
index 3ad6988..c546a8c 100644
--- a/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRMailboxMapper.java
+++ b/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRMailboxMapper.java
@@ -40,6 +40,7 @@ import org.apache.james.mailbox.jcr.AbstractJCRScalingMapper;
 import org.apache.james.mailbox.jcr.MailboxSessionJCRRepository;
 import org.apache.james.mailbox.jcr.mail.model.JCRMailbox;
 import org.apache.james.mailbox.model.MailboxACL;
+import org.apache.james.mailbox.model.MailboxId;
 import org.apache.james.mailbox.model.MailboxPath;
 import org.apache.james.mailbox.store.mail.MailboxMapper;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
@@ -108,6 +109,18 @@ public class JCRMailboxMapper extends AbstractJCRScalingMapper implements
Mailbo
         }
     }
 
+    @Override
+    public Mailbox findMailboxById(MailboxId id) throws MailboxException, MailboxNotFoundException
{
+        try {
+            Node node = getSession().getNodeByIdentifier(id.serialize());
+            return new JCRMailbox(node, getLogger());
+        } catch (PathNotFoundException e) {
+            throw new MailboxNotFoundException(id.serialize());
+        } catch (RepositoryException e) {
+            throw new MailboxException("Unable to find mailbox " + id.serialize(), e);
+        }
+    }
+
 
     /*
      * (non-Javadoc)

http://git-wip-us.apache.org/repos/asf/james-project/blob/22ed8ef2/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/JPAMailboxMapper.java
----------------------------------------------------------------------
diff --git a/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/JPAMailboxMapper.java
b/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/JPAMailboxMapper.java
index fd64f9c..c3c7a3a 100644
--- a/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/JPAMailboxMapper.java
+++ b/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/JPAMailboxMapper.java
@@ -33,6 +33,7 @@ import org.apache.james.mailbox.exception.MailboxNotFoundException;
 import org.apache.james.mailbox.jpa.JPAId;
 import org.apache.james.mailbox.jpa.JPATransactionalMapper;
 import org.apache.james.mailbox.model.MailboxACL;
+import org.apache.james.mailbox.model.MailboxId;
 import org.apache.james.mailbox.model.MailboxPath;
 import org.apache.james.mailbox.store.mail.MailboxMapper;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
@@ -98,6 +99,18 @@ public class JPAMailboxMapper extends JPATransactionalMapper implements
MailboxM
         } 
     }
 
+    @Override
+    public Mailbox findMailboxById(MailboxId id) throws MailboxException, MailboxNotFoundException
{
+        JPAId mailboxId = (JPAId)id;
+        try {
+            return (Mailbox) getEntityManager().createNamedQuery("findMailboxById").setParameter("idParam",
mailboxId.getRawId()).getSingleResult();
+        } catch (NoResultException e) {
+            throw new MailboxNotFoundException(mailboxId.serialize());
+        } catch (PersistenceException e) {
+            throw new MailboxException("Search of mailbox " + mailboxId.serialize() + " failed",
e);
+        } 
+    }
+
     /**
      * @see org.apache.james.mailbox.store.mail.MailboxMapper#delete(Mailbox)
      */

http://git-wip-us.apache.org/repos/asf/james-project/blob/22ed8ef2/mailbox/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/MaildirMailboxMapper.java
----------------------------------------------------------------------
diff --git a/mailbox/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/MaildirMailboxMapper.java
b/mailbox/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/MaildirMailboxMapper.java
index 629afb9..73c02d3 100644
--- a/mailbox/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/MaildirMailboxMapper.java
+++ b/mailbox/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/MaildirMailboxMapper.java
@@ -37,6 +37,7 @@ import org.apache.james.mailbox.maildir.MaildirMessageName;
 import org.apache.james.mailbox.maildir.MaildirStore;
 import org.apache.james.mailbox.model.MailboxACL;
 import org.apache.james.mailbox.model.MailboxConstants;
+import org.apache.james.mailbox.model.MailboxId;
 import org.apache.james.mailbox.model.MailboxPath;
 import org.apache.james.mailbox.store.mail.MailboxMapper;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
@@ -113,6 +114,12 @@ public class MaildirMailboxMapper extends NonTransactionalMapper implements
Mail
         return cacheMailbox(mailbox);
     }
     
+    @Override
+    public Mailbox findMailboxById(MailboxId id) throws MailboxException, MailboxNotFoundException
{
+        MaildirId mailboxId = (MaildirId)id;
+        return getCachedMailbox(mailboxId);
+    }
+    
     /**
      * @see org.apache.james.mailbox.store.mail.MailboxMapper#findMailboxWithPathLike(org.apache.james.mailbox.model.MailboxPath)
      */

http://git-wip-us.apache.org/repos/asf/james-project/blob/22ed8ef2/mailbox/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryMailboxMapper.java
----------------------------------------------------------------------
diff --git a/mailbox/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryMailboxMapper.java
b/mailbox/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryMailboxMapper.java
index 2afecc0..3862b68 100644
--- a/mailbox/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryMailboxMapper.java
+++ b/mailbox/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryMailboxMapper.java
@@ -28,6 +28,7 @@ import org.apache.james.mailbox.exception.MailboxException;
 import org.apache.james.mailbox.exception.MailboxNotFoundException;
 import org.apache.james.mailbox.inmemory.InMemoryId;
 import org.apache.james.mailbox.model.MailboxACL;
+import org.apache.james.mailbox.model.MailboxId;
 import org.apache.james.mailbox.model.MailboxPath;
 import org.apache.james.mailbox.store.mail.MailboxMapper;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
@@ -75,6 +76,16 @@ public class InMemoryMailboxMapper implements MailboxMapper {
         }
     }
 
+    public synchronized Mailbox findMailboxById(MailboxId id) throws MailboxException {
+        InMemoryId mailboxId = (InMemoryId)id;
+        Mailbox result = mailboxesById.get(mailboxId);
+        if (result == null) {
+            throw new MailboxNotFoundException(mailboxId.serialize());
+        } else {
+            return result;
+        }
+    }
+
     /**
      * @see org.apache.james.mailbox.store.mail.MailboxMapper#findMailboxWithPathLike(org.apache.james.mailbox.model.MailboxPath)
      */

http://git-wip-us.apache.org/repos/asf/james-project/blob/22ed8ef2/mailbox/store/src/main/java/org/apache/james/mailbox/store/SimpleMailboxSession.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/SimpleMailboxSession.java
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/SimpleMailboxSession.java
index d6f1db0..24e5374 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/SimpleMailboxSession.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/SimpleMailboxSession.java
@@ -198,4 +198,12 @@ public class SimpleMailboxSession implements MailboxSession, MailboxSession.User
         return type;
     }
 
+    @Override
+    public boolean isSameUser(String username) {
+        if (this.userName == null) {
+            return username == null;
+        }
+        return this.userName.equalsIgnoreCase(username);
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/22ed8ef2/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java
----------------------------------------------------------------------
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 18f4727..09bba85 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.LockAwareExecution;
 import org.apache.james.mailbox.MailboxSession;
 import org.apache.james.mailbox.MailboxSession.SessionType;
 import org.apache.james.mailbox.MailboxSessionIdGenerator;
+import org.apache.james.mailbox.MessageManager;
 import org.apache.james.mailbox.RequestAware;
 import org.apache.james.mailbox.StandardMailboxMetaDataComparator;
 import org.apache.james.mailbox.acl.GroupMembershipResolver;
@@ -399,7 +400,7 @@ public class StoreMailboxManager implements MailboxManager {
     }
 
     @Override
-    public org.apache.james.mailbox.MessageManager getMailbox(MailboxPath mailboxPath, MailboxSession
session)
+    public MessageManager getMailbox(MailboxPath mailboxPath, MailboxSession session)
             throws MailboxException {
         final MailboxMapper mapper = mailboxSessionMapperFactory.getMailboxMapper(session);
         Mailbox mailboxRow = mapper.findMailboxByPath(mailboxPath);
@@ -411,13 +412,43 @@ public class StoreMailboxManager implements MailboxManager {
         } else {
             session.getLog().debug("Loaded mailbox " + mailboxPath);
 
-            StoreMessageManager m = createMessageManager(mailboxRow, session);
-            m.setFetchBatchSize(fetchBatchSize);
-            return m;
+            StoreMessageManager messageManager = createMessageManager(mailboxRow, session);
+            messageManager.setFetchBatchSize(fetchBatchSize);
+            return messageManager;
         }
     }
 
     @Override
+    public MessageManager getMailbox(MailboxId mailboxId, MailboxSession session)
+            throws MailboxException {
+        MailboxMapper mapper = mailboxSessionMapperFactory.getMailboxMapper(session);
+        Mailbox mailboxRow = mapper.findMailboxById(mailboxId);
+
+        if (mailboxRow == null) {
+            session.getLog().info("Mailbox '" + mailboxId.serialize() + "' not found.");
+            throw new MailboxNotFoundException(mailboxId.serialize());
+        }
+        
+        if (! belongsToCurrentUser(mailboxRow, session)) {
+            session.getLog().info("Mailbox '" + mailboxId.serialize() + "' does not belong
to user '" + session.getUser() + "' but to '" + mailboxRow.getUser());
+            throw new MailboxNotFoundException(mailboxId.serialize());
+        }
+
+        session.getLog().debug("Loaded mailbox " + mailboxId.serialize());
+
+        StoreMessageManager messageManager = createMessageManager(mailboxRow, session);
+        messageManager.setFetchBatchSize(fetchBatchSize);
+        return messageManager;
+    }
+
+    private boolean belongsToCurrentUser(Mailbox mailbox, MailboxSession session) {
+        if (session.getUser() == null) {
+            return mailbox.getUser() == null;
+        }
+        return session.getUser().isSameUser(mailbox.getUser());
+    }
+
+    @Override
     public void createMailbox(MailboxPath mailboxPath, final MailboxSession mailboxSession)
             throws MailboxException {
         mailboxSession.getLog().debug("createMailbox " + mailboxPath);

http://git-wip-us.apache.org/repos/asf/james-project/blob/22ed8ef2/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/MailboxMapper.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/MailboxMapper.java
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/MailboxMapper.java
index a1f5878..f46a2f8 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/MailboxMapper.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/MailboxMapper.java
@@ -23,6 +23,7 @@ import java.util.List;
 import org.apache.james.mailbox.exception.MailboxException;
 import org.apache.james.mailbox.exception.MailboxNotFoundException;
 import org.apache.james.mailbox.model.MailboxACL;
+import org.apache.james.mailbox.model.MailboxId;
 import org.apache.james.mailbox.model.MailboxPath;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
 import org.apache.james.mailbox.store.transaction.Mapper;
@@ -63,6 +64,17 @@ public interface MailboxMapper extends Mapper {
             throws MailboxException, MailboxNotFoundException;
 
     /**
+     * Return the {@link Mailbox} for the given name
+     * 
+     * @param mailboxId
+     * @return mailbox
+     * @throws MailboxException
+     * @throws MailboxNotFoundException
+     */
+    Mailbox findMailboxById(MailboxId mailboxId)
+            throws MailboxException, MailboxNotFoundException;
+
+    /**
      * Return a List of {@link Mailbox} which name is like the given name
      * 
      * @param mailboxPath

http://git-wip-us.apache.org/repos/asf/james-project/blob/22ed8ef2/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreMailboxManagerTest.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreMailboxManagerTest.java
b/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreMailboxManagerTest.java
index 1fa865d..975e3e3 100644
--- a/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreMailboxManagerTest.java
+++ b/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreMailboxManagerTest.java
@@ -20,10 +20,20 @@
 package org.apache.james.mailbox.store;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MessageManager;
 import org.apache.james.mailbox.acl.SimpleGroupMembershipResolver;
 import org.apache.james.mailbox.acl.UnionMailboxACLResolver;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.exception.MailboxNotFoundException;
+import org.apache.james.mailbox.mock.MockMailboxSession;
+import org.apache.james.mailbox.model.MailboxId;
 import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.store.mail.MailboxMapper;
+import org.apache.james.mailbox.store.mail.model.Mailbox;
 import org.apache.james.mailbox.store.mail.model.impl.MessageParser;
 import org.apache.james.mailbox.store.mail.model.impl.SimpleMailbox;
 import org.junit.Before;
@@ -31,17 +41,26 @@ import org.junit.Test;
 
 public class StoreMailboxManagerTest {
 
+    private static final String CURRENT_USER = "user";
+    private static final MailboxId MAILBOX_ID = TestId.of(123);
     public static final int UID_VALIDITY = 42;
     private StoreMailboxManager storeMailboxManager;
+    private MailboxMapper mockedMailboxMapper;
+    private MailboxSession mockedMailboxSession;
 
     @Before
-    public void setUp() {
-        storeMailboxManager = new StoreMailboxManager(null, new MockAuthenticator(), new
JVMMailboxPathLocker(), new UnionMailboxACLResolver(), new SimpleGroupMembershipResolver(),
new MessageParser());
+    public void setUp() throws MailboxException {
+        MailboxSessionMapperFactory mockedMapperFactory = mock(MailboxSessionMapperFactory.class);
+        mockedMailboxSession = new MockMailboxSession(CURRENT_USER);
+        mockedMailboxMapper = mock(MailboxMapper.class);
+        when(mockedMapperFactory.getMailboxMapper(mockedMailboxSession))
+            .thenReturn(mockedMailboxMapper);
+        storeMailboxManager = new StoreMailboxManager(mockedMapperFactory, new MockAuthenticator(),
new JVMMailboxPathLocker(), new UnionMailboxACLResolver(), new SimpleGroupMembershipResolver(),
new MessageParser());
     }
 
     @Test
     public void belongsToNamespaceAndUserShouldReturnTrueWithIdenticalMailboxes() {
-        MailboxPath path = new MailboxPath("namespace", "user", "name");
+        MailboxPath path = new MailboxPath("namespace", CURRENT_USER, "name");
         assertThat(storeMailboxManager.belongsToNamespaceAndUser(path, new SimpleMailbox(path,
UID_VALIDITY))).isTrue();
     }
 
@@ -53,9 +72,9 @@ public class StoreMailboxManagerTest {
 
     @Test
     public void belongsToNamespaceAndUserShouldReturnTrueWithIdenticalMailboxesWithNullNamespace()
{
-        MailboxPath path = new MailboxPath(null, "user", "name");
+        MailboxPath path = new MailboxPath(null, CURRENT_USER, "name");
         assertThat(storeMailboxManager.belongsToNamespaceAndUser(path,
-            new SimpleMailbox(new MailboxPath(null, "user", "name"), UID_VALIDITY))).isTrue();
+            new SimpleMailbox(new MailboxPath(null, CURRENT_USER, "name"), UID_VALIDITY))).isTrue();
     }
 
     @Test
@@ -67,21 +86,21 @@ public class StoreMailboxManagerTest {
 
     @Test
     public void belongsToNamespaceAndUserShouldReturnTrueWithMailboxWithSameNamespaceAndUser()
{
-        MailboxPath path = new MailboxPath("namespace", "user", "name");
+        MailboxPath path = new MailboxPath("namespace", CURRENT_USER, "name");
         assertThat(storeMailboxManager.belongsToNamespaceAndUser(path,
-            new SimpleMailbox(new MailboxPath("namespace", "user", "name2"), UID_VALIDITY))).isTrue();
+            new SimpleMailbox(new MailboxPath("namespace", CURRENT_USER, "name2"), UID_VALIDITY))).isTrue();
     }
 
     @Test
     public void belongsToNamespaceAndUserShouldReturnFalseWithDifferentNamespace() {
-        MailboxPath path = new MailboxPath("namespace", "user", "name");
+        MailboxPath path = new MailboxPath("namespace", CURRENT_USER, "name");
         assertThat(storeMailboxManager.belongsToNamespaceAndUser(path,
-            new SimpleMailbox(new MailboxPath("namespace2", "user", "name"), UID_VALIDITY))).isFalse();
+            new SimpleMailbox(new MailboxPath("namespace2", CURRENT_USER, "name"), UID_VALIDITY))).isFalse();
     }
 
     @Test
     public void belongsToNamespaceAndUserShouldReturnFalseWithDifferentUser() {
-        MailboxPath path = new MailboxPath("namespace", "user", "name");
+        MailboxPath path = new MailboxPath("namespace", CURRENT_USER, "name");
         assertThat(storeMailboxManager.belongsToNamespaceAndUser(path,
             new SimpleMailbox(new MailboxPath("namespace", "user2", "name"), UID_VALIDITY))).isFalse();
     }
@@ -89,7 +108,7 @@ public class StoreMailboxManagerTest {
     public void belongsToNamespaceAndUserShouldReturnFalseWithOneOfTheUserNull() {
         MailboxPath path = new MailboxPath("namespace", null, "name");
         assertThat(storeMailboxManager.belongsToNamespaceAndUser(path,
-            new SimpleMailbox(new MailboxPath("namespace", "user", "name"), UID_VALIDITY))).isFalse();
+            new SimpleMailbox(new MailboxPath("namespace", CURRENT_USER, "name"), UID_VALIDITY))).isFalse();
     }
     @Test
     public void belongsToNamespaceAndUserShouldReturnFalseIfNamespaceAreDifferentWithNullUser()
{
@@ -98,6 +117,47 @@ public class StoreMailboxManagerTest {
             new SimpleMailbox(new MailboxPath("namespace2", null, "name"), UID_VALIDITY))).isFalse();
     }
 
+    @Test(expected = MailboxNotFoundException.class)
+    public void getMailboxShouldThrowWhenUnknownId() throws Exception {
+        when(mockedMailboxMapper.findMailboxById(MAILBOX_ID)).thenReturn(null);
 
+        storeMailboxManager.getMailbox(MAILBOX_ID, mockedMailboxSession);
+    }
+
+    @Test
+    public void getMailboxShouldReturnMailboxManagerWhenKnownId() throws Exception {
+        Mailbox mockedMailbox = mock(Mailbox.class);
+        when(mockedMailbox.getUser()).thenReturn(CURRENT_USER);
+        when(mockedMailbox.getMailboxId()).thenReturn(MAILBOX_ID);
+        when(mockedMailboxMapper.findMailboxById(MAILBOX_ID)).thenReturn(mockedMailbox);
+
+        MessageManager expected = storeMailboxManager.getMailbox(MAILBOX_ID, mockedMailboxSession);
+
+        assertThat(expected.getId()).isEqualTo(MAILBOX_ID);
+    }
+
+    @Test
+    public void getMailboxShouldReturnMailboxManagerWhenKnownIdAndDifferentCaseUser() throws
Exception {
+        Mailbox mockedMailbox = mock(Mailbox.class);
+        when(mockedMailbox.getUser()).thenReturn("uSEr");
+        when(mockedMailbox.getMailboxId()).thenReturn(MAILBOX_ID);
+        when(mockedMailboxMapper.findMailboxById(MAILBOX_ID)).thenReturn(mockedMailbox);
+
+        MessageManager expected = storeMailboxManager.getMailbox(MAILBOX_ID, mockedMailboxSession);
+
+        assertThat(expected.getId()).isEqualTo(MAILBOX_ID);
+    }
+
+    @Test(expected = MailboxNotFoundException.class)
+    public void getMailboxShouldThrowWhenMailboxDoesNotMatchUser() throws Exception {
+        Mailbox mockedMailbox = mock(Mailbox.class);
+        when(mockedMailbox.getUser()).thenReturn("other.user");
+        when(mockedMailbox.getMailboxId()).thenReturn(MAILBOX_ID);
+        when(mockedMailboxMapper.findMailboxById(MAILBOX_ID)).thenReturn(mockedMailbox);
+
+        MessageManager expected = storeMailboxManager.getMailbox(MAILBOX_ID, mockedMailboxSession);
+
+        assertThat(expected.getId()).isEqualTo(MAILBOX_ID);
+    }
 }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/22ed8ef2/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/MailboxMapperTest.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/MailboxMapperTest.java
b/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/MailboxMapperTest.java
index 2bf50c4..d80c252 100644
--- a/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/MailboxMapperTest.java
+++ b/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/MailboxMapperTest.java
@@ -26,6 +26,7 @@ import java.util.List;
 
 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.MailboxMapper;
 import org.apache.james.mailbox.store.mail.model.impl.SimpleMailbox;
@@ -248,7 +249,23 @@ public class MailboxMapperTest<T extends MapperProvider> {
         MailboxPath regexPath = new MailboxPath(benwaInboxPath.getNamespace(), benwaInboxPath.getUser(),
"INB?X");
         assertThat(mailboxMapper.findMailboxWithPathLike(regexPath)).isEmpty();
     }
+
+    @ContractTest
+    public void findMailboxByIdShouldReturnExistingMailbox() throws MailboxException {
+        saveAll();
+        Mailbox actual = mailboxMapper.findMailboxById(benwaInboxMailbox.getMailboxId());
+        assertThat(actual).isEqualTo(benwaInboxMailbox);
+    }
     
+    @ContractTest
+    public void findMailboxByIdShouldFailWhenAbsent() throws MailboxException {
+        expected.expect(MailboxNotFoundException.class);
+        saveAll();
+        MailboxId removed = benwaInboxMailbox.getMailboxId();
+        mailboxMapper.delete(benwaInboxMailbox);
+        mailboxMapper.findMailboxById(removed);
+    }
+
     private void saveAll() throws MailboxException{
         mailboxMapper.save(benwaInboxMailbox);
         mailboxMapper.save(benwaWorkMailbox);

http://git-wip-us.apache.org/repos/asf/james-project/blob/22ed8ef2/protocols/imap/src/test/java/org/apache/james/imap/processor/LSubProcessorTest.java
----------------------------------------------------------------------
diff --git a/protocols/imap/src/test/java/org/apache/james/imap/processor/LSubProcessorTest.java
b/protocols/imap/src/test/java/org/apache/james/imap/processor/LSubProcessorTest.java
index 58856f9..709754f 100644
--- a/protocols/imap/src/test/java/org/apache/james/imap/processor/LSubProcessorTest.java
+++ b/protocols/imap/src/test/java/org/apache/james/imap/processor/LSubProcessorTest.java
@@ -263,6 +263,11 @@ public class LSubProcessorTest {
                 public String getUserName() {
                     return "test";
                 }
+
+                @Override
+                public boolean isSameUser(String username) {
+                    return "test".equalsIgnoreCase(username);
+                }
                 
             }));     
             oneOf(manager).subscriptions(with(same(mailboxSession)));will(returnValue(subscriptions));
    

http://git-wip-us.apache.org/repos/asf/james-project/blob/22ed8ef2/protocols/imap/src/test/java/org/apache/james/imap/processor/base/MailboxEventAnalyserTest.java
----------------------------------------------------------------------
diff --git a/protocols/imap/src/test/java/org/apache/james/imap/processor/base/MailboxEventAnalyserTest.java
b/protocols/imap/src/test/java/org/apache/james/imap/processor/base/MailboxEventAnalyserTest.java
index c6d2e89..39bcb27 100644
--- a/protocols/imap/src/test/java/org/apache/james/imap/processor/base/MailboxEventAnalyserTest.java
+++ b/protocols/imap/src/test/java/org/apache/james/imap/processor/base/MailboxEventAnalyserTest.java
@@ -338,7 +338,10 @@ public class MailboxEventAnalyserTest {
                 }
             };
         }
-        
+
+        public MessageManager getMailbox(MailboxId mailboxId, MailboxSession session) throws
MailboxException {
+            return getMailbox((MailboxPath)null, session);
+        }
         
         public char getDelimiter() {
             return '.';

http://git-wip-us.apache.org/repos/asf/james-project/blob/22ed8ef2/server/protocols/jmap/src/test/java/org/apache/james/jmap/FirstUserConnectionFilterThreadTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/FirstUserConnectionFilterThreadTest.java
b/server/protocols/jmap/src/test/java/org/apache/james/jmap/FirstUserConnectionFilterThreadTest.java
index c64cace..bde96d6 100644
--- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/FirstUserConnectionFilterThreadTest.java
+++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/FirstUserConnectionFilterThreadTest.java
@@ -136,6 +136,11 @@ public class FirstUserConnectionFilterThreadTest {
         }
 
         @Override
+        public MessageManager getMailbox(MailboxId mailboxId, MailboxSession session) throws
MailboxException {
+            return null;
+        }
+
+        @Override
         public void createMailbox(MailboxPath mailboxPath, MailboxSession mailboxSession)
throws MailboxException {
         }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/22ed8ef2/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/GetMessagesMethodTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/GetMessagesMethodTest.java
b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/GetMessagesMethodTest.java
index 8b2ccdd..09f06e0 100644
--- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/GetMessagesMethodTest.java
+++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/GetMessagesMethodTest.java
@@ -98,6 +98,11 @@ public class GetMessagesMethodTest {
         public List<Locale> getLocalePreferences() {
             return ImmutableList.of();
         }
+
+        @Override
+        public boolean isSameUser(String username) {
+            return this.username.equalsIgnoreCase(username);
+        }
     }
     
     private static final User ROBERT = new User("robert", "secret");

http://git-wip-us.apache.org/repos/asf/james-project/blob/22ed8ef2/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetVacationResponseMethodTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetVacationResponseMethodTest.java
b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetVacationResponseMethodTest.java
index 85c9a5e..485ec15 100644
--- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetVacationResponseMethodTest.java
+++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetVacationResponseMethodTest.java
@@ -68,6 +68,11 @@ public class SetVacationResponseMethodTest {
         public List<Locale> getLocalePreferences() {
             return null;
         }
+
+        @Override
+        public boolean isSameUser(String username) {
+            return USERNAME.equalsIgnoreCase(username);
+        }
     };
     public static final String SUBJECT = "subject";
 


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