Class FileBodyMaterialiser

java.lang.Object
org.mockserver.mock.action.http.FileBodyMaterialiser

public class FileBodyMaterialiser extends Object
Materialises a FileBody into a concrete BodyWithContentType (a StringBody or a BinaryBody) by reading the referenced file, so the file CONTENTS - not the file path string - reach the wire.

This is the single shared implementation of that logic, invoked from every response-producing funnel (static response, object callback, class callback, response template, and forward responseOverride) as well as from request-body matching against a FileBody. Keeping it here (the action-handler layer) rather than in the codec keeps file I/O out of BodyDecoderEncoder while still giving the caller access to the request (for templating) and the Configuration (for template engines).

  • A supported templateType (VELOCITY/MUSTACHE) with a request present -> the file is rendered through that engine against the request, preserving the declared content type.
  • No templateType, an unsupported one (e.g. JavaScript), or no request available -> the file is served verbatim: a text content type yields a StringBody; a binary or absent content type yields a BinaryBody of the raw bytes so binary files (images, PDFs, archives) are not charset-corrupted.
  • A missing / unreadable file -> FileBodyException carrying the path for server-side logging.
Author:
jamesdbloom
  • Constructor Details

  • Method Details

    • materialise

      public BodyWithContentType materialise(FileBody fileBody, HttpRequest httpRequest)
      Materialises the given FileBody. When httpRequest is null (or the template type is unsupported/absent) the file is served verbatim; otherwise a supported template type renders the file against the request.
      Throws:
      FileBodyException - when the referenced file cannot be read
    • isFileTemplatingSupported

      public static boolean isFileTemplatingSupported(HttpTemplate.TemplateType templateType)
      Only Velocity and Mustache are supported for templating a FileBody; any other type (e.g. JavaScript) or null means the file is not template-rendered and is served verbatim.