Class StandardHttpClient

java.lang.Object
io.leandev.appfuse.http.StandardHttpClient
All Implemented Interfaces:
AutoCloseable

public class StandardHttpClient extends Object implements AutoCloseable

標準 HTTP 客戶端實作

基於 Apache HttpClient 5.x 的高階 HTTP 客戶端封裝,提供:

  • 自動重試機制(線性退避策略)
  • Gateway 轉發支援
  • 認證令牌自動添加
  • 攔截器機制(before/after/error)
  • 連線池統計
  • 自動異常映射(HTTP 狀態碼 → 異常類別)

使用範例

```java // 基本使用 try (StandardHttpClient client = new StandardHttpClient()) { ClassicHttpRequest request = new HttpGet("https://api.example.com/users"); ClassicHttpResponse response = client.execute(request); // 處理回應 }

// 使用 Builder 配置 StandardHttpClient client = StandardHttpClient.builder() .withMaxTotal(200) .withTimeout(30) .withLogging() .withAuthenticator(() -> getAccessToken()) .build();

// 配合 HttpRequestParser/HttpResponseParser ClassicHttpResponse response = client.execute(request); User user = client.getResponseParser().read(response, User.class);


<h2>重試機制</h2>
支援自動重試失敗的 HTTP 請求,使用線性退避策略:

- 預設重試次數:0(不重試)
- 可透過 `setRetries(int)` 設定重試次數
- 使用 [LinearBackoffRetryStrategy] 進行重試


<h2>Gateway 轉發</h2>
支援將所有請求透過指定的 Gateway 轉發:
```java
client.setGateway(new URL("http://localhost:8080"));
// 所有請求都會透過 http://localhost:8080 轉發

攔截器機制

提供三種攔截時機:
  • beforeRequest - 請求發送前
  • afterResponse - 回應接收後
  • onError - 發生錯誤時

異常處理

自動將 HTTP 錯誤狀態碼映射為對應的異常:
Since:
1.0
See Also:
  • Field Details

    • FORWARDED_URL_HEADER

      public static final String FORWARDED_URL_HEADER
      公開的常數,供攔截器使用
      See Also:
  • Constructor Details

    • StandardHttpClient

      public StandardHttpClient()
    • StandardHttpClient

      public StandardHttpClient(org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager connectionManager)
    • StandardHttpClient

      public StandardHttpClient(org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager connectionManager, org.apache.hc.client5.http.config.RequestConfig requestConfig)
  • Method Details

    • setGateway

      public void setGateway(URL gateway)

      設定 Gateway URL

      設定後,所有請求都會透過 Gateway 轉發。

      Parameters:
      gateway - Gateway URL
    • addInterceptor

      public void addInterceptor(HttpClientInterceptor interceptor)
      新增攔截器
      Parameters:
      interceptor - 攔截器
    • execute

      public org.apache.hc.core5.http.ClassicHttpResponse execute(org.apache.hc.core5.http.ClassicHttpRequest request)
    • execute

      public org.apache.hc.core5.http.ClassicHttpResponse execute(org.apache.hc.core5.http.ClassicHttpRequest request, int retries)
    • handleHttpResponse

      protected void handleHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse response, org.apache.hc.core5.http.ClassicHttpRequest request)
      處理 HTTP 回應,檢查狀態碼並拋出對應的異常
      Parameters:
      response - HTTP 回應
      request - 原始請求(用於錯誤訊息)
      Throws:
      RuntimeException - 如果狀態碼 >= 400
    • close

      public void close() throws Exception
      Specified by:
      close in interface AutoCloseable
      Throws:
      Exception
    • getTotalStats

      public StandardHttpClient.Stats getTotalStats()
    • builder