Class OpenWeb

java.lang.Object
cloud.opencode.base.web.OpenWeb

public final class OpenWeb extends Object
Open Web 开放Web

Main facade for web utilities.

Web工具的主要门面。

Features | 主要功能:

  • Result shortcuts - 响应快捷方法
  • Page shortcuts - 分页快捷方法
  • Context management - 上下文管理
  • URL encoding - URL编码
  • Validation - 验证

Usage Examples | 使用示例:

// Success / failure results
Result<User> ok   = OpenWeb.ok(user);
Result<?>    fail = OpenWeb.fail(CommonResultCode.NOT_FOUND);

// Paging
PageResult<User> page = OpenWeb.page(users, 1, 10, 100).data();

// Request context
RequestContext ctx = OpenWeb.getContext();
String userId = OpenWeb.getUserId();

// URL helpers
String encoded = OpenWeb.urlEncode("hello world");
boolean validEmail = OpenWeb.isValidEmail("user@example.com");

Security | 安全性:

  • Thread-safe: Yes (stateless utility class) - 线程安全: 是(无状态工具类)
  • Null-safe: Partial (delegates to underlying utilities) - 空值安全: 部分(委托给底层工具)
Since:
JDK 25, opencode-base-web V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • ok

      public static <T> Result<T> ok()
      Create success result 创建成功结果
      Type Parameters:
      T - the data type | 数据类型
      Returns:
      the result | 结果
    • ok

      public static <T> Result<T> ok(T data)
      Create success result with data 创建带数据的成功结果
      Type Parameters:
      T - the data type | 数据类型
      Parameters:
      data - the data | 数据
      Returns:
      the result | 结果
    • ok

      public static <T> Result<T> ok(String message, T data)
      Create success result with message and data 创建带消息和数据的成功结果
      Type Parameters:
      T - the data type | 数据类型
      Parameters:
      message - the message | 消息
      data - the data | 数据
      Returns:
      the result | 结果
    • fail

      public static <T> Result<T> fail(String message)
      Create failure result 创建失败结果
      Type Parameters:
      T - the data type | 数据类型
      Parameters:
      message - the message | 消息
      Returns:
      the result | 结果
    • fail

      public static <T> Result<T> fail(String code, String message)
      Create failure result with code 创建带代码的失败结果
      Type Parameters:
      T - the data type | 数据类型
      Parameters:
      code - the code | 代码
      message - the message | 消息
      Returns:
      the result | 结果
    • fail

      public static <T> Result<T> fail(ResultCode resultCode)
      Create failure result with result code 使用响应码创建失败结果
      Type Parameters:
      T - the data type | 数据类型
      Parameters:
      resultCode - the result code | 响应码
      Returns:
      the result | 结果
    • fail

      public static <T> Result<T> fail(Throwable throwable)
      Create failure result from exception 从异常创建失败结果
      Type Parameters:
      T - the data type | 数据类型
      Parameters:
      throwable - the exception | 异常
      Returns:
      the result | 结果
    • page

      public static <T> PageResult<T> page(List<T> items, int page, int size, long total)
      Create page result 创建分页结果
      Type Parameters:
      T - the item type | 项类型
      Parameters:
      items - the items | 项列表
      page - the page number | 页码
      size - the page size | 页大小
      total - the total count | 总数
      Returns:
      the page result | 分页结果
    • pageRequest

      public static PageRequest pageRequest(int page, int size)
      Create page request 创建分页请求
      Parameters:
      page - the page number | 页码
      size - the page size | 页大小
      Returns:
      the page request | 分页请求
    • pageRequest

      public static PageRequest pageRequest(int page, int size, String sortBy, String sortOrder)
      Create page request with sort 创建带排序的分页请求
      Parameters:
      page - the page number | 页码
      size - the page size | 页大小
      sortBy - the sort field | 排序字段
      sortOrder - the sort order (asc/desc) | 排序顺序
      Returns:
      the page request | 分页请求
    • pageRequest

      public static PageRequest pageRequest(int page, int size, Sort sort)
      Create page request with sort 创建带排序的分页请求
      Parameters:
      page - the page number | 页码
      size - the page size | 页大小
      sort - the sort criteria | 排序条件
      Returns:
      the page request | 分页请求
    • getContext

      public static RequestContext getContext()
      Get current request context 获取当前请求上下文
      Returns:
      the request context or null | 请求上下文或null
    • setContext

      public static void setContext(RequestContext context)
      Set current request context 设置当前请求上下文
      Parameters:
      context - the request context | 请求上下文
    • clearContext

      public static void clearContext()
      Clear current request context 清除当前请求上下文
    • getUser

      public static UserContext getUser()
      Get current user context 获取当前用户上下文
      Returns:
      the user context or null | 用户上下文或null
    • getUserId

      public static String getUserId()
      Get current user ID 获取当前用户ID
      Returns:
      the user ID or null | 用户ID或null
    • getTraceId

      public static String getTraceId()
      Get current trace ID 获取当前追踪ID
      Returns:
      the trace ID or null | 追踪ID或null
    • urlEncode

      public static String urlEncode(String value)
      URL encode URL编码
      Parameters:
      value - the value to encode | 要编码的值
      Returns:
      the encoded value | 编码后的值
    • urlDecode

      public static String urlDecode(String value)
      URL decode URL解码
      Parameters:
      value - the value to decode | 要解码的值
      Returns:
      the decoded value | 解码后的值
    • base64Encode

      public static String base64Encode(String value)
      Base64 encode Base64编码
      Parameters:
      value - the value to encode | 要编码的值
      Returns:
      the encoded value | 编码后的值
    • base64Decode

      public static String base64Decode(String value)
      Base64 decode Base64解码
      Parameters:
      value - the value to decode | 要解码的值
      Returns:
      the decoded value | 解码后的值
    • base64UrlEncode

      public static String base64UrlEncode(String value)
      Base64 URL encode Base64 URL编码
      Parameters:
      value - the value to encode | 要编码的值
      Returns:
      the encoded value | 编码后的值
    • base64UrlDecode

      public static String base64UrlDecode(String value)
      Base64 URL decode Base64 URL解码
      Parameters:
      value - the value to decode | 要解码的值
      Returns:
      the decoded value | 解码后的值
    • parseQuery

      public static Map<String,String> parseQuery(String queryString)
      Parse query string 解析查询字符串
      Parameters:
      queryString - the query string | 查询字符串
      Returns:
      the parameters map | 参数映射
    • buildQuery

      public static String buildQuery(Map<String,String> params)
      Build query string 构建查询字符串
      Parameters:
      params - the parameters | 参数
      Returns:
      the query string | 查询字符串
    • isValidIp

      public static boolean isValidIp(String ip)
      Check if valid IP 检查是否有效IP
      Parameters:
      ip - the IP to check | 要检查的IP
      Returns:
      true if valid | 如果有效返回true
    • isValidEmail

      public static boolean isValidEmail(String email)
      Check if valid email 检查是否有效邮箱
      Parameters:
      email - the email to check | 要检查的邮箱
      Returns:
      true if valid | 如果有效返回true
    • isValidUrl

      public static boolean isValidUrl(String url)
      Check if valid URL 检查是否有效URL
      Parameters:
      url - the URL to check | 要检查的URL
      Returns:
      true if valid | 如果有效返回true
    • isPrivateIp

      public static boolean isPrivateIp(String ip)
      Check if private IP 检查是否私有IP
      Parameters:
      ip - the IP to check | 要检查的IP
      Returns:
      true if private | 如果是私有IP返回true