Class IbmMqResponseTransformer

java.lang.Object
com.trafficparrot.sdk.ibmmq.IbmMqResponseTransformer
All Implemented Interfaces:
com.trafficparrot.sdk.internal.RenderableInUI

public abstract class IbmMqResponseTransformer extends Object implements com.trafficparrot.sdk.internal.RenderableInUI
The base class of a custom native IBM MQ response transformer, which decides the message Traffic Parrot sends in reply to a request message.

 public class ReplyWithOrderStatus extends IbmMqResponseTransformer {
     @Override
     protected IbmMqResponse doTransform(Destination requestDestination, MQMessage requestMessage, IbmMqResponse response, Parameters parameters) throws IOException {
         String requestBody = SdkIbmMqUtils.readMessageBodySkipHeaders(requestMessage);
         String status = requestBody.contains("<urgent>true</urgent>") ? "EXPEDITED" : "QUEUED";
         return new IbmMqResponse(response.destination, "<reply><status>" + status + "</status></reply>");
     }
 }
 
Put the jar in lib/external, add the class name to trafficparrot.ibmmq.responsetransformers and restart Traffic Parrot. A mapping uses the transformer by naming its class in a response's ibmMqResponseTransformerClassName; each response names one transformer at most.

It runs when a request message 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 sent. Because the returned text is rendered, text copied into it from the request message is evaluated as a template too; to send request content as it is, return a message the extension has built, with IbmMqResponse.withResponseMessage(com.trafficparrot.sdk.messaging.Destination, com.ibm.mq.MQMessage, long, java.util.Optional<com.trafficparrot.sdk.messaging.DestinationLookup>), which is sent without rendering.

Traffic Parrot creates a new instance for each response it transforms. 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 IbmMqResponseTransformer.
  • Constructor Details

    • IbmMqResponseTransformer

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

      public IbmMqResponseTransformer(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 IbmMqResponse transform(Destination requestDestination, com.ibm.mq.MQMessage requestMessage, IbmMqResponse response, com.github.tomakehurst.wiremock.extension.Parameters parameters)
      Called by Traffic Parrot. Runs doTransform. If that throws, or returns a response whose ibmMqResponseTransformerClassName is anything but IbmMqResponse.NO_TRANSFORMER, the error is logged and the reply is a text message whose body is the error message. It goes to the reply-to queue the request names, or to the mapping's response destination when the request names none.
      Parameters:
      requestDestination - the queue or topic the request message arrived on
      requestMessage - the request message
      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 IbmMqResponse transform(Destination requestDestination, com.ibm.mq.MQMessage requestMessage, IbmMqResponse response, com.github.tomakehurst.wiremock.extension.Parameters parameters)
      The entry point for plugins compiled against the com.trafficparrot.sdk.jms types.
      Parameters:
      requestDestination - the queue or topic the request message arrived on
      requestMessage - the request message
      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 IbmMqResponse doTransform(Destination requestDestination, com.ibm.mq.MQMessage requestMessage, IbmMqResponse 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 queue or topic the request message arrived on
      requestMessage - the request message. SdkIbmMqUtils reads its body and leaves its read position where it was
      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 send. Its ibmMqResponseTransformerClassName must be IbmMqResponse.NO_TRANSFORMER, as the two-argument IbmMqResponse constructor and IbmMqResponse.withResponseMessage(com.trafficparrot.sdk.messaging.Destination, com.ibm.mq.MQMessage, long, java.util.Optional<com.trafficparrot.sdk.messaging.DestinationLookup>) set it
      Throws:
      Exception - if the response cannot be transformed; the reply is then a text message that names this class and the exception's message, sent as transform(com.trafficparrot.sdk.messaging.Destination, com.ibm.mq.MQMessage, com.trafficparrot.sdk.ibmmq.IbmMqResponse, com.github.tomakehurst.wiremock.extension.Parameters) describes, and the exception is logged
    • doTransform

      @Deprecated protected IbmMqResponse doTransform(Destination requestDestination, com.ibm.mq.MQMessage requestMessage, IbmMqResponse 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 queue or topic the request message arrived on
      requestMessage - the request message
      response - the mapping's response, as written
      parameters - Traffic Parrot's runtime properties
      Returns:
      the response to render and send
      Throws:
      Exception - if the response cannot be transformed
    • doTransform

      @Deprecated protected IbmMqResponse doTransform(Destination requestDestination, com.ibm.mq.MQMessage requestMessage, IbmMqResponse response) throws Exception
      For backwards compatibility, allow old extensions to override only the method without Parameters
      Parameters:
      requestDestination - the queue or topic the request message arrived on
      requestMessage - the request message
      response - the mapping's response, as written
      Returns:
      the response to render and send
      Throws:
      Exception - if the response cannot be transformed
    • doTransform

      @Deprecated protected IbmMqResponse doTransform(Destination requestDestination, com.ibm.mq.MQMessage requestMessage, IbmMqResponse 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 queue or topic the request message arrived on
      requestMessage - the request message
      response - the mapping's response, as written
      Returns:
      the response to render and send
      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