URIUtil { public static String getSndDomain(String domain) { if (domain == null) { return null; } if (domain.startsWith("www.")) { domain = domain.substring(4); } if (InternetDomainName.isValid(domain)) { final InternetDomainName idn = InternetDomainName.from(domain); if (idn.hasPublicSuffix()) { if (idn.isUnderPublicSuffix()) { return idn.topPrivateDomain().name(); } else if (idn.hasParent()) { final List<String> parts = idn.parts(); return parts.get(parts.size() - 2).concat(".").concat(parts.get(parts.size() - 1)); } } } return null; }  static String getSndDomainFromUrl(String url); static String getSndDomain(String domain);  }
@Test public void testSecondDomain() { String[] testStrings = new String[] { "google.com -> google.com", "www.webaddict.za -> webaddict.za", "wilma -> null", "wilma.blogSpot.com -> blogspot.com", "repubblica.it -> repubblica.it", "isti.pomino.cnr.it -> cnr.it", "thelakersnation.com -> thelakersnation.com", "bio2rdf.org -> bio2rdf.org", "awakenedmmo.org -> awakenedmmo.org", "cheminfov.informatics.indiana.edu -> indiana.edu", "data-gov.tw.rpi.edu -> rpi.edu", "www.test.webaddict.co.za -> webaddict.co.za", "www.webaddict.co.za -> webaddict.co.za", "mxr.mozilla.org -> mozilla.org", "www.yago.zitgist.com -> zitgist.com", "aims.fao.org -> fao.org", "en.wikipedia.org -> wikipedia.org", "127.0.0.100 -> null", "http: }; for (String s : testStrings) { String[] elems = s.split("\\s+->\\s+"); String domain = elems[0]; String expectedSecondDomain = elems[1]; logger.info("Check if sndDomain of {} equals {}", domain, expectedSecondDomain); final String sndDomain = URIUtil.getSndDomain(domain); assertEquals(sndDomain == null ? "null" : sndDomain, expectedSecondDomain); } assertEquals(null, URIUtil.getSndDomain("")); assertEquals(null, URIUtil.getSndDomain(null)); }