Class FileMessageResponse

java.lang.Object
com.trafficparrot.sdk.file.FileMessageResponse
All Implemented Interfaces:
Comparable<FileMessageResponse>

public class FileMessageResponse extends Object implements Comparable<FileMessageResponse>
The response of a file message mapping: the file Traffic Parrot writes when a request file matches. A FileMessageResponseTransformer receives the mapping's response and returns the one to write:

 return new FileMessageResponse(response.destination, "reply-" + requestMessage.fileName, "received " + requestMessage.fileName);
 
A response does not change; build a new one to change anything. The body is either text or the content of the bodyFileName file, never both; a transformer can also answer with a file of its own, copied rather than read, with ofFile(Destination, String, Path).
  • Field Details

    • NO_TRANSFORMER

      public static final String NO_TRANSFORMER
      The fileMessageResponseTransformerClassName of a response that uses no transformer. A response returned from a transformer must carry it.
      See Also:
    • destination

      public final Destination destination
      The directory the response file is written to: its name is the directory's path.
    • fileMessageResponseTransformerClassName

      public final String fileMessageResponseTransformerClassName
      The class name of the FileMessageResponseTransformer the mapping uses, or NO_TRANSFORMER.
    • fileName

      public final String fileName
      The name of the response file WRITTEN to the response directory - a transport output name, Handlebars-templated like text. Not to be confused with bodyFileName, which names a __files sidecar the body is READ from.
    • text

      public final String text
      The response file's content, which may hold Handlebars expressions. With a BYTES bodyType it is the content as Base64, written as the bytes it encodes. Null when the body comes from bodyFileName.
    • bodyFileName

      public final String bodyFileName
      A __files/<name> sidecar holding the response body, used instead of an inline text. Mutually exclusive with it - see validate().
    • bodyType

      public final MessageBodyType bodyType
      How the body is written: BYTES as the bytes its Base64 text encodes, any other type as text. A body read from bodyFileName supports only TEXT and BYTES.
    • resolvedText

      public final transient String resolvedText
      Content resolved from bodyFileName at load time. Held here rather than in text so the persisted model keeps bodyFileName set and text null (non-destructive). transient so the filesystem mapper never writes it to disk; set only via withResolvedText(String) and read via effectiveText(). The web-UI mapper re-includes it (via a mix-in) so the editor can display the content.
  • Constructor Details

    • FileMessageResponse

      public FileMessageResponse(Destination destination, String fileMessageResponseTransformerClassName, String fileName, String text, String bodyFileName, MessageBodyType bodyType)
      Creates a response with every field a mapping file can set. Traffic Parrot reads a mapping's response through this constructor.
      Parameters:
      destination - the directory the response file is written to
      fileMessageResponseTransformerClassName - the transformer's class name, or NO_TRANSFORMER
      fileName - the name of the response file
      text - the content; null when bodyFileName is set
      bodyFileName - a file under __files to read the content from; null when text is set
      bodyType - how the body is written, as bodyType describes
    • FileMessageResponse

      public FileMessageResponse(Destination destination, String fileMessageResponseTransformerClassName, String fileName, String text, MessageBodyType bodyType)
      Creates a response whose body is given inline, with a body type: for a transformer that answers with binary content, pass NO_TRANSFORMER, the content as Base64 and MessageBodyType.BYTES.
      Parameters:
      destination - the directory the response file is written to
      fileMessageResponseTransformerClassName - the transformer's class name, or NO_TRANSFORMER, which a response returned from a transformer must carry
      fileName - the name of the response file
      text - the content
      bodyType - how the body is written, as bodyType describes
    • FileMessageResponse

      public FileMessageResponse(Destination destination, String fileName, String text)
      Creates a text response that uses no transformer, which is what a transformer usually returns.
      Parameters:
      destination - the directory the response file is written to
      fileName - the name of the response file
      text - the content
    • FileMessageResponse

      public FileMessageResponse(Destination destination, String fileMessageResponseTransformerClassName, String fileName, String text, MessageBodyType bodyType)
      Creates a response whose body is given inline, from the deprecated com.trafficparrot.sdk.jms types. It keeps extensions compiled against those types working; new code passes the com.trafficparrot.sdk.messaging types.
      Parameters:
      destination - the directory the response file is written to
      fileMessageResponseTransformerClassName - the transformer's class name, or NO_TRANSFORMER
      fileName - the name of the response file
      text - the content
      bodyType - how the body is written, as bodyType describes
    • FileMessageResponse

      public FileMessageResponse(Destination destination, String fileName, String text)
      Creates a text response that uses no transformer, from the deprecated com.trafficparrot.sdk.jms destination type. It keeps extensions compiled against that type working.
      Parameters:
      destination - the directory the response file is written to
      fileName - the name of the response file
      text - the content
  • Method Details

    • ofFile

      public static FileMessageResponse ofFile(Destination destination, String fileName, Path content)
      Creates a response whose content is the file content, for a transformer that answers with a file rather than text. However large the file, it is never read into memory: Traffic Parrot copies it into the response directory when it writes the reply, after the transformer returns, so it must still be there, unchanged, until then. Traffic Parrot does not modify, move or delete it. A relative path is resolved against Traffic Parrot's working directory, which start.sh sets to the installation directory; an absolute path does not depend on how Traffic Parrot was started. Handlebars renders the file name, not the file's content.

      Traffic Parrot reads whatever file is named here. So strip the directories from any value taken from the request, as getFileName() does in FileMessageResponseTransformer's example. Otherwise a request can name any file Traffic Parrot can read: a request whose body is ../trafficparrot.properties would be answered with that file.

      A request too large to read can be answered with its own file, FileMessage.contentFile(). The reply is then copied from a copy of it, deleted once the reply is written, since the request is removed before the reply is written. The copy is made in a directory of its own in java.io.tmpdir, which on Linux and macOS only Traffic Parrot's user can enter; java.io.tmpdir needs room for one such copy per request directory replayed.

      Parameters:
      destination - the directory the response file is written to
      fileName - the name of the response file
      content - the file the reply is copied from
      Returns:
      the response, which uses no transformer, as a transformer's must; its text, bodyFileName and bodyType are null
      Throws:
      IllegalArgumentException - if content is not a file Traffic Parrot can read, saying why
      UncheckedIOException - if the size of content cannot be read
    • withResolvedText

      public FileMessageResponse withResolvedText(String resolvedText)
      Returns a copy with resolvedText set (the content read from bodyFileName at load time). Every persisted field is preserved so a subsequent save re-writes bodyFileName with null text, keeping the sidecar reference intact.
      Parameters:
      resolvedText - the content of the bodyFileName file
      Returns:
      the copy
    • withResolvedBodyFile

      public FileMessageResponse withResolvedBodyFile(Path bodyFile, long bytes)
      Returns a copy whose body is the bodyFileName sidecar bodyFile, of bytes bytes, held by reference because it is too large to read into memory: createResponseMessage() copies it, and effectiveText() is null. Every persisted field is preserved, as by withResolvedText(String).
      Parameters:
      bodyFile - the file to copy the reply from
      bytes - the file's size in bytes
      Returns:
      the copy
    • resolvedBodyFile

      public Optional<Path> resolvedBodyFile()
      The file a reply is copied from: a body file too large to read into memory, or the file a transformer answered with; empty otherwise. Read it, do not modify it: it is the mapping's __files sidecar or the transformer's file.
      Returns:
      the body file, or empty when the body is held in memory
    • resolvedBodyFileBytes

      public Long resolvedBodyFileBytes()
      The size of resolvedBodyFile() in bytes, or null when the body is not held by reference.
      Returns:
      the size in bytes, or null
    • effectiveText

      public String effectiveText()
      The response body content to write: the file-resolved resolvedText when set, otherwise the inline text.
      Returns:
      the body, or null when there is none in memory: no body at all, or one held as resolvedBodyFile()
    • validate

      public void validate() throws IOException
      Checks the response is one Traffic Parrot can write: it does not set both text and bodyFileName, a body read from bodyFileName has a TEXT or BYTES body type or none, and a BYTES body is valid Base64.
      Throws:
      IOException - if it is not, with a message saying which rule it breaks
    • toString

      public String toString()
      A BYTES body is given by its length: it can be megabytes of base64, and every response sent is logged. A long inline text is given by its start and length.
      Overrides:
      toString in class Object
    • compareTo

      public int compareTo(FileMessageResponse other)
      Specified by:
      compareTo in interface Comparable<FileMessageResponse>
    • createResponseMessage

      public FileMessage createResponseMessage()
      A body file held by reference is copied as it is. A BYTES body is base64 (inline, or resolved from a __files sidecar), written as the exact bytes it encodes; any other body is written as its text.
      Returns:
      the file to write, named fileName and timestamped now
      Throws:
      IllegalArgumentException - if a BYTES body is not valid Base64
      UncheckedIOException - if the size of a body file held by reference cannot be read
    • hasTransformer

      public boolean hasTransformer()
      Whether the response names a transformer.
      Returns:
      true unless fileMessageResponseTransformerClassName is blank or NO_TRANSFORMER