JAMES-1710 Role should be able to handle x- specific roles
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/c96225d4 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/c96225d4 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/c96225d4 Branch: refs/heads/master Commit: c96225d4480ea7622a18a8a0a42c5601cfee8472 Parents: fefc9c5 Author: Matthieu Baechler <[email protected]> Authored: Fri Mar 25 17:36:04 2016 +0100 Committer: Raphael Ouazana <[email protected]> Committed: Tue Mar 29 14:22:38 2016 +0200 ---------------------------------------------------------------------- .../apache/james/jmap/model/mailbox/Role.java | 71 +++++++++++++++----- .../exception/MailboxRoleNotFoundException.java | 2 +- .../james/jmap/model/mailbox/RoleTest.java | 5 ++ 3 files changed, 61 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/c96225d4/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/mailbox/Role.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/mailbox/Role.java b/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/mailbox/Role.java index b173e23..deaf90c 100644 --- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/mailbox/Role.java +++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/mailbox/Role.java @@ -19,38 +19,77 @@ package org.apache.james.jmap.model.mailbox; import java.util.Locale; +import java.util.Map; import java.util.Optional; +import java.util.function.Function; +import java.util.stream.Collectors; import com.fasterxml.jackson.annotation.JsonValue; +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Objects; +import com.google.common.collect.ImmutableList; -public enum Role { - - INBOX("inbox"), - ARCHIVE("archive"), - DRAFTS("drafts"), - OUTBOX("outbox"), - SENT("sent"), - TRASH("trash"), - SPAM("spam"), - TEMPLATES("templates"); +public class Role { + public static final String USER_DEFINED_ROLE_PREFIX = "x-"; + + public static final Role INBOX = new Role("inbox"); + public static final Role ARCHIVE = new Role("archive"); + public static final Role DRAFTS = new Role("drafts"); + public static final Role OUTBOX = new Role("outbox"); + public static final Role SENT = new Role("sent"); + public static final Role TRASH = new Role("trash"); + public static final Role SPAM = new Role("spam"); + public static final Role TEMPLATES = new Role("templates"); + + private static final Map<String, Role> ROLES = + ImmutableList.<Role>of(INBOX, ARCHIVE, DRAFTS, OUTBOX, SENT, TRASH, SPAM, TEMPLATES) + .stream() + .collect(Collectors.toMap((Role x) -> x.name.toLowerCase(Locale.ENGLISH), Function.identity())); + private final String name; - Role(String name) { + @VisibleForTesting Role(String name) { this.name = name; } public static Optional<Role> from(String name) { - for (Role role : values()) { - if (role.serialize().equals(name.toLowerCase(Locale.ENGLISH))) { - return Optional.of(role); - } + Optional<Role> predefinedRole = Optional.ofNullable(ROLES.get(name.toLowerCase(Locale.ENGLISH))); + if (predefinedRole.isPresent()) { + return predefinedRole; + } else { + return tryBuildCustomRole(name); } - return Optional.empty(); } + private static Optional<Role> tryBuildCustomRole(String name) { + if (name.startsWith(USER_DEFINED_ROLE_PREFIX)) { + return Optional.of(new Role(name)); + } + return Optional.empty(); + } + @JsonValue public String serialize() { return name; } + + @Override + public int hashCode() { + return Objects.hashCode(name); + } + + @Override + public boolean equals(Object object) { + if (object instanceof Role) { + Role that = (Role) object; + return Objects.equal(this.name, that.name); + } + return false; + } + + @Override + public String toString() { + return Objects.toStringHelper(this).add("name", name).toString(); + } } http://git-wip-us.apache.org/repos/asf/james-project/blob/c96225d4/server/protocols/jmap/src/main/java/org/apache/james/jmap/send/exception/MailboxRoleNotFoundException.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/send/exception/MailboxRoleNotFoundException.java b/server/protocols/jmap/src/main/java/org/apache/james/jmap/send/exception/MailboxRoleNotFoundException.java index 39d73e1..574edf1 100644 --- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/send/exception/MailboxRoleNotFoundException.java +++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/send/exception/MailboxRoleNotFoundException.java @@ -24,7 +24,7 @@ import org.apache.james.queue.api.MailQueue.MailQueueException; public class MailboxRoleNotFoundException extends MailQueueException { public MailboxRoleNotFoundException(Role role) { - super("Unable to find a mailbox with role " + role.name()); + super("Unable to find a mailbox with role " + role.serialize()); } } http://git-wip-us.apache.org/repos/asf/james-project/blob/c96225d4/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/mailbox/RoleTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/mailbox/RoleTest.java b/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/mailbox/RoleTest.java index 9ad4baa..e65bfa6 100644 --- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/mailbox/RoleTest.java +++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/mailbox/RoleTest.java @@ -54,4 +54,9 @@ public class RoleTest { } } + @Test + public void fromShouldReturnSomethingWhenXPrefixedRole() { + assertThat(Role.from("x-client-specific-role")).isEqualTo(Optional.of(new Role("x-client-specific-role"))); + } + } \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
