Returns a HostSpecifier built from the provided specifier, which is already known to be valid. If the specifier might be invalid, use #from(String) instead. The specifier must be in one of these formats: A domain name, like google.com A IPv4 a
(String specifier)
| 69 | |
| 70 | |
| 71 | public static HostSpecifier fromValid(String specifier) { |
| 72 | // Verify that no port was specified, and strip optional brackets from |
| 73 | // IPv6 literals. |
| 74 | final HostAndPort parsedHost = HostAndPort.fromString(specifier); |
| 75 | Preconditions.checkArgument(!parsedHost.hasPort()); |
| 76 | |
| 77 | final String host = parsedHost.getHostText(); |
| 78 | |
| 79 | // Try to interpret the specifier as an IP address. Note we build |
| 80 | // the address rather than using the .is* methods because we want to |
| 81 | // use InetAddresses.toUriString to convert the result to a string in |
| 82 | // canonical form. |
| 83 | InetAddress addr = null; |
| 84 | try { |
| 85 | addr = InetAddresses.forString(host); |
| 86 | } catch (IllegalArgumentException e) { |
| 87 | // It is not an IPv4 or IPv6 literal |
| 88 | } |
| 89 | if (addr != null) { |
| 90 | return new HostSpecifier(InetAddresses.toUriString(addr)); |
| 91 | } |
| 92 | |
| 93 | // It is not any kind of IP address; must be a domain name or invalid. |
| 94 | |
| 95 | // TODO(user): different versions of this for different factories? |
| 96 | final InternetDomainName domain = InternetDomainName.from(host); |
| 97 | if (domain.hasPublicSuffix()) { |
| 98 | return new HostSpecifier(domain.toString()); |
| 99 | } |
| 100 | throw new IllegalArgumentException("Domain name does not have a recognized public suffix: " + host); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Attempts to return a {@code HostSpecifier} for the given string, throwing an exception if |
no test coverage detected