Class FileMessageResponse
- All Implemented Interfaces:
Comparable<FileMessageResponse>
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 Summary
FieldsModifier and TypeFieldDescriptionfinal StringA__files/<name>sidecar holding the response body, used instead of an inlinetext.final MessageBodyTypeHow the body is written:BYTESas the bytes its Base64 text encodes, any other type as text.final DestinationThe directory the response file is written to: itsnameis the directory's path.final StringThe class name of theFileMessageResponseTransformerthe mapping uses, orNO_TRANSFORMER.final StringThe name of the response file WRITTEN to the response directory - a transport output name, Handlebars-templated liketext.static final StringThefileMessageResponseTransformerClassNameof a response that uses no transformer.final StringContent resolved frombodyFileNameat load time.final StringThe response file's content, which may hold Handlebars expressions. -
Constructor Summary
ConstructorsConstructorDescriptionFileMessageResponse(Destination destination, String fileName, String text) Creates a text response that uses no transformer, from the deprecatedcom.trafficparrot.sdk.jmsdestination type.FileMessageResponse(Destination destination, String fileMessageResponseTransformerClassName, String fileName, String text, MessageBodyType bodyType) Creates a response whose body is given inline, from the deprecatedcom.trafficparrot.sdk.jmstypes.FileMessageResponse(Destination destination, String fileName, String text) Creates a text response that uses no transformer, which is what a transformer usually returns.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, passNO_TRANSFORMER, the content as Base64 andMessageBodyType.BYTES.FileMessageResponse(Destination destination, String fileMessageResponseTransformerClassName, String fileName, String text, String bodyFileName, MessageBodyType bodyType) Creates a response with every field a mapping file can set. -
Method Summary
Modifier and TypeMethodDescriptionintcompareTo(FileMessageResponse other) A body file held by reference is copied as it is.The response body content to write: the file-resolvedresolvedTextwhen set, otherwise the inlinetext.booleanWhether the response names a transformer.static FileMessageResponseofFile(Destination destination, String fileName, Path content) Creates a response whose content is the filecontent, for a transformer that answers with a file rather than text.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.The size ofresolvedBodyFile()in bytes, or null when the body is not held by reference.toString()A BYTES body is given by its length: it can be megabytes of base64, and every response sent is logged.voidvalidate()Checks the response is one Traffic Parrot can write: it does not set bothtextandbodyFileName, a body read frombodyFileNamehas aTEXTorBYTESbody type or none, and aBYTESbody is valid Base64.withResolvedBodyFile(Path bodyFile, long bytes) Returns a copy whose body is thebodyFileNamesidecarbodyFile, ofbytesbytes, held by reference because it is too large to read into memory:createResponseMessage()copies it, andeffectiveText()is null.withResolvedText(String resolvedText) Returns a copy withresolvedTextset (the content read frombodyFileNameat load time).
-
Field Details
-
NO_TRANSFORMER
ThefileMessageResponseTransformerClassNameof a response that uses no transformer. A response returned from a transformer must carry it.- See Also:
-
destination
The directory the response file is written to: itsnameis the directory's path. -
fileMessageResponseTransformerClassName
The class name of theFileMessageResponseTransformerthe mapping uses, orNO_TRANSFORMER. -
fileName
The name of the response file WRITTEN to the response directory - a transport output name, Handlebars-templated liketext. Not to be confused withbodyFileName, which names a__filessidecar the body is READ from. -
text
The response file's content, which may hold Handlebars expressions. With aBYTESbodyTypeit is the content as Base64, written as the bytes it encodes. Null when the body comes frombodyFileName. -
bodyFileName
A__files/<name>sidecar holding the response body, used instead of an inlinetext. Mutually exclusive with it - seevalidate(). -
bodyType
How the body is written:BYTESas the bytes its Base64 text encodes, any other type as text. A body read frombodyFileNamesupports onlyTEXTandBYTES. -
resolvedText
Content resolved frombodyFileNameat load time. Held here rather than intextso the persisted model keepsbodyFileNameset andtextnull (non-destructive).transientso the filesystem mapper never writes it to disk; set only viawithResolvedText(String)and read viaeffectiveText(). 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 tofileMessageResponseTransformerClassName- the transformer's class name, orNO_TRANSFORMERfileName- the name of the response filetext- the content; null whenbodyFileNameis setbodyFileName- a file under__filesto read the content from; null whentextis setbodyType- how the body is written, asbodyTypedescribes
-
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, passNO_TRANSFORMER, the content as Base64 andMessageBodyType.BYTES.- Parameters:
destination- the directory the response file is written tofileMessageResponseTransformerClassName- the transformer's class name, orNO_TRANSFORMER, which a response returned from a transformer must carryfileName- the name of the response filetext- the contentbodyType- how the body is written, asbodyTypedescribes
-
FileMessageResponse
Creates a text response that uses no transformer, which is what a transformer usually returns.- Parameters:
destination- the directory the response file is written tofileName- the name of the response filetext- 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 deprecatedcom.trafficparrot.sdk.jmstypes. It keeps extensions compiled against those types working; new code passes thecom.trafficparrot.sdk.messagingtypes.- Parameters:
destination- the directory the response file is written tofileMessageResponseTransformerClassName- the transformer's class name, orNO_TRANSFORMERfileName- the name of the response filetext- the contentbodyType- how the body is written, asbodyTypedescribes
-
FileMessageResponse
Creates a text response that uses no transformer, from the deprecatedcom.trafficparrot.sdk.jmsdestination type. It keeps extensions compiled against that type working.- Parameters:
destination- the directory the response file is written tofileName- the name of the response filetext- the content
-
-
Method Details
-
ofFile
Creates a response whose content is the filecontent, 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, whichstart.shsets 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 inFileMessageResponseTransformer's example. Otherwise a request can name any file Traffic Parrot can read: a request whose body is../trafficparrot.propertieswould 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 injava.io.tmpdir, which on Linux and macOS only Traffic Parrot's user can enter;java.io.tmpdirneeds room for one such copy per request directory replayed.- Parameters:
destination- the directory the response file is written tofileName- the name of the response filecontent- the file the reply is copied from- Returns:
- the response, which uses no transformer, as a transformer's must; its
text,bodyFileNameandbodyTypeare null - Throws:
IllegalArgumentException- ifcontentis not a file Traffic Parrot can read, saying whyUncheckedIOException- if the size ofcontentcannot be read
-
withResolvedText
Returns a copy withresolvedTextset (the content read frombodyFileNameat load time). Every persisted field is preserved so a subsequent save re-writesbodyFileNamewith nulltext, keeping the sidecar reference intact.- Parameters:
resolvedText- the content of thebodyFileNamefile- Returns:
- the copy
-
withResolvedBodyFile
Returns a copy whose body is thebodyFileNamesidecarbodyFile, ofbytesbytes, held by reference because it is too large to read into memory:createResponseMessage()copies it, andeffectiveText()is null. Every persisted field is preserved, as bywithResolvedText(String).- Parameters:
bodyFile- the file to copy the reply frombytes- the file's size in bytes- Returns:
- the copy
-
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__filessidecar or the transformer's file.- Returns:
- the body file, or empty when the body is held in memory
-
resolvedBodyFileBytes
The size ofresolvedBodyFile()in bytes, or null when the body is not held by reference.- Returns:
- the size in bytes, or null
-
effectiveText
The response body content to write: the file-resolvedresolvedTextwhen set, otherwise the inlinetext.- Returns:
- the body, or null when there is none in memory: no body at all, or one held as
resolvedBodyFile()
-
validate
Checks the response is one Traffic Parrot can write: it does not set bothtextandbodyFileName, a body read frombodyFileNamehas aTEXTorBYTESbody type or none, and aBYTESbody is valid Base64.- Throws:
IOException- if it is not, with a message saying which rule it breaks
-
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. -
compareTo
- Specified by:
compareToin interfaceComparable<FileMessageResponse>
-
createResponseMessage
A body file held by reference is copied as it is. A BYTES body is base64 (inline, or resolved from a__filessidecar), written as the exact bytes it encodes; any other body is written as its text.- Returns:
- the file to write, named
fileNameand timestamped now - Throws:
IllegalArgumentException- if aBYTESbody is not valid Base64UncheckedIOException- 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
fileMessageResponseTransformerClassNameis blank orNO_TRANSFORMER
-