MAILBOX-307 Mailbox ACL JSON representation SHOULD be explicit
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/64948992 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/64948992 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/64948992 Branch: refs/heads/master Commit: 649489920fcbef807fdfab54f6624df73e56be78 Parents: 58c84a2 Author: benwa <btell...@linagora.com> Authored: Wed Sep 27 13:31:33 2017 +0700 Committer: Matthieu Baechler <matth...@apache.org> Committed: Fri Sep 29 09:20:40 2017 +0200 ---------------------------------------------------------------------- .../apache/james/mailbox/model/MailboxACL.java | 13 ---- .../cassandra/mail/CassandraACLMapper.java | 10 +-- .../store/json/MailboxACLJsonConverter.java | 64 ++++++++++++++++++++ .../json/SimpleMailboxACLJsonConverter.java | 64 -------------------- .../store/json/event/MailboxConverter.java | 6 +- .../store/json/MailboxACLJsonConverterTest.java | 27 ++++----- 6 files changed, 85 insertions(+), 99 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/64948992/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxACL.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxACL.java b/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxACL.java index 64180fb..a7afa02 100644 --- a/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxACL.java +++ b/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxACL.java @@ -199,14 +199,6 @@ public class MailboxACL { this.value = EnumSet.of(Right.forChar(right.asCharacter())); } - /* Used for json serialization (probably a bad idea) */ - public Rfc4314Rights(int serializedRights) { - this(copyOf(Right.allRights - .stream() - .filter(right -> ((serializedRights >> right.ordinal()) & 1) != 0) - .collect(Collectors.toList()))); - } - public Rfc4314Rights(String serializedRfc4314Rights) throws UnsupportedRightException { this.value = copyOf(serializedRfc4314Rights.chars() .mapToObj(i -> (char) i) @@ -264,11 +256,6 @@ public class MailboxACL { return value.contains(Right.forChar(right.asCharacter())); } - /* Used for json serialization (probably a bad idea) */ - public int serializeAsInteger() { - return value.stream().mapToInt(x -> 1 << x.ordinal()).sum(); - } - public boolean equals(Object o) { if (o instanceof Rfc4314Rights) { Rfc4314Rights that = (Rfc4314Rights) o; http://git-wip-us.apache.org/repos/asf/james-project/blob/64948992/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraACLMapper.java ---------------------------------------------------------------------- diff --git a/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraACLMapper.java b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraACLMapper.java index 98d46c7..a0dccf0 100644 --- a/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraACLMapper.java +++ b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraACLMapper.java @@ -41,7 +41,7 @@ import org.apache.james.mailbox.cassandra.table.CassandraMailboxTable; import org.apache.james.mailbox.exception.MailboxException; import org.apache.james.mailbox.exception.UnsupportedRightException; import org.apache.james.mailbox.model.MailboxACL; -import org.apache.james.mailbox.store.json.SimpleMailboxACLJsonConverter; +import org.apache.james.mailbox.store.json.MailboxACLJsonConverter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -143,7 +143,7 @@ public class CassandraACLMapper { executor.executeVoid( insertStatement.bind() .setUUID(CassandraACLTable.ID, cassandraId.asUuid()) - .setString(CassandraACLTable.ACL, SimpleMailboxACLJsonConverter.toJson(mailboxACL))) + .setString(CassandraACLTable.ACL, MailboxACLJsonConverter.toJson(mailboxACL))) .join(); } catch (JsonProcessingException e) { throw Throwables.propagate(e); @@ -169,7 +169,7 @@ public class CassandraACLMapper { return executor.execute( conditionalUpdateStatement.bind() .setUUID(CassandraACLTable.ID, cassandraId.asUuid()) - .setString(CassandraACLTable.ACL, SimpleMailboxACLJsonConverter.toJson(aclWithVersion.mailboxACL)) + .setString(CassandraACLTable.ACL, MailboxACLJsonConverter.toJson(aclWithVersion.mailboxACL)) .setLong(CassandraACLTable.VERSION, aclWithVersion.version + 1) .setLong(OLD_VERSION, aclWithVersion.version)) .join(); @@ -183,7 +183,7 @@ public class CassandraACLMapper { return executor.execute( conditionalInsertStatement.bind() .setUUID(CassandraACLTable.ID, cassandraId.asUuid()) - .setString(CassandraACLTable.ACL, SimpleMailboxACLJsonConverter.toJson(acl))) + .setString(CassandraACLTable.ACL, MailboxACLJsonConverter.toJson(acl))) .join(); } catch (JsonProcessingException exception) { throw Throwables.propagate(exception); @@ -201,7 +201,7 @@ public class CassandraACLMapper { private MailboxACL deserializeACL(CassandraId cassandraId, String serializedACL) { try { - return SimpleMailboxACLJsonConverter.toACL(serializedACL); + return MailboxACLJsonConverter.toACL(serializedACL); } catch(IOException exception) { LOG.error("Unable to read stored ACL. " + "We will use empty ACL instead." + http://git-wip-us.apache.org/repos/asf/james-project/blob/64948992/mailbox/store/src/main/java/org/apache/james/mailbox/store/json/MailboxACLJsonConverter.java ---------------------------------------------------------------------- diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/json/MailboxACLJsonConverter.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/json/MailboxACLJsonConverter.java new file mode 100644 index 0000000..8fdfc83 --- /dev/null +++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/json/MailboxACLJsonConverter.java @@ -0,0 +1,64 @@ +/**************************************************************** + * 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.json; + +import java.io.IOException; + +import org.apache.james.mailbox.model.MailboxACL; + +import com.fasterxml.jackson.annotation.JsonValue; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.KeyDeserializer; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.module.SimpleModule; + +public class MailboxACLJsonConverter { + + interface Rfc4314RightsMixIn { + @JsonValue + String serialize(); + } + + static class ACLKeyDeserializer extends KeyDeserializer { + @Override + public Object deserializeKey(String key, DeserializationContext deserializationContext) throws IOException { + return MailboxACL.EntryKey.deserialize(key); + } + } + + private static final ObjectMapper objectMapper = new ObjectMapper(); + + static { + SimpleModule module = new SimpleModule() + .addKeyDeserializer(MailboxACL.EntryKey.class, new ACLKeyDeserializer()); + objectMapper + .addMixIn(MailboxACL.Rfc4314Rights.class, Rfc4314RightsMixIn.class) + .registerModule(module); + } + + public static String toJson(MailboxACL acl) throws JsonProcessingException { + return objectMapper.writeValueAsString(acl); + } + + public static MailboxACL toACL(String jsonACLString) throws IOException { + return objectMapper.readValue(jsonACLString, MailboxACL.class); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/64948992/mailbox/store/src/main/java/org/apache/james/mailbox/store/json/SimpleMailboxACLJsonConverter.java ---------------------------------------------------------------------- diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/json/SimpleMailboxACLJsonConverter.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/json/SimpleMailboxACLJsonConverter.java deleted file mode 100644 index a19e613..0000000 --- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/json/SimpleMailboxACLJsonConverter.java +++ /dev/null @@ -1,64 +0,0 @@ -/**************************************************************** - * 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.json; - -import java.io.IOException; - -import org.apache.james.mailbox.model.MailboxACL; - -import com.fasterxml.jackson.annotation.JsonValue; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.DeserializationContext; -import com.fasterxml.jackson.databind.KeyDeserializer; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.module.SimpleModule; - -public class SimpleMailboxACLJsonConverter { - - interface Rfc4314RightsMixIn { - @JsonValue - int serializeAsInteger(); - } - - static class ACLKeyDeserializer extends KeyDeserializer { - @Override - public Object deserializeKey(String key, DeserializationContext deserializationContext) throws IOException { - return MailboxACL.EntryKey.deserialize(key); - } - } - - private static final ObjectMapper objectMapper = new ObjectMapper(); - - static { - SimpleModule module = new SimpleModule() - .addKeyDeserializer(MailboxACL.EntryKey.class, new ACLKeyDeserializer()); - objectMapper - .addMixIn(MailboxACL.Rfc4314Rights.class, Rfc4314RightsMixIn.class) - .registerModule(module); - } - - public static String toJson(MailboxACL acl) throws JsonProcessingException { - return objectMapper.writeValueAsString(acl); - } - - public static MailboxACL toACL(String jsonACLString) throws IOException { - return objectMapper.readValue(jsonACLString, MailboxACL.class); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/64948992/mailbox/store/src/main/java/org/apache/james/mailbox/store/json/event/MailboxConverter.java ---------------------------------------------------------------------- diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/json/event/MailboxConverter.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/json/event/MailboxConverter.java index 185899e..6df34f5 100644 --- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/json/event/MailboxConverter.java +++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/json/event/MailboxConverter.java @@ -24,7 +24,7 @@ import java.io.IOException; import org.apache.james.mailbox.MailboxListener; import org.apache.james.mailbox.model.MailboxPath; import org.apache.james.mailbox.store.event.EventFactory; -import org.apache.james.mailbox.store.json.SimpleMailboxACLJsonConverter; +import org.apache.james.mailbox.store.json.MailboxACLJsonConverter; import org.apache.james.mailbox.store.json.event.dto.MailboxDataTransferObject; import org.apache.james.mailbox.store.mail.model.Mailbox; import org.apache.james.mailbox.store.mail.model.MailboxIdDeserialisationException; @@ -51,7 +51,7 @@ public class MailboxConverter { mailboxDataTransferObject.getName()), mailboxDataTransferObject.getUidValidity()); try { - mailbox.setACL(SimpleMailboxACLJsonConverter.toACL(mailboxDataTransferObject.getSerializedACL())); + mailbox.setACL(MailboxACLJsonConverter.toACL(mailboxDataTransferObject.getSerializedACL())); mailbox.setMailboxId(mailboxIdDeserializer.deserialize(mailboxDataTransferObject.getSerializedMailboxId())); } catch (IOException e) { LOGGER.warn("Failed to deserialize ACL", e); @@ -82,7 +82,7 @@ public class MailboxConverter { private String getSerializedACL(Mailbox mailbox) { try { - return SimpleMailboxACLJsonConverter.toJson(mailbox.getACL()); + return MailboxACLJsonConverter.toJson(mailbox.getACL()); } catch (JsonProcessingException e) { return "{\"entries\":{}}"; } http://git-wip-us.apache.org/repos/asf/james-project/blob/64948992/mailbox/store/src/test/java/org/apache/james/mailbox/store/json/MailboxACLJsonConverterTest.java ---------------------------------------------------------------------- diff --git a/mailbox/store/src/test/java/org/apache/james/mailbox/store/json/MailboxACLJsonConverterTest.java b/mailbox/store/src/test/java/org/apache/james/mailbox/store/json/MailboxACLJsonConverterTest.java index 99bcd8f..fb8dcbb 100644 --- a/mailbox/store/src/test/java/org/apache/james/mailbox/store/json/MailboxACLJsonConverterTest.java +++ b/mailbox/store/src/test/java/org/apache/james/mailbox/store/json/MailboxACLJsonConverterTest.java @@ -66,7 +66,6 @@ public class MailboxACLJsonConverterTest { } public ACLMapBuilder addSingleGroupEntryToMap() { - Rfc4314Rights rights = new Rfc4314Rights(DeleteMailbox, DeleteMessages, Lookup, Post, Read, Write, WriteSeenFlag); EntryKey key = new EntryKey("group", NameType.group, true); map.put(key, rights); @@ -81,59 +80,59 @@ public class MailboxACLJsonConverterTest { @Test public void emptyACLShouldBeWellSerialized() throws Exception { - assertThatJson(SimpleMailboxACLJsonConverter.toJson(MailboxACL.EMPTY)) + assertThatJson(MailboxACLJsonConverter.toJson(MailboxACL.EMPTY)) .isEqualTo("{\"entries\":{}}") .when(Option.IGNORING_ARRAY_ORDER); } @Test public void singleUserEntryACLShouldBeWellSerialized() throws Exception { - assertThatJson(SimpleMailboxACLJsonConverter.toJson(new ACLMapBuilder().addSingleUserEntryToMap().buildAsACL())) - .isEqualTo("{\"entries\":{\"-user\":2040}}") + assertThatJson(MailboxACLJsonConverter.toJson(new ACLMapBuilder().addSingleUserEntryToMap().buildAsACL())) + .isEqualTo("{\"entries\":{\"-user\":\"klprstwx\"}}") .when(Option.IGNORING_ARRAY_ORDER); } @Test public void singleGroupEntryACLShouldBeWellSerialized() throws Exception { - assertThatJson(SimpleMailboxACLJsonConverter.toJson(new ACLMapBuilder().addSingleGroupEntryToMap().buildAsACL())) - .isEqualTo("{\"entries\":{\"-$group\":2032}}") + assertThatJson(MailboxACLJsonConverter.toJson(new ACLMapBuilder().addSingleGroupEntryToMap().buildAsACL())) + .isEqualTo("{\"entries\":{\"-$group\":\"lprstwx\"}}") .when(Option.IGNORING_ARRAY_ORDER); } @Test public void singleSpecialEntryACLShouldBeWellSerialized() throws Exception { - assertThatJson(SimpleMailboxACLJsonConverter.toJson(new ACLMapBuilder().addSingleSpecialEntryToMap().buildAsACL())) - .isEqualTo("{\"entries\":{\"-special\":1968}}") + assertThatJson(MailboxACLJsonConverter.toJson(new ACLMapBuilder().addSingleSpecialEntryToMap().buildAsACL())) + .isEqualTo("{\"entries\":{\"-special\":\"lpstwx\"}}") .when(Option.IGNORING_ARRAY_ORDER); } @Test public void multipleEntriesACLShouldBeWellSerialized() throws Exception { - assertThatJson(SimpleMailboxACLJsonConverter.toJson(new ACLMapBuilder().addSingleUserEntryToMap().addSingleGroupEntryToMap().buildAsACL())) - .isEqualTo("{\"entries\":{\"-user\":2040,\"-$group\":2032}}") + assertThatJson(MailboxACLJsonConverter.toJson(new ACLMapBuilder().addSingleUserEntryToMap().addSingleGroupEntryToMap().buildAsACL())) + .isEqualTo("{\"entries\":{\"-user\":\"klprstwx\",\"-$group\":\"lprstwx\"}}") .when(Option.IGNORING_ARRAY_ORDER); } @Test public void emptyACLShouldBeWellDeSerialized() throws Exception { - assertThat(SimpleMailboxACLJsonConverter.toACL("{\"entries\":{}}")).isEqualTo(MailboxACL.EMPTY); + assertThat(MailboxACLJsonConverter.toACL("{\"entries\":{}}")).isEqualTo(MailboxACL.EMPTY); } @Test public void singleUserEntryACLShouldBeWellDeSerialized() throws Exception { - assertThatJson(SimpleMailboxACLJsonConverter.toACL("{\"entries\":{\"-user\":2040}}")) + assertThatJson(MailboxACLJsonConverter.toACL("{\"entries\":{\"-user\":\"klprstwx\"}}")) .isEqualTo(new ACLMapBuilder().addSingleUserEntryToMap().buildAsACL()); } @Test public void singleGroupEntryACLShouldBeWellDeSerialized() throws Exception { - assertThatJson(SimpleMailboxACLJsonConverter.toACL("{\"entries\":{\"-$group\":2032}}")) + assertThatJson(MailboxACLJsonConverter.toACL("{\"entries\":{\"-$group\":\"lprstwx\"}}")) .isEqualTo(new ACLMapBuilder().addSingleGroupEntryToMap().buildAsACL()); } @Test public void multipleEntriesACLShouldBeWellDeSerialized() throws Exception { - assertThatJson(SimpleMailboxACLJsonConverter.toACL("{\"entries\":{\"-user\":2040,\"-$group\":2032}}")) + assertThatJson(MailboxACLJsonConverter.toACL("{\"entries\":{\"-user\":\"klprstwx\",\"-$group\":\"lprstwx\"}}")) .isEqualTo(new ACLMapBuilder().addSingleUserEntryToMap().addSingleGroupEntryToMap().buildAsACL()); } --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org