Package org.kiwiproject.net
Class KiwiInternetAddresses
java.lang.Object
org.kiwiproject.net.KiwiInternetAddresses
Utilities for
InetAddress and other things related to internet addresses. Note that Google Guava also has
the InetAddresses class, so if something is not in here, check there. This set of
utilities only contains things that are not in the JDK or Guava.
Note that some of the methods accept (e.g. as default value) or return KiwiInternetAddresses.SimpleHostInfo. This is because
the InetAddress class has no public constructors and can only be instantiated by its static factory
methods. As a result, if you want to specify your own custom host name and/or IP address as the default value, for
example to use a different host alias or subnet IP, then you must use the methods that work with KiwiInternetAddresses.SimpleHostInfo.
Otherwise, when working with InetAddress instances, using InetAddress.getLoopbackAddress() is about the
only other default value you could use.
Last, note that KiwiInternetAddresses.SimpleHostInfo only contains the host name and host address as a string.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumEnum that defines the IP scheme to use when looking up a machine's IP addresses.static classSimple value class encapsulating a host name and IP address -
Method Summary
Modifier and TypeMethodDescriptionfindFirstMatchingAddress(List<String> subnetCidrs, List<String> ipAddresses) Attempts to find the first IP address from a given list of ip addresses that matches one of the given subnet CIDRs.findFirstMatchingAddress(List<String> subnetCidrs, KiwiInternetAddresses.IpScheme ipScheme) Attempts to find the first IP address on the machine that matches one of the given subnet CIDRs.static StringfindFirstMatchingAddressOrNull(List<String> subnetCidrs, List<String> ipAddresses) Finds the first IP address from a given list of ip addresses that matches one of the given subnet CIDRs or returns null if not found.static StringfindFirstMatchingAddressOrNull(List<String> subnetCidrs, KiwiInternetAddresses.IpScheme ipScheme) Finds the first IP address on the machine that matches one of the given subnet CIDRs or returns null if not found.static StringfindFirstMatchingAddressOrThrow(List<String> subnetCidrs, List<String> ipAddresses) Finds the first IP address on the machine that matches one of the given subnet CIDRs or throws IllegalStateException if not found.static StringfindFirstMatchingAddressOrThrow(List<String> subnetCidrs, KiwiInternetAddresses.IpScheme ipScheme) Finds the first IP address on the machine that matches one of the given subnet CIDRs or throws IllegalStateException if not found.findMatchingAddresses(List<String> subnetCidrs, List<String> ipAddresses) Finds all IP addresses from a given list of ip addresses that matches one of the given subnet CIDRs.findMatchingAddresses(List<String> subnetCidrs, KiwiInternetAddresses.IpScheme ipScheme) Finds all IP addresses on the machine that matches one of the given subnet CIDRs.static Optional<InetAddress>Get local host as an optional ofInetAddressstatic InetAddressgetLocalHostInetAddress(InetAddress defaultValue) Get local host as aInetAddress.static InetAddressgetLocalHostInetAddress(Supplier<InetAddress> defaultValueSupplier) Get local host as aInetAddress.Get local host as an optional ofKiwiInternetAddresses.SimpleHostInfo.getLocalHostInfo(Supplier<KiwiInternetAddresses.SimpleHostInfo> defaultValueSupplier) Get local host as aKiwiInternetAddresses.SimpleHostInfo.getLocalHostInfo(KiwiInternetAddresses.SimpleHostInfo defaultValue) Get local host as aKiwiInternetAddresses.SimpleHostInfo.static com.google.common.net.HostAndPorthostAndPortFrom(String url) Returns aHostAndPortfrom the given URL.static com.google.common.net.HostAndPorthostAndPortFrom(URL url) Returns aHostAndPortfrom the given URL.Returns the port in the given URL, or an empty optional otherwise.Returns the port in the given URL, or an empty optional otherwise.
-
Method Details
-
getLocalHostInfo
Get local host as an optional ofKiwiInternetAddresses.SimpleHostInfo.- Returns:
- optional containing local host info, or empty if any error occurred
-
getLocalHostInetAddress
Get local host as an optional ofInetAddress- Returns:
- optional containing local host info, or empty if any error occurred
-
getLocalHostInfo
public static KiwiInternetAddresses.SimpleHostInfo getLocalHostInfo(KiwiInternetAddresses.SimpleHostInfo defaultValue) Get local host as aKiwiInternetAddresses.SimpleHostInfo.- Parameters:
defaultValue- the default value to use if local host could not be obtained for any reason- Returns:
- the local host info, or the specified
defaultValueif any error occurred
-
getLocalHostInetAddress
Get local host as aInetAddress.- Parameters:
defaultValue- the default value to use if local host address could not be obtained for any reason- Returns:
- the local host info, or the specified
defaultValueif any error occurred
-
getLocalHostInfo
public static KiwiInternetAddresses.SimpleHostInfo getLocalHostInfo(Supplier<KiwiInternetAddresses.SimpleHostInfo> defaultValueSupplier) Get local host as aKiwiInternetAddresses.SimpleHostInfo.- Parameters:
defaultValueSupplier- supplier for the default value to use if local host could not be obtained for any reason- Returns:
- the local host info, or the value obtained from the
defaultValueSupplierif any error occurred
-
getLocalHostInetAddress
Get local host as aInetAddress.- Parameters:
defaultValueSupplier- supplier for the default value to use if local host address could not be obtained for any reason- Returns:
- the local host info, or the value obtained from the
defaultValueSupplierif any error occurred
-
portFrom
Returns the port in the given URL, or an empty optional otherwise.- Parameters:
url- a URL- Returns:
- an optional containing the port, or empty
- Throws:
UncheckedMalformedURLException- if the given string URL is not valid- See Also:
-
portFrom
Returns the port in the given URL, or an empty optional otherwise.- Parameters:
url- a URL- Returns:
- an optional containing the port, or empty if the URL did not have a port
- See Also:
- Implementation Note:
- The
URL.getPort()returns-1if there is no port. Here we change that to anOptional
-
hostAndPortFrom
Returns aHostAndPortfrom the given URL.- Parameters:
url- a URL as a string- Returns:
- HostAndPort instance
-
hostAndPortFrom
Returns aHostAndPortfrom the given URL.- Parameters:
url- a URL- Returns:
- HostAndPort instance
-
findFirstMatchingAddressOrNull
public static String findFirstMatchingAddressOrNull(List<String> subnetCidrs, KiwiInternetAddresses.IpScheme ipScheme) Finds the first IP address on the machine that matches one of the given subnet CIDRs or returns null if not found. TheKiwiInternetAddresses.IpSchemeis used to filter the IP addresses by IPv4 or IPv6.- Parameters:
subnetCidrs- A list of CIDRs used to match against the machine's IP addresses.ipScheme- Whether to filter by IPv4 or IPv6.- Returns:
- the first found matching IP address or null.
-
findFirstMatchingAddressOrNull
public static String findFirstMatchingAddressOrNull(List<String> subnetCidrs, List<String> ipAddresses) Finds the first IP address from a given list of ip addresses that matches one of the given subnet CIDRs or returns null if not found.- Parameters:
subnetCidrs- A list of CIDRs used to match against the machine's IP addresses.ipAddresses- A list of IP addresses to search for a match.- Returns:
- the first found matching IP address or null.
-
findFirstMatchingAddressOrThrow
public static String findFirstMatchingAddressOrThrow(List<String> subnetCidrs, KiwiInternetAddresses.IpScheme ipScheme) Finds the first IP address on the machine that matches one of the given subnet CIDRs or throws IllegalStateException if not found. TheKiwiInternetAddresses.IpSchemeis used to filter the IP addresses by IPv4 or IPv6.- Parameters:
subnetCidrs- A list of CIDRs used to match against the machine's IP addresses.ipScheme- Whether to filter by IPv4 or IPv6.- Returns:
- the first found matching IP address.
- Throws:
IllegalStateException- if a matching IP address can not be found.
-
findFirstMatchingAddressOrThrow
public static String findFirstMatchingAddressOrThrow(List<String> subnetCidrs, List<String> ipAddresses) Finds the first IP address on the machine that matches one of the given subnet CIDRs or throws IllegalStateException if not found.- Parameters:
subnetCidrs- A list of CIDRs used to match against the machine's IP addresses.ipAddresses- A list of IP addresses to search for a match.- Returns:
- the first found matching IP address.
- Throws:
IllegalStateException- if a matching IP address can not be found.
-
findFirstMatchingAddress
public static Optional<String> findFirstMatchingAddress(List<String> subnetCidrs, KiwiInternetAddresses.IpScheme ipScheme) Attempts to find the first IP address on the machine that matches one of the given subnet CIDRs. TheKiwiInternetAddresses.IpSchemeis used to filter the IP addresses by IPv4 or IPv6.- Parameters:
subnetCidrs- A list of CIDRs used to match against the machine's IP addresses.ipScheme- Whether to filter by IPv4 or IPv6- Returns:
- an Optional containing the first found matching IP address or
Optional.empty()if not found.
-
findFirstMatchingAddress
public static Optional<String> findFirstMatchingAddress(List<String> subnetCidrs, List<String> ipAddresses) Attempts to find the first IP address from a given list of ip addresses that matches one of the given subnet CIDRs.- Parameters:
subnetCidrs- A list of CIDRs used to match against the machine's IP addresses.ipAddresses- A list of IP addresses to search for a match.- Returns:
- an Optional containing the first found matching IP address or
Optional.empty()if not found.
-
findMatchingAddresses
public static List<String> findMatchingAddresses(List<String> subnetCidrs, KiwiInternetAddresses.IpScheme ipScheme) Finds all IP addresses on the machine that matches one of the given subnet CIDRs. TheKiwiInternetAddresses.IpSchemeis used to filter the IP addresses by IPv4 or IPv6.- Parameters:
subnetCidrs- A list of CIDRs used to match against the machine's IP addresses.ipScheme- Whether to filter by IPv4 or IPv6- Returns:
- a list of matching IP addresses.
-
findMatchingAddresses
public static List<String> findMatchingAddresses(List<String> subnetCidrs, List<String> ipAddresses) Finds all IP addresses from a given list of ip addresses that matches one of the given subnet CIDRs.- Parameters:
subnetCidrs- A list of CIDRs used to match against the machine's IP addresses.ipAddresses- A list of IP addresses to search for a match.- Returns:
- a list of matching IP addresses.
-