james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rouaz...@apache.org
Subject [02/50] [abbrv] james-project git commit: JAMES-1877 Extract HeloName provider and test it
Date Tue, 10 Jan 2017 14:18:17 GMT
JAMES-1877 Extract HeloName provider and test it


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

Branch: refs/heads/master
Commit: 1cb969bdda7db0ba8f1c8b52adeb27186bb566e7
Parents: e82996e
Author: Benoit Tellier <btellier@linagora.com>
Authored: Tue Nov 29 10:27:13 2016 +0700
Committer: Benoit Tellier <btellier@linagora.com>
Committed: Tue Jan 10 11:37:10 2017 +0700

----------------------------------------------------------------------
 .../james/transport/mailets/RemoteDelivery.java | 30 ++------
 .../remoteDelivery/HeloNameProvider.java        | 53 +++++++++++++
 .../remoteDelivery/HeloNameProviderTest.java    | 79 ++++++++++++++++++++
 3 files changed, 140 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/1cb969bd/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RemoteDelivery.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RemoteDelivery.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RemoteDelivery.java
index 0e7e48d..c4113b5 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RemoteDelivery.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RemoteDelivery.java
@@ -53,12 +53,10 @@ import javax.mail.internet.MimeMultipart;
 import javax.mail.internet.MimePart;
 import javax.mail.internet.ParseException;
 
-import com.sun.mail.smtp.SMTPTransport;
 import org.apache.james.dnsservice.api.DNSService;
 import org.apache.james.dnsservice.api.TemporaryResolutionException;
 import org.apache.james.dnsservice.library.MXHostAddressIterator;
 import org.apache.james.domainlist.api.DomainList;
-import org.apache.james.domainlist.api.DomainListException;
 import org.apache.james.lifecycle.api.LifecycleUtil;
 import org.apache.james.metrics.api.Metric;
 import org.apache.james.metrics.api.MetricFactory;
@@ -67,6 +65,7 @@ import org.apache.james.queue.api.MailQueue;
 import org.apache.james.queue.api.MailQueue.MailQueueException;
 import org.apache.james.queue.api.MailQueue.MailQueueItem;
 import org.apache.james.queue.api.MailQueueFactory;
+import org.apache.james.transport.mailets.remoteDelivery.HeloNameProvider;
 import org.apache.james.transport.mailets.remoteDelivery.RemoteDeliverySocketFactory;
 import org.apache.james.transport.util.Patterns;
 import org.apache.james.util.TimeConverter;
@@ -77,6 +76,8 @@ import org.apache.mailet.MailetContext;
 import org.apache.mailet.base.GenericMailet;
 import org.slf4j.Logger;
 
+import com.sun.mail.smtp.SMTPTransport;
+
 /**
  * <p>The RemoteDelivery mailet delivers messages to a remote SMTP server able to deliver
or forward messages to their final
  * destination.
@@ -252,8 +253,6 @@ public class RemoteDelivery extends GenericMailet implements Runnable
{
 
     private MailQueue queue;
 
-    private String heloName;
-
     private Logger logger;
 
     private boolean usePriority;
@@ -266,6 +265,7 @@ public class RemoteDelivery extends GenericMailet implements Runnable
{
 
     private MetricFactory metricFactory;
     private Metric outgoingMailsMetric;
+    private HeloNameProvider heloNameProvider;
 
     @Inject
     public void setDomainList(DomainList domainList) {
@@ -444,7 +444,7 @@ public class RemoteDelivery extends GenericMailet implements Runnable
{
             dnsProblemRetry = Integer.parseInt(dnsRetry);
         }
 
-        heloName = getInitParameter("heloName");
+        heloNameProvider = new HeloNameProvider(getInitParameter("heloName"), domainList);
 
         String prio = getInitParameter("usePriority");
         if (prio != null) {
@@ -751,7 +751,7 @@ public class RemoteDelivery extends GenericMailet implements Runnable
{
         props.put("mail.smtp.connectiontimeout", connectionTimeout + "");
         props.put("mail.smtp.sendpartial", String.valueOf(sendPartial));
 
-        props.put("mail.smtp.localhost", getHeloName());
+        props.put("mail.smtp.localhost", heloNameProvider.getHeloName());
 
         // handle starttls
         props.put("mail.smtp.starttls.enable", String.valueOf(startTLS));
@@ -970,7 +970,7 @@ public class RemoteDelivery extends GenericMailet implements Runnable
{
                     SMTPTransport transport = null;
                     try {
                         transport =  (SMTPTransport) session.getTransport(outgoingMailServer);
-                        transport.setLocalHost( props.getProperty("mail.smtp.localhost",
heloName) );
+                        transport.setLocalHost( props.getProperty("mail.smtp.localhost",
heloNameProvider.getHeloName()) );
                         try {
                             if (authUser != null) {
                                 transport.connect(outgoingMailServer.getHostName(), authUser,
authPass);
@@ -1555,7 +1555,7 @@ public class RemoteDelivery extends GenericMailet implements Runnable
{
         PrintWriter out = new PrintWriter(sout, true);
         String machine;
         try {
-            machine = getHeloName();
+            machine = heloNameProvider.getHeloName();
 
         } catch (Exception e) {
             machine = "[address unknown]";
@@ -1631,18 +1631,4 @@ public class RemoteDelivery extends GenericMailet implements Runnable
{
         return new MXHostAddressIterator(gateways, dnsServer, false, logger);
     }
 
-    protected String getHeloName() {
-        if (heloName == null) {
-            // TODO: Maybe we should better just lookup the hostname via dns
-            try {
-                return domainList.getDefaultDomain();
-            } catch (DomainListException e) {
-                log("Unable to access DomainList", e);
-                return "localhost";
-            }
-        } else {
-            return heloName;
-        }
-    }
-
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/1cb969bd/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/HeloNameProvider.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/HeloNameProvider.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/HeloNameProvider.java
new file mode 100644
index 0000000..b91c68e
--- /dev/null
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/HeloNameProvider.java
@@ -0,0 +1,53 @@
+/****************************************************************
+ * 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.transport.mailets.remoteDelivery;
+
+import org.apache.james.domainlist.api.DomainList;
+import org.apache.james.domainlist.api.DomainListException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class HeloNameProvider {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(HeloNameProvider.class);
+    public static final String LOCALHOST = "localhost";
+
+    private final String heloName;
+    private final DomainList domainList;
+
+    public HeloNameProvider(String heloName, DomainList domainList) {
+        this.heloName = heloName;
+        this.domainList = domainList;
+    }
+
+    public String getHeloName() {
+        if (heloName == null) {
+            // TODO: Maybe we should better just lookup the hostname via dns
+            try {
+                return domainList.getDefaultDomain();
+            } catch (DomainListException e) {
+                LOGGER.warn("Unable to access DomainList", e);
+                return LOCALHOST;
+            }
+        } else {
+            return heloName;
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/1cb969bd/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remoteDelivery/HeloNameProviderTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remoteDelivery/HeloNameProviderTest.java
b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remoteDelivery/HeloNameProviderTest.java
new file mode 100644
index 0000000..987b99b
--- /dev/null
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remoteDelivery/HeloNameProviderTest.java
@@ -0,0 +1,79 @@
+/****************************************************************
+ * 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.transport.mailets.remoteDelivery;
+
+import static org.mockito.Mockito.mock;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.when;
+
+import org.apache.james.domainlist.api.DomainList;
+import org.apache.james.domainlist.api.DomainListException;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+public class HeloNameProviderTest {
+
+    public static final String DOMAIN = "domain";
+
+    @Rule
+    public ExpectedException expectedException = ExpectedException.none();
+
+    private DomainList domainList;
+
+    @Before
+    public void setUp() {
+        domainList = mock(DomainList.class);
+    }
+
+    @Test
+    public void getHeloNameShouldReturnNonNullProvidedHeloName() {
+        HeloNameProvider heloNameProvider = new HeloNameProvider(DOMAIN, domainList);
+
+        assertThat(heloNameProvider.getHeloName()).isEqualTo(DOMAIN);
+    }
+
+    @Test
+    public void getHeloNameShouldReturnDomainListDefaultDomainOnNullHeloName() throws DomainListException
{
+        when(domainList.getDefaultDomain()).thenReturn(DOMAIN);
+        HeloNameProvider heloNameProvider = new HeloNameProvider(null, domainList);
+
+        assertThat(heloNameProvider.getHeloName()).isEqualTo(DOMAIN);
+    }
+
+    @Test
+    public void getHeloNameShouldReturnLocalhostOnDomainListException() throws DomainListException
{
+        when(domainList.getDefaultDomain()).thenThrow(new DomainListException("any message"));
+        HeloNameProvider heloNameProvider = new HeloNameProvider(null, domainList);
+
+        assertThat(heloNameProvider.getHeloName()).isEqualTo(HeloNameProvider.LOCALHOST);
+    }
+
+    @Test
+    public void getHeloNameShouldPropagateRuntimeExceptions() throws DomainListException
{
+        when(domainList.getDefaultDomain()).thenThrow(new RuntimeException());
+        HeloNameProvider heloNameProvider = new HeloNameProvider(null, domainList);
+
+        expectedException.expect(RuntimeException.class);
+        heloNameProvider.getHeloName();
+    }
+
+}


---------------------------------------------------------------------
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