Class TikaHttpClient

java.lang.Object
org.apache.tika.http.TikaHttpClient
All Implemented Interfaces:
Closeable, AutoCloseable

public class TikaHttpClient extends Object implements Closeable
Lightweight HTTP client for Tika parser modules that call external REST endpoints (embedding APIs, VLM services, etc.).

Built on HttpClient with a daemon thread executor so the JVM — including forked PipesServer processes — shuts down cleanly without waiting for idle HTTP threads.

This class has no runtime dependencies beyond the JDK and tika-core. Obtain an instance via build(int) and close it when done to release the underlying executor.

A hosted inference engine caps the requests in flight per key and answers the excess with 429 at once, usually without a Retry-After; a saturated one answers 502, 503 or 504. Those four statuses are retried up to maxRetries times with a short jittered backoff (250 ms doubling to 4 s), or the Retry-After the server sends, never sleeping past the budget the parse granted the call. Any other status fails at once. The retries cover collisions between a few parse workers, not a sustained excess of workers over the key's concurrency.

Since:
Apache Tika 4.0
  • Field Details

  • Method Details

    • build

      public static TikaHttpClient build(int connectTimeoutSeconds)
      Create a new TikaHttpClient with a daemon-thread executor.
      Parameters:
      connectTimeoutSeconds - TCP connection timeout in seconds
    • build

      public static TikaHttpClient build(int connectTimeoutSeconds, int maxRetries)
      Create a new TikaHttpClient with a daemon-thread executor.
      Parameters:
      connectTimeoutSeconds - TCP connection timeout in seconds
      maxRetries - how many times a 429, 502, 503 or 504 answer is retried; 0 fails on the first one
    • getMaxRetries

      public int getMaxRetries()
    • postJson

      public String postJson(String url, String jsonBody, Map<String,String> headers, long requestedTimeoutMillis) throws IOException, TikaException
      POST a JSON body to url and return the response body as a string.

      Equivalent to postJson(String, String, Map, long, ParseContext) with a null context: requestedTimeoutMillis is granted unclipped, no checkpointing.

      Parameters:
      url - target URL
      jsonBody - request body (UTF-8 JSON)
      headers - additional HTTP headers (e.g. Authorization)
      requestedTimeoutMillis - read timeout in millis; 0 uses the default timeout
      Returns:
      response body string
      Throws:
      IOException - on network error
      TikaException - on non-2xx HTTP status
    • postJson

      public String postJson(String url, String jsonBody, Map<String,String> headers, long requestedTimeoutMillis, ParseContext context) throws IOException, TikaException
      Same as postJson(String, String, Map, long), but bounds the wait to min(requestedTimeoutMillis, ParseTimeout.remainingMillis()) (see ParseTimeout.budgetFor(long)) so no single call can outlast the task's total timeout regardless of its own configuration. While waiting, checkpoints the ParseTimeout in context (if any) every 1000L ms -- see ProcessUtils.execute(ProcessBuilder, ParseContext, long, int, int) for the same rationale applied to subprocess calls. A null context means the budget is granted unclipped.
      Parameters:
      url - target URL
      jsonBody - request body (UTF-8 JSON)
      headers - additional HTTP headers (e.g. Authorization)
      requestedTimeoutMillis - the timeout the caller's own configuration asks for, in millis; 0 uses the default timeout
      context - may be null
      Returns:
      response body string
      Throws:
      IOException - on network error
      TikaException - on non-2xx HTTP status
    • get

      public String get(String url, Map<String,String> headers, long requestedTimeoutMillis) throws IOException, TikaException
      GET url and return the response body as a string. Useful for health-check probes at init time.

      Equivalent to get(String, Map, long, ParseContext) with a null context.

      Parameters:
      url - target URL
      headers - additional HTTP headers
      requestedTimeoutMillis - read timeout in millis; 0 uses the default timeout
      Returns:
      response body string
      Throws:
      IOException - on network error
      TikaException - on non-2xx HTTP status
    • get

      public String get(String url, Map<String,String> headers, long requestedTimeoutMillis, ParseContext context) throws IOException, TikaException
      Same as get(String, Map, long), but bounds the wait to min(requestedTimeoutMillis, ParseTimeout.remainingMillis()) and checkpoints while waiting -- see postJson(String, String, Map, long, ParseContext).
      Parameters:
      url - target URL
      headers - additional HTTP headers
      requestedTimeoutMillis - the timeout the caller's own configuration asks for, in millis; 0 uses the default timeout
      context - may be null
      Returns:
      response body string
      Throws:
      IOException - on network error
      TikaException - on non-2xx HTTP status
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable