Class HttpHeaders

java.lang.Object
cloud.opencode.base.web.http.HttpHeaders
All Implemented Interfaces:
Iterable<Map.Entry<String, List<String>>>

public final class HttpHeaders extends Object implements Iterable<Map.Entry<String, List<String>>>
HTTP Headers - HTTP Request/Response Headers Container HTTP 头部 - HTTP 请求/响应头部容器

This class provides a case-insensitive header container with support for multiple values per header name.

此类提供不区分大小写的头部容器,支持每个头部名称有多个值。

Example | 示例:

HttpHeaders headers = HttpHeaders.of()
    .add("Content-Type", "application/json")
    .add("Accept", "application/json")
    .add("X-Custom", "value1")
    .add("X-Custom", "value2");

String contentType = headers.get("content-type"); // Case-insensitive
List<String> custom = headers.getAll("X-Custom");

Features | 主要功能:

  • Case-insensitive header storage - 不区分大小写的头部存储
  • Multi-value header support - 多值头部支持
  • Fluent API for header manipulation - 流式头部操作API
  • Standard header name constants - 标准头部名称常量

Usage Examples | 使用示例:

HttpHeaders headers = HttpHeaders.of()
    .contentType("application/json")
    .bearerAuth("token123");
String ct = headers.get("content-type");

Security | 安全性:

  • Thread-safe: No - 否
  • Null-safe: No (header name should not be null) - 否(头部名称不应为null)
Since:
JDK 25, opencode-base-web V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Field Details

  • Method Details

    • of

      public static HttpHeaders of()
      Creates empty headers. 创建空头部。
      Returns:
      empty HttpHeaders - 空的 HttpHeaders
    • of

      public static HttpHeaders of(Map<String,String> map)
      Creates headers from a map. 从 Map 创建头部。
      Parameters:
      map - the headers map - 头部 Map
      Returns:
      HttpHeaders - HttpHeaders
    • from

      public static HttpHeaders from(HttpHeaders jdkHeaders)
      Creates headers from JDK HttpHeaders. 从 JDK HttpHeaders 创建头部。
      Parameters:
      jdkHeaders - the JDK headers - JDK 头部
      Returns:
      HttpHeaders - HttpHeaders
    • copyOf

      public static HttpHeaders copyOf(HttpHeaders source)
      Creates a copy of the headers. 创建头部的副本。
      Parameters:
      source - the source headers - 源头部
      Returns:
      HttpHeaders copy - HttpHeaders 副本
    • add

      public HttpHeaders add(String name, String value)
      Adds a header value. 添加头部值。
      Parameters:
      name - the header name - 头部名称
      value - the header value - 头部值
      Returns:
      this for chaining - 用于链式调用
    • set

      public HttpHeaders set(String name, String value)
      Sets a header value (replaces existing). 设置头部值(替换现有值)。
      Parameters:
      name - the header name - 头部名称
      value - the header value - 头部值
      Returns:
      this for chaining - 用于链式调用
    • set

      public HttpHeaders set(String name, List<String> values)
      Sets multiple values for a header. 为头部设置多个值。
      Parameters:
      name - the header name - 头部名称
      values - the header values - 头部值列表
      Returns:
      this for chaining - 用于链式调用
    • remove

      public HttpHeaders remove(String name)
      Removes a header. 移除头部。
      Parameters:
      name - the header name - 头部名称
      Returns:
      this for chaining - 用于链式调用
    • clear

      public HttpHeaders clear()
      Clears all headers. 清除所有头部。
      Returns:
      this for chaining - 用于链式调用
    • get

      public String get(String name)
      Gets the first value for a header. 获取头部的第一个值。
      Parameters:
      name - the header name - 头部名称
      Returns:
      the first value or null - 第一个值或 null
    • getOrDefault

      public String getOrDefault(String name, String defaultValue)
      Gets the first value for a header with default. 获取头部的第一个值,带默认值。
      Parameters:
      name - the header name - 头部名称
      defaultValue - the default value - 默认值
      Returns:
      the first value or default - 第一个值或默认值
    • getAll

      public List<String> getAll(String name)
      Gets all values for a header. 获取头部的所有值。
      Parameters:
      name - the header name - 头部名称
      Returns:
      the values list (never null) - 值列表(永不为 null)
    • contains

      public boolean contains(String name)
      Checks if a header exists. 检查头部是否存在。
      Parameters:
      name - the header name - 头部名称
      Returns:
      true if exists - 如果存在返回 true
    • size

      public int size()
      Gets the number of headers. 获取头部数量。
      Returns:
      the count - 数量
    • isEmpty

      public boolean isEmpty()
      Checks if headers are empty. 检查头部是否为空。
      Returns:
      true if empty - 如果为空返回 true
    • names

      public Set<String> names()
      Gets all header names. 获取所有头部名称。
      Returns:
      the names set - 名称集合
    • toMap

      public Map<String, List<String>> toMap()
      Converts to unmodifiable map. 转换为不可修改的 Map。
      Returns:
      the map - Map
    • toSingleValueMap

      public Map<String,String> toSingleValueMap()
      Converts to single-value map. 转换为单值 Map。
      Returns:
      the map - Map
    • getContentType

      public ContentType getContentType()
      Gets Content-Type header. 获取 Content-Type 头部。
      Returns:
      the content type or null - 内容类型或 null
    • getContentLength

      public long getContentLength()
      Gets Content-Length header. 获取 Content-Length 头部。
      Returns:
      the content length or -1 - 内容长度或 -1
    • contentType

      public HttpHeaders contentType(String contentType)
      Sets Content-Type header. 设置 Content-Type 头部。
      Parameters:
      contentType - the content type - 内容类型
      Returns:
      this for chaining - 用于链式调用
    • contentType

      public HttpHeaders contentType(ContentType contentType)
      Sets Content-Type header. 设置 Content-Type 头部。
      Parameters:
      contentType - the content type - 内容类型
      Returns:
      this for chaining - 用于链式调用
    • accept

      public HttpHeaders accept(String accept)
      Sets Accept header. 设置 Accept 头部。
      Parameters:
      accept - the accept value - Accept 值
      Returns:
      this for chaining - 用于链式调用
    • bearerAuth

      public HttpHeaders bearerAuth(String token)
      Sets Authorization header with Bearer token. 使用 Bearer token 设置 Authorization 头部。
      Parameters:
      token - the bearer token - Bearer token
      Returns:
      this for chaining - 用于链式调用
    • basicAuth

      public HttpHeaders basicAuth(String username, String password)
      Sets Authorization header with Basic auth. 使用 Basic auth 设置 Authorization 头部。
      Parameters:
      username - the username - 用户名
      password - the password - 密码
      Returns:
      this for chaining - 用于链式调用
    • userAgent

      public HttpHeaders userAgent(String userAgent)
      Sets User-Agent header. 设置 User-Agent 头部。
      Parameters:
      userAgent - the user agent - User-Agent
      Returns:
      this for chaining - 用于链式调用
    • iterator

      public Iterator<Map.Entry<String, List<String>>> iterator()
      Specified by:
      iterator in interface Iterable<Map.Entry<String, List<String>>>
    • toString

      public String toString()
      Overrides:
      toString in class Object