Java Code Examples for com.google.common.net.InternetDomainName#toString()

The following examples show how to use com.google.common.net.InternetDomainName#toString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LinkRelevance.java    From ache with Apache License 2.0 6 votes vote down vote up
public static String getTopLevelDomain(String host) {
    InternetDomainName domain = null;
    try {
        domain = getDomainName(host);
        if(domain.isUnderPublicSuffix()) {
            return domain.topPrivateDomain().toString();
        } else {
            // if the domain is a public suffix, just use it as top level domain
            return domain.toString();
        }
    } catch (IllegalArgumentException e) {
        // when host is an IP address, use it as TLD
        if(InetAddresses.isInetAddress(host)) {
            return host;
        }
        throw new IllegalStateException("Invalid top private domain name=["+domain+"] in URL=["+host+"]", e);
    }
}
 
Example 2
Source File: URLRegexTransformer.java    From webarchive-commons with Apache License 2.0 6 votes vote down vote up
public static String hostToPublicSuffix(String host) {
	InternetDomainName idn;

	try {
		idn = InternetDomainName.from(host);
	} catch(IllegalArgumentException e) {
		return host;
	}
	InternetDomainName tmp = idn.publicSuffix();
	if(tmp == null) {
		return host;
	}
	String pubSuff = tmp.toString();
	int idx = host.lastIndexOf(".", host.length() - (pubSuff.length()+2));
	if(idx == -1) {
		return host;
	}
	return host.substring(idx+1);
}
 
Example 3
Source File: InternetUtils.java    From vividus with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the domain with desired levels.
 * <p>For example:</p>
 * <ul>
 * <li>for URI: "https://www.by.example.com" and levels = 2 method will return example.com</li>
 * <li>for URI: "https://www.by.example.com" and levels = 5 method will return www.by.example.com</li>
 * </ul>
 * @param uri Uri to process
 * @param domainLevels desired domain levels
 * @return processed domain with desired levels
 */
public static String getDomainName(URI uri, int domainLevels)
{
    InternetDomainName domaneName = InternetDomainName.from(uri.getHost());
    List<String> domainParts = domaneName.parts();
    if (domainLevels < domainParts.size())
    {
        List<String> resultDomainParts = domainParts.subList(domainParts.size() - domainLevels, domainParts.size());
        return String.join(DOMAIN_PARTS_SEPARATOR, resultDomainParts);
    }
    return domaneName.toString();
}
 
Example 4
Source File: DigitalAssetLinksRepository.java    From input-samples with Apache License 2.0 5 votes vote down vote up
public static String getCanonicalDomain(String domain) {
    InternetDomainName idn = InternetDomainName.from(domain);
    while (idn != null && !idn.isTopPrivateDomain()) {
        idn = idn.parent();
    }
    return idn == null ? null : idn.toString();
}
 
Example 5
Source File: DigitalAssetLinksRepository.java    From android-AutofillFramework with Apache License 2.0 5 votes vote down vote up
public static String getCanonicalDomain(String domain) {
    InternetDomainName idn = InternetDomainName.from(domain);
    while (idn != null && !idn.isTopPrivateDomain()) {
        idn = idn.parent();
    }
    return idn == null ? null : idn.toString();
}
 
Example 6
Source File: HostFlowUtils.java    From nomulus with Apache License 2.0 5 votes vote down vote up
/** Checks that a host name is valid. */
public static InternetDomainName validateHostName(String name) throws EppException {
  checkArgumentNotNull(name, "Must specify host name to validate");
  if (name.length() > 253) {
    throw new HostNameTooLongException();
  }
  String hostNameLowerCase = Ascii.toLowerCase(name);
  if (!name.equals(hostNameLowerCase)) {
    throw new HostNameNotLowerCaseException(hostNameLowerCase);
  }
  try {
    String hostNamePunyCoded = Idn.toASCII(name);
    if (!name.equals(hostNamePunyCoded)) {
      throw new HostNameNotPunyCodedException(hostNamePunyCoded);
    }
    InternetDomainName hostName = InternetDomainName.from(name);
    if (!name.equals(hostName.toString())) {
      throw new HostNameNotNormalizedException(hostName.toString());
    }
    // The effective TLD is, in order of preference, the registry suffix, if the TLD is a real TLD
    // published in the public suffix list (https://publicsuffix.org/, note that a registry suffix
    // is in the "ICANN DOMAINS" in that list); or a TLD managed by Nomulus (in-bailiwick), found
    // by #findTldForName; or just the last part of a domain name.
    InternetDomainName effectiveTld =
        hostName.isUnderRegistrySuffix()
            ? hostName.registrySuffix()
            : findTldForName(hostName).orElse(InternetDomainName.from("invalid"));
    // Checks whether a hostname is deep enough. Technically a host can be just one level beneath
    // the effective TLD (e.g. example.com) but we require by policy that it has to be at least
    // one part beyond that (e.g. ns1.example.com).
    if (hostName.parts().size() < effectiveTld.parts().size() + 2) {
      throw new HostNameTooShallowException();
    }
    return hostName;
  } catch (IllegalArgumentException e) {
    throw new InvalidHostNameException();
  }
}
 
Example 7
Source File: DomainFlowUtils.java    From nomulus with Apache License 2.0 5 votes vote down vote up
static void validateNameserversCountForTld(String tld, InternetDomainName domainName, int count)
    throws EppException {
  // For TLDs with a nameserver allow list, all domains must have at least 1 nameserver.
  ImmutableSet<String> tldNameserversAllowList =
      Registry.get(tld).getAllowedFullyQualifiedHostNames();
  if (!tldNameserversAllowList.isEmpty() && count == 0) {
    throw new NameserversNotSpecifiedForTldWithNameserverAllowListException(
        domainName.toString());
  }
  if (count > MAX_NAMESERVERS_PER_DOMAIN) {
    throw new TooManyNameserversException(
        String.format("Only %d nameservers are allowed per domain", MAX_NAMESERVERS_PER_DOMAIN));
  }
}
 
Example 8
Source File: DomainFlowUtils.java    From nomulus with Apache License 2.0 5 votes vote down vote up
/**
 * Check that if there's a claims notice it's on the claims list, and that if there's not one it's
 * not on the claims list.
 */
static void verifyClaimsNoticeIfAndOnlyIfNeeded(
    InternetDomainName domainName, boolean hasSignedMarks, boolean hasClaimsNotice)
    throws EppException {
  boolean isInClaimsList =
      ClaimsListShard.get().getClaimKey(domainName.parts().get(0)).isPresent();
  if (hasClaimsNotice && !isInClaimsList) {
    throw new UnexpectedClaimsNoticeException(domainName.toString());
  }
  if (!hasClaimsNotice && isInClaimsList && !hasSignedMarks) {
    throw new MissingClaimsNoticeException(domainName.toString());
  }
}
 
Example 9
Source File: TargetModelElasticSearch.java    From ache with Apache License 2.0 5 votes vote down vote up
public TargetModelElasticSearch(TargetModelCbor model) {

        URL url = Urls.toJavaURL(model.url);
        String rawContent = (String) model.response.get("body");

        Page page = new Page(url, rawContent);
        page.setParsedData(new ParsedData(new PaginaURL(url, rawContent)));

        this.html = rawContent;
        this.url = model.url;
        this.retrieved = new Date(model.timestamp * 1000);
        this.words = page.getParsedData().getWords();
        this.wordsMeta = page.getParsedData().getWordsMeta();
        this.title = page.getParsedData().getTitle();
        this.domain = url.getHost();

        try {
            this.text = DefaultExtractor.getInstance().getText(page.getContentAsString());
        } catch (Exception e) {
            this.text = "";
        }

        InternetDomainName domainName = InternetDomainName.from(page.getDomainName());
        if (domainName.isUnderPublicSuffix()) {
            this.topPrivateDomain = domainName.topPrivateDomain().toString();
        } else {
            this.topPrivateDomain = domainName.toString();
        }
    }
 
Example 10
Source File: DomainFlowUtils.java    From nomulus with Apache License 2.0 4 votes vote down vote up
static void verifyNotReserved(InternetDomainName domainName, boolean isSunrise)
    throws EppException {
  if (isReserved(domainName, isSunrise)) {
    throw new DomainReservedException(domainName.toString());
  }
}