Class JmsResponseTransformer
- All Implemented Interfaces:
com.trafficparrot.sdk.internal.RenderableInUI
- Direct Known Subclasses:
JmsResponseTransformer,TextJmsResponseTransformer
TextJmsResponseTransformer.
public class ReplyWithPriority extends JmsResponseTransformer {
@Override
protected JmsResponse doTransformJakarta(Destination requestDestination, jakarta.jms.Message requestMessage, JmsResponse response, Parameters parameters) throws jakarta.jms.JMSException {
String priority = requestMessage.getJMSPriority() > 4 ? "EXPEDITED" : "NORMAL";
return new JmsResponse(response.destination, "{\"priority\": \"" + priority + "\"}");
}
}
Override doTransformJakarta(com.trafficparrot.sdk.messaging.Destination, jakarta.jms.Message, com.trafficparrot.sdk.messaging.JmsResponse, com.github.tomakehurst.wiremock.extension.Parameters) to work with jakarta.jms messages, or doTransform(com.trafficparrot.sdk.messaging.Destination, javax.jms.Message, com.trafficparrot.sdk.messaging.JmsResponse, com.github.tomakehurst.wiremock.extension.Parameters) to work
with javax.jms ones, as extensions written for the javax.jms API do; Traffic Parrot calls
whichever the class declares, and doTransformJakarta if it declares both.
Put the jar in lib/external, add the class name to trafficparrot.jms.responsetransformers and
restart Traffic Parrot. A mapping uses the transformer by naming its class in the response's
jmsResponseTransformerClassName; a mapping 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
JmsResponse.withJakartaResponseMessage(com.trafficparrot.sdk.messaging.Destination, jakarta.jms.Message, long, java.util.Optional<com.trafficparrot.sdk.messaging.DestinationLookup>), which is sent without rendering.
Traffic Parrot creates a new instance for each request message. 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 afterJmsResponseTransformer. -
Constructor Summary
ConstructorsConstructorDescriptionCreates the transformer with aloggernamed afterJmsResponseTransformer.JmsResponseTransformer(org.slf4j.Logger logger) Creates the transformer with a logger of the extension's choice. -
Method Summary
Modifier and TypeMethodDescriptionprotected JmsResponsedoTransform(Destination requestDestination, javax.jms.Message requestMessage, JmsResponse response, com.github.tomakehurst.wiremock.extension.Parameters parameters) javax.jms extensions override this method.protected JmsResponsedoTransformJakarta(Destination requestDestination, jakarta.jms.Message requestMessage, JmsResponse response, com.github.tomakehurst.wiremock.extension.Parameters parameters) jakarta.jms extensions override this method.The name Traffic Parrot's web interface shows for this transformer in a mapping's list of response transformers.booleanWhether Traffic Parrot callsdoTransformJakarta(com.trafficparrot.sdk.messaging.Destination, jakarta.jms.Message, com.trafficparrot.sdk.messaging.JmsResponse, com.github.tomakehurst.wiremock.extension.Parameters)rather thandoTransform(com.trafficparrot.sdk.messaging.Destination, javax.jms.Message, com.trafficparrot.sdk.messaging.JmsResponse, com.github.tomakehurst.wiremock.extension.Parameters).final JmsResponsetransform(Destination requestDestination, javax.jms.Message requestMessage, JmsResponse response, com.github.tomakehurst.wiremock.extension.Parameters parameters) Entry point for javax plugins.final JmsResponsetransformJakarta(Destination requestDestination, jakarta.jms.Message requestMessage, JmsResponse response, com.github.tomakehurst.wiremock.extension.Parameters parameters) Entry point called by the framework for jakarta plugins.
-
Field Details
-
logger
protected final org.slf4j.Logger loggerThe logger given to the constructor; by default one named afterJmsResponseTransformer.
-
-
Constructor Details
-
JmsResponseTransformer
public JmsResponseTransformer()Creates the transformer with aloggernamed afterJmsResponseTransformer. -
JmsResponseTransformer
public JmsResponseTransformer(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
-
transformJakarta
public final JmsResponse transformJakarta(Destination requestDestination, jakarta.jms.Message requestMessage, JmsResponse response, com.github.tomakehurst.wiremock.extension.Parameters parameters) Entry point called by the framework for jakarta plugins. Receives the native jakarta message directly -- no bridging needed. Only usetransform(com.trafficparrot.sdk.messaging.Destination, javax.jms.Message, com.trafficparrot.sdk.messaging.JmsResponse, com.github.tomakehurst.wiremock.extension.Parameters)for old javax plugins.Runs
doTransformJakarta(com.trafficparrot.sdk.messaging.Destination, jakarta.jms.Message, com.trafficparrot.sdk.messaging.JmsResponse, com.github.tomakehurst.wiremock.extension.Parameters). If that throws, or returns a response whosejmsResponseTransformerClassNameis anything butJmsResponse.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 destination 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 onrequestMessage- the request messageresponse- the mapping's response, as writtenparameters- Traffic Parrot's runtime properties, asdoTransformJakarta(com.trafficparrot.sdk.messaging.Destination, jakarta.jms.Message, com.trafficparrot.sdk.messaging.JmsResponse, com.github.tomakehurst.wiremock.extension.Parameters)describes- Returns:
- what
doTransformJakartareturned, or the error reply
-
transform
public final JmsResponse transform(Destination requestDestination, javax.jms.Message requestMessage, JmsResponse response, com.github.tomakehurst.wiremock.extension.Parameters parameters) Entry point for javax plugins. For jakarta plugins, the framework callstransformJakarta(com.trafficparrot.sdk.messaging.Destination, jakarta.jms.Message, com.trafficparrot.sdk.messaging.JmsResponse, com.github.tomakehurst.wiremock.extension.Parameters)instead. This method still handles jakarta plugins (via bridging) for any external callers.An error is handled as
transformJakarta(com.trafficparrot.sdk.messaging.Destination, jakarta.jms.Message, com.trafficparrot.sdk.messaging.JmsResponse, com.github.tomakehurst.wiremock.extension.Parameters)handles it.- Parameters:
requestDestination- the queue or topic the request message arrived onrequestMessage- the request messageresponse- the mapping's response, as writtenparameters- Traffic Parrot's runtime properties, asdoTransform(com.trafficparrot.sdk.messaging.Destination, javax.jms.Message, com.trafficparrot.sdk.messaging.JmsResponse, com.github.tomakehurst.wiremock.extension.Parameters)describes- Returns:
- what the transformation returned, or the error reply
-
doTransform
protected JmsResponse doTransform(Destination requestDestination, javax.jms.Message requestMessage, JmsResponse response, com.github.tomakehurst.wiremock.extension.Parameters parameters) throws Exception javax.jms extensions override this method.- Parameters:
requestDestination- the queue or topic the request message arrived onrequestMessage- the request message, as ajavax.jmsview of the message Traffic Parrot receivedresponse- the mapping's response, as written. ItsJmsResponse.jmsResponseTransformerParametersare the mapping's own parameters for this transformerparameters- 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 send. Its
jmsResponseTransformerClassNamemust beJmsResponse.NO_TRANSFORMER, as the two-argumentJmsResponseconstructors andJmsResponseBuilderset 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 astransformJakarta(com.trafficparrot.sdk.messaging.Destination, jakarta.jms.Message, com.trafficparrot.sdk.messaging.JmsResponse, com.github.tomakehurst.wiremock.extension.Parameters)describes, and the exception is loggedIllegalStateException- if the extension overrides neither this method nordoTransformJakarta(com.trafficparrot.sdk.messaging.Destination, jakarta.jms.Message, com.trafficparrot.sdk.messaging.JmsResponse, com.github.tomakehurst.wiremock.extension.Parameters)
-
doTransformJakarta
protected JmsResponse doTransformJakarta(Destination requestDestination, jakarta.jms.Message requestMessage, JmsResponse response, com.github.tomakehurst.wiremock.extension.Parameters parameters) throws Exception jakarta.jms extensions override this method.- Parameters:
requestDestination- the queue or topic the request message arrived onrequestMessage- the request message, as Traffic Parrot received itresponse- the mapping's response, as written. ItsJmsResponse.jmsResponseTransformerParametersare the mapping's own parameters for this transformerparameters- 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 send. Its
jmsResponseTransformerClassNamemust beJmsResponse.NO_TRANSFORMER, as the two-argumentJmsResponseconstructors,JmsResponse.withJakartaResponseMessage(com.trafficparrot.sdk.messaging.Destination, jakarta.jms.Message, long, java.util.Optional<com.trafficparrot.sdk.messaging.DestinationLookup>)andJmsResponseBuilderset 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 astransformJakarta(com.trafficparrot.sdk.messaging.Destination, jakarta.jms.Message, com.trafficparrot.sdk.messaging.JmsResponse, com.github.tomakehurst.wiremock.extension.Parameters)describes, and the exception is loggedIllegalStateException- if the extension does not override this method
-
isJakartaPlugin
public boolean isJakartaPlugin()Whether Traffic Parrot callsdoTransformJakarta(com.trafficparrot.sdk.messaging.Destination, jakarta.jms.Message, com.trafficparrot.sdk.messaging.JmsResponse, com.github.tomakehurst.wiremock.extension.Parameters)rather thandoTransform(com.trafficparrot.sdk.messaging.Destination, javax.jms.Message, com.trafficparrot.sdk.messaging.JmsResponse, com.github.tomakehurst.wiremock.extension.Parameters).- Returns:
- true if the extension's own class declares
doTransformJakarta; one it inherits does not count
-
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
-