PROTOCOLS-117 MailboxPath sanitizing should be handled by MailboxPath

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

Branch: refs/heads/master
Commit: 7e8aaea41677ff280065719d1e0d9ae2507ea8ee
Parents: 3757e5e
Author: benwa <btell...@linagora.com>
Authored: Wed Nov 8 16:35:40 2017 +0700
Committer: Antoine Duprat <adup...@linagora.com>
Committed: Mon Nov 13 16:25:59 2017 +0100

----------------------------------------------------------------------
 .../apache/james/mailbox/model/MailboxPath.java | 16 ++++++
 .../james/mailbox/model/MailboxPathTest.java    | 56 ++++++++++++++++++++
 .../mailbox/store/StoreMailboxManager.java      | 14 +----
 3 files changed, 73 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/7e8aaea4/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxPath.java
----------------------------------------------------------------------
diff --git 
a/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxPath.java 
b/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxPath.java
index 1f4f2f9..02daf0b 100644
--- a/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxPath.java
+++ b/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxPath.java
@@ -127,6 +127,22 @@ public class MailboxPath {
         return levels;
     }
 
+    public MailboxPath sanitize(char delimiter) {
+        if (name == null) {
+            return this;
+        }
+        if (name.endsWith(String.valueOf(delimiter))) {
+            int length = name.length();
+            String sanitizedName = name.substring(0, length - 1);
+            return new MailboxPath(
+                namespace,
+                user,
+                sanitizedName);
+        }
+        return this;
+    }
+
+
     public String asString() {
         return namespace + ":" + user + ":" + name;
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/7e8aaea4/mailbox/api/src/test/java/org/apache/james/mailbox/model/MailboxPathTest.java
----------------------------------------------------------------------
diff --git 
a/mailbox/api/src/test/java/org/apache/james/mailbox/model/MailboxPathTest.java 
b/mailbox/api/src/test/java/org/apache/james/mailbox/model/MailboxPathTest.java
index 2761658..5292690 100644
--- 
a/mailbox/api/src/test/java/org/apache/james/mailbox/model/MailboxPathTest.java
+++ 
b/mailbox/api/src/test/java/org/apache/james/mailbox/model/MailboxPathTest.java
@@ -68,4 +68,60 @@ public class MailboxPathTest {
             .containsExactly(
                 MailboxPath.forUser("user", null));
     }
+
+    @Test
+    public void sanitizeShouldNotThrowOnNullMailboxName() {
+        assertThat(MailboxPath.forUser("user", null)
+            .sanitize('.'))
+            .isEqualTo(
+                MailboxPath.forUser("user", null));
+    }
+
+    @Test
+    public void sanitizeShouldReturnEmptyWhenEmpty() {
+        assertThat(MailboxPath.forUser("user", "")
+            .sanitize('.'))
+            .isEqualTo(
+                MailboxPath.forUser("user", ""));
+    }
+
+    @Test
+    public void sanitizeShouldRemoveMaximumOneTrailingDelimiterWhenAlone() {
+        assertThat(MailboxPath.forUser("user", ".")
+            .sanitize('.'))
+            .isEqualTo(
+                MailboxPath.forUser("user", ""));
+    }
+
+    @Test
+    public void sanitizeShouldPreserveHeadingDelimiter() {
+        assertThat(MailboxPath.forUser("user", ".a")
+            .sanitize('.'))
+            .isEqualTo(
+                MailboxPath.forUser("user", ".a"));
+    }
+
+    @Test
+    public void sanitizeShouldRemoveTrailingDelimiter() {
+        assertThat(MailboxPath.forUser("user", "a.")
+            .sanitize('.'))
+            .isEqualTo(
+                MailboxPath.forUser("user", "a"));
+    }
+
+    @Test
+    public void sanitizeShouldRemoveMaximumOneTrailingDelimiter() {
+        assertThat(MailboxPath.forUser("user", "a..")
+            .sanitize('.'))
+            .isEqualTo(
+                MailboxPath.forUser("user", "a."));
+    }
+
+    @Test
+    public void sanitizeShouldPreserveRedundantDelimiters() {
+        assertThat(MailboxPath.forUser("user", "a..a")
+            .sanitize('.'))
+            .isEqualTo(
+                MailboxPath.forUser("user", "a..a"));
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/7e8aaea4/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 7664f84..5f499aa 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
@@ -490,7 +490,7 @@ public class StoreMailboxManager implements MailboxManager {
         if (mailboxPath.getName().isEmpty()) {
             LOGGER.warn("Ignoring mailbox with empty name");
         } else {
-            MailboxPath sanitizedMailboxPath = 
sanitizeMailboxPath(mailboxPath);
+            MailboxPath sanitizedMailboxPath = 
mailboxPath.sanitize(mailboxSession.getPathDelimiter());
             if (mailboxExists(sanitizedMailboxPath, mailboxSession))
                 throw new 
MailboxExistsException(sanitizedMailboxPath.asString());
             // Create parents first
@@ -519,18 +519,6 @@ public class StoreMailboxManager implements MailboxManager 
{
         return Optional.empty();
     }
 
-    private MailboxPath sanitizeMailboxPath(MailboxPath mailboxPath) {
-        if (mailboxPath.getName().endsWith(String.valueOf(getDelimiter()))) {
-            int length = mailboxPath.getName().length();
-            String sanitizedName = mailboxPath.getName().substring(0, length - 
1);
-            return new MailboxPath(
-                mailboxPath.getNamespace(),
-                mailboxPath.getUser(),
-                sanitizedName);
-        }
-        return mailboxPath;
-    }
-
     @Override
     public void deleteMailbox(final MailboxPath mailboxPath, final 
MailboxSession session) throws MailboxException {
         LOGGER.info("deleteMailbox " + mailboxPath);


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

Reply via email to