james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rouaz...@apache.org
Subject [2/2] james-project git commit: MAILBOX-270: annotation key, valid the limitation of components
Date Fri, 09 Sep 2016 07:29:11 GMT
MAILBOX-270: annotation key, valid the limitation of components


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

Branch: refs/heads/master
Commit: 6d83bc854fc772773e86e78fc29b125559e5f9d1
Parents: e0e8581
Author: Quynh Nguyen <qnguyen@linagora.com>
Authored: Wed Aug 31 11:30:33 2016 +0700
Committer: Quynh Nguyen <qnguyen@linagora.com>
Committed: Fri Sep 9 09:47:25 2016 +0700

----------------------------------------------------------------------
 .../mailbox/model/MailboxAnnotationKey.java     | 27 +++++++++++++++++---
 .../mailbox/model/MailboxAnnotationKeyTest.java | 20 +++++++++++++++
 2 files changed, 44 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/6d83bc85/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxAnnotationKey.java
----------------------------------------------------------------------
diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxAnnotationKey.java
b/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxAnnotationKey.java
index 09906b4..54dd2d7 100644
--- a/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxAnnotationKey.java
+++ b/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxAnnotationKey.java
@@ -31,16 +31,20 @@ public class MailboxAnnotationKey {
 
     private static final CharMatcher NAME_ANNOTATION_PATTERN = CharMatcher.JAVA_LETTER_OR_DIGIT
         .or(CharMatcher.is('/'));
+    public static final int MINIMUM_COMPONENTS = 2;
+    public static final int MINIMUM_COMPONENTS_OF_VENDOR = 4;
+    public static final int SECOND_COMPONENT_INDEX = 1;
+    public static final String VENDOR_COMPONENT = "vendor";
 
     private final String key;
 
     public MailboxAnnotationKey(String key) {
-        Preconditions.checkArgument(isValid(key),
-            "Key must start with '/' and not end with '/' and does not contain charater with
hex from '\u0000' to '\u00019' or {'*', '%', two consecutive '/'} ");
         this.key = key;
+        Preconditions.checkArgument(isValid(),
+            "Key must start with '/' and not end with '/' and does not contain charater with
hex from '\u0000' to '\u00019' or {'*', '%', two consecutive '/'} ");
     }
 
-    private boolean isValid(String key) {
+    private boolean isValid() {
         if (StringUtils.isBlank(key)) {
             return false;
         }
@@ -59,9 +63,26 @@ public class MailboxAnnotationKey {
         if (!NAME_ANNOTATION_PATTERN.matchesAllOf(key)) {
             return false;
         }
+        int componentsNo = countComponents();
+        if (isVendorKey() && componentsNo < MINIMUM_COMPONENTS_OF_VENDOR) {
+            return false;
+        }
+        if (componentsNo < MINIMUM_COMPONENTS) {
+            return false;
+        }
         return true;
     }
 
+    private boolean isVendorKey() {
+        String[] components = StringUtils.split(key, SLASH_CHARACTER);
+
+        if (components.length >= MINIMUM_COMPONENTS &&
+            VENDOR_COMPONENT.equalsIgnoreCase(components[SECOND_COMPONENT_INDEX])) {
+            return true;
+        }
+        return false;
+    }
+
     public int countComponents() {
         return StringUtils.countMatches(key, SLASH_CHARACTER);
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/6d83bc85/mailbox/api/src/test/java/org/apache/james/mailbox/model/MailboxAnnotationKeyTest.java
----------------------------------------------------------------------
diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/model/MailboxAnnotationKeyTest.java
b/mailbox/api/src/test/java/org/apache/james/mailbox/model/MailboxAnnotationKeyTest.java
index e6f3503..89e7b89 100644
--- a/mailbox/api/src/test/java/org/apache/james/mailbox/model/MailboxAnnotationKeyTest.java
+++ b/mailbox/api/src/test/java/org/apache/james/mailbox/model/MailboxAnnotationKeyTest.java
@@ -93,4 +93,24 @@ public class MailboxAnnotationKeyTest {
         MailboxAnnotationKey annotationKey = new MailboxAnnotationKey("/private/comment/user/name");
         assertThat(annotationKey.countComponents()).isEqualTo(4);
     }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void keyMustContainAtLeastTwoComponents() throws Exception {
+        MailboxAnnotationKey annotationKey = new MailboxAnnotationKey("/private");
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void keyVendorShouldThrowsExceptionWithTwoComponents() throws Exception {
+        MailboxAnnotationKey annotationKey = new MailboxAnnotationKey("/private/vendor");
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void keyVendorShouldThrowsExceptionWithThreeComponents() throws Exception {
+        MailboxAnnotationKey annotationKey = new MailboxAnnotationKey("/shared/vendor/token");
+    }
+
+    @Test
+    public void keyVendorShouldOKWithFourComponents() throws Exception {
+        MailboxAnnotationKey annotationKey = new MailboxAnnotationKey("/shared/vendor/token/comment");
+    }
 }
\ No newline at end of file


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