Interface BodySource


public interface BodySource
Minimal abstraction over the pieces of an HTTP message that the shared body-dispatch logic in BodyMatching needs to match an actual message body against a template body matcher.

Both HttpRequest (request matching, via HttpRequestPropertiesMatcher) and HttpResponse (response matching, via HttpResponseMatcher) are adapted to this interface so a single dispatch implementation drives both — giving the response body matcher the same JSON/XML/form conversion, optional-body short-circuit, multipart decoding and binary original/compressed dual-match the request body matcher already has.

The methods mirror the existing message accessors exactly:

  • getBodyAsString() — the decoded body as a string, or null when absent.
  • getBodyAsRawBytes() — the decoded raw body bytes (never null; empty array when absent).
  • getOriginalBody() — the on-the-wire (e.g. still-compressed) bytes when the message arrived compressed and was decompressed, otherwise null.
  • getFirstHeader(String) — the first value of a header (the Content-Type header carries the multipart boundary and selects the JSON/XML/form conversion); "" when absent.
  • getOrComputeConvertedBody(Supplier) — memoises the expensive XML/form-to-JSON conversion per message so the parse runs once per message rather than once per candidate matcher. A message without a cache simply computes on every call.
  • Method Details

    • getBodyAsString

      String getBodyAsString()
    • getBodyAsRawBytes

      byte[] getBodyAsRawBytes()
    • getOriginalBody

      byte[] getOriginalBody()
    • getFirstHeader

      String getFirstHeader(String name)
    • getOrComputeConvertedBody

      String getOrComputeConvertedBody(Supplier<String> supplier)
      Returns the memoised result of the (potentially expensive) XML/form-to-JSON conversion, computing and caching it on first call when the underlying message supports caching. Messages without a cache (e.g. responses) invoke the supplier on every call — the supplier is a pure function of the immutable body so the result is identical either way.
    • requestForLogging

      default HttpRequest requestForLogging()
      The originating HttpRequest when this source wraps a request, otherwise null. Used solely so a JSON-conversion failure log on the request path reports the actual request exactly as before; the response path has no request to report and returns null.