Record Class MediaType
java.lang.Object
java.lang.Record
cloud.opencode.base.web.http.MediaType
- Record Components:
type- the primary type (e.g., "application") | 主类型subtype- the sub-type (e.g., "json") | 子类型parameters- the media type parameters (e.g., charset, q) | 媒体类型参数
HTTP Media Type (MIME Type) representation and Accept header parsing
HTTP 媒体类型(MIME 类型)表示和 Accept 头部解析
Provides RFC 7231 compliant media type handling including quality factor parsing, content negotiation, and wildcard matching.
提供符合 RFC 7231 的媒体类型处理,包括质量因子解析、内容协商和通配符匹配。
Features | 主要功能:
- Media type parsing with parameters - 带参数的媒体类型解析
- Accept header parsing with quality factor sorting - Accept 头部解析与质量因子排序
- Content negotiation (bestMatch) - 内容协商(最佳匹配)
- Wildcard support (* / *) - 通配符支持
- Common media type constants - 常见媒体类型常量
Usage Examples | 使用示例:
// Parse a single media type
MediaType json = MediaType.parse("application/json; charset=utf-8");
// Parse Accept header
List<MediaType> accepted = MediaType.parseAccept("text/html, application/json;q=0.9");
// Content negotiation
Optional<MediaType> best = MediaType.bestMatch(accepted,
List.of(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML));
Security | 安全性:
- Thread-safe: Yes (immutable record) - 线程安全: 是(不可变记录)
- Null-safe: Yes (factory methods reject null) - 空值安全: 是(工厂方法拒绝 null)
- Since:
- JDK 25, opencode-base-web V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionFind the best match from available media types given acceptable types.final booleanIndicates whether some other object is "equal to" this one.Get the charset parameter value.doubleGet the quality factor (q parameter), defaulting to 1.0.final inthashCode()Returns a hash code value for this object.booleanCheck if this media type includes the other (wildcard matching).booleanCheck if this is the wildcard type (*/*).booleanCheck if the subtype is a wildcard.mimeType()Get the MIME type string ("type/subtype").static MediaTypeCreate a MediaType with type and subtype only.static MediaTypeCreate a MediaType with type, subtype, and parameters.Returns the value of theparametersrecord component.static MediaTypeParse a single media type string (e.g., "application/json; charset=utf-8").parseAccept(String acceptHeader) Parse an HTTP Accept header into a list of media types sorted by quality factor (descending).subtype()Returns the value of thesubtyperecord component.toString()Return the full media type string including parameters.type()Returns the value of thetyperecord component.
-
Field Details
-
APPLICATION_JSON
application/json -
APPLICATION_XML
application/xml -
TEXT_PLAIN
text/plain -
TEXT_HTML
text/html -
APPLICATION_OCTET_STREAM
application/octet-stream -
ALL
Wildcard media type: */*
-
-
Constructor Details
-
MediaType
-
-
Method Details
-
of
-
of
-
parse
Parse a single media type string (e.g., "application/json; charset=utf-8"). 解析单个媒体类型字符串。- Parameters:
value- the media type string | 媒体类型字符串- Returns:
- the parsed media type | 解析后的媒体类型
- Throws:
IllegalArgumentException- if value is null, blank, or malformed | 如果值为 null、空白或格式错误
-
parseAccept
Parse an HTTP Accept header into a list of media types sorted by quality factor (descending). 将 HTTP Accept 头部解析为按质量因子降序排列的媒体类型列表。- Parameters:
acceptHeader- the Accept header value | Accept 头部值- Returns:
- the sorted list of media types | 排序后的媒体类型列表
- Throws:
IllegalArgumentException- if acceptHeader is null or blank | 如果头部值为 null 或空白
-
bestMatch
Find the best match from available media types given acceptable types. 根据可接受的类型从可用媒体类型中找到最佳匹配。Iterates acceptable types in quality-factor order and returns the first available type that is included by an acceptable type.
按质量因子顺序遍历可接受类型,返回第一个被可接受类型包含的可用类型。
- Parameters:
acceptable- the acceptable media types (from Accept header) | 可接受的媒体类型available- the available media types (server can produce) | 可用的媒体类型- Returns:
- the best match, or empty if none | 最佳匹配,如果没有则为空
-
getQuality
public double getQuality()Get the quality factor (q parameter), defaulting to 1.0. 获取质量因子(q 参数),默认 1.0。- Returns:
- the quality factor [0.0, 1.0] | 质量因子
-
includes
Check if this media type includes the other (wildcard matching). 检查此媒体类型是否包含另一个(通配符匹配)。A wildcard type includes all types. A wildcard subtype includes all subtypes of the same primary type.
- Parameters:
other- the other media type | 另一个媒体类型- Returns:
- true if this type includes the other | 如果包含返回 true
-
isWildcard
public boolean isWildcard()Check if this is the wildcard type (*/*). 检查是否为通配符类型。- Returns:
- true if wildcard | 如果为通配符返回 true
-
isWildcardSubtype
public boolean isWildcardSubtype()Check if the subtype is a wildcard. 检查子类型是否为通配符。- Returns:
- true if subtype is wildcard | 如果子类型为通配符返回 true
-
mimeType
Get the MIME type string ("type/subtype"). 获取 MIME 类型字符串("type/subtype")。- Returns:
- the MIME type | MIME 类型
-
getCharset
-
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). -
type
-
subtype
-
parameters
Returns the value of theparametersrecord component.- Returns:
- the value of the
parametersrecord component
-