Class JmsResponse

java.lang.Object
com.trafficparrot.sdk.messaging.JmsResponse
All Implemented Interfaces:
Comparable<JmsResponse>
Direct Known Subclasses:
JmsResponse

public class JmsResponse extends Object implements Comparable<JmsResponse>
The response of a JMS mapping: the message Traffic Parrot sends when a request message matches. A JmsResponseTransformer receives the mapping's response and returns the one to send:

 return new JmsResponse(response.destination, "{\"status\": \"received\"}");
 
or, to send a message it has built itself, withJakartaResponseMessage(com.trafficparrot.sdk.messaging.Destination, jakarta.jms.Message, long, java.util.Optional<com.trafficparrot.sdk.messaging.DestinationLookup>). 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.
  • Field Details

    • NO_TRANSFORMER

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

      public final Destination destination
      The queue or topic the response is sent to, unless destinationLookup sends it to the request's reply-to destination.
    • jmsResponseTransformerClassName

      public final String jmsResponseTransformerClassName
      The class name of the JmsResponseTransformer the mapping uses, or NO_TRANSFORMER.
    • jmsResponseTransformerParameters

      public final com.github.tomakehurst.wiremock.extension.Parameters jmsResponseTransformerParameters
      The mapping's own parameters for its transformer, which reads them from the response it is given; null when the mapping has none. They are not part of the parameters argument of doTransform.
    • text

      public final String text
      The message body, which may hold Handlebars expressions. How it is written depends on the bodyType: as text, as the bytes its Base64 or printable-characters text encodes, or as the Java object or protobuf message its JSON describes. Null when the body comes from bodyFileName or the response carries a message.
    • bodyFileName

      public final String bodyFileName
      A file under __files that holds the body, used in place of an inline text. Supported only with the TEXT body type.
    • bodyType

      public final MessageBodyType bodyType
      How text is turned into the message body; TEXT when none is given.
    • jmsMessageType

      public final String jmsMessageType
      The class name of a kind of JMS message, javax.jms.TextMessage or javax.jms.BytesMessage; when none is given, the one JmsMessageType.jmsMessageType(com.trafficparrot.sdk.messaging.MessageBodyType) gives for the bodyType. It is saved with the mapping. The kind of message Traffic Parrot sends is decided by the bodyType, not by this.
    • properties

      public final List<JmsProperty> properties
      The JMS properties to set on the response message; empty when there are none.
    • destinationLookup

      public final Optional<DestinationLookup> destinationLookup
      Where the response is sent. Empty, or the default constant, sends it to the reply-to destination the request names and to destination when it names none; USE_MAPPING_RESPONSE_DESTINATION sends it to destination.
    • fixedDelayMilliseconds

      public final long fixedDelayMilliseconds
      How long Traffic Parrot waits before it sends the response, in milliseconds; 0 for no delay.
    • 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.
    • responseMessage

      @Deprecated public final transient Optional<javax.jms.Message> responseMessage
      Deprecated.
      Use jakartaResponseMessage instead. This field is kept for backward compatibility with old javax.jms customer plugins.
    • jakartaResponseMessage

      public final transient Optional<jakarta.jms.Message> jakartaResponseMessage
      A JmsResponseTransformer can return a jakarta.jms.Message object instead of just transforming the mapping values. The framework reads this field first; if empty, falls back to responseMessage for old plugins.
  • Constructor Details

    • JmsResponse

      public JmsResponse(Destination destination, String jmsResponseTransformerClassName, com.github.tomakehurst.wiremock.extension.Parameters jmsResponseTransformerParameters, String text, String bodyFileName, MessageBodyType bodyType, String jmsMessageType, List<JmsProperty> properties, Long fixedDelayInMilliseconds, Optional<DestinationLookup> destinationLookup, Optional<javax.jms.Message> responseMessage)
      Creates a response with every field a mapping file can set. Traffic Parrot reads a mapping's response through this constructor.
      Parameters:
      destination - the queue or topic to send the response to
      jmsResponseTransformerClassName - the transformer's class name, or NO_TRANSFORMER
      jmsResponseTransformerParameters - the mapping's parameters for its transformer; may be null
      text - the body; null when bodyFileName is set
      bodyFileName - a file under __files to read the body from; null when text is set
      bodyType - how the text is turned into the message body; null for TEXT
      jmsMessageType - the class name kept as jmsMessageType; null for the one that suits the body type
      properties - the JMS properties to set on the message; null for none
      fixedDelayInMilliseconds - the delay before sending, in milliseconds; null for none
      destinationLookup - where the response is sent, as destinationLookup describes
      responseMessage - a javax.jms message to send as it is, in place of one built from the other fields; a mapping file cannot set it
    • JmsResponse

      protected JmsResponse(Destination destination, String jmsResponseTransformerClassName, com.github.tomakehurst.wiremock.extension.Parameters jmsResponseTransformerParameters, String text, String bodyFileName, MessageBodyType bodyType, String jmsMessageType, List<? extends JmsProperty> properties, Long fixedDelayInMilliseconds, Optional<DestinationLookup> destinationLookup, Optional<javax.jms.Message> responseMessage, Optional<jakarta.jms.Message> jakartaResponseMessage, String resolvedText)
      Creates a response with every field, for a subclass.
      Parameters:
      destination - the queue or topic to send the response to
      jmsResponseTransformerClassName - the transformer's class name, or NO_TRANSFORMER
      jmsResponseTransformerParameters - the mapping's parameters for its transformer; may be null
      text - the body; null when bodyFileName is set
      bodyFileName - a file under __files to read the body from; null when text is set
      bodyType - how the text is turned into the message body; null for TEXT
      jmsMessageType - the class name kept as jmsMessageType; null for the one that suits the body type
      properties - the JMS properties to set on the message; null for none. The list is kept as given, not copied
      fixedDelayInMilliseconds - the delay before sending, in milliseconds; null for none
      destinationLookup - where the response is sent, as destinationLookup describes
      responseMessage - a javax.jms message to send as it is; empty to build one
      jakartaResponseMessage - a jakarta.jms message to send as it is; empty to build one
      resolvedText - the content read from bodyFileName; null when there is none
    • JmsResponse

      public JmsResponse(Destination destination, String jmsResponseTransformerClassName, String text, MessageBodyType bodyType, String jmsMessageType, List<JmsProperty> properties, Long fixedDelayInMilliseconds)
      Creates a response with an inline body, JMS properties, a delay and a JMS message type. It goes to the reply-to destination the request names, or to destination when it names none.
      Parameters:
      destination - the queue or topic to send the response to
      jmsResponseTransformerClassName - the transformer's class name, or NO_TRANSFORMER, which a response returned from a transformer must carry
      text - the body
      bodyType - how the text is turned into the message body; null for TEXT
      jmsMessageType - the class name kept as jmsMessageType; null for the one that suits the body type
      properties - the JMS properties to set on the message; null for none
      fixedDelayInMilliseconds - the delay before sending, in milliseconds; null for none
    • JmsResponse

      public JmsResponse(Destination destination, String jmsResponseTransformerClassName, String text, MessageBodyType bodyType, List<JmsProperty> properties, long fixedDelayInMilliseconds)
      Creates a response with an inline body, JMS properties and a delay, sent as the kind of JMS message that suits the body type.
      Parameters:
      destination - the queue or topic to send the response to
      jmsResponseTransformerClassName - the transformer's class name, or NO_TRANSFORMER, which a response returned from a transformer must carry
      text - the body
      bodyType - how the text is turned into the message body; null for TEXT
      properties - the JMS properties to set on the message; null for none
      fixedDelayInMilliseconds - the delay before sending, in milliseconds; 0 for none
    • JmsResponse

      public JmsResponse(Destination destination, String jmsResponseTransformerClassName, String text, MessageBodyType bodyType)
      Creates a response with an inline body of a given type, no JMS properties and no delay.
      Parameters:
      destination - the queue or topic to send the response to
      jmsResponseTransformerClassName - the transformer's class name, or NO_TRANSFORMER, which a response returned from a transformer must carry
      text - the body
      bodyType - how the text is turned into the message body; null for TEXT
    • JmsResponse

      public JmsResponse(Destination destination, String jmsResponseTransformerClassName, String text)
      Creates a text response with no JMS properties and no delay.
      Parameters:
      destination - the queue or topic to send the response to
      jmsResponseTransformerClassName - the transformer's class name, or NO_TRANSFORMER, which a response returned from a transformer must carry
      text - the body
    • JmsResponse

      public JmsResponse(Destination destination, String text)
      Creates a text response that uses no transformer, which is what a transformer usually returns. It has no JMS properties and no delay.
      Parameters:
      destination - the queue or topic to send the response to when the request names no reply-to destination
      text - the body
    • JmsResponse

      public JmsResponse(Destination destination, String jmsResponseTransformerClassName, Object object) throws com.fasterxml.jackson.core.JsonProcessingException
      Creates a response whose body is a Java object, held as its JSON with the object's class as the body type.
      Parameters:
      destination - the queue or topic to send the response to
      jmsResponseTransformerClassName - the transformer's class name, or NO_TRANSFORMER, which a response returned from a transformer must carry
      object - the object to send
      Throws:
      com.fasterxml.jackson.core.JsonProcessingException - if the object cannot be written as JSON
    • JmsResponse

      public JmsResponse(Destination destination, Object object) throws com.fasterxml.jackson.core.JsonProcessingException
      Creates a response that uses no transformer and whose body is a Java object, held as its JSON with the object's class as the body type.
      Parameters:
      destination - the queue or topic to send the response to
      object - the object to send
      Throws:
      com.fasterxml.jackson.core.JsonProcessingException - if the object cannot be written as JSON
  • Method Details

    • base64BytesResponse

      public static JmsResponse base64BytesResponse(Destination destination, byte[] bytes)
      A response that uses no transformer and whose body is these bytes, held as Base64 with the BYTES body type.
      Parameters:
      destination - the queue or topic to send the response to
      bytes - the body
      Returns:
      the response
    • base64BytesResponse

      public static JmsResponse base64BytesResponse(Destination destination, String jmsResponseTransformerClassName, byte[] bytes)
      A response whose body is these bytes, held as Base64 with the BYTES body type.
      Parameters:
      destination - the queue or topic to send the response to
      jmsResponseTransformerClassName - the transformer's class name, or NO_TRANSFORMER
      bytes - the body
      Returns:
      the response
    • printableCharactersBytesResponse

      public static JmsResponse printableCharactersBytesResponse(Destination destination, byte[] bytes)
      A response that uses no transformer and whose body is these bytes, held in the printable-characters encoding with the PRINTABLE_CHARACTERS body type, which keeps readable text readable in the mapping file.
      Parameters:
      destination - the queue or topic to send the response to
      bytes - the body
      Returns:
      the response
    • printableCharactersBytesResponse

      public static JmsResponse printableCharactersBytesResponse(Destination destination, String jmsResponseTransformerClassName, byte[] bytes)
      A response whose body is these bytes, held in the printable-characters encoding with the PRINTABLE_CHARACTERS body type.
      Parameters:
      destination - the queue or topic to send the response to
      jmsResponseTransformerClassName - the transformer's class name, or NO_TRANSFORMER
      bytes - the body
      Returns:
      the response
    • withJakartaResponseMessage

      public static JmsResponse withJakartaResponseMessage(Destination destination, jakarta.jms.Message jakartaMessage, long fixedDelayMilliseconds, Optional<DestinationLookup> destinationLookup)
      A response that sends a jakarta.jms message the transformer has built itself, in place of one built from a body and a body type.
      Parameters:
      destination - the queue or topic to send the response to
      jakartaMessage - the message to send
      fixedDelayMilliseconds - the delay before sending, in milliseconds; 0 for none
      destinationLookup - where the response is sent, as destinationLookup describes
      Returns:
      the response, which uses no transformer
    • withResolvedText

      public JmsResponse 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
    • effectiveText

      public String effectiveText()
      The response body content to send: the file-resolved resolvedText when set, otherwise the inline text.
      Returns:
      the body, or null when the response has none
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • compareTo

      public int compareTo(JmsResponse other)
      Specified by:
      compareTo in interface Comparable<JmsResponse>
    • hasTransformer

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

      public void validate() throws IOException
      Checks the response is one Traffic Parrot can send: it does not set both text and bodyFileName, a body read from bodyFileName has the TEXT body type, and the text of a Java object body is JSON that reads as an object of its class (Handlebars expressions are read as null).
      Throws:
      IOException - if it is not, with a message saying which rule it breaks