JAMES-2352 define localhost Domain as a constant

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

Branch: refs/heads/master
Commit: bb240956cea0340962fad79638eb8c2212ab2bde
Parents: cb3a6e6
Author: Matthieu Baechler <matth...@apache.org>
Authored: Fri Mar 16 15:36:50 2018 +0100
Committer: benwa <btell...@linagora.com>
Committed: Tue Mar 27 15:13:47 2018 +0700

----------------------------------------------------------------------
 .../main/java/org/apache/james/core/Domain.java |  2 ++
 .../mailet/base/test/FakeMailContext.java       |  2 +-
 .../james/rrt/api/RecipientRewriteTable.java    |  1 +
 .../domainlist/lib/AbstractDomainList.java      | 14 ++++++++-----
 .../user/lib/AbstractJamesUsersRepository.java  |  2 +-
 .../domainlist/api/mock/SimpleDomainList.java   |  2 +-
 .../AbstractDomainListPrivateMethodsTest.java   | 22 ++++++++++----------
 .../lib/AbstractRecipientRewriteTableTest.java  | 12 +++++------
 .../impl/JamesMailetContext.java                | 20 +++++-------------
 .../apache/james/smtpserver/SMTPServerTest.java |  2 +-
 .../james/smtpserver/ValidRcptHandlerTest.java  |  4 ++--
 11 files changed, 40 insertions(+), 43 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/bb240956/core/src/main/java/org/apache/james/core/Domain.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/james/core/Domain.java 
b/core/src/main/java/org/apache/james/core/Domain.java
index 1675f15..c2d081e 100644
--- a/core/src/main/java/org/apache/james/core/Domain.java
+++ b/core/src/main/java/org/apache/james/core/Domain.java
@@ -26,6 +26,8 @@ import com.google.common.base.Preconditions;
 
 public class Domain {
 
+    public static final Domain LOCALHOST = Domain.of("localhost");
+
     public static Domain of(String domain) {
         Preconditions.checkNotNull(domain);
         Preconditions.checkArgument(!domain.isEmpty() && 
!domain.contains("@"));

http://git-wip-us.apache.org/repos/asf/james-project/blob/bb240956/mailet/base/src/test/java/org/apache/mailet/base/test/FakeMailContext.java
----------------------------------------------------------------------
diff --git 
a/mailet/base/src/test/java/org/apache/mailet/base/test/FakeMailContext.java 
b/mailet/base/src/test/java/org/apache/mailet/base/test/FakeMailContext.java
index 1cee292..27c372b 100644
--- a/mailet/base/src/test/java/org/apache/mailet/base/test/FakeMailContext.java
+++ b/mailet/base/src/test/java/org/apache/mailet/base/test/FakeMailContext.java
@@ -417,7 +417,7 @@ public class FakeMailContext implements MailetContext {
 
     @Override
     public boolean isLocalServer(Domain domain) {
-        return domain.equals(Domain.of("localhost"));
+        return domain.equals(Domain.LOCALHOST);
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/james-project/blob/bb240956/server/data/data-api/src/main/java/org/apache/james/rrt/api/RecipientRewriteTable.java
----------------------------------------------------------------------
diff --git 
a/server/data/data-api/src/main/java/org/apache/james/rrt/api/RecipientRewriteTable.java
 
b/server/data/data-api/src/main/java/org/apache/james/rrt/api/RecipientRewriteTable.java
index 40ef443..8ecbe0e 100644
--- 
a/server/data/data-api/src/main/java/org/apache/james/rrt/api/RecipientRewriteTable.java
+++ 
b/server/data/data-api/src/main/java/org/apache/james/rrt/api/RecipientRewriteTable.java
@@ -29,6 +29,7 @@ import org.apache.james.rrt.lib.Mappings;
 public interface RecipientRewriteTable {
 
     interface Domains {
+
         Domain WILDCARD = new Domain(RecipientRewriteTable.WILDCARD) {
 
             @Override

http://git-wip-us.apache.org/repos/asf/james-project/blob/bb240956/server/data/data-library/src/main/java/org/apache/james/domainlist/lib/AbstractDomainList.java
----------------------------------------------------------------------
diff --git 
a/server/data/data-library/src/main/java/org/apache/james/domainlist/lib/AbstractDomainList.java
 
b/server/data/data-library/src/main/java/org/apache/james/domainlist/lib/AbstractDomainList.java
index 849dbf6..455ef9d 100644
--- 
a/server/data/data-library/src/main/java/org/apache/james/domainlist/lib/AbstractDomainList.java
+++ 
b/server/data/data-library/src/main/java/org/apache/james/domainlist/lib/AbstractDomainList.java
@@ -21,8 +21,8 @@ package org.apache.james.domainlist.lib;
 
 import java.net.InetAddress;
 import java.net.UnknownHostException;
-import java.util.Arrays;
 import java.util.List;
+import java.util.Optional;
 import java.util.stream.Stream;
 
 import org.apache.commons.configuration.ConfigurationException;
@@ -32,6 +32,7 @@ import org.apache.james.dnsservice.api.DNSService;
 import org.apache.james.domainlist.api.DomainList;
 import org.apache.james.domainlist.api.DomainListException;
 import org.apache.james.lifecycle.api.Configurable;
+import org.apache.james.util.StreamUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -51,7 +52,6 @@ public abstract class AbstractDomainList implements 
DomainList, Configurable {
 
     private static final Logger LOGGER = 
LoggerFactory.getLogger(AbstractDomainList.class);
 
-    protected static final String LOCALHOST = "localhost";
     public static final String CONFIGURE_AUTODETECT = "autodetect";
     public static final String CONFIGURE_AUTODETECT_IP = "autodetectIP";
     public static final String CONFIGURE_DEFAULT_DOMAIN = "defaultDomain";
@@ -85,7 +85,8 @@ public abstract class AbstractDomainList implements 
DomainList, Configurable {
     }
 
     protected void addConfiguredDomains(HierarchicalConfiguration config) {
-        Arrays.stream(config.getStringArray(CONFIGURE_DOMAIN_NAMES))
+        StreamUtils.ofNullable(config.getStringArray(CONFIGURE_DOMAIN_NAMES))
+            .filter(s -> !s.isEmpty())
             .map(Domain::of)
             .forEach(
                 Throwing.consumer((Domain domain) -> {
@@ -110,7 +111,10 @@ public abstract class AbstractDomainList implements 
DomainList, Configurable {
 
     @VisibleForTesting void configureDefaultDomain(HierarchicalConfiguration 
config) throws ConfigurationException {
         try {
-            
setDefaultDomain(Domain.of(config.getString(CONFIGURE_DEFAULT_DOMAIN, 
LOCALHOST)));
+            Optional.ofNullable(
+                config.getString(CONFIGURE_DEFAULT_DOMAIN, 
Domain.LOCALHOST.asString()))
+                .map(Domain::of)
+                
.ifPresent(Throwing.consumer(this::setDefaultDomain).sneakyThrow());
 
             String hostName = InetAddress.getLocalHost().getHostName();
             if (mayChangeDefaultDomain()) {
@@ -124,7 +128,7 @@ public abstract class AbstractDomainList implements 
DomainList, Configurable {
     }
 
     private boolean mayChangeDefaultDomain() {
-        return LOCALHOST.equals(defaultDomain);
+        return Domain.LOCALHOST.equals(defaultDomain);
     }
 
     private void setDefaultDomain(Domain defaultDomain) throws 
DomainListException {

http://git-wip-us.apache.org/repos/asf/james-project/blob/bb240956/server/data/data-library/src/main/java/org/apache/james/user/lib/AbstractJamesUsersRepository.java
----------------------------------------------------------------------
diff --git 
a/server/data/data-library/src/main/java/org/apache/james/user/lib/AbstractJamesUsersRepository.java
 
b/server/data/data-library/src/main/java/org/apache/james/user/lib/AbstractJamesUsersRepository.java
index 040745f..3941868 100644
--- 
a/server/data/data-library/src/main/java/org/apache/james/user/lib/AbstractJamesUsersRepository.java
+++ 
b/server/data/data-library/src/main/java/org/apache/james/user/lib/AbstractJamesUsersRepository.java
@@ -186,7 +186,7 @@ public abstract class AbstractJamesUsersRepository extends 
AbstractUsersReposito
                         domain = Domain.of(user.substring(index + 1, 
user.length()));
                     } else {
                         username = user;
-                        domain = Domain.of("localhost");
+                        domain = Domain.LOCALHOST;
                     }
                     try {
                         mappings.put(user, getMappings(username, domain));

http://git-wip-us.apache.org/repos/asf/james-project/blob/bb240956/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 256625c..e48f21c 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
@@ -61,6 +61,6 @@ public class SimpleDomainList implements DomainList {
 
     @Override
     public Domain getDefaultDomain() {
-        return Domain.of("localhost");
+        return Domain.LOCALHOST;
     }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/james-project/blob/bb240956/server/data/data-library/src/test/java/org/apache/james/domainlist/lib/AbstractDomainListPrivateMethodsTest.java
----------------------------------------------------------------------
diff --git 
a/server/data/data-library/src/test/java/org/apache/james/domainlist/lib/AbstractDomainListPrivateMethodsTest.java
 
b/server/data/data-library/src/test/java/org/apache/james/domainlist/lib/AbstractDomainListPrivateMethodsTest.java
index 9cdabd4..a2ccf9f 100644
--- 
a/server/data/data-library/src/test/java/org/apache/james/domainlist/lib/AbstractDomainListPrivateMethodsTest.java
+++ 
b/server/data/data-library/src/test/java/org/apache/james/domainlist/lib/AbstractDomainListPrivateMethodsTest.java
@@ -86,21 +86,21 @@ public class AbstractDomainListPrivateMethodsTest {
     public void 
setDefaultDomainShouldSetFromConfigurationWhenDifferentFromLocalhost() throws 
Exception {
         HierarchicalConfiguration configuration = 
mock(HierarchicalConfiguration.class);
         String expectedDefaultDomain = "myDomain.org";
-        
when(configuration.getString(AbstractDomainList.CONFIGURE_DEFAULT_DOMAIN, 
AbstractDomainList.LOCALHOST))
+        
when(configuration.getString(AbstractDomainList.CONFIGURE_DEFAULT_DOMAIN, 
Domain.LOCALHOST.asString()))
             .thenReturn(expectedDefaultDomain);
 
         domainList.configureDefaultDomain(configuration);
 
-        
assertThat(domainList.getDefaultDomain()).isEqualTo(expectedDefaultDomain);
+        
assertThat(domainList.getDefaultDomain()).isEqualTo(Domain.of(expectedDefaultDomain));
     }
 
     @Test
     public void setDefaultDomainShouldSetFromHostnameWhenEqualsToLocalhost() 
throws Exception {
         HierarchicalConfiguration configuration = 
mock(HierarchicalConfiguration.class);
-        
when(configuration.getString(AbstractDomainList.CONFIGURE_DEFAULT_DOMAIN, 
AbstractDomainList.LOCALHOST))
-            .thenReturn(AbstractDomainList.LOCALHOST);
+        
when(configuration.getString(AbstractDomainList.CONFIGURE_DEFAULT_DOMAIN, 
Domain.LOCALHOST.asString()))
+            .thenReturn(Domain.LOCALHOST.asString());
 
-        String expectedDefaultDomain = 
InetAddress.getLocalHost().getHostName();
+        Domain expectedDefaultDomain = 
Domain.of(InetAddress.getLocalHost().getHostName());
         domainList.configureDefaultDomain(configuration);
 
         
assertThat(domainList.getDefaultDomain()).isEqualTo(expectedDefaultDomain);
@@ -109,8 +109,8 @@ public class AbstractDomainListPrivateMethodsTest {
     @Test
     public void 
setDefaultDomainShouldCreateFromHostnameWhenEqualsToLocalhost() throws 
Exception {
         HierarchicalConfiguration configuration = 
mock(HierarchicalConfiguration.class);
-        
when(configuration.getString(AbstractDomainList.CONFIGURE_DEFAULT_DOMAIN, 
AbstractDomainList.LOCALHOST))
-            .thenReturn(AbstractDomainList.LOCALHOST);
+        
when(configuration.getString(AbstractDomainList.CONFIGURE_DEFAULT_DOMAIN, 
Domain.LOCALHOST.asString()))
+            .thenReturn(Domain.LOCALHOST.asString());
 
         Domain expectedDefaultDomain = 
Domain.of(InetAddress.getLocalHost().getHostName());
         domainList.configureDefaultDomain(configuration);
@@ -121,8 +121,8 @@ public class AbstractDomainListPrivateMethodsTest {
     @Test
     public void setDefaultDomainShouldNotCreateTwiceWhenCallingTwoTimes() 
throws Exception {
         HierarchicalConfiguration configuration = 
mock(HierarchicalConfiguration.class);
-        
when(configuration.getString(AbstractDomainList.CONFIGURE_DEFAULT_DOMAIN, 
AbstractDomainList.LOCALHOST))
-            .thenReturn(AbstractDomainList.LOCALHOST);
+        
when(configuration.getString(AbstractDomainList.CONFIGURE_DEFAULT_DOMAIN, 
Domain.LOCALHOST.asString()))
+            .thenReturn(Domain.LOCALHOST.asString());
 
         Domain expectedDefaultDomain = 
Domain.of(InetAddress.getLocalHost().getHostName());
         domainList.configureDefaultDomain(configuration);
@@ -135,7 +135,7 @@ public class AbstractDomainListPrivateMethodsTest {
     public void setDefaultDomainShouldAddDomainWhenNotContained() throws 
Exception {
         HierarchicalConfiguration configuration = 
mock(HierarchicalConfiguration.class);
         String expectedDefaultDomain = "myDomain.org";
-        
when(configuration.getString(AbstractDomainList.CONFIGURE_DEFAULT_DOMAIN, 
AbstractDomainList.LOCALHOST))
+        
when(configuration.getString(AbstractDomainList.CONFIGURE_DEFAULT_DOMAIN, 
Domain.LOCALHOST.asString()))
             .thenReturn(expectedDefaultDomain);
 
         domainList.configureDefaultDomain(configuration);
@@ -147,7 +147,7 @@ public class AbstractDomainListPrivateMethodsTest {
     public void setDefaultDomainShouldNotFailWhenDomainContained() throws 
Exception {
         HierarchicalConfiguration configuration = 
mock(HierarchicalConfiguration.class);
         String expectedDefaultDomain = "myDomain.org";
-        
when(configuration.getString(AbstractDomainList.CONFIGURE_DEFAULT_DOMAIN, 
AbstractDomainList.LOCALHOST))
+        
when(configuration.getString(AbstractDomainList.CONFIGURE_DEFAULT_DOMAIN, 
Domain.LOCALHOST.asString()))
             .thenReturn(expectedDefaultDomain);
 
         domainList.addDomain(Domain.of(expectedDefaultDomain));

http://git-wip-us.apache.org/repos/asf/james-project/blob/bb240956/server/data/data-library/src/test/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTableTest.java
----------------------------------------------------------------------
diff --git 
a/server/data/data-library/src/test/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTableTest.java
 
b/server/data/data-library/src/test/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTableTest.java
index 8bfb554..a67cb33 100644
--- 
a/server/data/data-library/src/test/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTableTest.java
+++ 
b/server/data/data-library/src/test/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTableTest.java
@@ -88,7 +88,7 @@ public abstract class AbstractRecipientRewriteTableTest {
     @Test
     public void testStoreAndRetrieveRegexMapping() throws 
ErrorMappingException, RecipientRewriteTableException {
         String user = "test";
-        Domain domain = Domain.of("localhost");
+        Domain domain = Domain.LOCALHOST;
         // String regex = "(.*):{$1}@localhost";
         // String regex2 = "(.+):{$1}@test";
         String regex = "(.*)@localhost";
@@ -119,7 +119,7 @@ public abstract class AbstractRecipientRewriteTableTest {
     public void testStoreAndRetrieveAddressMapping() throws 
ErrorMappingException, RecipientRewriteTableException {
 
         String user = "test";
-        Domain domain = Domain.of("localhost");
+        Domain domain = Domain.LOCALHOST;
         String address = "test@localhost2";
         String address2 = "test@james";
 
@@ -142,7 +142,7 @@ public abstract class AbstractRecipientRewriteTableTest {
     @Test
     public void testStoreAndRetrieveErrorMapping() throws 
ErrorMappingException, RecipientRewriteTableException {
         String user = "test";
-        Domain domain = Domain.of("localhost");
+        Domain domain = Domain.LOCALHOST;
         String error = "bounce!";
 
         assertThat(virtualUserTable.getMappings(user, domain)).describedAs("No 
mapping").isNull();
@@ -165,7 +165,7 @@ public abstract class AbstractRecipientRewriteTableTest {
     public void testStoreAndRetrieveWildCardAddressMapping() throws 
ErrorMappingException, RecipientRewriteTableException {
         String user = "test";
         String user2 = "test2";
-        Domain domain = Domain.of("localhost");
+        Domain domain = Domain.LOCALHOST;
         String address = "test@localhost2";
         String address2 = "test@james";
 
@@ -273,7 +273,7 @@ public abstract class AbstractRecipientRewriteTableTest {
     @Test
     public void addMappingShouldThrowWhenMappingAlreadyExists() throws 
Exception {
         String user = "test";
-        Domain domain = Domain.of("localhost");
+        Domain domain = Domain.LOCALHOST;
         String address = "test@localhost2";
 
         expectedException.expect(RecipientRewriteTableException.class);
@@ -285,7 +285,7 @@ public abstract class AbstractRecipientRewriteTableTest {
     @Test
     public void 
addMappingShouldNotThrowWhenMappingAlreadyExistsWithAnOtherType() throws 
Exception {
         String user = "test";
-        Domain domain = Domain.of("localhost");
+        Domain domain = Domain.LOCALHOST;
         String address = "test@localhost2";
 
         addMapping(user, domain, address, ADDRESS_TYPE);

http://git-wip-us.apache.org/repos/asf/james-project/blob/bb240956/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/JamesMailetContext.java
----------------------------------------------------------------------
diff --git 
a/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/JamesMailetContext.java
 
b/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/JamesMailetContext.java
index a52d80f..210a848 100644
--- 
a/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/JamesMailetContext.java
+++ 
b/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/JamesMailetContext.java
@@ -456,22 +456,12 @@ public class JamesMailetContext implements MailetContext, 
Configurable {
             // list of supported domains that isn't localhost. If that
             // doesn't work, use the hostname, even if it is localhost.
             if (postMasterAddress.indexOf('@') < 0) {
-                Domain domainName = null; // the domain to use
-                // loop through candidate domains until we find one or exhaust
-                // the
-                // list
-                Domain localhost = Domain.of("localhost");
-                for (Domain dom : domains.getDomains()) {
-                    if (!(localhost.equals(dom))) {
-                        domainName = dom; // ok, not localhost, so
-                        // use it
-                    }
-                }
+                Domain domainName = domains.getDomains().stream()
+                    .filter(domain -> !Domain.LOCALHOST.equals(domain))
+                    .findFirst()
+                    .orElse(domains.getDefaultDomain());
 
-                // if we found a suitable domain, use it. Otherwise fallback to
-                // the
-                // host name.
-                postMasterAddress = postMasterAddress + "@" + (domainName != 
null ? domainName.name() : domains.getDefaultDomain());
+                postMasterAddress = postMasterAddress + "@" + 
domainName.asString();
             }
             try {
                 this.postmaster = new MailAddress(postMasterAddress);

http://git-wip-us.apache.org/repos/asf/james-project/blob/bb240956/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SMTPServerTest.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SMTPServerTest.java
 
b/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SMTPServerTest.java
index e9f6d1a..26acc87 100644
--- 
a/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SMTPServerTest.java
+++ 
b/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SMTPServerTest.java
@@ -234,7 +234,7 @@ public class SMTPServerTest {
         queue = queueFactory.createQueue(MailQueueFactory.SPOOL);
         chain.put("mailqueuefactory", MailQueueFactory.class, queueFactory);
         MemoryDomainList domainList = new 
MemoryDomainList(mock(DNSService.class));
-        domainList.addDomain(Domain.of("localhost"));
+        domainList.addDomain(Domain.LOCALHOST);
         chain.put("domainlist", DomainList.class, domainList);
         
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/bb240956/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/ValidRcptHandlerTest.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/ValidRcptHandlerTest.java
 
b/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/ValidRcptHandlerTest.java
index 88755b6..c9db765 100644
--- 
a/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/ValidRcptHandlerTest.java
+++ 
b/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/ValidRcptHandlerTest.java
@@ -116,8 +116,8 @@ public class ValidRcptHandlerTest {
     private RecipientRewriteTable setUpRecipientRewriteTable(DomainList 
domainList) throws RecipientRewriteTableException {
         MemoryRecipientRewriteTable memoryRecipientRewriteTable = new 
MemoryRecipientRewriteTable();
         memoryRecipientRewriteTable.setDomainList(domainList);
-        memoryRecipientRewriteTable.addAddressMapping(USER1, 
Domain.of("localhost"), "address");
-        memoryRecipientRewriteTable.addErrorMapping(USER2, 
Domain.of("localhost"), "554 BOUNCE");
+        memoryRecipientRewriteTable.addAddressMapping(USER1, Domain.LOCALHOST, 
"address");
+        memoryRecipientRewriteTable.addErrorMapping(USER2, Domain.LOCALHOST, 
"554 BOUNCE");
         return memoryRecipientRewriteTable;
     }
 


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

Reply via email to