Class NetworkBodyStore

java.lang.Object
com.shaft.capture.storage.NetworkBodyStore

public final class NetworkBodyStore extends Object
Persists request/response bodies as external files with SHA-256 integrity and optional truncation. Mirrors ExternalTestDataWriter's atomic temp-file+move pattern.
  • Constructor Details

    • NetworkBodyStore

      public NetworkBodyStore()
  • Method Details

    • store

      public BodyRef store(byte[] body, String contentType, Path sessionDir)
      Stores a body as an external file and returns a BodyRef with integrity metadata. The stored file name is derived from the SHA-256 of the original full body.
      Parameters:
      body - raw body bytes (may be null or empty)
      contentType - optional media/content-type of the body, recorded as the BodyRef encoding
      sessionDir - directory where the body file will be stored (session-relative)
      Returns:
      BodyRef with a ref to the stored file, sha256/size of original body, its encoding, and truncation flag, or null if body is null/empty
      Throws:
      CaptureFormatException - if write fails
    • store

      public BodyRef store(byte[] body, String contentType, Path sessionDir, int maxBodyBytes)
      Stores a body as an external file with a configurable size cap and returns a BodyRef.
      Parameters:
      body - raw body bytes (may be null or empty)
      contentType - optional media/content-type of the body, recorded as the BodyRef encoding
      sessionDir - directory where the body file will be stored (session-relative)
      maxBodyBytes - maximum bytes to store (original body size is always recorded)
      Returns:
      BodyRef with a ref to the stored file, sha256/size of original full body, its encoding, and truncation flag, or null if body is null/empty
      Throws:
      CaptureFormatException - if write fails
    • read

      public String read(BodyRef bodyRef, Path sessionDir)
      Reads back a body previously stored by store(byte[], String, Path), verifying it still matches its recorded SHA-256 digest. Returns an empty string for a null ref, a ref whose file is missing, or a ref whose file no longer matches its digest (evidence tampering/corruption) -- callers that need bodies purely for codegen classification/rendering treat "unavailable" the same as "no body" rather than failing generation outright.
      Parameters:
      bodyRef - reference returned by a prior store(byte[], String, Path) call, or null
      sessionDir - directory the body was stored in (must match the store call)
      Returns:
      the stored body decoded as UTF-8 text, or "" if unavailable
    • readBytes

      public byte[] readBytes(BodyRef bodyRef, Path sessionDir)
      Reads back a body's raw bytes previously stored by store(byte[], String, Path), verifying it still matches its recorded SHA-256 digest. Unlike read(BodyRef, Path), this does not assume UTF-8 text, so callers that must tell binary content apart from text (for example HAR body emission, which base64-encodes non-textual bodies) can make that call themselves from the stored BodyRef.encoding() content type instead of losing information to a lossy UTF-8 decode.
      Parameters:
      bodyRef - reference returned by a prior store(byte[], String, Path) call, or null
      sessionDir - directory the body was stored in (must match the store call)
      Returns:
      the stored body's raw bytes, or an empty array if unavailable