Class RecordedTrafficImporter

java.lang.Object
org.mockserver.imports.RecordedTrafficImporter

public class RecordedTrafficImporter extends Object
Re-imports recorded request/response pairs from the append-only NDJSON archive written by RecordedRequestsFileSystemPersistence.

The archive is newline-delimited JSON — one serialized HttpRequestAndHttpResponse per line (see persistRecordedRequestsToDisk). This importer parses each non-blank line back into an HttpRequestAndHttpResponse using the same HttpRequestAndHttpResponseSerializer that produced it, so the format round-trips. Unlike HarImporter / PostmanCollectionImporter (which build expectations), the imported pairs are re-injected into the event log as recorded exchanges and become retrievable exactly like in-memory recordings.

Crash-tolerant. A malformed line is skipped and counted, not fatal — the write path flushes line + "\n" per exchange, so a hard kill can leave a truncated mid-JSON final line. Aborting the whole import on that one artefact would defeat the crash-recovery guarantee, so this importer returns every intact exchange and reports how many lines it skipped. It only throws when the input contains non-blank lines but none parse (i.e. the body is not a recorded-traffic archive at all). Blank/empty input yields an empty result (0 imported).

Redaction is on by default (see ImportRedaction) as a defence-in-depth re-mask; the persist side already masks secrets on write when mockserver.redactSecretsInLog is enabled.

  • Constructor Details

    • RecordedTrafficImporter

      public RecordedTrafficImporter(MockServerLogger mockServerLogger)
  • Method Details

    • importRecordedTraffic

      public RecordedTrafficImporter.Result importRecordedTraffic(String ndjson)
      Parse an NDJSON recorded-traffic archive with redaction enabled (the default).
      Parameters:
      ndjson - the archive content — one HttpRequestAndHttpResponse JSON object per line
      Returns:
      the parsed pairs and skipped-line count
      Throws:
      IllegalArgumentException - only when the input has non-blank lines but none parse
    • importRecordedTraffic

      public RecordedTrafficImporter.Result importRecordedTraffic(String ndjson, ImportRedaction.Options redactionOptions)
      Parse an NDJSON recorded-traffic archive, applying the supplied redaction options. Malformed or truncated lines (e.g. the final line left half-written by a crash) are skipped and counted rather than aborting the whole import; see the class javadoc.
      Parameters:
      ndjson - the archive content — one HttpRequestAndHttpResponse JSON object per line
      redactionOptions - controls whether/how sensitive data is masked; pass ImportRedaction.Options.disabled() to keep values verbatim
      Returns:
      the parsed pairs and skipped-line count (both zero for blank/empty input)
      Throws:
      IllegalArgumentException - only when the input has non-blank lines but none parse