Skip to content

Commit

Permalink
JAMES-2366 use an object MappingSource for input of RRT rewriting
Browse files Browse the repository at this point in the history
  • Loading branch information
mbaechler committed Apr 24, 2018
1 parent bfec1af commit ff8107e
Show file tree
Hide file tree
Showing 27 changed files with 614 additions and 581 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,15 +22,12 @@
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;


import java.util.AbstractMap;

import org.apache.james.GuiceJamesServer; import org.apache.james.GuiceJamesServer;
import org.apache.james.MemoryJmapTestRule; import org.apache.james.MemoryJmapTestRule;
import org.apache.james.cli.util.OutputCapture; import org.apache.james.cli.util.OutputCapture;
import org.apache.james.mailbox.store.search.ListeningMessageSearchIndex; import org.apache.james.mailbox.store.search.ListeningMessageSearchIndex;
import org.apache.james.modules.server.JMXServerModule; import org.apache.james.modules.server.JMXServerModule;
import org.apache.james.rrt.lib.Mapping; import org.apache.james.rrt.lib.Mapping;
import org.apache.james.rrt.lib.Mappings;
import org.apache.james.rrt.lib.MappingsImpl; import org.apache.james.rrt.lib.MappingsImpl;
import org.apache.james.utils.DataProbeImpl; import org.apache.james.utils.DataProbeImpl;
import org.junit.After; import org.junit.After;
Expand Down Expand Up @@ -137,12 +134,12 @@ public void addAddressMappingShouldWork() throws Exception {
ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "addaddressmapping", USER, DOMAIN, redirectionAddress}); ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "addaddressmapping", USER, DOMAIN, redirectionAddress});


assertThat(dataProbe.listMappings()) assertThat(dataProbe.listMappings())
.containsOnly( .hasSize(1)
new AbstractMap.SimpleEntry<String, Mappings>( .containsEntry(
MAIL_ADDRESS, MAIL_ADDRESS,
MappingsImpl.builder() MappingsImpl.builder()
.add(Mapping.address(redirectionAddress)) .add(Mapping.address(redirectionAddress))
.build())); .build());
} }


@Test @Test
Expand Down Expand Up @@ -186,12 +183,12 @@ public void addRegexMappingShouldWork() throws Exception {
ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "addregexmapping", USER, DOMAIN, regex}); ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "addregexmapping", USER, DOMAIN, regex});


assertThat(dataProbe.listMappings()) assertThat(dataProbe.listMappings())
.containsOnly( .hasSize(1)
new AbstractMap.SimpleEntry<String, Mappings>( .containsEntry(
MAIL_ADDRESS, MAIL_ADDRESS,
MappingsImpl.builder() MappingsImpl.builder()
.add(Mapping.regex(regex)) .add(Mapping.regex(regex))
.build())); .build());
} }


@Test @Test
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@


import javax.management.MalformedObjectNameException; import javax.management.MalformedObjectNameException;


import org.apache.james.core.Domain;
import org.apache.james.domainlist.api.DomainListManagementMBean; import org.apache.james.domainlist.api.DomainListManagementMBean;
import org.apache.james.probe.DataProbe; import org.apache.james.probe.DataProbe;
import org.apache.james.rrt.api.RecipientRewriteTableManagementMBean; import org.apache.james.rrt.api.RecipientRewriteTableManagementMBean;
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.james.domainlist.api.DomainList; import org.apache.james.domainlist.api.DomainList;
import org.apache.james.probe.DataProbe; import org.apache.james.probe.DataProbe;
import org.apache.james.rrt.api.RecipientRewriteTable; import org.apache.james.rrt.api.RecipientRewriteTable;
import org.apache.james.rrt.lib.MappingSource;
import org.apache.james.rrt.lib.Mappings; import org.apache.james.rrt.lib.Mappings;
import org.apache.james.user.api.UsersRepository; import org.apache.james.user.api.UsersRepository;


Expand Down Expand Up @@ -100,7 +101,14 @@ public List<String> listDomains() throws Exception {


@Override @Override
public Map<String, Mappings> listMappings() throws Exception { public Map<String, Mappings> listMappings() throws Exception {
return recipientRewriteTable.getAllMappings(); return recipientRewriteTable.getAllMappings()
.entrySet()
.stream()
.collect(
Guavate.toImmutableMap(
entry -> entry.getKey().asString(),
entry -> entry.getValue()));

} }


@Override @Override
Expand All @@ -110,47 +118,55 @@ public Mappings listUserDomainMappings(String user, String domain) throws Except


@Override @Override
public void addAddressMapping(String user, String domain, String toAddress) throws Exception { public void addAddressMapping(String user, String domain, String toAddress) throws Exception {
recipientRewriteTable.addAddressMapping(user, Domain.of(domain), toAddress); MappingSource source = MappingSource.fromUser(user, domain);
recipientRewriteTable.addAddressMapping(source, toAddress);
} }


@Override @Override
public void removeAddressMapping(String user, String domain, String fromAddress) throws Exception { public void removeAddressMapping(String user, String domain, String fromAddress) throws Exception {
recipientRewriteTable.removeAddressMapping(user, Domain.of(domain), fromAddress); MappingSource source = MappingSource.fromUser(user, domain);
recipientRewriteTable.removeAddressMapping(source, fromAddress);
} }


@Override @Override
public void addRegexMapping(String user, String domain, String regex) throws Exception { public void addRegexMapping(String user, String domain, String regex) throws Exception {
recipientRewriteTable.addRegexMapping(user, Domain.of(domain), regex); MappingSource source = MappingSource.fromUser(user, domain);
recipientRewriteTable.addRegexMapping(source, regex);
} }




@Override @Override
public void removeRegexMapping(String user, String domain, String regex) throws Exception { public void removeRegexMapping(String user, String domain, String regex) throws Exception {
recipientRewriteTable.removeRegexMapping(user, Domain.of(domain), regex); MappingSource source = MappingSource.fromUser(user, domain);
recipientRewriteTable.removeRegexMapping(source, regex);
} }


@Override @Override
public void addDomainAliasMapping(String aliasDomain, String deliveryDomain) throws Exception { public void addDomainAliasMapping(String aliasDomain, String deliveryDomain) throws Exception {
recipientRewriteTable.addAliasDomainMapping(Domain.of(aliasDomain), Domain.of(deliveryDomain)); recipientRewriteTable.addAliasDomainMapping(MappingSource.fromDomain(Domain.of(aliasDomain)), Domain.of(deliveryDomain));
} }


@Override @Override
public void addForwardMapping(String user, String domain, String address) throws Exception { public void addForwardMapping(String user, String domain, String address) throws Exception {
recipientRewriteTable.addForwardMapping(user, Domain.of(domain), address); MappingSource source = MappingSource.fromUser(user, domain);
recipientRewriteTable.addForwardMapping(source, address);
} }


@Override @Override
public void removeForwardMapping(String user, String domain, String address) throws Exception { public void removeForwardMapping(String user, String domain, String address) throws Exception {
recipientRewriteTable.removeForwardMapping(user, Domain.of(domain), address); MappingSource source = MappingSource.fromUser(user, domain);
recipientRewriteTable.removeForwardMapping(source, address);
} }


@Override @Override
public void addGroupMapping(String toUser, String toDomain, String fromAddress) throws Exception { public void addGroupMapping(String toUser, String toDomain, String fromAddress) throws Exception {
recipientRewriteTable.addGroupMapping(toUser, Domain.of(toDomain), fromAddress); MappingSource source = MappingSource.fromUser(toUser, toDomain);
recipientRewriteTable.addGroupMapping(source, fromAddress);
} }


@Override @Override
public void removeGroupMapping(String toUser, String toDomain, String fromAddress) throws Exception { public void removeGroupMapping(String toUser, String toDomain, String fromAddress) throws Exception {
recipientRewriteTable.removeGroupMapping(toUser, Domain.of(toDomain), fromAddress); MappingSource source = MappingSource.fromUser(toUser, toDomain);
recipientRewriteTable.removeGroupMapping(source, fromAddress);
} }
} }
4 changes: 4 additions & 0 deletions server/data/data-api/pom.xml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
<name>Apache James :: Server :: Data :: API</name> <name>Apache James :: Server :: Data :: API</name>


<dependencies> <dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>james-core</artifactId>
</dependency>
<dependency> <dependency>
<!-- only used by JamesUser (for MailAddress) --> <!-- only used by JamesUser (for MailAddress) -->
<groupId>${project.groupId}</groupId> <groupId>${project.groupId}</groupId>
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@


import org.apache.james.core.Domain; import org.apache.james.core.Domain;
import org.apache.james.rrt.lib.Mapping; import org.apache.james.rrt.lib.Mapping;
import org.apache.james.rrt.lib.MappingSource;
import org.apache.james.rrt.lib.Mappings; import org.apache.james.rrt.lib.Mappings;


/** /**
Expand Down Expand Up @@ -50,132 +51,51 @@ public String toString() {
*/ */
String WILDCARD = "*"; String WILDCARD = "*";


/** void addMapping(MappingSource source, Mapping mapping) throws RecipientRewriteTableException;
* Return the mapped MailAddress for the given address. Return null if no
* matched mapping was found
*
* @param user
* the MailAddress
* @return the mapped mailAddress
* @throws ErrorMappingException
* get thrown if an error mapping was found
* @throws RecipientRewriteTableException
*/
Mappings getMappings(String user, Domain domain) throws ErrorMappingException, RecipientRewriteTableException;


/** void removeMapping(MappingSource source, Mapping mapping) throws RecipientRewriteTableException;
* Add regex mapping
*
* @param user
* the username. Null if no username should be used
* @param domain
* the domain. Null if no domain should be used
* @param regex
* the regex.
* @throws RecipientRewriteTableException
*/
void addRegexMapping(String user, Domain domain, String regex) throws RecipientRewriteTableException;


/** void addRegexMapping(MappingSource source, String regex) throws RecipientRewriteTableException;
* Remove regex mapping
*
* @param user
* the username. Null if no username should be used
* @param domain
* the domain. Null if no domain should be used
* @param regex
* the regex.
* @throws RecipientRewriteTableException
*/
void removeRegexMapping(String user, Domain domain, String regex) throws RecipientRewriteTableException;


/*** void removeRegexMapping(MappingSource source, String regex) throws RecipientRewriteTableException;
* Add address mapping
*
* @param user
* the username. Null if no username should be used
* @param domain
* the domain. Null if no domain should be used
* @param address
* @throws RecipientRewriteTableException
*/
void addAddressMapping(String user, Domain domain, String address) throws RecipientRewriteTableException;


/** void addAddressMapping(MappingSource source, String address) throws RecipientRewriteTableException;
* Remove address mapping
*
* @param user
* the username. Null if no username should be used
* @param domain
* the domain. Null if no domain should be used
* @param address
* @throws RecipientRewriteTableException
*/
void removeAddressMapping(String user, Domain domain, String address) throws RecipientRewriteTableException;


/** void removeAddressMapping(MappingSource source, String address) throws RecipientRewriteTableException;
* Add error mapping
* void addErrorMapping(MappingSource source, String error) throws RecipientRewriteTableException;
* @param user
* the username. Null if no username should be used void removeErrorMapping(MappingSource source, String error) throws RecipientRewriteTableException;
* @param domain
* the domain. Null if no domain should be used void addAliasDomainMapping(MappingSource source, Domain realDomain) throws RecipientRewriteTableException;
* @param error
* the regex. void removeAliasDomainMapping(MappingSource source, Domain realDomain) throws RecipientRewriteTableException;
* @throws RecipientRewriteTableException
*/ void addForwardMapping(MappingSource source, String address) throws RecipientRewriteTableException;
void addErrorMapping(String user, Domain domain, String error) throws RecipientRewriteTableException;
void removeForwardMapping(MappingSource source, String address) throws RecipientRewriteTableException;

void addGroupMapping(MappingSource source, String address) throws RecipientRewriteTableException;

void removeGroupMapping(MappingSource source, String address) throws RecipientRewriteTableException;


/** /**
* Remove error mapping * Return the Mappings for the given source. Return null if no
* * matched mapping was found
* @param user *
* the username. Null if no username should be used * @throws ErrorMappingException
* @param domain * get thrown if an error mapping was found
* the domain. Null if no domain should be used
* @param error
* @throws RecipientRewriteTableException
*/ */
void removeErrorMapping(String user, Domain domain, String error) throws RecipientRewriteTableException; Mappings getMappings(String user, Domain domain) throws ErrorMappingException, RecipientRewriteTableException;


/** /**
* Return the explicit mapping stored for the given user and domain. Return * Return the explicit mapping stored for the given user and domain. Return
* null if no mapping was found * null if no mapping was found
* *
* @param user
* the username
* @param domain
* the domain
* @return the collection which holds the mappings. * @return the collection which holds the mappings.
* @throws RecipientRewriteTableException * @throws RecipientRewriteTableException
*/ */
Mappings getUserDomainMappings(String user, Domain domain) throws RecipientRewriteTableException; Mappings getUserDomainMappings(MappingSource source) throws RecipientRewriteTableException;

/**
* Add mapping
*
* @param user
* the username. Null if no username should be used
* @param domain
* the domain. Null if no domain should be used
* @param mapping
* the mapping
* @throws RecipientRewriteTableException
*/
void addMapping(String user, Domain domain, Mapping mapping) throws RecipientRewriteTableException;

/**
* Remove mapping
*
* @param user
* the username. Null if no username should be used
* @param domain
* the domain. Null if no domain should be used
* @param mapping
* the mapping
* @throws RecipientRewriteTableException
*/
void removeMapping(String user, Domain domain, Mapping mapping) throws RecipientRewriteTableException;


/** /**
* Return a Map which holds all mappings. The key is the user@domain and the * Return a Map which holds all mappings. The key is the user@domain and the
Expand All @@ -184,37 +104,7 @@ public String toString() {
* @return Map which holds all mappings * @return Map which holds all mappings
* @throws RecipientRewriteTableException * @throws RecipientRewriteTableException
*/ */
Map<String, Mappings> getAllMappings() throws RecipientRewriteTableException; Map<MappingSource, Mappings> getAllMappings() throws RecipientRewriteTableException;

/**
* Add aliasDomain mapping
*
* @param aliasDomain
* the aliasdomain which should be mapped to the realDomain
* @param realDomain
* the realDomain
* @throws RecipientRewriteTableException
*/
void addAliasDomainMapping(Domain aliasDomain, Domain realDomain) throws RecipientRewriteTableException;

/**
* Remove aliasDomain mapping
*
* @param aliasDomain
* the aliasdomain which should be mapped to the realDomain
* @param realDomain
* the realDomain
* @throws RecipientRewriteTableException
*/
void removeAliasDomainMapping(Domain aliasDomain, Domain realDomain) throws RecipientRewriteTableException;

void addForwardMapping(String user, Domain domain, String address) throws RecipientRewriteTableException;

void removeForwardMapping(String user, Domain domain, String address) throws RecipientRewriteTableException;

void addGroupMapping(String user, Domain domain, String address) throws RecipientRewriteTableException;

void removeGroupMapping(String user, Domain domain, String address) throws RecipientRewriteTableException;


class ErrorMappingException extends Exception { class ErrorMappingException extends Exception {


Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@


package org.apache.james.rrt.lib; package org.apache.james.rrt.lib;


import java.io.Serializable;
import java.util.Optional; import java.util.Optional;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.stream.Stream; import java.util.stream.Stream;
Expand Down Expand Up @@ -211,7 +212,7 @@ public Stream<Mapping> handleIdentity(Stream<Mapping> mapping) {


Stream<Mapping> handleIdentity(Stream<Mapping> nonRecursiveResult); Stream<Mapping> handleIdentity(Stream<Mapping> nonRecursiveResult);


class Impl implements Mapping { class Impl implements Mapping, Serializable {


private final Type type; private final Type type;
private final String mapping; private final String mapping;
Expand Down
Loading

0 comments on commit ff8107e

Please sign in to comment.