JAMES-2015 move InDnsService from smtp-core to dns-service-api
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/2fd6dd4f Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/2fd6dd4f Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/2fd6dd4f Branch: refs/heads/master Commit: 2fd6dd4f7c7e9bbb72952042760d65bd5c5ac86d Parents: 427eeda Author: Luc DUZAN <[email protected]> Authored: Thu May 4 19:45:15 2017 +0200 Committer: benwa <[email protected]> Committed: Wed May 17 15:09:07 2017 +0700 ---------------------------------------------------------------------- .../smtp/host/CassandraJamesSmtpHostSystem.java | 2 +- .../apache/james/mpt/smtp/SmtpHostSystem.java | 2 +- .../james/mpt/smtp/dns/InMemoryDNSService.java | 124 ----------------- server/dns-service/dnsservice-api/pom.xml | 6 + .../dnsservice/api/InMemoryDNSService.java | 134 +++++++++++++++++++ 5 files changed, 142 insertions(+), 126 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/2fd6dd4f/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/host/CassandraJamesSmtpHostSystem.java ---------------------------------------------------------------------- diff --git a/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/host/CassandraJamesSmtpHostSystem.java b/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/host/CassandraJamesSmtpHostSystem.java index d0cad96..82ffba8 100644 --- a/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/host/CassandraJamesSmtpHostSystem.java +++ b/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/host/CassandraJamesSmtpHostSystem.java @@ -33,7 +33,7 @@ import org.apache.james.modules.protocols.SMTPServerModule; import org.apache.james.mpt.monitor.SystemLoggingMonitor; import org.apache.james.mpt.session.ExternalSessionFactory; import org.apache.james.mpt.smtp.SmtpHostSystem; -import org.apache.james.mpt.smtp.dns.InMemoryDNSService; +import org.apache.james.dnsservice.api.InMemoryDNSService; import org.apache.james.utils.DataProbeImpl; import org.junit.rules.TemporaryFolder; http://git-wip-us.apache.org/repos/asf/james-project/blob/2fd6dd4f/mpt/impl/smtp/core/src/main/java/org/apache/james/mpt/smtp/SmtpHostSystem.java ---------------------------------------------------------------------- diff --git a/mpt/impl/smtp/core/src/main/java/org/apache/james/mpt/smtp/SmtpHostSystem.java b/mpt/impl/smtp/core/src/main/java/org/apache/james/mpt/smtp/SmtpHostSystem.java index 65805ce..2eac29f 100644 --- a/mpt/impl/smtp/core/src/main/java/org/apache/james/mpt/smtp/SmtpHostSystem.java +++ b/mpt/impl/smtp/core/src/main/java/org/apache/james/mpt/smtp/SmtpHostSystem.java @@ -20,7 +20,7 @@ package org.apache.james.mpt.smtp; import org.apache.james.mpt.api.HostSystem; -import org.apache.james.mpt.smtp.dns.InMemoryDNSService; +import org.apache.james.dnsservice.api.InMemoryDNSService; public interface SmtpHostSystem extends HostSystem { http://git-wip-us.apache.org/repos/asf/james-project/blob/2fd6dd4f/mpt/impl/smtp/core/src/main/java/org/apache/james/mpt/smtp/dns/InMemoryDNSService.java ---------------------------------------------------------------------- diff --git a/mpt/impl/smtp/core/src/main/java/org/apache/james/mpt/smtp/dns/InMemoryDNSService.java b/mpt/impl/smtp/core/src/main/java/org/apache/james/mpt/smtp/dns/InMemoryDNSService.java deleted file mode 100644 index 24c2ff7..0000000 --- a/mpt/impl/smtp/core/src/main/java/org/apache/james/mpt/smtp/dns/InMemoryDNSService.java +++ /dev/null @@ -1,124 +0,0 @@ -/**************************************************************** - * 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.mpt.smtp.dns; - -import java.net.InetAddress; -import java.net.UnknownHostException; -import java.util.Collection; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.function.Predicate; - -import org.apache.james.dnsservice.api.DNSService; -import org.apache.james.dnsservice.api.TemporaryResolutionException; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.Maps; -import com.google.common.net.InetAddresses; - -public class InMemoryDNSService implements DNSService { - - private Map<String,DNSRecord> records; - - public InMemoryDNSService() { - records = Maps.newHashMap(); - records.put("0.0.0.0", dnsRecordFor(InetAddresses.forString("0.0.0.0"))); - records.put("127.0.0.1", dnsRecordFor(InetAddresses.forString("127.0.0.1"))); - } - - private DNSRecord dnsRecordFor(InetAddress addresses) { - return dnsRecordFor(ImmutableList.of(), ImmutableList.of(), addresses); - } - - private DNSRecord dnsRecordFor(Collection<String> mxRecords, Collection<String> txtRecords, InetAddress... addresses) { - return new DNSRecord(addresses, mxRecords, txtRecords); - } - - public void registerRecord(String hostname, InetAddress[] addresses,Collection<String> mxRecords, Collection<String> txtRecords ){ - records.put(hostname, dnsRecordFor(mxRecords, txtRecords, addresses)); - } - - public void dropRecord(String hostname){ - records.remove(hostname); - } - - @Override - public Collection<String> findMXRecords(String hostname) throws TemporaryResolutionException { - return hostRecord(hostname).mxRecords; - } - - @Override - public Collection<String> findTXTRecords(String hostname) { - return hostRecord(hostname).txtRecords; - } - - @Override - public InetAddress[] getAllByName(String host) throws UnknownHostException { - return hostRecord(host).addresses; - } - - @Override - public InetAddress getByName(String host) throws UnknownHostException { - return hostRecord(host).addresses[0]; - } - - private DNSRecord hostRecord(String host) { - Predicate<? super Entry<String, DNSRecord>> filterByKey = (entry) -> entry.getKey().equals(host); - return getDNSEntry(filterByKey).getValue(); - } - - @Override - public InetAddress getLocalHost() throws UnknownHostException { - return InetAddress.getLocalHost(); - } - - @Override - public String getHostName(InetAddress addr) { - Predicate<? super Entry<String, DNSRecord>> filterByValue = (entry) -> entry.getValue().contains(addr); - return getDNSEntry(filterByValue).getKey(); - } - - private Entry<String, DNSRecord> getDNSEntry(Predicate<? super Entry<String, DNSRecord>> filter) { - return records.entrySet().stream() - .filter(filter) - .findFirst() - .get(); - } - - private static class DNSRecord { - - final InetAddress[] addresses; - final Collection<String> mxRecords; - final Collection<String> txtRecords; - private final List<InetAddress> addressList; - - public DNSRecord(InetAddress[] adresses, Collection<String> mxRecords, Collection<String> txtRecords) { - this.addresses = adresses; - this.mxRecords = mxRecords; - this.txtRecords = txtRecords; - this.addressList = ImmutableList.copyOf(addresses); - } - - public boolean contains(InetAddress address){ - return addressList.contains(address); - } - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/james-project/blob/2fd6dd4f/server/dns-service/dnsservice-api/pom.xml ---------------------------------------------------------------------- diff --git a/server/dns-service/dnsservice-api/pom.xml b/server/dns-service/dnsservice-api/pom.xml index ed9877c..cf7cc91 100644 --- a/server/dns-service/dnsservice-api/pom.xml +++ b/server/dns-service/dnsservice-api/pom.xml @@ -38,6 +38,12 @@ <artifactId>junit</artifactId> <scope>test</scope> </dependency> + + <dependency> + <groupId>com.google.guava</groupId> + <artifactId>guava</artifactId> + <version>18.0</version> + </dependency> </dependencies> <build> http://git-wip-us.apache.org/repos/asf/james-project/blob/2fd6dd4f/server/dns-service/dnsservice-api/src/main/java/org/apache/james/dnsservice/api/InMemoryDNSService.java ---------------------------------------------------------------------- diff --git a/server/dns-service/dnsservice-api/src/main/java/org/apache/james/dnsservice/api/InMemoryDNSService.java b/server/dns-service/dnsservice-api/src/main/java/org/apache/james/dnsservice/api/InMemoryDNSService.java new file mode 100644 index 0000000..2622d63 --- /dev/null +++ b/server/dns-service/dnsservice-api/src/main/java/org/apache/james/dnsservice/api/InMemoryDNSService.java @@ -0,0 +1,134 @@ +/**************************************************************** + * 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.dnsservice.api; + +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import com.google.common.base.Predicate; +import com.google.common.collect.FluentIterable; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Maps; +import com.google.common.net.InetAddresses; + +public class InMemoryDNSService implements DNSService { + + private Map<String,DNSRecord> records; + + public InMemoryDNSService() { + records = Maps.newHashMap(); + records.put("0.0.0.0", dnsRecordFor(InetAddresses.forString("0.0.0.0"))); + records.put("127.0.0.1", dnsRecordFor(InetAddresses.forString("127.0.0.1"))); + } + + private DNSRecord dnsRecordFor(InetAddress addresses) { + Collection<String> emptyList = ImmutableList.of(); + return dnsRecordFor(emptyList, emptyList, addresses); + } + + private DNSRecord dnsRecordFor(Collection<String> mxRecords, Collection<String> txtRecords, InetAddress... addresses) { + return new DNSRecord(addresses, mxRecords, txtRecords); + } + + public void registerRecord(String hostname, InetAddress[] addresses,Collection<String> mxRecords, Collection<String> txtRecords ){ + records.put(hostname, dnsRecordFor(mxRecords, txtRecords, addresses)); + } + + public void dropRecord(String hostname){ + records.remove(hostname); + } + + @Override + public Collection<String> findMXRecords(String hostname) throws TemporaryResolutionException { + return hostRecord(hostname).mxRecords; + } + + @Override + public Collection<String> findTXTRecords(String hostname) { + return hostRecord(hostname).txtRecords; + } + + @Override + public InetAddress[] getAllByName(String host) throws UnknownHostException { + return hostRecord(host).addresses; + } + + @Override + public InetAddress getByName(String host) throws UnknownHostException { + return hostRecord(host).addresses[0]; + } + + private DNSRecord hostRecord(final String host) { + Predicate<? super Entry<String, DNSRecord>> filterByKey = new Predicate<Entry<String, DNSRecord>>() { + @Override + public boolean apply(Entry<String, DNSRecord> entry) { + return entry.getKey().equals(host); + } + }; + return getDNSEntry(filterByKey).getValue(); + } + + @Override + public InetAddress getLocalHost() throws UnknownHostException { + return InetAddress.getLocalHost(); + } + + @Override + public String getHostName(final InetAddress addr) { + Predicate<? super Entry<String, DNSRecord>> filterByValue = new Predicate<Entry<String, DNSRecord>>() { + @Override + public boolean apply(Entry<String, DNSRecord> entry) { + return entry.getValue().contains(addr); + } + }; + + return getDNSEntry(filterByValue).getKey(); + } + + private Entry<String, DNSRecord> getDNSEntry(Predicate<? super Entry<String, DNSRecord>> filter) { + return FluentIterable.from(records.entrySet()) + .filter(filter) + .first() + .get(); + } + + private static class DNSRecord { + + final InetAddress[] addresses; + final Collection<String> mxRecords; + final Collection<String> txtRecords; + private final List<InetAddress> addressList; + + public DNSRecord(InetAddress[] adresses, Collection<String> mxRecords, Collection<String> txtRecords) { + this.addresses = adresses; + this.mxRecords = mxRecords; + this.txtRecords = txtRecords; + this.addressList = ImmutableList.copyOf(addresses); + } + + public boolean contains(InetAddress address){ + return addressList.contains(address); + } + } +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
