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) | 媒体类型参数

public record MediaType(String type, String subtype, Map<String,String> parameters) extends Record
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 Details

    • APPLICATION_JSON

      public static final MediaType APPLICATION_JSON
      application/json
    • APPLICATION_XML

      public static final MediaType APPLICATION_XML
      application/xml
    • TEXT_PLAIN

      public static final MediaType TEXT_PLAIN
      text/plain
    • TEXT_HTML

      public static final MediaType TEXT_HTML
      text/html
    • APPLICATION_OCTET_STREAM

      public static final MediaType APPLICATION_OCTET_STREAM
      application/octet-stream
    • ALL

      public static final MediaType ALL
      Wildcard media type: */*
  • Constructor Details

    • MediaType

      public MediaType(String type, String subtype, Map<String,String> parameters)
      Compact constructor: normalizes type/subtype to lowercase, defensively copies parameters. 紧凑构造器:将类型/子类型规范化为小写,防御性复制参数。
  • Method Details

    • of

      public static MediaType of(String type, String subtype)
      Create a MediaType with type and subtype only. 仅使用主类型和子类型创建 MediaType。
      Parameters:
      type - the primary type | 主类型
      subtype - the sub-type | 子类型
      Returns:
      the media type | 媒体类型
    • of

      public static MediaType of(String type, String subtype, Map<String,String> params)
      Create a MediaType with type, subtype, and parameters. 使用主类型、子类型和参数创建 MediaType。
      Parameters:
      type - the primary type | 主类型
      subtype - the sub-type | 子类型
      params - the parameters | 参数
      Returns:
      the media type | 媒体类型
    • parse

      public static MediaType parse(String value)
      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

      public static List<MediaType> parseAccept(String acceptHeader)
      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

      public static Optional<MediaType> bestMatch(List<MediaType> acceptable, List<MediaType> available)
      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

      public boolean includes(MediaType other)
      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

      public String mimeType()
      Get the MIME type string ("type/subtype"). 获取 MIME 类型字符串("type/subtype")。
      Returns:
      the MIME type | MIME 类型
    • getCharset

      public Optional<String> getCharset()
      Get the charset parameter value. 获取 charset 参数值。
      Returns:
      the charset, or empty if not present | 字符集,如果不存在则为空
    • toString

      public String toString()
      Return the full media type string including parameters. 返回包含参数的完整媒体类型字符串。
      Specified by:
      toString in class Record
      Returns:
      the string representation | 字符串表示
    • 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.
    • type

      public String type()
      Returns the value of the type record component.
      Returns:
      the value of the type record component
    • subtype

      public String subtype()
      Returns the value of the subtype record component.
      Returns:
      the value of the subtype record component
    • parameters

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