Skip to content

Commit

Permalink
JAMES-2015 Improve DNSService and InMemoryDnsService API
Browse files Browse the repository at this point in the history
  • Loading branch information
Luc DUZAN authored and chibenwa committed May 17, 2017
1 parent 787b759 commit d174d5e
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 38 deletions.
Expand Up @@ -69,7 +69,7 @@ public void setUp() throws Exception {
super.setUp();
InetAddress containerIp = InetAddresses.forString(fakeSmtp.getIp());
hostSystem.getInMemoryDnsService()
.registerRecord("yopmail.com", new InetAddress[]{containerIp}, ImmutableList.of("yopmail.com"), ImmutableList.of());
.registerRecord("yopmail.com", containerIp, "yopmail.com");
hostSystem.addAddressMapping(USER, DOMAIN, "ray@yopmail.com");

RestAssured.requestSpecification = new RequestSpecBuilder()
Expand Down
Expand Up @@ -23,8 +23,10 @@
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.google.common.collect.ImmutableList;
import org.apache.commons.configuration.DefaultConfigurationBuilder;
import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.james.dnsservice.api.DNSService;
Expand Down Expand Up @@ -61,8 +63,8 @@ public String getHostName(InetAddress inet) {
}

@Override
public InetAddress[] getAllByName(String name) throws UnknownHostException {
return new InetAddress[]{InetAddress.getByName("127.0.0.1")};
public Collection<InetAddress> getAllByName(String name) throws UnknownHostException {
return ImmutableList.of(InetAddress.getByName("127.0.0.1"));
}

@Override
Expand Down
Expand Up @@ -22,6 +22,7 @@
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;

Expand Down Expand Up @@ -178,7 +179,7 @@ private static List<String> getDomainsIP(List<String> domains, DNSService dns, L
private static List<String> getDomainIP(String domain, DNSService dns, Logger log) {
List<String> domainIP = new ArrayList<String>();
try {
InetAddress[] addrs = dns.getAllByName(domain);
Collection<InetAddress> addrs = dns.getAllByName(domain);
for (InetAddress addr : addrs) {
String ip = addr.getHostAddress();
if (!domainIP.contains(ip)) {
Expand Down
Expand Up @@ -23,6 +23,9 @@

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collection;

import com.google.common.collect.ImmutableList;
import org.apache.james.dnsservice.api.DNSService;
import org.apache.james.dnsservice.api.mock.MockDNSService;
import org.apache.james.domainlist.api.DomainList;
Expand All @@ -31,6 +34,8 @@
import org.junit.Before;
import org.junit.Test;

import javax.management.ImmutableDescriptor;

public abstract class AbstractDomainListTest {

private final String DOMAIN_1 = "domain1.tld";
Expand Down Expand Up @@ -181,8 +186,8 @@ public String getHostName(InetAddress inet) {
}

@Override
public InetAddress[] getAllByName(String name) throws UnknownHostException {
return new InetAddress[]{InetAddress.getByName("127.0.0.1")};
public Collection<InetAddress> getAllByName(String name) throws UnknownHostException {
return ImmutableList.of(InetAddress.getByName("127.0.0.1"));
}

@Override
Expand Down
Expand Up @@ -66,7 +66,7 @@ public interface DNSService {
*
* @return An array of InetAddress
*/
InetAddress[] getAllByName(String host) throws UnknownHostException;
Collection<InetAddress> getAllByName(String host) throws UnknownHostException;

/**
* Resolve the given hostname to an InetAddress based on the DNS Server. It
Expand Down
Expand Up @@ -44,14 +44,19 @@ public InMemoryDNSService() {

private DNSRecord dnsRecordFor(InetAddress addresses) {
Collection<String> emptyList = ImmutableList.of();
return dnsRecordFor(emptyList, emptyList, addresses);
return dnsRecordFor(emptyList, emptyList, ImmutableList.of(addresses));
}

private DNSRecord dnsRecordFor(Collection<String> mxRecords, Collection<String> txtRecords, InetAddress... addresses) {
private DNSRecord dnsRecordFor(Collection<String> mxRecords, Collection<String> txtRecords, List<InetAddress> addresses) {
return new DNSRecord(addresses, mxRecords, txtRecords);
}

public void registerRecord(String hostname, InetAddress[] addresses,Collection<String> mxRecords, Collection<String> txtRecords ){
public void registerRecord(String hostname, InetAddress address, String mxRecord) {
Collection<String> emptyTxtRecords = ImmutableList.of();
registerRecord(hostname, ImmutableList.of(address), ImmutableList.of(mxRecord), emptyTxtRecords);
}

public void registerRecord(String hostname, List<InetAddress> addresses, Collection<String> mxRecords, Collection<String> txtRecords ){
records.put(hostname, dnsRecordFor(mxRecords, txtRecords, addresses));
}

Expand All @@ -70,13 +75,13 @@ public Collection<String> findTXTRecords(String hostname) {
}

@Override
public InetAddress[] getAllByName(String host) throws UnknownHostException {
public List<InetAddress> getAllByName(String host) throws UnknownHostException {
return hostRecord(host).addresses;
}

@Override
public InetAddress getByName(String host) throws UnknownHostException {
return hostRecord(host).addresses[0];
return hostRecord(host).addresses.get(0);
}

private DNSRecord hostRecord(final String host) {
Expand Down Expand Up @@ -115,20 +120,18 @@ private Entry<String, DNSRecord> getDNSEntry(Predicate<? super Entry<String, DNS

private static class DNSRecord {

final InetAddress[] addresses;
final Collection<String> mxRecords;
final Collection<String> txtRecords;
private final List<InetAddress> addressList;
private final List<InetAddress> addresses;

public DNSRecord(InetAddress[] adresses, Collection<String> mxRecords, Collection<String> txtRecords) {
this.addresses = adresses;
public DNSRecord(List<InetAddress> addresses, Collection<String> mxRecords, Collection<String> txtRecords) {
this.addresses = addresses;
this.mxRecords = mxRecords;
this.txtRecords = txtRecords;
this.addressList = ImmutableList.copyOf(addresses);
}

public boolean contains(InetAddress address){
return addressList.contains(address);
return addresses.contains(address);
}
}
}
Expand Up @@ -18,6 +18,7 @@
****************************************************************/
package org.apache.james.dnsservice.api;

import com.google.common.collect.ImmutableList;
import org.apache.james.dnsservice.api.mock.MockDNSService;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
Expand All @@ -26,6 +27,7 @@

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collection;

/**
* Basic tests for AbstractDNSServer. The goal is to verify that the interface
Expand All @@ -44,8 +46,8 @@ public String getHostName(InetAddress inet) {
}

@Override
public InetAddress[] getAllByName(String name) throws UnknownHostException {
return InetAddress.getAllByName(name);
public Collection<InetAddress> getAllByName(String name) throws UnknownHostException {
return ImmutableList.copyOf(InetAddress.getAllByName(name));
}

@Override
Expand Down
Expand Up @@ -20,7 +20,9 @@

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collection;

import com.google.common.collect.ImmutableList;
import org.apache.james.dnsservice.api.DNSService;

/**
Expand Down Expand Up @@ -52,8 +54,8 @@ public String getHostName(InetAddress inet) {
}

@Override
public InetAddress[] getAllByName(String name) throws UnknownHostException {
return new InetAddress[]{InetAddress.getByName("127.0.0.1")};
public Collection<InetAddress> getAllByName(String name) throws UnknownHostException {
return ImmutableList.of(InetAddress.getByName("127.0.0.1"));
}

@Override
Expand All @@ -77,8 +79,8 @@ public String getHostName(InetAddress inet) {
}

@Override
public InetAddress[] getAllByName(String name) throws UnknownHostException {
return new InetAddress[]{InetAddress.getByName("::1")};
public Collection<InetAddress> getAllByName(String name) throws UnknownHostException {
return ImmutableList.of(InetAddress.getByName("::1"));
}

@Override
Expand Down
Expand Up @@ -49,7 +49,7 @@ public Collection<String> findTXTRecords(String hostname) {
* @see org.apache.james.dnsservice.api.DNSService#getAllByName(String)
*/
@Override
public InetAddress[] getAllByName(String host) throws UnknownHostException {
public Collection<InetAddress> getAllByName(String host) throws UnknownHostException {
throw new UnsupportedOperationException("Unimplemented Stub Method");
}

Expand Down
Expand Up @@ -18,6 +18,7 @@
****************************************************************/
package org.apache.james.dnsservice.dnsjava;

import com.google.common.collect.ImmutableList;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.james.dnsservice.api.DNSService;
Expand Down Expand Up @@ -461,17 +462,17 @@ public InetAddress getByName(String host) throws UnknownHostException {
}

@Override
public InetAddress[] getAllByName(String host) throws UnknownHostException {
public Collection<InetAddress> getAllByName(String host) throws UnknownHostException {
TimeMetric timeMetric = metricFactory.timer("getAllByName");
String name = allowIPLiteral(host);
try {
// Check if its local
if (name.equalsIgnoreCase(localHostName) || name.equalsIgnoreCase(localCanonicalHostName) || name.equals(localAddress)) {
return new InetAddress[]{getLocalHost()};
return ImmutableList.of(getLocalHost());
}

InetAddress addr = org.xbill.DNS.Address.getByAddress(name);
return new InetAddress[]{addr};
return ImmutableList.of(addr);
} catch (UnknownHostException e) {
Record[] records = lookupNoException(name, Type.A, "A");

Expand All @@ -481,7 +482,7 @@ public InetAddress[] getAllByName(String host) throws UnknownHostException {
ARecord a = (ARecord) records[i];
addrs[i] = InetAddress.getByAddress(name, a.getAddress().getAddress());
}
return addrs;
return ImmutableList.copyOf(addrs);
} else
throw e;
} finally {
Expand Down
Expand Up @@ -22,10 +22,12 @@

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.google.common.collect.ImmutableList;
import org.apache.james.dnsservice.api.DNSService;
import org.apache.mailet.HostAddress;
import org.slf4j.Logger;
Expand All @@ -42,7 +44,7 @@
public class MXHostAddressIterator implements Iterator<HostAddress> {

private final Iterator<HostAddress> addresses;

public MXHostAddressIterator(Iterator<String> hosts, DNSService dns, boolean useSingleIP, Logger logger) {
this(hosts, 25, dns, useSingleIP, logger);
}
Expand All @@ -57,15 +59,15 @@ public MXHostAddressIterator(Iterator<String> hosts, int defaultPort, DNSService
Map.Entry<String, String> hostAndPort = extractHostAndPort(nextHostname, defaultPort);

try {
final InetAddress[] addrs;
final Collection<InetAddress> addrs;
if (useSingleIP) {
addrs = new InetAddress[]{dns.getByName(hostAndPort.getKey())};
addrs = ImmutableList.of(dns.getByName(hostAndPort.getKey()));
} else {
addrs = dns.getAllByName(hostAndPort.getKey());
}
for (InetAddress addr : addrs) {
hAddresses.add(new HostAddress(hostAndPort.getKey(),
"smtp://" + addr.getHostAddress() + ":" + hostAndPort.getValue()));
"smtp://" + addr.getHostAddress() + ":" + hostAndPort.getValue()));
}
} catch (UnknownHostException uhe) {
// this should never happen, since we just got
Expand Down
Expand Up @@ -22,6 +22,8 @@
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Collection;

import com.google.common.collect.ImmutableList;
import org.apache.james.dnsservice.api.DNSService;
import org.apache.james.dnsservice.api.TemporaryResolutionException;
import static org.junit.Assert.*;
Expand Down Expand Up @@ -56,9 +58,9 @@ public InetAddress getByName(String host) throws UnknownHostException {
* Every time this method is called it will return two InetAddress instances
*/
@Override
public InetAddress[] getAllByName(String host) throws UnknownHostException {
public Collection<InetAddress> getAllByName(String host) throws UnknownHostException {
InetAddress addr = InetAddress.getLocalHost();
return new InetAddress[]{addr, addr};
return ImmutableList.of(addr, addr);
}

@Override
Expand Down Expand Up @@ -111,7 +113,7 @@ public InetAddress getByName(String host) throws UnknownHostException {
* Every time this method is called it will return two InetAddress instances
*/
@Override
public InetAddress[] getAllByName(String host) throws UnknownHostException {
public Collection<InetAddress> getAllByName(String host) throws UnknownHostException {
throw new UnknownHostException();
}

Expand Down
Expand Up @@ -42,6 +42,7 @@
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import com.google.common.collect.ImmutableList;
import org.apache.commons.net.ProtocolCommandEvent;
import org.apache.commons.net.ProtocolCommandListener;
import org.apache.commons.net.smtp.SMTPClient;
Expand Down Expand Up @@ -101,8 +102,8 @@ public Iterator<HostAddress> getSMTPHostAddresses(String domainName) {
}

@Override
public InetAddress[] getAllByName(String host) throws UnknownHostException {
return new InetAddress[]{getByName(host)};
public Collection<InetAddress> getAllByName(String host) throws UnknownHostException {
return ImmutableList.of(getByName(host));
}

@Override
Expand Down

0 comments on commit d174d5e

Please sign in to comment.