JAMES-1877 Extract DNS resolution to a DNS helper

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

Branch: refs/heads/master
Commit: 730a7ab7ef7dea7c2db5b45855f89b31ccb1534a
Parents: 7f8cf9e
Author: Benoit Tellier <btell...@linagora.com>
Authored: Fri Dec 2 10:56:14 2016 +0700
Committer: Benoit Tellier <btell...@linagora.com>
Committed: Tue Jan 10 15:12:52 2017 +0700

----------------------------------------------------------------------
 .../mailets/remoteDelivery/DnsHelper.java       | 69 ++++++++++++++++++++
 .../mailets/remoteDelivery/MailDelivrer.java    | 51 ++++-----------
 2 files changed, 83 insertions(+), 37 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/730a7ab7/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/DnsHelper.java
----------------------------------------------------------------------
diff --git 
a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/DnsHelper.java
 
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/DnsHelper.java
new file mode 100644
index 0000000..5e92040
--- /dev/null
+++ 
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/DnsHelper.java
@@ -0,0 +1,69 @@
+/****************************************************************
+ * 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 java.util.Collection;
+import java.util.Iterator;
+
+import org.apache.james.dnsservice.api.DNSService;
+import org.apache.james.dnsservice.api.TemporaryResolutionException;
+import org.apache.james.dnsservice.library.MXHostAddressIterator;
+import org.apache.mailet.HostAddress;
+import org.slf4j.Logger;
+
+@SuppressWarnings("deprecation")
+public class DnsHelper {
+
+    private final DNSService dnsServer;
+    private final RemoteDeliveryConfiguration configuration;
+    private final Logger logger;
+
+    public DnsHelper(DNSService dnsServer, RemoteDeliveryConfiguration 
configuration, Logger logger) {
+        this.dnsServer = dnsServer;
+        this.configuration = configuration;
+        this.logger = logger;
+    }
+
+    public Iterator<HostAddress> retrieveHostAddressIterator(String host) 
throws TemporaryResolutionException {
+        if (configuration.getGatewayServer().isEmpty()) {
+            return new 
MXHostAddressIterator(dnsServer.findMXRecords(host).iterator(), dnsServer, 
false, logger);
+        } else {
+            return 
getGatewaySMTPHostAddresses(configuration.getGatewayServer());
+        }
+    }
+
+    /**
+     * Returns an Iterator over org.apache.mailet.HostAddress, a specialized
+     * subclass of javax.mail.URLName, which provides location information for
+     * servers that are specified as mail handlers for the given hostname. If 
no
+     * host is found, the Iterator returned will be empty and the first call to
+     * hasNext() will return false. The Iterator is a nested iterator: the 
outer
+     * iteration is over each gateway, and the inner iteration is over
+     * potentially multiple A records for each gateway.
+     *
+     * @param gatewayServers - Collection of host[:port] Strings
+     * @return an Iterator over HostAddress instances, sorted by priority
+     * @since v2.2.0a16-unstable
+     */
+    private Iterator<HostAddress> 
getGatewaySMTPHostAddresses(Collection<String> gatewayServers) {
+        return new MXHostAddressIterator(gatewayServers.iterator(), dnsServer, 
false, logger);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/730a7ab7/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/MailDelivrer.java
----------------------------------------------------------------------
diff --git 
a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/MailDelivrer.java
 
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/MailDelivrer.java
index 941ef21..df77eb8 100644
--- 
a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/MailDelivrer.java
+++ 
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/MailDelivrer.java
@@ -34,7 +34,6 @@ import javax.mail.internet.ParseException;
 
 import org.apache.james.dnsservice.api.DNSService;
 import org.apache.james.dnsservice.api.TemporaryResolutionException;
-import org.apache.james.dnsservice.library.MXHostAddressIterator;
 import org.apache.mailet.HostAddress;
 import org.apache.mailet.Mail;
 import org.apache.mailet.MailAddress;
@@ -46,14 +45,14 @@ public class MailDelivrer {
 
     private final RemoteDeliveryConfiguration configuration;
     private final MailDelivrerToHost mailDelivrerToHost;
-    private final DNSService dnsServer;
+    private final DnsHelper dnsHelper;
     private final MessageComposer messageComposer;
     private final Logger logger;
 
     public MailDelivrer(RemoteDeliveryConfiguration configuration, 
MailDelivrerToHost mailDelivrerToHost, DNSService dnsServer, Logger logger) {
         this.configuration = configuration;
         this.mailDelivrerToHost = mailDelivrerToHost;
-        this.dnsServer = dnsServer;
+        this.dnsHelper = new DnsHelper(dnsServer, configuration, logger);
         this.messageComposer = new MessageComposer(configuration);
         this.logger = logger;
     }
@@ -107,27 +106,23 @@ public class MailDelivrer {
             logger.debug("Attempting to deliver " + mail.getName());
         }
 
-        // Figure out which servers to try to send to. This collection
-        // will hold all the possible target servers
-        Iterator<HostAddress> targetServers;
-        if (configuration.getGatewayServer().isEmpty()) {
-            MailAddress rcpt = mail.getRecipients().iterator().next();
-            String host = rcpt.getDomain();
-
-            // Lookup the possible targets
-            try {
-                targetServers = new 
MXHostAddressIterator(dnsServer.findMXRecords(host).iterator(), dnsServer, 
false, logger);
-            } catch (TemporaryResolutionException e) {
-                return handleTemporaryResolutionException(mail, host);
-            }
+        String host = retrieveTargetHostname(mail);
+        try {
+            // Figure out which servers to try to send to. This collection
+            // will hold all the possible target servers
+            Iterator<HostAddress> targetServers = 
dnsHelper.retrieveHostAddressIterator(host);
             if (!targetServers.hasNext()) {
                 return handleNoTargetServer(mail, host);
             }
-        } else {
-            targetServers = 
getGatewaySMTPHostAddresses(configuration.getGatewayServer());
+            return doDeliver(mail, mail.getMessage(), 
InternetAddressConverter.convert(mail.getRecipients()), targetServers);
+        } catch (TemporaryResolutionException e) {
+            return handleTemporaryResolutionException(mail, host);
         }
+    }
 
-        return doDeliver(mail, mail.getMessage(), 
InternetAddressConverter.convert(mail.getRecipients()), targetServers);
+    private String retrieveTargetHostname(Mail mail) {
+        MailAddress rcpt = mail.getRecipients().iterator().next();
+        return rcpt.getDomain();
     }
 
     @SuppressWarnings("deprecation")
@@ -337,24 +332,6 @@ public class MailDelivrer {
         }
     }
 
-    /**
-     * Returns an Iterator over org.apache.mailet.HostAddress, a specialized
-     * subclass of javax.mail.URLName, which provides location information for
-     * servers that are specified as mail handlers for the given hostname. If 
no
-     * host is found, the Iterator returned will be empty and the first call to
-     * hasNext() will return false. The Iterator is a nested iterator: the 
outer
-     * iteration is over each gateway, and the inner iteration is over
-     * potentially multiple A records for each gateway.
-     *
-     * @param gatewayServers - Collection of host[:port] Strings
-     * @return an Iterator over HostAddress instances, sorted by priority
-     * @since v2.2.0a16-unstable
-     */
-    @SuppressWarnings("deprecation")
-    private Iterator<HostAddress> 
getGatewaySMTPHostAddresses(Collection<String> gatewayServers) {
-        return new MXHostAddressIterator(gatewayServers.iterator(), dnsServer, 
false, logger);
-    }
-
     private void logSendFailedException(SendFailedException sfe) {
         if (configuration.isDebug()) {
             EnhancedMessagingException enhancedMessagingException = new 
EnhancedMessagingException(sfe);


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