Class FileMessageResponseTransformer
- All Implemented Interfaces:
com.trafficparrot.sdk.internal.RenderableInUI
public class AcknowledgeOrder extends FileMessageResponseTransformer {
@Override
protected FileMessageResponse doTransform(Destination requestDestination, FileMessage requestMessage, FileMessageResponse response, Parameters parameters) {
String status = requestMessage.text.contains("<urgent>true</urgent>") ? "EXPEDITED" : "QUEUED";
return new FileMessageResponse(response.destination, response.fileName, "<ack><status>" + status + "</status></ack>");
}
}
To answer with a file, which is copied rather than read into memory however large it is, return
FileMessageResponse.ofFile(Destination, String, java.nio.file.Path):
// getFileName() strips any directories the request's file name carries, so the request cannot leave statements/
Path statement = Paths.get("statements", Paths.get(requestMessage.fileName).getFileName() + ".pdf");
return FileMessageResponse.ofFile(response.destination, response.fileName, statement);
Strip the directories from any value taken from the request, as getFileName() does here, or a request can
name any file Traffic Parrot can read: a file name or body of ../trafficparrot.properties would be answered
with that file.
Put the jar in lib/external, add the class name to trafficparrot.file.responsetransformers and
restart Traffic Parrot. A mapping uses the transformer by naming its class in the response's
fileMessageResponseTransformerClassName; a mapping names one transformer at most.
It runs when a request file has matched the mapping, before Traffic Parrot renders the response: the response it receives is the mapping's as written, Handlebars expressions included, and the one it returns is then rendered and written. Because the returned file name and text are rendered, anything copied into them from the request file is evaluated as a template too, so decide from the request's content and write the reply's own text, as the example does, or leave request content to a Handlebars expression in the mapping.
Traffic Parrot creates a new instance for each request file. To use what Traffic Parrot offers extensions, such as
its database connections, give the class a public constructor that takes a
TrafficParrotServices.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final org.slf4j.LoggerThe logger given to the constructor; by default one named afterFileMessageResponseTransformer. -
Constructor Summary
ConstructorsConstructorDescriptionCreates the transformer with aloggernamed afterFileMessageResponseTransformer.FileMessageResponseTransformer(org.slf4j.Logger logger) Creates the transformer with a logger of the extension's choice. -
Method Summary
Modifier and TypeMethodDescriptionprotected FileMessageResponsedoTransform(Destination requestDestination, FileMessage requestMessage, FileMessageResponse response) Deprecated.protected FileMessageResponsedoTransform(Destination requestDestination, FileMessage requestMessage, FileMessageResponse response, com.github.tomakehurst.wiremock.extension.Parameters parameters) Deprecated.OverridedoTransform(Destination, FileMessage, FileMessageResponse, Parameters)instead.protected FileMessageResponsedoTransform(Destination requestDestination, FileMessage requestMessage, FileMessageResponse response) Deprecated.OverridedoTransform(Destination, FileMessage, FileMessageResponse, Parameters)instead.protected FileMessageResponsedoTransform(Destination requestDestination, FileMessage requestMessage, FileMessageResponse response, com.github.tomakehurst.wiremock.extension.Parameters parameters) The transformation: override this one, and no otherdoTransform.The name Traffic Parrot's web interface shows for this transformer in a mapping's list of response transformers.final FileMessageResponsetransform(Destination requestDestination, FileMessage requestMessage, FileMessageResponse response, com.github.tomakehurst.wiremock.extension.Parameters parameters) The entry point for plugins compiled against thecom.trafficparrot.sdk.jmstypes.final FileMessageResponsetransform(Destination requestDestination, FileMessage requestMessage, FileMessageResponse response, com.github.tomakehurst.wiremock.extension.Parameters parameters) Called by Traffic Parrot.
-
Field Details
-
logger
protected final org.slf4j.Logger loggerThe logger given to the constructor; by default one named afterFileMessageResponseTransformer.
-
-
Constructor Details
-
FileMessageResponseTransformer
public FileMessageResponseTransformer()Creates the transformer with aloggernamed afterFileMessageResponseTransformer. -
FileMessageResponseTransformer
public FileMessageResponseTransformer(org.slf4j.Logger logger) Creates the transformer with a logger of the extension's choice.- Parameters:
logger- theloggerto use, for example one named after the extension's own class
-
-
Method Details
-
transform
public final FileMessageResponse transform(Destination requestDestination, FileMessage requestMessage, FileMessageResponse response, com.github.tomakehurst.wiremock.extension.Parameters parameters) Called by Traffic Parrot. RunsdoTransform. If that throws, or returns a response whosefileMessageResponseTransformerClassNameis anything butFileMessageResponse.NO_TRANSFORMER, the error is logged and the reply is a text file with the mapping's file name and directory whose content is the error message.- Parameters:
requestDestination- the directory the request file was picked up fromrequestMessage- the request fileresponse- the mapping's response, as writtenparameters- Traffic Parrot's runtime properties, asdoTransformdescribes- Returns:
- what
doTransformreturned, or the error reply
-
transform
public final FileMessageResponse transform(Destination requestDestination, FileMessage requestMessage, FileMessageResponse response, com.github.tomakehurst.wiremock.extension.Parameters parameters) The entry point for plugins compiled against thecom.trafficparrot.sdk.jmstypes.- Parameters:
requestDestination- the directory the request file was picked up fromrequestMessage- the request fileresponse- the mapping's response, as writtenparameters- Traffic Parrot's runtime properties, asdoTransformdescribes- Returns:
- what
doTransformreturned, or the error reply
-
doTransform
protected FileMessageResponse doTransform(Destination requestDestination, FileMessage requestMessage, FileMessageResponse response, com.github.tomakehurst.wiremock.extension.Parameters parameters) throws Exception The transformation: override this one, and no otherdoTransform. The other three are the forms earlier extensions override; each is reached only when the ones before it are not overridden.- Parameters:
requestDestination- the directory the request file was picked up from: itsnameis the directory's pathrequestMessage- the request fileresponse- the mapping's response, as writtenparameters- Traffic Parrot's runtime properties:(Properties) parameters.get("properties")for the virtual service and"sharedProperties"for the installation root, each withdataDirectory,databaseConnectionsFileandscenarioDirectory- Returns:
- the response to render and write. Its
fileMessageResponseTransformerClassNamemust beFileMessageResponse.NO_TRANSFORMER, as the three-argumentFileMessageResponseconstructor sets it - Throws:
Exception- if the response cannot be transformed; the reply is then a text file with the mapping's file name and directory that names this class and the exception's message, and the exception is logged
-
doTransform
@Deprecated protected FileMessageResponse doTransform(Destination requestDestination, FileMessage requestMessage, FileMessageResponse response, com.github.tomakehurst.wiremock.extension.Parameters parameters) throws Exception Deprecated.OverridedoTransform(Destination, FileMessage, FileMessageResponse, Parameters)instead.The transformation as extensions compiled against thecom.trafficparrot.sdk.jmstypes declare it.- Parameters:
requestDestination- the directory the request file was picked up fromrequestMessage- the request fileresponse- the mapping's response, as writtenparameters- Traffic Parrot's runtime properties- Returns:
- the response to render and write
- Throws:
Exception- if the response cannot be transformed
-
doTransform
@Deprecated protected FileMessageResponse doTransform(Destination requestDestination, FileMessage requestMessage, FileMessageResponse response) throws Exception Deprecated.OverridedoTransform(Destination, FileMessage, FileMessageResponse, Parameters)instead.For backwards compatibility, allow old extensions to override only the method withoutParameters- Parameters:
requestDestination- the directory the request file was picked up fromrequestMessage- the request fileresponse- the mapping's response, as written- Returns:
- the response to render and write
- Throws:
Exception- if the response cannot be transformed
-
doTransform
@Deprecated protected FileMessageResponse doTransform(Destination requestDestination, FileMessage requestMessage, FileMessageResponse response) throws Exception Deprecated.OverridedoTransform(Destination, FileMessage, FileMessageResponse, Parameters)instead.The transformation as the earliest extensions declare it: withoutParameters, and with thecom.trafficparrot.sdk.jmsdestination type.- Parameters:
requestDestination- the directory the request file was picked up fromrequestMessage- the request fileresponse- the mapping's response, as written- Returns:
- the response to render and write
- Throws:
Exception- if the response cannot be transformedIllegalStateException- if the extension overrides nodoTransformmethod
-
getDisplayName
The name Traffic Parrot's web interface shows for this transformer in a mapping's list of response transformers.- Specified by:
getDisplayNamein interfacecom.trafficparrot.sdk.internal.RenderableInUI- Returns:
- the class's simple name, unless overridden
-
doTransform(Destination, FileMessage, FileMessageResponse, Parameters)instead.