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 2015/09/22 12:17:46 UTC

svn commit: r1704522 - in /james/mailbox/trunk: api/src/main/java/org/apache/james/mailbox/quota/ store/src/main/java/org/apache/james/mailbox/store/quota/ store/src/test/java/org/apache/james/mailbox/store/quota/

Author: btellier
Date: Tue Sep 22 10:17:43 2015
New Revision: 1704522

URL: http://svn.apache.org/viewvc?rev=1704522&view=rev
Log:
MAILBOX-64 QuotaRootResolver and default implementation

Added:
    james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/quota/QuotaRootResolver.java
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/quota/DefaultQuotaRootResolver.java
    james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/quota/DefaultQuotaRootResolverTest.java

Added: james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/quota/QuotaRootResolver.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/quota/QuotaRootResolver.java?rev=1704522&view=auto
==============================================================================
--- james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/quota/QuotaRootResolver.java (added)
+++ james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/quota/QuotaRootResolver.java Tue Sep 22 10:17:43 2015
@@ -0,0 +1,43 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mailbox.quota;
+
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.QuotaRoot;
+
+import java.util.List;
+
+public interface QuotaRootResolver {
+
+    QuotaRoot createQuotaRoot(String quotaRootString);
+
+    /**
+     * Return the quotaRoot associated with the given mailbox name.
+     *
+     * @param mailboxPath The name of the mailbox
+     * @return QuotaRoot ruling this mailbox ( we uses user owning this mailbox name )
+     * @throws MailboxException
+     */
+    QuotaRoot getQuotaRoot(MailboxPath mailboxPath) throws MailboxException;
+
+    List<MailboxPath> retrieveAssociatedMailboxes(QuotaRoot quotaRoot, MailboxSession mailboxSession) throws MailboxException;
+}

Added: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/quota/DefaultQuotaRootResolver.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/quota/DefaultQuotaRootResolver.java?rev=1704522&view=auto
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/quota/DefaultQuotaRootResolver.java (added)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/quota/DefaultQuotaRootResolver.java Tue Sep 22 10:17:43 2015
@@ -0,0 +1,75 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mailbox.store.quota;
+
+import com.google.common.base.Function;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Splitter;
+import com.google.common.collect.Lists;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.QuotaRoot;
+import org.apache.james.mailbox.quota.QuotaRootResolver;
+import org.apache.james.mailbox.store.MailboxSessionMapperFactory;
+import org.apache.james.mailbox.store.mail.model.Mailbox;
+
+import java.util.List;
+
+public class DefaultQuotaRootResolver implements QuotaRootResolver {
+
+    public static final String SEPARATOR = "&"; // Character illegal for mailbox naming in regard of RFC 3501 section 5.1
+
+    private final MailboxSessionMapperFactory factory;
+
+    public DefaultQuotaRootResolver(MailboxSessionMapperFactory factory) {
+        this.factory = factory;
+    }
+
+    @Override
+    public QuotaRoot createQuotaRoot(String quotaRootValue) {
+        return QuotaRootImpl.quotaRoot(quotaRootValue);
+    }
+
+    @Override
+    public QuotaRoot getQuotaRoot(MailboxPath mailboxPath) throws MailboxException {
+        Preconditions.checkArgument(!mailboxPath.getNamespace().contains(SEPARATOR), "Namespace should not contain " + SEPARATOR);
+        Preconditions.checkArgument(!mailboxPath.getUser().contains(SEPARATOR), "Username should not contain " + SEPARATOR);
+        return QuotaRootImpl.quotaRoot(mailboxPath.getNamespace() + SEPARATOR + mailboxPath.getUser());
+    }
+
+    @SuppressWarnings("unchecked")
+    @Override
+    public List<MailboxPath> retrieveAssociatedMailboxes(QuotaRoot quotaRoot, MailboxSession mailboxSession) throws MailboxException {
+        List<String> parts = Lists.newArrayList(Splitter.on(SEPARATOR).split(quotaRoot.getValue()));
+        if (parts.size() != 2) {
+            throw new MailboxException(quotaRoot + " used as QuotaRoot should not contain 2 \""+SEPARATOR+"\"");
+        }
+        String namespace = parts.get(0);
+        String user = parts.get(1);
+        return Lists.transform(factory.getMailboxMapper(mailboxSession).findMailboxWithPathLike(new MailboxPath(namespace, user, "%")),
+            new Function<Mailbox, MailboxPath>() {
+                @Override
+                public MailboxPath apply(Mailbox idMailbox) {
+                    return new MailboxPath(idMailbox.getNamespace(), idMailbox.getUser(), idMailbox.getName());
+                }
+            });
+    }
+}

Added: james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/quota/DefaultQuotaRootResolverTest.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/quota/DefaultQuotaRootResolverTest.java?rev=1704522&view=auto
==============================================================================
--- james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/quota/DefaultQuotaRootResolverTest.java (added)
+++ james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/quota/DefaultQuotaRootResolverTest.java Tue Sep 22 10:17:43 2015
@@ -0,0 +1,100 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mailbox.store.quota;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import com.google.common.collect.Lists;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.QuotaRoot;
+import org.apache.james.mailbox.store.MailboxSessionMapperFactory;
+import org.apache.james.mailbox.store.TestId;
+import org.apache.james.mailbox.store.mail.MailboxMapper;
+import org.apache.james.mailbox.store.mail.model.impl.SimpleMailbox;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+import java.util.List;
+
+public class DefaultQuotaRootResolverTest {
+
+    public static final MailboxPath MAILBOX_PATH = new MailboxPath("#private", "benwa", "INBOX");
+    public static final SimpleMailbox<TestId> MAILBOX = new SimpleMailbox<TestId>(MAILBOX_PATH, 10);
+    public static final MailboxPath PATH_LIKE = new MailboxPath("#private", "benwa", "%");
+    public static final MailboxPath MAILBOX_PATH_2 = new MailboxPath("#private", "benwa", "test");
+    public static final SimpleMailbox<TestId> MAILBOX_2 = new SimpleMailbox<TestId>(MAILBOX_PATH_2, 10);
+    public static final QuotaRoot QUOTA_ROOT = QuotaRootImpl.quotaRoot("#private&benwa");
+
+    private DefaultQuotaRootResolver testee;
+    private MailboxSessionMapperFactory<TestId> mockedFactory;
+
+    @SuppressWarnings("unchecked")
+    @Before
+    public void setUp() {
+        mockedFactory = (MailboxSessionMapperFactory<TestId>) mock(MailboxSessionMapperFactory.class);
+        testee = new DefaultQuotaRootResolver(mockedFactory);
+    }
+
+    @Test
+    public void getQuotaRootShouldReturnUserRelatedQuotaRoot() throws Exception {
+        assertThat(testee.getQuotaRoot(MAILBOX_PATH)).isEqualTo(QUOTA_ROOT);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void getQuotaRootShouldThrowWhenNamespaceContainsSeparator() throws Exception {
+        testee.getQuotaRoot(new MailboxPath("#pr&ivate", "benwa", "INBOX"));
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void getQuotaRootShouldThrowWhenUserContainsSeparator() throws Exception {
+        testee.getQuotaRoot(new MailboxPath("#private", "ben&wa", "INBOX"));
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void retrieveAssociatedMailboxesShouldWork() throws Exception {
+        final MailboxMapper<TestId> mockedMapper = (MailboxMapper<TestId>) mock(MailboxMapper.class);
+        when(mockedFactory.getMailboxMapper(null)).thenAnswer(new Answer<MailboxMapper<TestId>>() {
+            @Override
+            public MailboxMapper<TestId> answer(InvocationOnMock invocationOnMock) throws Throwable {
+                return mockedMapper;
+            }
+        });
+        when(mockedMapper.findMailboxWithPathLike(PATH_LIKE)).thenAnswer(new Answer<List<SimpleMailbox<TestId>>>() {
+            @Override
+            public List<SimpleMailbox<TestId>> answer(InvocationOnMock invocationOnMock) throws Throwable {
+                return Lists.newArrayList(MAILBOX, MAILBOX_2);
+            }
+        });
+        assertThat(testee.retrieveAssociatedMailboxes(QUOTA_ROOT, null)).containsOnly(MAILBOX_PATH, MAILBOX_PATH_2);
+    }
+
+    @Test(expected = MailboxException.class)
+    public void retrieveAssociatedMailboxesShouldThrowWhenQuotaRootNotContainsSeparator2Times() throws Exception {
+        testee.retrieveAssociatedMailboxes(QuotaRootImpl.quotaRoot("#private&be&nwa"), null);
+    }
+
+}



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