Record Class RequestContext
java.lang.Object
java.lang.Record
cloud.opencode.base.web.context.RequestContext
- Record Components:
traceId- the trace ID | 追踪IDrequestId- the request ID | 请求IDrequestTime- the request time | 请求时间clientIp- the client IP | 客户端IPuserAgent- 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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classRequest Context Builder 请求上下文构建器 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns the value of theattributesrecord component.static RequestContext.Builderbuilder()Create request context builder 创建请求上下文构建器clientIp()Returns the value of theclientIprecord component.final booleanIndicates whether some other object is "equal to" this one.<T> TgetAttribute(String key) Get attribute 获取属性<T> TgetAttribute(String key, T defaultValue) Get attribute with default value 获取属性(带默认值)Get user ID 获取用户IDGet username 获取用户名final inthashCode()Returns a hash code value for this object.booleanCheck if user is authenticated 检查用户是否已认证locale()Returns the value of thelocalerecord component.static RequestContextCreate simple request context 创建简单请求上下文Returns the value of therequestIdrecord component.Returns the value of therequestTimerecord component.final StringtoString()Returns a string representation of this record class.traceId()Returns the value of thetraceIdrecord component.user()Returns the value of theuserrecord component.Returns the value of theuserAgentrecord component.
-
Constructor Details
-
RequestContext
-
-
Method Details
-
builder
Create request context builder 创建请求上下文构建器- Returns:
- the builder | 构建器
-
of
Create simple request context 创建简单请求上下文- Parameters:
traceId- the trace ID | 追踪ID- Returns:
- the request context | 请求上下文
-
getAttribute
Get attribute 获取属性- Type Parameters:
T- the attribute type | 属性类型- Parameters:
key- the attribute key | 属性键- Returns:
- the attribute value or null | 属性值或null
-
getAttribute
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
-
getUsername
-
toString
-
hashCode
-
equals
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 withObjects::equals(Object,Object). -
traceId
-
requestId
-
requestTime
Returns the value of therequestTimerecord component.- Returns:
- the value of the
requestTimerecord component
-
clientIp
-
userAgent
-
locale
-
user
-
attributes
Returns the value of theattributesrecord component.- Returns:
- the value of the
attributesrecord component
-