Class HttpResponseDefinitionTransformer

java.lang.Object
com.trafficparrot.sdk.http.HttpResponseDefinitionTransformer
All Implemented Interfaces:
com.github.tomakehurst.wiremock.extension.Extension, com.github.tomakehurst.wiremock.extension.ResponseDefinitionTransformerV2

public abstract class HttpResponseDefinitionTransformer extends Object implements com.github.tomakehurst.wiremock.extension.ResponseDefinitionTransformerV2
The base class of a custom HTTP response definition transformer, which changes a mapping's response before Traffic Parrot renders it or, with HTTP response templating off, the request a proxied mapping sends to the backend.

 public class AddServedByHeader extends HttpResponseDefinitionTransformer {
     @Override
     public ResponseDefinition doTransform(ServeEvent serveEvent, Parameters parameters) {
         return ResponseDefinitionBuilder.like(serveEvent.getResponseDefinition())
                 .withHeader("X-Served-By", "Traffic Parrot")
                 .build();
     }

     @Override
     public String getName() {
         return "addServedByHeader";
     }
 }
 
Put the jar in lib/external, add the class name to trafficparrot.http.responsetransformers and restart Traffic Parrot. Every response a mapping gives then carries the header, a proxied mapping's included, beside any header of that name the mapping sends itself; a request that no mapping matches is not transformed. The transformer applies to every mapping unless applyGlobally() is overridden to return false; then only the mappings that name it (getName()) in their response's transformers use it.

It receives the mapping's response definition as written, Handlebars expressions included, and what it returns is what Traffic Parrot renders, so an expression it puts in the body is evaluated. To change the response after it is rendered, or the backend's response for a proxied mapping, extend HttpResponseTransformer instead. To change the request a proxied mapping sends to the backend, return a TransformedRequestDefinition; that works only while HTTP response templating is off (trafficparrot.http.handlebars.enabled=false), and Traffic Parrot ships with it on. To use what Traffic Parrot offers extensions, such as its database connections, give the class a public constructor that takes a TrafficParrotServices.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final org.slf4j.Logger
    A logger named after HttpResponseDefinitionTransformer, not after the extension's own class.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Creates the transformer with its logger.
  • Method Summary

    Modifier and Type
    Method
    Description
    com.github.tomakehurst.wiremock.http.ResponseDefinition
    doTransform(com.github.tomakehurst.wiremock.http.Request request, com.github.tomakehurst.wiremock.http.ResponseDefinition responseDefinition, com.github.tomakehurst.wiremock.common.FileSource fileSource, com.github.tomakehurst.wiremock.extension.Parameters parameters)
    Deprecated.
    com.github.tomakehurst.wiremock.http.ResponseDefinition
    doTransform(com.github.tomakehurst.wiremock.stubbing.ServeEvent serveEvent, com.github.tomakehurst.wiremock.extension.Parameters parameters)
    The transformation: override this one.
    com.github.tomakehurst.wiremock.http.ResponseDefinition
    transform(com.github.tomakehurst.wiremock.stubbing.ServeEvent serveEvent)
    Called by Traffic Parrot.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface com.github.tomakehurst.wiremock.extension.Extension

    getName, start, stop

    Methods inherited from interface com.github.tomakehurst.wiremock.extension.ResponseDefinitionTransformerV2

    applyGlobally
  • Field Details

    • logger

      protected final org.slf4j.Logger logger
      A logger named after HttpResponseDefinitionTransformer, not after the extension's own class.
  • Constructor Details

    • HttpResponseDefinitionTransformer

      protected HttpResponseDefinitionTransformer()
      Creates the transformer with its logger.
  • Method Details

    • transform

      public com.github.tomakehurst.wiremock.http.ResponseDefinition transform(com.github.tomakehurst.wiremock.stubbing.ServeEvent serveEvent)
      Called by Traffic Parrot. Runs doTransform(ServeEvent, Parameters) with the response definition's transformerParameters; if that throws, logs the exception and answers with status 500 and a body that names the transformer's class and the exception's message.
      Specified by:
      transform in interface com.github.tomakehurst.wiremock.extension.ResponseDefinitionTransformerV2
      Parameters:
      serveEvent - the request and the response definition so far
      Returns:
      what doTransform returned, or the 500 response definition
    • doTransform

      public com.github.tomakehurst.wiremock.http.ResponseDefinition doTransform(com.github.tomakehurst.wiremock.stubbing.ServeEvent serveEvent, com.github.tomakehurst.wiremock.extension.Parameters parameters) throws Exception
      The transformation: override this one. serveEvent.getRequest() is the request being answered and serveEvent.getResponseDefinition() the response definition so far: the mapping's, as changed by any transformer that ran before this one.

      Unless overridden it calls the deprecated doTransform(Request, ResponseDefinition, FileSource, Parameters) with a null file source, which keeps extensions written against it working.

      Parameters:
      serveEvent - the request and the response definition so far
      parameters - the response definition's transformerParameters; empty, never null, when it has none. For a mapping that names a transformer, Traffic Parrot adds its 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 definition for Traffic Parrot to render; return a TransformedRequestDefinition to change the request a proxied mapping sends to the backend, within the limits that class describes
      Throws:
      Exception - if the response definition cannot be transformed; the client then gets a 500 that names this class and the exception's message, and the exception is logged
    • doTransform

      @Deprecated public com.github.tomakehurst.wiremock.http.ResponseDefinition doTransform(com.github.tomakehurst.wiremock.http.Request request, com.github.tomakehurst.wiremock.http.ResponseDefinition responseDefinition, com.github.tomakehurst.wiremock.common.FileSource fileSource, com.github.tomakehurst.wiremock.extension.Parameters parameters) throws Exception
      Deprecated.
      The transformation as extensions written for earlier versions declare it. It is called only by doTransform(ServeEvent, Parameters), when that is not overridden.
      Parameters:
      request - the request being answered
      responseDefinition - the response definition so far
      fileSource - always null
      parameters - the response definition's transformerParameters; empty, never null, when it has none
      Returns:
      the response definition for Traffic Parrot to render
      Throws:
      Exception - if the response definition cannot be transformed
      UnsupportedOperationException - if the extension overrides neither doTransform method