Package org.kiwiproject.net
Class KiwiInternetAddresses
- java.lang.Object
-
- org.kiwiproject.net.KiwiInternetAddresses
-
public class KiwiInternetAddresses extends Object
Utilities forInetAddressand other things related to internet addresses. Note that Google Guava also has theInetAddressesclass, 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 theInetAddressclass 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 withKiwiInternetAddresses.SimpleHostInfo. Otherwise when working withInetAddressinstances, usingInetAddress.getLoopbackAddress()is about the only other default value you could use.Last, note that
KiwiInternetAddresses.SimpleHostInfoonly contains the host name and host address as a string.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classKiwiInternetAddresses.IpSchemeEnum that defines the IP scheme to use when looking up a machine's IP addresses.static classKiwiInternetAddresses.SimpleHostInfoSimple value class encapsulating a host name and IP address
-
Constructor Summary
Constructors Constructor Description KiwiInternetAddresses()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description 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.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.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.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.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.static Optional<InetAddress>getLocalHostInetAddress()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.static Optional<KiwiInternetAddresses.SimpleHostInfo>getLocalHostInfo()Get local host as an optional ofKiwiInternetAddresses.SimpleHostInfo.static KiwiInternetAddresses.SimpleHostInfogetLocalHostInfo(Supplier<KiwiInternetAddresses.SimpleHostInfo> defaultValueSupplier)Get local host as aKiwiInternetAddresses.SimpleHostInfo.static KiwiInternetAddresses.SimpleHostInfogetLocalHostInfo(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.static Optional<Integer>portFrom(String url)Returns the port in the given URL, or an empty optional otherwise.static Optional<Integer>portFrom(URL url)Returns the port in the given URL, or an empty optional otherwise.
-
-
-
Method Detail
-
getLocalHostInfo
public static Optional<KiwiInternetAddresses.SimpleHostInfo> getLocalHostInfo()
Get local host as an optional ofKiwiInternetAddresses.SimpleHostInfo.- Returns:
- optional containing local host info, or empty if any error occurred
-
getLocalHostInetAddress
public static Optional<InetAddress> 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
public static InetAddress getLocalHostInetAddress(InetAddress defaultValue)
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
public static InetAddress getLocalHostInetAddress(Supplier<InetAddress> defaultValueSupplier)
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
public static Optional<Integer> portFrom(String url)
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:
URL(String)
-
portFrom
public static Optional<Integer> portFrom(URL url)
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:
URL.getPort()- Implementation Note:
- The
URL.getPort()returns-1if there is no port. Here we change that to anOptional
-
hostAndPortFrom
public static com.google.common.net.HostAndPort hostAndPortFrom(String url)
Returns aHostAndPortfrom the given URL.- Parameters:
url- a URL as a string- Returns:
- HostAndPort instance
-
hostAndPortFrom
public static com.google.common.net.HostAndPort hostAndPortFrom(URL url)
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.
-
-