public void testToString() { for (String inputName : SOMEWHERE_UNDER_PS) { InternetDomainName domain = InternetDomainName.from(inputName); /* * We would ordinarily use constants for the expected results, but * doing it by derivation allows us to reuse the test case definitions * used in other tests. */ String expectedName = Ascii.toLowerCase(inputName); expectedName = expectedName.replaceAll("[\u3002\uFF0E\uFF61]", "."); if (expectedName.endsWith(".")) { expectedName = expectedName.substring(0, expectedName.length() - 1); } assertEquals(expectedName, domain.toString()); } }
private static Endpoint create(String host, int port) { requireNonNull(host, "host"); if (NetUtil.isValidIpV4Address(host)) { return new Endpoint(host, host, port, DEFAULT_WEIGHT, HostType.IPv4_ONLY); } if (NetUtil.isValidIpV6Address(host)) { final String ipV6Addr; if (host.charAt(0) == '[') { // Strip surrounding '[' and ']'. ipV6Addr = host.substring(1, host.length() - 1); } else { ipV6Addr = host; } return new Endpoint(ipV6Addr, ipV6Addr, port, DEFAULT_WEIGHT, HostType.IPv6_ONLY); } return new Endpoint(InternetDomainName.from(host).toString(), null, port, DEFAULT_WEIGHT, HostType.HOSTNAME_ONLY); }
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()); }
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()); }
return new HostSpecifier(domain.toString());
public void testParent() { assertEquals("com", InternetDomainName.from("google.com").parent().toString()); assertEquals("uk", InternetDomainName.from("co.uk").parent().toString()); assertEquals("google.com", InternetDomainName.from("www.google.com").parent().toString()); try { InternetDomainName.from("com").parent(); fail("'com' should throw ISE on .parent() call"); } catch (IllegalStateException expected) { } }
public void testParentChild() { InternetDomainName origin = InternetDomainName.from("foo.com"); InternetDomainName parent = origin.parent(); assertEquals("com", parent.toString()); // These would throw an exception if leniency were not preserved during parent() and child() // calls. InternetDomainName child = parent.child(LOTS_OF_DELTAS); child.child(LOTS_OF_DELTAS); }
public void testChild() { InternetDomainName domain = InternetDomainName.from("foo.com"); assertEquals("www.foo.com", domain.child("www").toString()); try { domain.child("www."); fail("www..google.com should have been invalid"); } catch (IllegalArgumentException expected) { } }
/** * 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. } } }
return new HostSpecifier(domain.toString());
return new HostSpecifier(domain.toString());
public void testRegistrySuffixExclusion() { InternetDomainName domain = InternetDomainName.from("foo.city.yokohama.jp"); assertTrue(domain.hasRegistrySuffix()); assertEquals("yokohama.jp", domain.registrySuffix().toString()); // Behold the weirdness! assertFalse(domain.registrySuffix().isRegistrySuffix()); }
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()); }
/** * 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()); } } }
/** * 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; }
String ret = idn.publicSuffix().toString(); if(ret.startsWith("InternetDomainName")) { return Joiner.on(".").join(idn.publicSuffix().parts());
/** * A deprecated synonym for {@link #toString()}. * * @deprecated Use {@link #toString()} */ @Deprecated public String name() { return toString(); }
/** * A deprecated synonym for {@link #toString()}. * * @deprecated Use {@link #toString()} */ @Deprecated public String name() { return toString(); }
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(); }
public void testChild() { InternetDomainName domain = InternetDomainName.from("foo.com"); assertEquals("www.foo.com", domain.child("www").toString()); try { domain.child("www."); fail("www..google.com should have been invalid"); } catch (IllegalArgumentException expected) { } }