/** * Extract the domain name (without subdomain). The Guava {@link InternetDomainName} implementation * provides a method to correctly handle this (and handles special cases for TLDs with multiple * names. eg. for lb01.store.amazon.co.uk, only amazon.co.uk would be extracted). * It uses https://publicsuffix.org behind the scenes. * <p> * Some domains (eg. completely randomly defined PTR domains) are not considered to have a public * suffix according to Guava. For those, the only option is to manually extract the domain with * string operations. This should be a rare case. */ public static void parseReverseLookupDomain(PtrDnsAnswer.Builder dnsAnswerBuilder, String hostname) { dnsAnswerBuilder.fullDomain(hostname); final InternetDomainName internetDomainName = InternetDomainName.from(hostname); if (internetDomainName.hasPublicSuffix()) { // Use Guava to extract domain name. final InternetDomainName topDomainName = internetDomainName.topDomainUnderRegistrySuffix(); dnsAnswerBuilder.domain(topDomainName.toString()); } else { /* Manually extract domain name. * Eg. for hostname test.some-domain.com, only some-domain.com will be extracted. */ String[] split = hostname.split("\\."); if (split.length > 1) { dnsAnswerBuilder.domain(split[split.length - 2] + "." + split[split.length - 1]); } else if (split.length == 1) { dnsAnswerBuilder.domain(hostname); // Domain is a single word with no dots. } else { dnsAnswerBuilder.domain(""); // Domain is blank. } } }
public void testRegistrySuffixMultipleUnders() { // PSL has both *.uk and *.sch.uk; the latter should win. // See http://code.google.com/p/guava-libraries/issues/detail?id=1176 InternetDomainName domain = InternetDomainName.from("www.essex.sch.uk"); assertTrue(domain.hasRegistrySuffix()); assertEquals("essex.sch.uk", domain.registrySuffix().toString()); assertEquals("www.essex.sch.uk", domain.topDomainUnderRegistrySuffix().toString()); }
/** * Extract the domain name (without subdomain). The Guava {@link InternetDomainName} implementation * provides a method to correctly handle this (and handles special cases for TLDs with multiple * names. eg. for lb01.store.amazon.co.uk, only amazon.co.uk would be extracted). * It uses https://publicsuffix.org behind the scenes. * <p> * Some domains (eg. completely randomly defined PTR domains) are not considered to have a public * suffix according to Guava. For those, the only option is to manually extract the domain with * string operations. This should be a rare case. */ public static void parseReverseLookupDomain(PtrDnsAnswer.Builder dnsAnswerBuilder, String hostname) { dnsAnswerBuilder.fullDomain(hostname); final InternetDomainName internetDomainName = InternetDomainName.from(hostname); if (internetDomainName.hasPublicSuffix()) { // Use Guava to extract domain name. final InternetDomainName topDomainName = internetDomainName.topDomainUnderRegistrySuffix(); dnsAnswerBuilder.domain(topDomainName.toString()); } else { /* Manually extract domain name. * Eg. for hostname test.some-domain.com, only some-domain.com will be extracted. */ String[] split = hostname.split("\\."); if (split.length > 1) { dnsAnswerBuilder.domain(split[split.length - 2] + "." + split[split.length - 1]); } else if (split.length == 1) { dnsAnswerBuilder.domain(hostname); // Domain is a single word with no dots. } else { dnsAnswerBuilder.domain(""); // Domain is blank. } } }
public void testRegistrySuffixMultipleUnders() { // PSL has both *.uk and *.sch.uk; the latter should win. // See http://code.google.com/p/guava-libraries/issues/detail?id=1176 InternetDomainName domain = InternetDomainName.from("www.essex.sch.uk"); assertTrue(domain.hasRegistrySuffix()); assertEquals("essex.sch.uk", domain.registrySuffix().toString()); assertEquals("www.essex.sch.uk", domain.topDomainUnderRegistrySuffix().toString()); }