james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From matth...@apache.org
Subject [1/2] james-project git commit: JAMES-1854 Conversion User -> MailAddress
Date Thu, 05 Apr 2018 13:37:35 GMT
Repository: james-project
Updated Branches:
  refs/heads/master 5fbaeba92 -> 9866ac382


JAMES-1854 Conversion User -> MailAddress

This allows containing virtual hosting definition in the UsersRepository
by allowing calling an helper method.


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

Branch: refs/heads/master
Commit: 9866ac382bd67b6cfd8efbc8e605ffdafdbddeb7
Parents: 34b2f98
Author: benwa <btellier@linagora.com>
Authored: Tue Apr 3 09:41:01 2018 +0700
Committer: Matthieu Baechler <matthieu@apache.org>
Committed: Thu Apr 5 15:32:08 2018 +0200

----------------------------------------------------------------------
 .../apache/james/user/api/UsersRepository.java  |  7 +++++
 .../data/data-ldap-integration-testing/pom.xml  |  5 +++
 .../ldap/ReadOnlyUsersLDAPRepositoryTest.java   | 15 +++++----
 server/data/data-ldap/pom.xml                   |  5 +++
 .../user/ldap/ReadOnlyUsersLDAPRepository.java  | 25 ++++++++++++---
 .../ldap/ReadOnlyUsersLDAPRepositoryTest.java   | 17 ++++++++---
 .../james/user/lib/AbstractUsersRepository.java | 12 ++++++++
 .../domainlist/api/mock/SimpleDomainList.java   |  4 +--
 .../user/lib/AbstractUsersRepositoryTest.java   | 32 ++++++++++++++++++--
 9 files changed, 102 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/9866ac38/server/data/data-api/src/main/java/org/apache/james/user/api/UsersRepository.java
----------------------------------------------------------------------
diff --git a/server/data/data-api/src/main/java/org/apache/james/user/api/UsersRepository.java
b/server/data/data-api/src/main/java/org/apache/james/user/api/UsersRepository.java
index db8cc3a..cfc9c8f 100644
--- a/server/data/data-api/src/main/java/org/apache/james/user/api/UsersRepository.java
+++ b/server/data/data-api/src/main/java/org/apache/james/user/api/UsersRepository.java
@@ -136,6 +136,13 @@ public interface UsersRepository {
      * @throws UsersRepositoryException
      */
     String getUser(MailAddress mailAddress) throws UsersRepositoryException;
+
+    /**
+     * Returns one of the possible mail addresses to be used to send a mail to that user
+     *
+     * This makes sense as it handles virtual-hosting logic.
+     */
+    MailAddress getMailAddressFor(org.apache.james.core.User user) throws UsersRepositoryException;
     
     /**
      * Return true if the user is an admin for this repository

http://git-wip-us.apache.org/repos/asf/james-project/blob/9866ac38/server/data/data-ldap-integration-testing/pom.xml
----------------------------------------------------------------------
diff --git a/server/data/data-ldap-integration-testing/pom.xml b/server/data/data-ldap-integration-testing/pom.xml
index e99bd77..9e037ba 100644
--- a/server/data/data-ldap-integration-testing/pom.xml
+++ b/server/data/data-ldap-integration-testing/pom.xml
@@ -70,6 +70,11 @@
             <scope>test</scope>
         </dependency>
         <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>jcl-over-slf4j</artifactId>
         </dependency>

http://git-wip-us.apache.org/repos/asf/james-project/blob/9866ac38/server/data/data-ldap-integration-testing/src/test/java/org/apache/james/user/ldap/ReadOnlyUsersLDAPRepositoryTest.java
----------------------------------------------------------------------
diff --git a/server/data/data-ldap-integration-testing/src/test/java/org/apache/james/user/ldap/ReadOnlyUsersLDAPRepositoryTest.java
b/server/data/data-ldap-integration-testing/src/test/java/org/apache/james/user/ldap/ReadOnlyUsersLDAPRepositoryTest.java
index bd8ed52..e78f976 100644
--- a/server/data/data-ldap-integration-testing/src/test/java/org/apache/james/user/ldap/ReadOnlyUsersLDAPRepositoryTest.java
+++ b/server/data/data-ldap-integration-testing/src/test/java/org/apache/james/user/ldap/ReadOnlyUsersLDAPRepositoryTest.java
@@ -19,11 +19,12 @@
 package org.apache.james.user.ldap;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
 
-import org.apache.commons.configuration.ConfigurationException;
 import org.apache.commons.configuration.HierarchicalConfiguration;
 import org.apache.commons.configuration.plist.PropertyListConfiguration;
 import org.apache.james.core.MailAddress;
+import org.apache.james.domainlist.api.DomainList;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -43,10 +44,12 @@ public class ReadOnlyUsersLDAPRepositoryTest {
 
     private LdapGenericContainer ldapContainer;
     private ReadOnlyUsersLDAPRepository ldapRepository;
+    private DomainList domainList;
 
     @Before
-    public void setup() throws Exception {
+    public void setup() {
         startLdapContainer();
+        domainList = mock(DomainList.class);
     }
 
     private void startLdapContainer() {
@@ -57,13 +60,13 @@ public class ReadOnlyUsersLDAPRepositoryTest {
         ldapContainer.start();
     }
 
-    private void startUsersRepository(HierarchicalConfiguration ldapRepositoryConfiguration)
throws ConfigurationException, Exception {
-        ldapRepository = new ReadOnlyUsersLDAPRepository();
+    private void startUsersRepository(HierarchicalConfiguration ldapRepositoryConfiguration)
throws Exception {
+        ldapRepository = new ReadOnlyUsersLDAPRepository(domainList);
         ldapRepository.configure(ldapRepositoryConfiguration);
         ldapRepository.init();
     }
 
-    private HierarchicalConfiguration ldapRepositoryConfiguration() throws ConfigurationException
{
+    private HierarchicalConfiguration ldapRepositoryConfiguration() {
         PropertyListConfiguration configuration = new PropertyListConfiguration();
         configuration.addProperty("[@ldapHost]", ldapContainer.getLdapHost());
         configuration.addProperty("[@principal]", "cn=admin\\,dc=james\\,dc=org");
@@ -78,7 +81,7 @@ public class ReadOnlyUsersLDAPRepositoryTest {
         return configuration;
     }
 
-    private HierarchicalConfiguration ldapRepositoryConfigurationWithVirtualHosting() throws
ConfigurationException {
+    private HierarchicalConfiguration ldapRepositoryConfigurationWithVirtualHosting() {
         PropertyListConfiguration configuration = new PropertyListConfiguration();
         configuration.addProperty("[@ldapHost]", ldapContainer.getLdapHost());
         configuration.addProperty("[@principal]", "cn=admin\\,dc=james\\,dc=org");

http://git-wip-us.apache.org/repos/asf/james-project/blob/9866ac38/server/data/data-ldap/pom.xml
----------------------------------------------------------------------
diff --git a/server/data/data-ldap/pom.xml b/server/data/data-ldap/pom.xml
index cc3c26c..9f39be1 100644
--- a/server/data/data-ldap/pom.xml
+++ b/server/data/data-ldap/pom.xml
@@ -71,6 +71,11 @@
             <artifactId>assertj-core</artifactId>
         </dependency>
         <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>jcl-over-slf4j</artifactId>
         </dependency>

http://git-wip-us.apache.org/repos/asf/james-project/blob/9866ac38/server/data/data-ldap/src/main/java/org/apache/james/user/ldap/ReadOnlyUsersLDAPRepository.java
----------------------------------------------------------------------
diff --git a/server/data/data-ldap/src/main/java/org/apache/james/user/ldap/ReadOnlyUsersLDAPRepository.java
b/server/data/data-ldap/src/main/java/org/apache/james/user/ldap/ReadOnlyUsersLDAPRepository.java
index c7e2764..3007d85 100644
--- a/server/data/data-ldap/src/main/java/org/apache/james/user/ldap/ReadOnlyUsersLDAPRepository.java
+++ b/server/data/data-ldap/src/main/java/org/apache/james/user/ldap/ReadOnlyUsersLDAPRepository.java
@@ -30,6 +30,7 @@ import java.util.Properties;
 import java.util.Set;
 
 import javax.annotation.PostConstruct;
+import javax.inject.Inject;
 import javax.naming.Context;
 import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
@@ -45,6 +46,7 @@ import org.apache.commons.configuration.HierarchicalConfiguration;
 import org.apache.commons.lang.StringUtils;
 import org.apache.directory.api.ldap.model.filter.FilterEncoder;
 import org.apache.james.core.MailAddress;
+import org.apache.james.domainlist.api.DomainList;
 import org.apache.james.lifecycle.api.Configurable;
 import org.apache.james.user.api.UsersRepository;
 import org.apache.james.user.api.UsersRepositoryException;
@@ -338,12 +340,12 @@ public class ReadOnlyUsersLDAPRepository implements UsersRepository,
Configurabl
     // retries.
     private int maxRetries = 0;
 
-    /**
-     * Creates a new instance of ReadOnlyUsersLDAPRepository.
-     *
-     */
-    public ReadOnlyUsersLDAPRepository() {
+    private final DomainList domainList;
+
+    @Inject
+    public ReadOnlyUsersLDAPRepository(DomainList domainList) {
         super();
+        this.domainList = domainList;
     }
 
     /**
@@ -784,4 +786,17 @@ public class ReadOnlyUsersLDAPRepository implements UsersRepository,
Configurabl
     public boolean isReadOnly() {
         return true;
     }
+
+
+    @Override
+    public MailAddress getMailAddressFor(org.apache.james.core.User user) throws UsersRepositoryException
{
+        try {
+            if (supportVirtualHosting()) {
+                return new MailAddress(user.asString());
+            }
+            return new MailAddress(user.getLocalPart(), domainList.getDefaultDomain());
+        } catch (Exception e) {
+            throw new UsersRepositoryException("Failed to compute mail address associated
with the user", e);
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/9866ac38/server/data/data-ldap/src/test/java/org/apache/james/user/ldap/ReadOnlyUsersLDAPRepositoryTest.java
----------------------------------------------------------------------
diff --git a/server/data/data-ldap/src/test/java/org/apache/james/user/ldap/ReadOnlyUsersLDAPRepositoryTest.java
b/server/data/data-ldap/src/test/java/org/apache/james/user/ldap/ReadOnlyUsersLDAPRepositoryTest.java
index 517b9b2..f7b7a6b 100644
--- a/server/data/data-ldap/src/test/java/org/apache/james/user/ldap/ReadOnlyUsersLDAPRepositoryTest.java
+++ b/server/data/data-ldap/src/test/java/org/apache/james/user/ldap/ReadOnlyUsersLDAPRepositoryTest.java
@@ -20,11 +20,14 @@
 package org.apache.james.user.ldap;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
 
 import org.apache.commons.configuration.ConfigurationException;
 import org.apache.commons.configuration.ConversionException;
 import org.apache.commons.configuration.HierarchicalConfiguration;
 import org.apache.commons.configuration.plist.PropertyListConfiguration;
+import org.apache.james.domainlist.api.DomainList;
+import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
@@ -33,10 +36,16 @@ public class ReadOnlyUsersLDAPRepositoryTest {
 
     @Rule
     public ExpectedException expectedException = ExpectedException.none();
+    private DomainList domainList;
+
+    @Before
+    public void setUp() {
+        domainList = mock(DomainList.class);
+    }
 
     @Test
     public void supportVirtualHostingShouldReturnFalseByDefault() throws Exception {
-        ReadOnlyUsersLDAPRepository usersLDAPRepository = new ReadOnlyUsersLDAPRepository();
+        ReadOnlyUsersLDAPRepository usersLDAPRepository = new ReadOnlyUsersLDAPRepository(domainList);
         usersLDAPRepository.configure(ldapRepositoryConfiguration());
 
         assertThat(usersLDAPRepository.supportVirtualHosting()).isFalse();
@@ -47,7 +56,7 @@ public class ReadOnlyUsersLDAPRepositoryTest {
         HierarchicalConfiguration configuration = ldapRepositoryConfiguration();
         configuration.addProperty(ReadOnlyUsersLDAPRepository.SUPPORTS_VIRTUAL_HOSTING, "true");
 
-        ReadOnlyUsersLDAPRepository usersLDAPRepository = new ReadOnlyUsersLDAPRepository();
+        ReadOnlyUsersLDAPRepository usersLDAPRepository = new ReadOnlyUsersLDAPRepository(domainList);
         usersLDAPRepository.configure(configuration);
 
         assertThat(usersLDAPRepository.supportVirtualHosting()).isTrue();
@@ -58,7 +67,7 @@ public class ReadOnlyUsersLDAPRepositoryTest {
         HierarchicalConfiguration configuration = ldapRepositoryConfiguration();
         configuration.addProperty(ReadOnlyUsersLDAPRepository.SUPPORTS_VIRTUAL_HOSTING, "false");
 
-        ReadOnlyUsersLDAPRepository usersLDAPRepository = new ReadOnlyUsersLDAPRepository();
+        ReadOnlyUsersLDAPRepository usersLDAPRepository = new ReadOnlyUsersLDAPRepository(domainList);
         usersLDAPRepository.configure(configuration);
 
         assertThat(usersLDAPRepository.supportVirtualHosting()).isFalse();
@@ -69,7 +78,7 @@ public class ReadOnlyUsersLDAPRepositoryTest {
         HierarchicalConfiguration configuration = ldapRepositoryConfiguration();
         configuration.addProperty(ReadOnlyUsersLDAPRepository.SUPPORTS_VIRTUAL_HOSTING, "bad");
 
-        ReadOnlyUsersLDAPRepository usersLDAPRepository = new ReadOnlyUsersLDAPRepository();
+        ReadOnlyUsersLDAPRepository usersLDAPRepository = new ReadOnlyUsersLDAPRepository(domainList);
 
         expectedException.expect(ConversionException.class);
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/9866ac38/server/data/data-library/src/main/java/org/apache/james/user/lib/AbstractUsersRepository.java
----------------------------------------------------------------------
diff --git a/server/data/data-library/src/main/java/org/apache/james/user/lib/AbstractUsersRepository.java
b/server/data/data-library/src/main/java/org/apache/james/user/lib/AbstractUsersRepository.java
index 895f623..dad5f8d 100644
--- a/server/data/data-library/src/main/java/org/apache/james/user/lib/AbstractUsersRepository.java
+++ b/server/data/data-library/src/main/java/org/apache/james/user/lib/AbstractUsersRepository.java
@@ -144,4 +144,16 @@ public abstract class AbstractUsersRepository implements UsersRepository,
Config
     public boolean isReadOnly() {
         return false;
     }
+
+    @Override
+    public MailAddress getMailAddressFor(User user) throws UsersRepositoryException {
+        try {
+            if (supportVirtualHosting()) {
+                return new MailAddress(user.asString());
+            }
+            return new MailAddress(user.getLocalPart(), domainList.getDefaultDomain());
+        } catch (Exception e) {
+            throw new UsersRepositoryException("Failed to compute mail address associated
with the user", e);
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/9866ac38/server/data/data-library/src/test/java/org/apache/james/domainlist/api/mock/SimpleDomainList.java
----------------------------------------------------------------------
diff --git a/server/data/data-library/src/test/java/org/apache/james/domainlist/api/mock/SimpleDomainList.java
b/server/data/data-library/src/test/java/org/apache/james/domainlist/api/mock/SimpleDomainList.java
index e48f21c..f754bd3 100644
--- a/server/data/data-library/src/test/java/org/apache/james/domainlist/api/mock/SimpleDomainList.java
+++ b/server/data/data-library/src/test/java/org/apache/james/domainlist/api/mock/SimpleDomainList.java
@@ -35,12 +35,12 @@ public class SimpleDomainList implements DomainList {
     private final List<Domain> domains = new LinkedList<>();
 
     @Override
-    public boolean containsDomain(Domain domain) throws DomainListException {
+    public boolean containsDomain(Domain domain) {
         return domains.contains(domain);
     }
 
     @Override
-    public List<Domain> getDomains() throws DomainListException {
+    public List<Domain> getDomains() {
         return ImmutableList.copyOf(domains);
     }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/9866ac38/server/data/data-library/src/test/java/org/apache/james/user/lib/AbstractUsersRepositoryTest.java
----------------------------------------------------------------------
diff --git a/server/data/data-library/src/test/java/org/apache/james/user/lib/AbstractUsersRepositoryTest.java
b/server/data/data-library/src/test/java/org/apache/james/user/lib/AbstractUsersRepositoryTest.java
index 4f49809..83b1bbc 100644
--- a/server/data/data-library/src/test/java/org/apache/james/user/lib/AbstractUsersRepositoryTest.java
+++ b/server/data/data-library/src/test/java/org/apache/james/user/lib/AbstractUsersRepositoryTest.java
@@ -39,7 +39,8 @@ public abstract class AbstractUsersRepositoryTest {
 
     private static final Domain DOMAIN = Domain.of("domain");
 
-    protected AbstractUsersRepository usersRepository; 
+    protected AbstractUsersRepository usersRepository;
+    private SimpleDomainList domainList;
 
     /**
      * Create the repository to be tested.
@@ -56,7 +57,7 @@ public abstract class AbstractUsersRepositoryTest {
     
     public void setUp() throws Exception { 
         this.usersRepository = getUsersRepository();
-        SimpleDomainList domainList = new SimpleDomainList();
+        domainList = new SimpleDomainList();
         domainList.addDomain(DOMAIN);
         usersRepository.setDomainList(domainList);
         user1 = login("username");
@@ -69,7 +70,7 @@ public abstract class AbstractUsersRepositoryTest {
         disposeUsersRepository();
     }
     
-    private String login(String login) throws UsersRepositoryException {
+    private String login(String login) {
         if (usersRepository.supportVirtualHosting()) {
             return login + '@' + DOMAIN.name();
         } else {
@@ -345,4 +346,29 @@ public abstract class AbstractUsersRepositoryTest {
 
         assertThat(usersRepository.isAdministrator(user1)).isFalse();
     }
+
+    @Test
+    public void getMailAddressForShouldBeIdentityWhenVirtualHosting() throws Exception {
+        usersRepository.setEnableVirtualHosting(true);
+
+        // Some implementations do not support changing virtual hosting value
+        Assume.assumeTrue(usersRepository.supportVirtualHosting());
+
+        String username = "user@domain";
+        assertThat(usersRepository.getMailAddressFor(org.apache.james.core.User.fromUsername(username)))
+            .isEqualTo(username);
+    }
+
+    @Test
+    public void getMailAddressForShouldAppendDefaultDomainWhenNoVirtualHosting() throws Exception
{
+        usersRepository.setEnableVirtualHosting(false);
+        usersRepository.setDomainList(domainList);
+
+        // Some implementations do not support changing virtual hosting value
+        Assume.assumeFalse(usersRepository.supportVirtualHosting());
+
+        String username = "user";
+        assertThat(usersRepository.getMailAddressFor(org.apache.james.core.User.fromUsername(username)))
+            .isEqualTo(new MailAddress(username, domainList.getDefaultDomain()));
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message