Class IpAddressUtil

java.lang.Object
org.codelibs.fess.util.IpAddressUtil

public final class IpAddressUtil extends Object
Utility class for handling IP addresses, particularly IPv6 addresses in URLs. This class provides methods to properly format IPv6 addresses for use in URLs by adding brackets where necessary.
  • Method Details

    • isIPv6Address

      public static boolean isIPv6Address(String address)
      Determines if the given address string represents an IPv6 address. This method validates the address using InetAddress to ensure it's a valid IPv6 address.
      Parameters:
      address - the IP address string to check
      Returns:
      true if the address is a valid IPv6 address, false otherwise
    • formatForUrl

      public static String formatForUrl(String address)
      Formats an IP address string for use in a URL. IPv6 addresses are wrapped in brackets, IPv4 addresses are returned as-is.
      Parameters:
      address - the IP address string to format
      Returns:
      the formatted address (IPv6 with brackets, IPv4 unchanged)
    • compressIPv6

      protected static String compressIPv6(String ipv6Address)
      Compresses an IPv6 address string to its canonical compressed form. For example, "0:0:0:0:0:0:0:1" becomes "::1"
      Parameters:
      ipv6Address - the IPv6 address string to compress
      Returns:
      the compressed IPv6 address string
    • getUrlHost

      public static String getUrlHost(InetAddress address)
      Generates a URL-safe host string from an InetAddress. For IPv6 addresses, this wraps the address in brackets and compresses it. For IPv4 addresses, returns the address as-is.
      Parameters:
      address - the InetAddress to format
      Returns:
      the URL-safe host string
    • buildUrl

      public static String buildUrl(String protocol, InetAddress address, int port, String path)
      Builds a URL from protocol, InetAddress, port, and path. Properly handles IPv6 addresses by wrapping them in brackets.
      Parameters:
      protocol - the protocol (e.g., "http", "https")
      address - the InetAddress for the host
      port - the port number
      path - the path (should start with "/" or be empty)
      Returns:
      the complete URL string
    • buildUrl

      public static String buildUrl(String protocol, String host, int port, String path)
      Builds a URL from protocol, hostname string, port, and path. Properly handles IPv6 addresses by wrapping them in brackets if needed.
      Parameters:
      protocol - the protocol (e.g., "http", "https")
      host - the hostname or IP address string
      port - the port number
      path - the path (should start with "/" or be empty)
      Returns:
      the complete URL string