Class IpAddressMatcher

java.lang.Object
com.axelor.common.net.IpAddressMatcher

public final class IpAddressMatcher extends Object
Matches IP addresses against an IP address or a CIDR range.

Both IPv4 and IPv6 are supported. IPv4-mapped IPv6 addresses (e.g. ::ffff:192.0.2.1) are handled as their IPv4 equivalent.

Only literal IP addresses are accepted: host names are rejected and no DNS lookup is ever performed.

  • Method Details

    • of

      public static IpAddressMatcher of(String value)
      Creates a matcher from an IP address (e.g. 192.0.2.1) or a CIDR range (e.g. 192.0.2.0/24, 2001:db8::/32).

      Host bits of a CIDR range are cleared, so 192.0.2.1/24 is the same as 192.0.2.0/24.

      Parameters:
      value - the IP address or CIDR range
      Returns:
      the matcher
      Throws:
      IllegalArgumentException - if the value is not a valid IP address or CIDR range
    • splitList

      public static List<String> splitList(String value)
      Splits a comma-separated list of IP addresses or CIDR ranges.

      Entries are trimmed and empty entries are ignored.

      Parameters:
      value - the list
      Returns:
      the entries, empty if the value is blank
    • parseList

      public static List<IpAddressMatcher> parseList(String value)
      Parses a comma-separated list of IP addresses or CIDR ranges.
      Parameters:
      value - the list
      Returns:
      the matchers, empty if the value is blank
      Throws:
      IllegalArgumentException - if any entry is not a valid IP address or CIDR range
    • matchesAny

      public static boolean matchesAny(Collection<IpAddressMatcher> matchers, String address)
      Checks whether an address matches any of the given matchers.
      Parameters:
      matchers - the matchers
      address - the IP address
      Returns:
      true if the address is a valid IP address matched by any matcher
    • parseAddress

      public static InetAddress parseAddress(String value)
      Parses a literal IP address, without any DNS lookup.

      IPv6 addresses may be enclosed in brackets and may have a zone index (%eth0), which is ignored. IPv4-mapped IPv6 addresses are converted to IPv4 addresses.

      Parameters:
      value - the IP address
      Returns:
      the address, or null if the value is not a literal IP address
    • matches

      public boolean matches(String address)
      Checks whether the address matches this IP address or CIDR range.
      Parameters:
      address - the IP address
      Returns:
      true if matching, false otherwise or if the address is invalid
    • matches

      public boolean matches(InetAddress address)
      Checks whether the address matches this IP address or CIDR range.
      Parameters:
      address - the IP address
      Returns:
      true if matching, false otherwise
    • toString

      public String toString()
      Returns the normalized IP address or CIDR range.
      Overrides:
      toString in class Object