/** * Normalizes and encodes a hostname and port to be included in URL. * In particular, this method makes sure that IPv6 address literals have the proper * formatting to be included in URLs. * * @param host The address to be included in the URL. * @param port The port for the URL address. * @return The proper URL string encoded IP address and port. * @throws java.net.UnknownHostException Thrown, if the hostname cannot be translated into a URL. */ public static String hostAndPortToUrlString(String host, int port) throws UnknownHostException { return ipAddressAndPortToUrlString(InetAddress.getByName(host), port); }
/** * Encodes an IP address and port to be included in URL. in particular, this method makes * sure that IPv6 addresses have the proper formatting to be included in URLs. * * @param address The socket address with the IP address and port. * @return The proper URL string encoded IP address and port. */ public static String socketAddressToUrlString(InetSocketAddress address) { if (address.isUnresolved()) { throw new IllegalArgumentException("Address cannot be resolved: " + address.getHostString()); } return ipAddressAndPortToUrlString(address.getAddress(), address.getPort()); }
@Test public void testIPv6URLEncoding() { try { final String addressString = "2001:db8:10:11:12:ff00:42:8329"; final String bracketedAddressString = '[' + addressString + ']'; final int port = 23453; InetAddress address = InetAddress.getByName(addressString); InetSocketAddress socketAddress = new InetSocketAddress(address, port); assertEquals(bracketedAddressString, NetUtils.ipAddressToUrlString(address)); assertEquals(bracketedAddressString + ':' + port, NetUtils.ipAddressAndPortToUrlString(address, port)); assertEquals(bracketedAddressString + ':' + port, NetUtils.socketAddressToUrlString(socketAddress)); } catch (Exception e) { e.printStackTrace(); fail(e.getMessage()); } }
@Test public void testIPv4URLEncoding() { try { final String addressString = "10.244.243.12"; final int port = 23453; InetAddress address = InetAddress.getByName(addressString); InetSocketAddress socketAddress = new InetSocketAddress(address, port); assertEquals(addressString, NetUtils.ipAddressToUrlString(address)); assertEquals(addressString + ':' + port, NetUtils.ipAddressAndPortToUrlString(address, port)); assertEquals(addressString + ':' + port, NetUtils.socketAddressToUrlString(socketAddress)); } catch (Exception e) { e.printStackTrace(); fail(e.getMessage()); } }
/** * Normalizes and encodes a hostname and port to be included in URL. * In particular, this method makes sure that IPv6 address literals have the proper * formatting to be included in URLs. * * @param host The address to be included in the URL. * @param port The port for the URL address. * @return The proper URL string encoded IP address and port. * @throws java.net.UnknownHostException Thrown, if the hostname cannot be translated into a URL. */ public static String hostAndPortToUrlString(String host, int port) throws UnknownHostException { return ipAddressAndPortToUrlString(InetAddress.getByName(host), port); }
/** * Normalizes and encodes a hostname and port to be included in URL. * In particular, this method makes sure that IPv6 address literals have the proper * formatting to be included in URLs. * * @param host The address to be included in the URL. * @param port The port for the URL address. * @return The proper URL string encoded IP address and port. * @throws java.net.UnknownHostException Thrown, if the hostname cannot be translated into a URL. */ public static String hostAndPortToUrlString(String host, int port) throws UnknownHostException { return ipAddressAndPortToUrlString(InetAddress.getByName(host), port); }
/** * Encodes an IP address and port to be included in URL. in particular, this method makes * sure that IPv6 addresses have the proper formatting to be included in URLs. * * @param address The socket address with the IP address and port. * @return The proper URL string encoded IP address and port. */ public static String socketAddressToUrlString(InetSocketAddress address) { if (address.isUnresolved()) { throw new IllegalArgumentException("Address cannot be resolved: " + address.getHostString()); } return ipAddressAndPortToUrlString(address.getAddress(), address.getPort()); }
/** * Encodes an IP address and port to be included in URL. in particular, this method makes * sure that IPv6 addresses have the proper formatting to be included in URLs. * * @param address The socket address with the IP address and port. * @return The proper URL string encoded IP address and port. */ public static String socketAddressToUrlString(InetSocketAddress address) { if (address.isUnresolved()) { throw new IllegalArgumentException("Address cannot be resolved: " + address.getHostString()); } return ipAddressAndPortToUrlString(address.getAddress(), address.getPort()); }