Skip to content

Commit

Permalink
JAMES-1595 Promote Mappings to a actual object
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/james/project/trunk@1711974 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mbaechler committed Nov 2, 2015
1 parent 83d9026 commit 1db45b2
Show file tree
Hide file tree
Showing 30 changed files with 440 additions and 264 deletions.
4 changes: 4 additions & 0 deletions server/container/cli/pom.xml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
<groupId>org.apache.james</groupId> <groupId>org.apache.james</groupId>
<artifactId>james-server-data-api</artifactId> <artifactId>james-server-data-api</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.apache.james</groupId>
<artifactId>james-server-data-library</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.apache.james</groupId> <groupId>org.apache.james</groupId>
<artifactId>james-server-mailbox-adapter</artifactId> <artifactId>james-server-mailbox-adapter</artifactId>
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
****************************************************************/ ****************************************************************/
package org.apache.james.cli; package org.apache.james.cli;


import com.google.common.annotations.VisibleForTesting; import java.io.IOException;
import com.google.common.base.Joiner; import java.io.PrintStream;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.HelpFormatter;
Expand All @@ -40,12 +44,10 @@
import org.apache.james.cli.type.CmdType; import org.apache.james.cli.type.CmdType;
import org.apache.james.cli.utils.ValueWithUnit; import org.apache.james.cli.utils.ValueWithUnit;
import org.apache.james.mailbox.model.Quota; import org.apache.james.mailbox.model.Quota;
import org.apache.james.rrt.lib.Mappings;


import java.io.IOException; import com.google.common.annotations.VisibleForTesting;
import java.io.PrintStream; import com.google.common.base.Joiner;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;


/** /**
* Command line utility for managing various aspect of the James server. * Command line utility for managing various aspect of the James server.
Expand Down Expand Up @@ -186,7 +188,7 @@ private void executeCommand(String[] arguments, CmdType cmdType) throws Exceptio
print(probe.listMappings(), System.out); print(probe.listMappings(), System.out);
break; break;
case LISTUSERDOMAINMAPPINGS: case LISTUSERDOMAINMAPPINGS:
Collection<String> userDomainMappings = probe.listUserDomainMappings(arguments[1], arguments[2]); Mappings userDomainMappings = probe.listUserDomainMappings(arguments[1], arguments[2]);
print(userDomainMappings.toArray(new String[0]), System.out); print(userDomainMappings.toArray(new String[0]), System.out);
break; break;
case ADDADDRESSMAPPING: case ADDADDRESSMAPPING:
Expand Down Expand Up @@ -304,16 +306,16 @@ private String formatMessageValue(long value) {
return String.valueOf(value); return String.valueOf(value);
} }


private void print(Map<String, Collection<String>> map, PrintStream out) { private void print(Map<String, ? extends Iterable<String>> map, PrintStream out) {
if (map != null) { if (map != null) {
for (Entry<String, Collection<String>> entry : map.entrySet()) { for (Entry<String, ? extends Iterable<String>> entry : map.entrySet()) {
out.println(entry.getKey() + '=' + collectionToString(entry)); out.println(entry.getKey() + '=' + collectionToString(entry));
} }
out.println(); out.println();
} }
} }


private String collectionToString(Entry<String, Collection<String>> entry) { private String collectionToString(Entry<String, ? extends Iterable<String>> entry) {
return Joiner.on(',').join(entry.getValue()); return Joiner.on(',').join(entry.getValue());
} }


Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;


import org.apache.james.rrt.lib.Mappings;

public interface ServerProbe extends Closeable { public interface ServerProbe extends Closeable {
/** /**
* Add a user to this mail server. * Add a user to this mail server.
Expand Down Expand Up @@ -109,7 +111,7 @@ public interface ServerProbe extends Closeable {
* @return a Map which holds all mappings. * @return a Map which holds all mappings.
* @throws Exception * @throws Exception
*/ */
public Map<String, Collection<String>> listMappings() throws Exception; public Map<String, Mappings> listMappings() throws Exception;


/** /**
* Add address mapping. * Add address mapping.
Expand Down Expand Up @@ -149,7 +151,7 @@ public interface ServerProbe extends Closeable {
* found. * found.
* @throws Exception * @throws Exception
*/ */
public Collection<String> listUserDomainMappings(String user, String domain) throws Exception; public Mappings listUserDomainMappings(String user, String domain) throws Exception;


/** /**
* Remove regex mapping. * Remove regex mapping.
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.apache.james.domainlist.api.DomainListManagementMBean; import org.apache.james.domainlist.api.DomainListManagementMBean;
import org.apache.james.mailbox.exception.MailboxException; import org.apache.james.mailbox.exception.MailboxException;
import org.apache.james.rrt.api.RecipientRewriteTableManagementMBean; import org.apache.james.rrt.api.RecipientRewriteTableManagementMBean;
import org.apache.james.rrt.lib.Mappings;
import org.apache.james.user.api.UsersRepositoryManagementMBean; import org.apache.james.user.api.UsersRepositoryManagementMBean;


public class JmxServerProbe implements ServerProbe { public class JmxServerProbe implements ServerProbe {
Expand Down Expand Up @@ -169,7 +170,7 @@ public String[] listDomains() throws Exception {
} }


@Override @Override
public Map<String, Collection<String>> listMappings() throws Exception { public Map<String, Mappings> listMappings() throws Exception {
return virtualUserTableProxy.getAllMappings(); return virtualUserTableProxy.getAllMappings();
} }


Expand All @@ -184,7 +185,7 @@ public void removeAddressMapping(String user, String domain, String fromAddress)
} }


@Override @Override
public Collection<String> listUserDomainMappings(String user, String domain) throws Exception { public Mappings listUserDomainMappings(String user, String domain) throws Exception {
return virtualUserTableProxy.getUserDomainMappings(user, domain); return virtualUserTableProxy.getUserDomainMappings(user, domain);
} }


Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import static org.easymock.EasyMock.expectLastCall; import static org.easymock.EasyMock.expectLastCall;


import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;


import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLine;
Expand All @@ -39,6 +38,8 @@
import org.apache.james.cli.probe.ServerProbe; import org.apache.james.cli.probe.ServerProbe;
import org.apache.james.cli.type.CmdType; import org.apache.james.cli.type.CmdType;
import org.apache.james.mailbox.model.Quota; import org.apache.james.mailbox.model.Quota;
import org.apache.james.rrt.lib.Mappings;
import org.apache.james.rrt.lib.MappingsImpl;
import org.easymock.IMocksControl; import org.easymock.IMocksControl;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
Expand Down Expand Up @@ -160,7 +161,7 @@ public void listMappingsCommandShouldWork() throws Exception {
String[] arguments = { "-h", "127.0.0.1", "-p", "9999", CmdType.LISTMAPPINGS.getCommand()}; String[] arguments = { "-h", "127.0.0.1", "-p", "9999", CmdType.LISTMAPPINGS.getCommand()};
CommandLine commandLine = ServerCmd.parseCommandLine(arguments); CommandLine commandLine = ServerCmd.parseCommandLine(arguments);


expect(serverProbe.listMappings()).andReturn(new HashMap<String, Collection<String>>()); expect(serverProbe.listMappings()).andReturn(new HashMap<String, Mappings>());


control.replay(); control.replay();
testee.executeCommandLine(commandLine); testee.executeCommandLine(commandLine);
Expand All @@ -174,7 +175,7 @@ public void listUserDomainMappingsCommandShouldWork() throws Exception {
String[] arguments = { "-h", "127.0.0.1", "-p", "9999", CmdType.LISTUSERDOMAINMAPPINGS.getCommand(), user, domain}; String[] arguments = { "-h", "127.0.0.1", "-p", "9999", CmdType.LISTUSERDOMAINMAPPINGS.getCommand(), user, domain};
CommandLine commandLine = ServerCmd.parseCommandLine(arguments); CommandLine commandLine = ServerCmd.parseCommandLine(arguments);


expect(serverProbe.listUserDomainMappings(user, domain)).andReturn(new ArrayList<String>()); expect(serverProbe.listUserDomainMappings(user, domain)).andReturn(MappingsImpl.empty());


control.replay(); control.replay();
testee.executeCommandLine(commandLine); testee.executeCommandLine(commandLine);
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;


import org.apache.james.rrt.lib.Mappings;

/** /**
* Interface which should be implemented of classes which map recipients. * Interface which should be implemented of classes which map recipients.
*/ */
Expand Down Expand Up @@ -57,7 +59,7 @@ public interface RecipientRewriteTable {
* get thrown if an error mapping was found * get thrown if an error mapping was found
* @throws RecipientRewriteTableException * @throws RecipientRewriteTableException
*/ */
Collection<String> getMappings(String user, String domain) throws ErrorMappingException, RecipientRewriteTableException; Mappings getMappings(String user, String domain) throws ErrorMappingException, RecipientRewriteTableException;


/** /**
* Add regex mapping * Add regex mapping
Expand Down Expand Up @@ -145,7 +147,7 @@ public interface RecipientRewriteTable {
* @return the collection which holds the mappings. * @return the collection which holds the mappings.
* @throws RecipientRewriteTableException * @throws RecipientRewriteTableException
*/ */
Collection<String> getUserDomainMappings(String user, String domain) throws RecipientRewriteTableException; Mappings getUserDomainMappings(String user, String domain) throws RecipientRewriteTableException;


/** /**
* Add mapping * Add mapping
Expand Down Expand Up @@ -180,7 +182,7 @@ public interface RecipientRewriteTable {
* @return Map which holds all mappings * @return Map which holds all mappings
* @throws RecipientRewriteTableException * @throws RecipientRewriteTableException
*/ */
Map<String, Collection<String>> getAllMappings() throws RecipientRewriteTableException; Map<String, Mappings> getAllMappings() throws RecipientRewriteTableException;


/** /**
* Add aliasDomain mapping * Add aliasDomain mapping
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;


import org.apache.james.rrt.lib.Mappings;

/** /**
* Expose virtualusertable management functionality through JMX. * Expose virtualusertable management functionality through JMX.
*/ */
Expand Down Expand Up @@ -145,7 +147,7 @@ public interface RecipientRewriteTableManagementMBean {
* @throws Exception * @throws Exception
* If an error occurred * If an error occurred
*/ */
Collection<String> getUserDomainMappings(String user, String domain) throws Exception; Mappings getUserDomainMappings(String user, String domain) throws Exception;


/** /**
* Try to identify the right method based on the prefix of the mapping and * Try to identify the right method based on the prefix of the mapping and
Expand Down Expand Up @@ -185,5 +187,5 @@ public interface RecipientRewriteTableManagementMBean {
* @throws Exception * @throws Exception
* If an error occurred * If an error occurred
*/ */
Map<String, Collection<String>> getAllMappings() throws Exception; Map<String, Mappings> getAllMappings() throws Exception;
} }
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.lib;

import java.util.Collection;
import java.util.List;

public interface Mappings extends Iterable<String> {

Collection<String> getMappings();

void addAll(Mappings toAdd);

void add(String mapping);

boolean contains(String mapping);

int size();

boolean remove(String mapping);

void addAll(List<String> target);

boolean isEmpty();

String[] toArray(String[] strings);

}
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@
****************************************************************/ ****************************************************************/
package org.apache.james.rrt.file; package org.apache.james.rrt.file;


import com.google.common.collect.Maps; import java.util.HashMap;
import java.util.Map;

import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.HierarchicalConfiguration; import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.james.rrt.api.RecipientRewriteTableException; import org.apache.james.rrt.api.RecipientRewriteTableException;
import org.apache.james.rrt.lib.AbstractRecipientRewriteTable; import org.apache.james.rrt.lib.AbstractRecipientRewriteTable;
import org.apache.james.rrt.lib.Mappings;
import org.apache.james.rrt.lib.MappingsImpl;
import org.apache.james.rrt.lib.RecipientRewriteTableUtil; import org.apache.james.rrt.lib.RecipientRewriteTableUtil;


import java.util.Collection; import com.google.common.collect.Maps;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


/** /**
* Class responsible to implement the Virtual User Table in XML disk file. * Class responsible to implement the Virtual User Table in XML disk file.
Expand Down Expand Up @@ -63,25 +64,25 @@ protected String mapAddressInternal(String user, String domain) throws Recipient
} }


@Override @Override
protected Collection<String> getUserDomainMappingsInternal(String user, String domain) throws RecipientRewriteTableException { protected Mappings getUserDomainMappingsInternal(String user, String domain) throws RecipientRewriteTableException {
if (mappings == null) { if (mappings == null) {
return null; return null;
} else { } else {
String maps = mappings.get(user + "@" + domain); String maps = mappings.get(user + "@" + domain);
if (maps != null) { if (maps != null) {
return RecipientRewriteTableUtil.mappingToCollection(maps); return MappingsImpl.fromRawString(maps);
} else { } else {
return null; return null;
} }
} }
} }


@Override @Override
protected Map<String, Collection<String>> getAllMappingsInternal() throws RecipientRewriteTableException { protected Map<String, Mappings> getAllMappingsInternal() throws RecipientRewriteTableException {
if (mappings != null && mappings.size() > 0) { if (mappings != null && mappings.size() > 0) {
Map<String, Collection<String>> mappingsNew = new HashMap<String, Collection<String>>(); Map<String, Mappings> mappingsNew = new HashMap<String, Mappings>();
for (String key : mappings.keySet()) { for (String key : mappings.keySet()) {
mappingsNew.put(key, RecipientRewriteTableUtil.mappingToCollection(mappings.get(key))); mappingsNew.put(key, MappingsImpl.fromRawString(mappings.get(key)));
} }
return mappingsNew; return mappingsNew;
} else { } else {
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,19 +18,20 @@
****************************************************************/ ****************************************************************/
package org.apache.james.rrt.file; package org.apache.james.rrt.file;


import java.util.ArrayList;
import java.util.List;

import org.apache.commons.configuration.DefaultConfigurationBuilder; import org.apache.commons.configuration.DefaultConfigurationBuilder;
import org.apache.james.rrt.api.RecipientRewriteTable; import org.apache.james.rrt.api.RecipientRewriteTable;
import org.apache.james.rrt.api.RecipientRewriteTableException; import org.apache.james.rrt.api.RecipientRewriteTableException;
import org.apache.james.rrt.lib.AbstractRecipientRewriteTable; import org.apache.james.rrt.lib.AbstractRecipientRewriteTable;
import org.apache.james.rrt.lib.AbstractRecipientRewriteTableTest; import org.apache.james.rrt.lib.AbstractRecipientRewriteTableTest;
import org.apache.james.rrt.lib.Mappings;
import org.apache.james.rrt.lib.MappingsImpl;
import org.apache.james.rrt.lib.RecipientRewriteTableUtil; import org.apache.james.rrt.lib.RecipientRewriteTableUtil;
import org.junit.Before; import org.junit.Before;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;


import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/** /**
* Test the XML Virtual User Table implementation. * Test the XML Virtual User Table implementation.
*/ */
Expand All @@ -56,10 +57,10 @@ protected AbstractRecipientRewriteTable getRecipientRewriteTable() throws Except
protected boolean addMapping(String user, String domain, String mapping, int type) throws protected boolean addMapping(String user, String domain, String mapping, int type) throws
RecipientRewriteTableException { RecipientRewriteTableException {


Collection<String> mappings = virtualUserTable.getUserDomainMappings(user, domain); Mappings mappings = virtualUserTable.getUserDomainMappings(user, domain);


if (mappings == null) { if (mappings == null) {
mappings = new ArrayList<String>(); mappings = MappingsImpl.empty();
} else { } else {
removeMappingsFromConfig(user, domain, mappings); removeMappingsFromConfig(user, domain, mappings);
} }
Expand Down Expand Up @@ -93,7 +94,7 @@ protected boolean addMapping(String user, String domain, String mapping, int typ
protected boolean removeMapping(String user, String domain, String mapping, int type) throws protected boolean removeMapping(String user, String domain, String mapping, int type) throws
RecipientRewriteTableException { RecipientRewriteTableException {


Collection<String> mappings = virtualUserTable.getUserDomainMappings(user, domain); Mappings mappings = virtualUserTable.getUserDomainMappings(user, domain);


if (mappings == null) { if (mappings == null) {
return false; return false;
Expand Down Expand Up @@ -124,7 +125,7 @@ protected boolean removeMapping(String user, String domain, String mapping, int
return true; return true;
} }


private void removeMappingsFromConfig(String user, String domain, Collection<String> mappings) { private void removeMappingsFromConfig(String user, String domain, Mappings mappings) {
List<String> stored = new ArrayList<String>(); List<String> stored = new ArrayList<String>();
for (String c : defaultConfiguration.getStringArray("mapping")) { for (String c : defaultConfiguration.getStringArray("mapping")) {
String mapping = user + "@" + domain + "=" + RecipientRewriteTableUtil.CollectionToMapping(mappings); String mapping = user + "@" + domain + "=" + RecipientRewriteTableUtil.CollectionToMapping(mappings);
Expand Down
Loading

0 comments on commit 1db45b2

Please sign in to comment.