Returns a {@code HostSpecifier} built from the provided {@code specifier}, which is already known to be valid. If the {@code specifier} might be invalid, use {@link #from(String)} instead. <p>The specifier must be in one of these formats: <ul> <li>A domain name, like {@code google.com} <li>A I
(String specifier)
| 69 | * @throws IllegalArgumentException if the specifier is not valid. |
| 70 | */ |
| 71 | public static HostSpecifier fromValid(String specifier) { |
| 72 | // Verify that no port was specified, and strip optional brackets from |
| 73 | // IPv6 literals. |
| 74 | HostAndPort parsedHost = HostAndPort.fromString(specifier); |
| 75 | Preconditions.checkArgument(!parsedHost.hasPort()); |
| 76 | String host = parsedHost.getHost(); |
| 77 | |
| 78 | // Try to interpret the specifier as an IP address. Note we build |
| 79 | // the address rather than using the .is* methods because we want to |
| 80 | // use InetAddresses.toUriString to convert the result to a string in |
| 81 | // canonical form. |
| 82 | InetAddress addr = null; |
| 83 | try { |
| 84 | addr = InetAddresses.forString(host); |
| 85 | } catch (IllegalArgumentException e) { |
| 86 | // It is not an IPv4 or IPv6 literal |
| 87 | } |
| 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 | InternetDomainName domain = InternetDomainName.from(host); |
| 97 | |
| 98 | if (domain.hasPublicSuffix()) { |
| 99 | return new HostSpecifier(domain.toString()); |
| 100 | } |
| 101 | |
| 102 | throw new IllegalArgumentException( |
| 103 | "Domain name does not have a recognized public suffix: " + host); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Attempts to return a {@code HostSpecifier} for the given string, throwing an exception if |