Class FileMessageResponseTransformer

java.lang.Object
com.trafficparrot.sdk.file.FileMessageResponseTransformer
All Implemented Interfaces:
com.trafficparrot.sdk.internal.RenderableInUI

public abstract class FileMessageResponseTransformer extends Object implements com.trafficparrot.sdk.internal.RenderableInUI
The base class of a custom file message response transformer, which decides the file Traffic Parrot writes in reply to a request file.

 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 Details

    • logger

      protected final org.slf4j.Logger logger
      The logger given to the constructor; by default one named after FileMessageResponseTransformer.
  • Constructor Details

    • FileMessageResponseTransformer

      public FileMessageResponseTransformer()
      Creates the transformer with a logger named after FileMessageResponseTransformer.
    • FileMessageResponseTransformer

      public FileMessageResponseTransformer(org.slf4j.Logger logger)
      Creates the transformer with a logger of the extension's choice.
      Parameters:
      logger - the logger to 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. Runs doTransform. If that throws, or returns a response whose fileMessageResponseTransformerClassName is anything but FileMessageResponse.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 from
      requestMessage - the request file
      response - the mapping's response, as written
      parameters - Traffic Parrot's runtime properties, as doTransform describes
      Returns:
      what doTransform returned, 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 the com.trafficparrot.sdk.jms types.
      Parameters:
      requestDestination - the directory the request file was picked up from
      requestMessage - the request file
      response - the mapping's response, as written
      parameters - Traffic Parrot's runtime properties, as doTransform describes
      Returns:
      what doTransform returned, 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 other doTransform. 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: its name is the directory's path
      requestMessage - the request file
      response - the mapping's response, as written
      parameters - Traffic Parrot's runtime properties: (Properties) parameters.get("properties") for the virtual service and "sharedProperties" for the installation root, each with dataDirectory, databaseConnectionsFile and scenarioDirectory
      Returns:
      the response to render and write. Its fileMessageResponseTransformerClassName must be FileMessageResponse.NO_TRANSFORMER, as the three-argument FileMessageResponse constructor 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
      The transformation as extensions compiled against the com.trafficparrot.sdk.jms types declare it.
      Parameters:
      requestDestination - the directory the request file was picked up from
      requestMessage - the request file
      response - the mapping's response, as written
      parameters - 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
      For backwards compatibility, allow old extensions to override only the method without Parameters
      Parameters:
      requestDestination - the directory the request file was picked up from
      requestMessage - the request file
      response - 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
      The transformation as the earliest extensions declare it: without Parameters, and with the com.trafficparrot.sdk.jms destination type.
      Parameters:
      requestDestination - the directory the request file was picked up from
      requestMessage - the request file
      response - the mapping's response, as written
      Returns:
      the response to render and write
      Throws:
      Exception - if the response cannot be transformed
      IllegalStateException - if the extension overrides no doTransform method
    • getDisplayName

      public String getDisplayName()
      The name Traffic Parrot's web interface shows for this transformer in a mapping's list of response transformers.
      Specified by:
      getDisplayName in interface com.trafficparrot.sdk.internal.RenderableInUI
      Returns:
      the class's simple name, unless overridden