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.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void setUp() throws Exception {
super.setUp(); super.setUp();
InetAddress containerIp = InetAddresses.forString(fakeSmtp.getIp()); InetAddress containerIp = InetAddresses.forString(fakeSmtp.getIp());
hostSystem.getInMemoryDnsService() 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"); hostSystem.addAddressMapping(USER, DOMAIN, "ray@yopmail.com");


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


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


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


@Override @Override
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Locale; 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) { private static List<String> getDomainIP(String domain, DNSService dns, Logger log) {
List<String> domainIP = new ArrayList<String>(); List<String> domainIP = new ArrayList<String>();
try { try {
InetAddress[] addrs = dns.getAllByName(domain); Collection<InetAddress> addrs = dns.getAllByName(domain);
for (InetAddress addr : addrs) { for (InetAddress addr : addrs) {
String ip = addr.getHostAddress(); String ip = addr.getHostAddress();
if (!domainIP.contains(ip)) { if (!domainIP.contains(ip)) {
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@


import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; 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.DNSService;
import org.apache.james.dnsservice.api.mock.MockDNSService; import org.apache.james.dnsservice.api.mock.MockDNSService;
import org.apache.james.domainlist.api.DomainList; import org.apache.james.domainlist.api.DomainList;
Expand All @@ -31,6 +34,8 @@
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;


import javax.management.ImmutableDescriptor;

public abstract class AbstractDomainListTest { public abstract class AbstractDomainListTest {


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


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


@Override @Override
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public interface DNSService {
* *
* @return An array of InetAddress * @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 * Resolve the given hostname to an InetAddress based on the DNS Server. It
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -44,14 +44,19 @@ public InMemoryDNSService() {


private DNSRecord dnsRecordFor(InetAddress addresses) { private DNSRecord dnsRecordFor(InetAddress addresses) {
Collection<String> emptyList = ImmutableList.of(); 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); 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)); records.put(hostname, dnsRecordFor(mxRecords, txtRecords, addresses));
} }


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


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


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


private DNSRecord hostRecord(final String host) { 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 { private static class DNSRecord {


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


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


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


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


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


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


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


@Override @Override
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@


import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; 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.DNSService;


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


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


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


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


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


Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
****************************************************************/ ****************************************************************/
package org.apache.james.dnsservice.dnsjava; package org.apache.james.dnsservice.dnsjava;


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


@Override @Override
public InetAddress[] getAllByName(String host) throws UnknownHostException { public Collection<InetAddress> getAllByName(String host) throws UnknownHostException {
TimeMetric timeMetric = metricFactory.timer("getAllByName"); TimeMetric timeMetric = metricFactory.timer("getAllByName");
String name = allowIPLiteral(host); String name = allowIPLiteral(host);
try { try {
// Check if its local // Check if its local
if (name.equalsIgnoreCase(localHostName) || name.equalsIgnoreCase(localCanonicalHostName) || name.equals(localAddress)) { 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); InetAddress addr = org.xbill.DNS.Address.getByAddress(name);
return new InetAddress[]{addr}; return ImmutableList.of(addr);
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
Record[] records = lookupNoException(name, Type.A, "A"); 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]; ARecord a = (ARecord) records[i];
addrs[i] = InetAddress.getByAddress(name, a.getAddress().getAddress()); addrs[i] = InetAddress.getByAddress(name, a.getAddress().getAddress());
} }
return addrs; return ImmutableList.copyOf(addrs);
} else } else
throw e; throw e;
} finally { } finally {
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@


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


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


private final Iterator<HostAddress> addresses; private final Iterator<HostAddress> addresses;

public MXHostAddressIterator(Iterator<String> hosts, DNSService dns, boolean useSingleIP, Logger logger) { public MXHostAddressIterator(Iterator<String> hosts, DNSService dns, boolean useSingleIP, Logger logger) {
this(hosts, 25, dns, useSingleIP, 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); Map.Entry<String, String> hostAndPort = extractHostAndPort(nextHostname, defaultPort);


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

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


@Override @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 * Every time this method is called it will return two InetAddress instances
*/ */
@Override @Override
public InetAddress[] getAllByName(String host) throws UnknownHostException { public Collection<InetAddress> getAllByName(String host) throws UnknownHostException {
throw new UnknownHostException(); throw new UnknownHostException();
} }


Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import javax.mail.MessagingException; import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage;


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


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


@Override @Override
Expand Down

0 comments on commit d174d5e

Please sign in to comment.