Class OpenUrl

java.lang.Object
cloud.opencode.base.web.url.OpenUrl

public final class OpenUrl extends Object
Open URL - URL Utility Class Open URL - URL 工具类

This class provides static utility methods for URL encoding, decoding, parsing, and validation.

此类提供 URL 编码、解码、解析和验证的静态工具方法。

Example | 示例:

// Encoding
String encoded = OpenUrl.encode("hello world"); // "hello+world"
String pathEncoded = OpenUrl.encodePath("path/to file"); // "path/to%20file"

// Decoding
String decoded = OpenUrl.decode("hello%20world"); // "hello world"

// Parsing
String host = OpenUrl.getHost("https://example.com/path"); // "example.com"
String path = OpenUrl.getPath("https://example.com/path"); // "/path"

// Validation
boolean valid = OpenUrl.isValid("https://example.com"); // true

Features | 主要功能:

  • URL encoding and decoding - URL编码和解码
  • URL parsing (scheme, host, port, path) - URL解析
  • URL validation - URL验证
  • IDN (Punycode) support - IDN(Punycode)支持

Usage Examples | 使用示例:

String encoded = OpenUrl.encode("hello world");
String host = OpenUrl.getHost("https://example.com/path");
boolean valid = OpenUrl.isValid("https://example.com");

Security | 安全性:

  • Thread-safe: Yes (stateless utility) - 是(无状态工具)
  • Null-safe: Yes (returns null for null input) - 是(null输入返回null)
Since:
JDK 25, opencode-base-web V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • encode

      public static String encode(String value)
      URL encodes a string. URL 编码字符串。
      Parameters:
      value - the value to encode - 要编码的值
      Returns:
      the encoded string - 编码的字符串
    • decode

      public static String decode(String value)
      URL decodes a string. URL 解码字符串。
      Parameters:
      value - the value to decode - 要解码的值
      Returns:
      the decoded string - 解码的字符串
    • encodePath

      public static String encodePath(String path)
      Encodes a path segment (preserves slashes). 编码路径段(保留斜杠)。
      Parameters:
      path - the path to encode - 要编码的路径
      Returns:
      the encoded path - 编码的路径
    • encodeParams

      public static String encodeParams(Map<String,String> params)
      Encodes query parameters from map. 从 Map 编码查询参数。
      Parameters:
      params - the parameters - 参数
      Returns:
      the encoded query string - 编码的查询字符串
    • builder

      public static UrlBuilder builder()
      Creates a URL builder. 创建 URL 构建器。
      Returns:
      the builder - 构建器
    • builder

      public static UrlBuilder builder(String url)
      Creates a URL builder from existing URL. 从现有 URL 创建构建器。
      Parameters:
      url - the base URL - 基础 URL
      Returns:
      the builder - 构建器
    • getScheme

      public static String getScheme(String url)
      Gets the scheme from URL. 从 URL 获取协议。
      Parameters:
      url - the URL - URL
      Returns:
      the scheme or null - 协议或 null
    • getHost

      public static String getHost(String url)
      Gets the host from URL. 从 URL 获取主机。
      Parameters:
      url - the URL - URL
      Returns:
      the host or null - 主机或 null
    • getPort

      public static int getPort(String url)
      Gets the port from URL. 从 URL 获取端口。
      Parameters:
      url - the URL - URL
      Returns:
      the port or -1 - 端口或 -1
    • getEffectivePort

      public static int getEffectivePort(String url)
      Gets the effective port (considering default ports). 获取有效端口(考虑默认端口)。
      Parameters:
      url - the URL - URL
      Returns:
      the effective port - 有效端口
    • getPath

      public static String getPath(String url)
      Gets the path from URL. 从 URL 获取路径。
      Parameters:
      url - the URL - URL
      Returns:
      the path or null - 路径或 null
    • getQuery

      public static String getQuery(String url)
      Gets the query string from URL. 从 URL 获取查询字符串。
      Parameters:
      url - the URL - URL
      Returns:
      the query string or null - 查询字符串或 null
    • getFragment

      public static String getFragment(String url)
      Gets the fragment from URL. 从 URL 获取片段。
      Parameters:
      url - the URL - URL
      Returns:
      the fragment or null - 片段或 null
    • parseQuery

      public static QueryString parseQuery(String url)
      Parses query string from URL. 从 URL 解析查询字符串。
      Parameters:
      url - the URL - URL
      Returns:
      the parsed query string - 解析的查询字符串
    • getQueryParam

      public static String getQueryParam(String url, String name)
      Gets query parameter from URL. 从 URL 获取查询参数。
      Parameters:
      url - the URL - URL
      name - the parameter name - 参数名
      Returns:
      the parameter value or null - 参数值或 null
    • join

      public static String join(String base, String... parts)
      Joins URL parts. 连接 URL 部分。
      Parameters:
      base - the base URL - 基础 URL
      parts - the parts to append - 要追加的部分
      Returns:
      the joined URL - 连接的 URL
    • normalize

      public static String normalize(String url)
      Normalizes a URL. 规范化 URL。
      Parameters:
      url - the URL - URL
      Returns:
      the normalized URL - 规范化的 URL
    • removeQuery

      public static String removeQuery(String url)
      Removes query string from URL. 从 URL 删除查询字符串。
      Parameters:
      url - the URL - URL
      Returns:
      the URL without query - 不含查询的 URL
    • removeFragment

      public static String removeFragment(String url)
      Removes fragment from URL. 从 URL 删除片段。
      Parameters:
      url - the URL - URL
      Returns:
      the URL without fragment - 不含片段的 URL
    • getBaseUrl

      public static String getBaseUrl(String url)
      Gets the base URL (scheme + host + port). 获取基础 URL(协议 + 主机 + 端口)。
      Parameters:
      url - the URL - URL
      Returns:
      the base URL - 基础 URL
    • isValid

      public static boolean isValid(String url)
      Checks if URL is valid. 检查 URL 是否有效。
      Parameters:
      url - the URL - URL
      Returns:
      true if valid - 如果有效返回 true
    • isValidUrl

      public static boolean isValidUrl(String url)
      Checks if URL matches pattern. 检查 URL 是否匹配模式。
      Parameters:
      url - the URL - URL
      Returns:
      true if matches - 如果匹配返回 true
    • isValidDomain

      public static boolean isValidDomain(String domain)
      Checks if domain is valid. 检查域名是否有效。
      Parameters:
      domain - the domain - 域名
      Returns:
      true if valid - 如果有效返回 true
    • isHttps

      public static boolean isHttps(String url)
      Checks if URL is HTTPS. 检查 URL 是否为 HTTPS。
      Parameters:
      url - the URL - URL
      Returns:
      true if HTTPS - 如果是 HTTPS 返回 true
    • isHttp

      public static boolean isHttp(String url)
      Checks if URL is HTTP. 检查 URL 是否为 HTTP。
      Parameters:
      url - the URL - URL
      Returns:
      true if HTTP - 如果是 HTTP 返回 true
    • toAscii

      public static String toAscii(String domain)
      Converts domain to ASCII (Punycode). 将域名转换为 ASCII(Punycode)。
      Parameters:
      domain - the domain - 域名
      Returns:
      the ASCII domain - ASCII 域名
    • toUnicode

      public static String toUnicode(String domain)
      Converts domain from ASCII (Punycode) to Unicode. 将域名从 ASCII(Punycode)转换为 Unicode。
      Parameters:
      domain - the ASCII domain - ASCII 域名
      Returns:
      the Unicode domain - Unicode 域名