class URIUtil {

  public static String getSndDomain(String domain) {
    if (domain == null) {
      return null;
    }
    // Remove www subdomain if it exists
    if (domain.startsWith("www.")) {
      domain = domain.substring(4);
    }
    if (InternetDomainName.isValid(domain)) { // the domain is valid according to the RFC3490
      final InternetDomainName idn = InternetDomainName.from(domain);
      if (idn.hasPublicSuffix()) { // the domain has a public suffix
        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;
  }

}