Refine search
/** * Returns the ancestor of the current domain at the given number of levels "higher" (rightward) * in the subdomain list. The number of levels must be non-negative, and less than {@code N-1}, * where {@code N} is the number of parts in the domain. * * <p>TODO: Reasonable candidate for addition to public API. */ private InternetDomainName ancestor(int levels) { return from(DOT_JOINER.join(parts.subList(levels, parts.size()))); }
final InternetDomainName domain = InternetDomainName.from(host); if (domain.hasPublicSuffix()) { return new HostSpecifier(domain.toString());
public void testIsValid() { final Iterable<String> validCases = Iterables.concat(VALID_NAME, PS, NO_PS, NON_PS); final Iterable<String> invalidCases = Iterables.concat(INVALID_NAME, VALID_IP_ADDRS, INVALID_IP_ADDRS); for (String valid : validCases) { assertTrue(valid, InternetDomainName.isValid(valid)); } for (String invalid : invalidCases) { assertFalse(invalid, InternetDomainName.isValid(invalid)); } }
/** * Returns the {@linkplain #isPublicSuffix() public suffix} portion of the domain name, or {@code * null} if no public suffix is present. * * @since 6.0 */ public InternetDomainName publicSuffix() { return hasPublicSuffix() ? ancestor(publicSuffixIndex) : null; }
/** * Returns an {@code InternetDomainName} that is the immediate ancestor of this one; that is, the * current domain with the leftmost part removed. For example, the parent of {@code * www.google.com} is {@code google.com}. * * @throws IllegalStateException if the domain has no parent, as determined by {@link #hasParent} */ public InternetDomainName parent() { checkState(hasParent(), "Domain '%s' has no parent", name); return ancestor(1); }
/** * Returns the portion of this domain name that is one level beneath the {@linkplain * #isPublicSuffix() public suffix}. For example, for {@code x.adwords.google.co.uk} it returns * {@code google.co.uk}, since {@code co.uk} is a public suffix. Similarly, for {@code * myblog.blogspot.com} it returns the same domain, {@code myblog.blogspot.com}, since {@code * blogspot.com} is a public suffix. * * <p>If {@link #isTopPrivateDomain()} is true, the current domain name instance is returned. * * <p>This method can be used to determine the probable highest level parent domain for which * cookies may be set, though even that depends on individual browsers' implementations of cookie * controls. * * @throws IllegalStateException if this domain does not end with a public suffix * @since 6.0 */ public InternetDomainName topPrivateDomain() { if (isTopPrivateDomain()) { return this; } checkState(isUnderPublicSuffix(), "Not under a public suffix: %s", name); return ancestor(publicSuffixIndex - 1); }
if(idn != null && idn.hasPublicSuffix()) { String ret = idn.publicSuffix().toString(); if(ret.startsWith("InternetDomainName")) { return Joiner.on(".").join(idn.publicSuffix().parts());
private static ImmutableList.Builder<String> parentLevels(InternetDomainName internetDomainName) { ImmutableList.Builder<String> levels; if(internetDomainName.hasParent()){ levels = parentLevels(internetDomainName.parent()); } else { levels = ImmutableList.builder(); } levels.add(internetDomainName.toString()); return levels; }
@Override public String apply(Endpoint input) { if (input.getRegion() != null) return input.getRegion(); String host = input.getPublicURL().getHost(); if (InternetDomainName.isValid(host)) { InternetDomainName domain = InternetDomainName.from(host); return domain.parts().get(0); } return provider; } }
public synchronized String getShortName() { if (_shortName == null) { String shortHostName; try { InternetDomainName domainName = InternetDomainName.from(_hostName); shortHostName = domainName.parts().get(0); } catch (Exception e) { shortHostName = _hostName; } if (_tableType == TableType.OFFLINE) { _shortName = shortHostName + SHORT_OFFLINE_SUFFIX; } else { _shortName = shortHostName + SHORT_REALTIME_SUFFIX; } } return _shortName; }
/** * Returns the ancestor of the current domain at the given number of levels * "higher" (rightward) in the subdomain list. The number of levels must be * non-negative, and less than {@code N-1}, where {@code N} is the number of * parts in the domain. * * <p>TODO: Reasonable candidate for addition to public API. */ private InternetDomainName ancestor(int levels) { return new InternetDomainName(parts.subList(levels, parts.size())); }
private String extractCompanyFromHostName(String hostname) { try { InternetDomainName domainName = InternetDomainName.from(hostname); return Normalize.brand(domainName.topPrivateDomain().parts().get(0)); } catch (RuntimeException e) { return null; } }
public void testTopPrivateDomain() { for (String name : TOP_PRIVATE_DOMAIN) { final InternetDomainName domain = InternetDomainName.from(name); assertFalse(name, domain.isPublicSuffix()); assertTrue(name, domain.hasPublicSuffix()); assertTrue(name, domain.isUnderPublicSuffix()); assertTrue(name, domain.isTopPrivateDomain()); assertEquals(domain.parent(), domain.publicSuffix()); } }
@Override @SuppressWarnings("unchecked") public <R extends HttpRequest> R bindToRequest(R request, Object payload) { checkNotNull(payload, "hostprefix"); checkArgument(isValid(request.getEndpoint().getHost()), "this is only valid for hostnames: " + request); InternetDomainName name = from(request.getEndpoint().getHost()).child(payload.toString()); return (R) request.toBuilder().endpoint(uriBuilder(request.getEndpoint()).host(name.toString()).build()).build(); } }
public void testPublicSuffixMultipleUnders() { // 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.hasPublicSuffix()); assertEquals("essex.sch.uk", domain.publicSuffix().toString()); assertEquals("www.essex.sch.uk", domain.topPrivateDomain().toString()); }
@Override public GeneralisedWebAddress getGeneralisedHostName(URL url) { String host = url.getHost(); // if (! url.getHost().contains(".")) { // return GeneralisedWebAddress.build(host, GeneralisedWebAddressType.HOST_ADDRESS); // } else InetAddress ip = extractInetAddress(host); if (ip != null) { if (ip instanceof Inet4Address ) { return GeneralisedWebAddress.build(host.replaceFirst("\\d+$", ""), GeneralisedWebAddressType.IPV4_ADDRESS); } else if (ip instanceof Inet6Address) { return GeneralisedWebAddress.build(ip.getHostAddress(), GeneralisedWebAddressType.IPV6_ADDRESS); } } else if (InternetDomainName.isValid(host)) { InternetDomainName domainName = InternetDomainName.from(host); if (domainName.isUnderPublicSuffix()) { return GeneralisedWebAddress.build(domainName.topPrivateDomain().toString(), GeneralisedWebAddressType.DOMAIN_NAME); } else if (domainName.hasParent()) { return GeneralisedWebAddress.build(domainName.parent().toString(), GeneralisedWebAddressType.DOMAIN_NAME); } return GeneralisedWebAddress.build(host, GeneralisedWebAddressType.HOST_ADDRESS); } return null; }
public void testPublicSuffixExclusion() { InternetDomainName domain = InternetDomainName.from("foo.city.yokohama.jp"); assertTrue(domain.hasPublicSuffix()); assertEquals("yokohama.jp", domain.publicSuffix().toString()); // Behold the weirdness! assertFalse(domain.publicSuffix().isPublicSuffix()); }
/** * Returns a {@link LimitedCookieStoreFacade} whose * {@link LimitedCookieStoreFacade#getCookies()} method returns only cookies * from {@code host} and its parent domains, if applicable. */ public CookieStore cookieStoreFor(String host) { CompositeCollection cookieCollection = new CompositeCollection(); if (InternetDomainName.isValid(host)) { InternetDomainName domain = InternetDomainName.from(host); while (domain != null) { Collection<Cookie> subset = hostSubset(domain.toString()); cookieCollection.addComposited(subset); if (domain.hasParent()) { domain = domain.parent(); } else { domain = null; } } } else { Collection<Cookie> subset = hostSubset(host.toString()); cookieCollection.addComposited(subset); } @SuppressWarnings("unchecked") List<Cookie> cookieList = new RestrictedCollectionWrappedList<Cookie>(cookieCollection); LimitedCookieStoreFacade store = new LimitedCookieStoreFacade(cookieList); return store; }
/** * 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. } } }
/** * Adds outlinks to whois:{domain} and whois:{ipAddress} */ protected void addWhoisLinks(CrawlURI curi) throws InterruptedException { CrawlHost ch = serverCache.getHostFor(curi.getUURI()); if (ch == null) { return; } if (ch.getIP() != null) { // do a whois lookup on the ip address addWhoisLink(curi, ch.getIP().getHostAddress()); } if (InternetDomainName.isValid(ch.getHostName())) { // do a whois lookup on the domain try { String topmostAssigned = InternetDomainName.from(ch.getHostName()).topPrivateDomain().toString(); addWhoisLink(curi, topmostAssigned); } catch (IllegalStateException e) { // java.lang.IllegalStateException: Not under a public suffix: mod.uk logger.warning("problem resolving topmost assigned domain, will try whois lookup on the plain hostname " + ch.getHostName() + " - " + e); addWhoisLink(curi, ch.getHostName()); } } }