Record Class RequestContext

java.lang.Object
java.lang.Record
cloud.opencode.base.web.context.RequestContext
Record Components:
traceId - the trace ID | 追踪ID
requestId - the request ID | 请求ID
requestTime - the request time | 请求时间
clientIp - the client IP | 客户端IP
userAgent - the user agent | 用户代理
locale - the locale | 区域设置
user - the user context | 用户上下文
attributes - additional attributes | 附加属性

public record RequestContext(String traceId, String requestId, Instant requestTime, String clientIp, String userAgent, Locale locale, UserContext user, Map<String,Object> attributes) extends Record
Request Context 请求上下文

Holds request-scoped information for the current request.

保存当前请求范围内的信息。

Features | 主要功能:

  • Immutable request-scoped data record - 不可变的请求范围数据记录
  • Trace ID and request ID tracking - 追踪 ID 和请求 ID 跟踪
  • User context association - 用户上下文关联
  • Builder pattern for flexible construction - 构建器模式支持灵活构建

Usage Examples | 使用示例:

// Create with builder
RequestContext ctx = RequestContext.builder()
    .traceId("abc123")
    .clientIp("192.168.1.1")
    .user(UserContext.of("1", "john"))
    .build();

// Simple creation
RequestContext ctx = RequestContext.of("trace-id-123");

Security | 安全性:

  • Thread-safe: Yes (immutable record) - 线程安全: 是(不可变记录)
  • Null-safe: Partial (traceId and clientIp can be null) - 空值安全: 部分(traceId 和 clientIp 可为 null)
Since:
JDK 25, opencode-base-web V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

  • Method Details

    • builder

      public static RequestContext.Builder builder()
      Create request context builder 创建请求上下文构建器
      Returns:
      the builder | 构建器
    • of

      public static RequestContext of(String traceId)
      Create simple request context 创建简单请求上下文
      Parameters:
      traceId - the trace ID | 追踪ID
      Returns:
      the request context | 请求上下文
    • getAttribute

      public <T> T getAttribute(String key)
      Get attribute 获取属性
      Type Parameters:
      T - the attribute type | 属性类型
      Parameters:
      key - the attribute key | 属性键
      Returns:
      the attribute value or null | 属性值或null
    • getAttribute

      public <T> T getAttribute(String key, T defaultValue)
      Get attribute with default value 获取属性(带默认值)
      Type Parameters:
      T - the attribute type | 属性类型
      Parameters:
      key - the attribute key | 属性键
      defaultValue - the default value | 默认值
      Returns:
      the attribute value or default | 属性值或默认值
    • isAuthenticated

      public boolean isAuthenticated()
      Check if user is authenticated 检查用户是否已认证
      Returns:
      true if authenticated | 如果已认证返回true
    • getUserId

      public String getUserId()
      Get user ID 获取用户ID
      Returns:
      the user ID or null | 用户ID或null
    • getUsername

      public String getUsername()
      Get username 获取用户名
      Returns:
      the username or null | 用户名或null
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • traceId

      public String traceId()
      Returns the value of the traceId record component.
      Returns:
      the value of the traceId record component
    • requestId

      public String requestId()
      Returns the value of the requestId record component.
      Returns:
      the value of the requestId record component
    • requestTime

      public Instant requestTime()
      Returns the value of the requestTime record component.
      Returns:
      the value of the requestTime record component
    • clientIp

      public String clientIp()
      Returns the value of the clientIp record component.
      Returns:
      the value of the clientIp record component
    • userAgent

      public String userAgent()
      Returns the value of the userAgent record component.
      Returns:
      the value of the userAgent record component
    • locale

      public Locale locale()
      Returns the value of the locale record component.
      Returns:
      the value of the locale record component
    • user

      public UserContext user()
      Returns the value of the user record component.
      Returns:
      the value of the user record component
    • attributes

      public Map<String,Object> attributes()
      Returns the value of the attributes record component.
      Returns:
      the value of the attributes record component