JAMES-1693 Provide an InMemory RRT

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

Branch: refs/heads/master
Commit: 940a0ec5ab70770e55fc7ac6c5c8c8df12a32be8
Parents: e62f6f6
Author: Benoit Tellier <[email protected]>
Authored: Mon Feb 29 12:06:02 2016 +0700
Committer: Matthieu Baechler <[email protected]>
Committed: Wed Mar 23 17:03:55 2016 +0100

----------------------------------------------------------------------
 server/data/data-memory/pom.xml                 |  26 +--
 .../rrt/memory/MemoryRecipientRewriteTable.java | 163 +++++++++++++++++++
 .../InMemoryRecipientRewriteTableTest.java      |  86 ++++++++++
 .../james/rrt/memory/InMemoryStepdefs.java      |  48 ++++++
 .../james/rrt/memory/RewriteTablesTest.java     |  32 ++++
 5 files changed, 343 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/940a0ec5/server/data/data-memory/pom.xml
----------------------------------------------------------------------
diff --git a/server/data/data-memory/pom.xml b/server/data/data-memory/pom.xml
index fcd01e3..aa35b9f 100644
--- a/server/data/data-memory/pom.xml
+++ b/server/data/data-memory/pom.xml
@@ -43,10 +43,6 @@
             <artifactId>james-server-data-library</artifactId>
         </dependency>
         <dependency>
-            <groupId>com.google.guava</groupId>
-            <artifactId>guava</artifactId>
-        </dependency>
-        <dependency>
             <groupId>org.apache.james</groupId>
             <artifactId>james-server-data-library</artifactId>
             <type>test-jar</type>
@@ -59,23 +55,29 @@
             <scope>test</scope>
         </dependency>
         <dependency>
-            <groupId>junit</groupId>
-            <artifactId>junit</artifactId>
+            <groupId>info.cukes</groupId>
+            <artifactId>cucumber-java</artifactId>
+            <scope>test</scope>
         </dependency>
         <dependency>
-            <groupId>org.assertj</groupId>
-            <artifactId>assertj-core</artifactId>
-            <version>${assertj-1.version}</version>
+            <groupId>info.cukes</groupId>
+            <artifactId>cucumber-junit</artifactId>
             <scope>test</scope>
         </dependency>
         <dependency>
             <groupId>info.cukes</groupId>
-            <artifactId>cucumber-java</artifactId>
+            <artifactId>cucumber-picocontainer</artifactId>
             <scope>test</scope>
         </dependency>
         <dependency>
-            <groupId>info.cukes</groupId>
-            <artifactId>cucumber-junit</artifactId>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.assertj</groupId>
+            <artifactId>assertj-core</artifactId>
+            <version>${assertj-1.version}</version>
             <scope>test</scope>
         </dependency>
         <dependency>

http://git-wip-us.apache.org/repos/asf/james-project/blob/940a0ec5/server/data/data-memory/src/main/java/org/apache/james/rrt/memory/MemoryRecipientRewriteTable.java
----------------------------------------------------------------------
diff --git 
a/server/data/data-memory/src/main/java/org/apache/james/rrt/memory/MemoryRecipientRewriteTable.java
 
b/server/data/data-memory/src/main/java/org/apache/james/rrt/memory/MemoryRecipientRewriteTable.java
new file mode 100644
index 0000000..4445207
--- /dev/null
+++ 
b/server/data/data-memory/src/main/java/org/apache/james/rrt/memory/MemoryRecipientRewriteTable.java
@@ -0,0 +1,163 @@
+/****************************************************************
+ * 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.rrt.memory;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.james.rrt.api.RecipientRewriteTableException;
+import org.apache.james.rrt.lib.AbstractRecipientRewriteTable;
+import org.apache.james.rrt.lib.Mappings;
+import org.apache.james.rrt.lib.MappingsImpl;
+
+import com.google.common.base.Function;
+import com.google.common.base.Objects;
+import com.google.common.base.Optional;
+import com.google.common.base.Predicate;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Multimaps;
+
+public class MemoryRecipientRewriteTable extends AbstractRecipientRewriteTable 
{
+
+    private static class InMemoryMappingEntry {
+        private final String user;
+        private final String domain;
+        private final String mapping;
+
+        public InMemoryMappingEntry(String user, String domain, String 
mapping) {
+            this.user = user;
+            this.domain = domain;
+            this.mapping = mapping;
+        }
+
+        public String getUser() {
+            return user;
+        }
+
+        public String getDomain() {
+            return domain;
+        }
+
+        public String getMapping() {
+            return mapping;
+        }
+
+        public String asKey() {
+            return getUser() + "@" + getDomain();
+        }
+
+        @Override
+        public boolean equals(Object o) {
+            if (o == null || this.getClass() != o.getClass()) {
+                return false;
+            }
+
+            InMemoryMappingEntry that = (InMemoryMappingEntry) o;
+
+            return Objects.equal(this.user, that.user)
+                && Objects.equal(this.domain, that.domain)
+                && Objects.equal(this.mapping, that.mapping);
+        }
+
+        @Override
+        public int hashCode() {
+            return Objects.hashCode(user, domain, mapping);
+        }
+    }
+
+    private final List<InMemoryMappingEntry> mappingEntries;
+
+    public MemoryRecipientRewriteTable() {
+        mappingEntries = new ArrayList<InMemoryMappingEntry>();
+    }
+
+    @Override
+    protected void addMappingInternal(String user, String domain, String 
mapping) throws RecipientRewriteTableException {
+        mappingEntries.add(new InMemoryMappingEntry(getFixedUser(user), 
getFixedDomain(domain), mapping));
+    }
+
+    @Override
+    protected void removeMappingInternal(String user, String domain, String 
mapping) throws RecipientRewriteTableException {
+        mappingEntries.remove(new InMemoryMappingEntry(getFixedUser(user), 
getFixedDomain(domain), mapping));
+    }
+
+    @Override
+    protected Mappings getUserDomainMappingsInternal(String user, String 
domain) throws RecipientRewriteTableException {
+        return retrieveMappings(user, domain)
+            .orNull();
+    }
+
+    @Override
+    protected String mapAddressInternal(String user, String domain) throws 
RecipientRewriteTableException {
+        Mappings mappings = retrieveMappings(user, domain)
+            .or(retrieveMappings(WILDCARD, domain)
+                .or(retrieveMappings(user, WILDCARD)
+                    .or(MappingsImpl.empty())));
+
+        return !mappings.isEmpty() ? mappings.serialize() : null;
+    }
+
+    @Override
+    protected Map<String, Mappings> getAllMappingsInternal() throws 
RecipientRewriteTableException {
+        if (mappingEntries.isEmpty()) {
+            return null;
+        }
+        Map<String, Collection<Mappings>> userMappingsMap = 
Multimaps.transformEntries(
+            Multimaps.index(mappingEntries, new Function<InMemoryMappingEntry, 
String>() {
+                public String apply(InMemoryMappingEntry mappingEntry) {
+                    return mappingEntry.asKey();
+                }
+            }), new Maps.EntryTransformer<String, InMemoryMappingEntry, 
Mappings>() {
+                public Mappings transformEntry(String s, InMemoryMappingEntry 
mappingEntry) {
+                    return 
MappingsImpl.fromRawString(mappingEntry.getMapping());
+                }
+            }).asMap();
+        return Maps.transformEntries(userMappingsMap, new 
Maps.EntryTransformer<String, Collection<Mappings>, Mappings>() {
+            public Mappings transformEntry(String s, Collection<Mappings> 
mappingsList) {
+                Mappings result = MappingsImpl.empty();
+                for (Mappings mappings : mappingsList) {
+                    result = result.union(mappings);
+                }
+                return result;
+            }
+        });
+    }
+
+    private Optional<Mappings> retrieveMappings(final String user, final 
String domain) {
+        List<String> userEntries = Lists.newArrayList(
+            Iterables.transform(
+                Iterables.filter(mappingEntries, new 
Predicate<InMemoryMappingEntry>() {
+                    public boolean apply(InMemoryMappingEntry mappingEntry) {
+                        return user.equals(mappingEntry.getUser()) && 
domain.equals(mappingEntry.getDomain());
+                    }
+                }), new Function<InMemoryMappingEntry, String>() {
+                    @Override
+                    public String apply(InMemoryMappingEntry mappingEntry) {
+                        return mappingEntry.getMapping();
+                    }
+                }));
+        return MappingsImpl.fromCollection(userEntries).toOptional();
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/940a0ec5/server/data/data-memory/src/test/java/org/apache/james/rrt/memory/InMemoryRecipientRewriteTableTest.java
----------------------------------------------------------------------
diff --git 
a/server/data/data-memory/src/test/java/org/apache/james/rrt/memory/InMemoryRecipientRewriteTableTest.java
 
b/server/data/data-memory/src/test/java/org/apache/james/rrt/memory/InMemoryRecipientRewriteTableTest.java
new file mode 100644
index 0000000..80032d9
--- /dev/null
+++ 
b/server/data/data-memory/src/test/java/org/apache/james/rrt/memory/InMemoryRecipientRewriteTableTest.java
@@ -0,0 +1,86 @@
+/****************************************************************
+ * 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.rrt.memory;
+
+import org.apache.commons.configuration.DefaultConfigurationBuilder;
+import org.apache.james.rrt.api.RecipientRewriteTableException;
+import org.apache.james.rrt.lib.AbstractRecipientRewriteTable;
+import org.apache.james.rrt.lib.AbstractRecipientRewriteTableTest;
+import org.slf4j.LoggerFactory;
+
+public class InMemoryRecipientRewriteTableTest extends 
AbstractRecipientRewriteTableTest {
+
+    @Override
+    protected AbstractRecipientRewriteTable getRecipientRewriteTable() throws 
Exception {
+        AbstractRecipientRewriteTable rrt = new MemoryRecipientRewriteTable();
+        rrt.setLog(LoggerFactory.getLogger("MockLog"));
+        rrt.configure(new DefaultConfigurationBuilder());
+        return rrt;
+    }
+
+    @Override
+    protected boolean addMapping(String user, String domain, String mapping, 
int type) throws RecipientRewriteTableException {
+        try {
+            switch (type) {
+            case ERROR_TYPE:
+                virtualUserTable.addErrorMapping(user, domain, mapping);
+                return true;
+            case REGEX_TYPE:
+                virtualUserTable.addRegexMapping(user, domain, mapping);
+                return true;
+            case ADDRESS_TYPE:
+                virtualUserTable.addAddressMapping(user, domain, mapping);
+                return true;
+            case ALIASDOMAIN_TYPE:
+                virtualUserTable.addAliasDomainMapping(domain, mapping);
+                return true;
+            default:
+                return false;
+            }
+        } catch (RecipientRewriteTableException e) {
+            return false;
+        }
+    }
+
+    @Override
+    protected boolean removeMapping(String user, String domain, String 
mapping, int type) throws RecipientRewriteTableException {
+        try {
+            switch (type) {
+            case ERROR_TYPE:
+                virtualUserTable.removeErrorMapping(user, domain, mapping);
+                return true;
+            case REGEX_TYPE:
+                virtualUserTable.removeRegexMapping(user, domain, mapping);
+                return true;
+            case ADDRESS_TYPE:
+                virtualUserTable.removeAddressMapping(user, domain, mapping);
+                return true;
+            case ALIASDOMAIN_TYPE:
+                virtualUserTable.removeAliasDomainMapping(domain, mapping);
+                return true;
+            default:
+                return false;
+            }
+        } catch (RecipientRewriteTableException e) {
+            return false;
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/940a0ec5/server/data/data-memory/src/test/java/org/apache/james/rrt/memory/InMemoryStepdefs.java
----------------------------------------------------------------------
diff --git 
a/server/data/data-memory/src/test/java/org/apache/james/rrt/memory/InMemoryStepdefs.java
 
b/server/data/data-memory/src/test/java/org/apache/james/rrt/memory/InMemoryStepdefs.java
new file mode 100644
index 0000000..118d3fd
--- /dev/null
+++ 
b/server/data/data-memory/src/test/java/org/apache/james/rrt/memory/InMemoryStepdefs.java
@@ -0,0 +1,48 @@
+/****************************************************************
+ * 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.rrt.memory;
+
+import org.apache.commons.configuration.DefaultConfigurationBuilder;
+import org.apache.james.rrt.lib.AbstractRecipientRewriteTable;
+import org.apache.james.rrt.lib.RewriteTablesStepdefs;
+import org.slf4j.LoggerFactory;
+
+import cucumber.api.java.Before;
+
+public class InMemoryStepdefs {
+
+    private final RewriteTablesStepdefs mainStepdefs;
+
+    public InMemoryStepdefs(RewriteTablesStepdefs mainStepdefs) {
+        this.mainStepdefs = mainStepdefs;
+    }
+
+    @Before
+    public void setup() throws Throwable {
+        mainStepdefs.rewriteTable = getRecipientRewriteTable(); 
+    }
+
+    private AbstractRecipientRewriteTable getRecipientRewriteTable() throws 
Exception {
+        MemoryRecipientRewriteTable rrt = new MemoryRecipientRewriteTable();
+        rrt.setLog(LoggerFactory.getLogger("MockLog"));
+        rrt.configure(new DefaultConfigurationBuilder());
+        return rrt;
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/940a0ec5/server/data/data-memory/src/test/java/org/apache/james/rrt/memory/RewriteTablesTest.java
----------------------------------------------------------------------
diff --git 
a/server/data/data-memory/src/test/java/org/apache/james/rrt/memory/RewriteTablesTest.java
 
b/server/data/data-memory/src/test/java/org/apache/james/rrt/memory/RewriteTablesTest.java
new file mode 100644
index 0000000..ee218f9
--- /dev/null
+++ 
b/server/data/data-memory/src/test/java/org/apache/james/rrt/memory/RewriteTablesTest.java
@@ -0,0 +1,32 @@
+/****************************************************************
+ * 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.rrt.memory;
+
+import org.junit.runner.RunWith;
+
+import cucumber.api.CucumberOptions;
+import cucumber.api.junit.Cucumber;
+
+@RunWith(Cucumber.class)
+@CucumberOptions(
+        features = { "classpath:cucumber/" },
+        glue = { "org.apache.james.rrt.lib", "org.apache.james.rrt.memory" }
+    )
+public class RewriteTablesTest {
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to