Skip to content

Commit

Permalink
JAMES-1877 Extract DNS resolution to a DNS helper
Browse files Browse the repository at this point in the history
  • Loading branch information
chibenwa committed Jan 10, 2017
1 parent 7f8cf9e commit 730a7ab
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 37 deletions.
@@ -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);
}

}
Expand Up @@ -34,7 +34,6 @@

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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -107,27 +106,23 @@ private ExecutionResult tryDeliver(Mail mail) throws MessagingException {
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")
Expand Down Expand Up @@ -337,24 +332,6 @@ private ExecutionResult handleNoTargetServer(Mail mail, String host) {
}
}

/**
* 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);
Expand Down

0 comments on commit 730a7ab

Please sign in to comment.