Class HttpResponseTransformer

java.lang.Object
com.github.tomakehurst.wiremock.extension.AbstractTransformer<com.github.tomakehurst.wiremock.http.Response>
com.github.tomakehurst.wiremock.extension.ResponseTransformer
com.trafficparrot.sdk.http.HttpResponseTransformer
All Implemented Interfaces:
com.github.tomakehurst.wiremock.extension.Extension

public abstract class HttpResponseTransformer extends com.github.tomakehurst.wiremock.extension.ResponseTransformer
The base class of a custom HTTP response transformer, which changes the response a client is about to receive.

 public class ServiceUnavailable extends HttpResponseTransformer {
     @Override
     protected Response doTransform(Request request, Response response, FileSource fileSource, Parameters parameters) {
         return Response.Builder.like(response).but().status(503).body("Try again later").build();
     }
 }
 
Put the jar in lib/external, add the class name to trafficparrot.http.responsetransformers and restart Traffic Parrot. A mapping uses the transformer by naming it in its response's transformers, as "serviceUnavailable" here (see getName()); override applyGlobally() to apply it to every mapping.

It runs after Traffic Parrot has rendered the mapping's response, that is evaluated its Handlebars expressions, and after a proxied mapping has received the backend's response. To change the response before it is rendered, extend HttpResponseDefinitionTransformer instead. 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 HttpResponseTransformer, 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
    boolean
    Whether the transformer applies to every mapping, or only to those that name it in transformers.
    protected abstract com.github.tomakehurst.wiremock.http.Response
    doTransform(com.github.tomakehurst.wiremock.http.Request request, com.github.tomakehurst.wiremock.http.Response response, com.github.tomakehurst.wiremock.common.FileSource fileSource, com.github.tomakehurst.wiremock.extension.Parameters parameters)
    The transformation: returns the response the client gets.
    The name a mapping puts in its response's transformers to use this transformer.
    final com.github.tomakehurst.wiremock.http.Response
    transform(com.github.tomakehurst.wiremock.http.Request request, com.github.tomakehurst.wiremock.http.Response response, com.github.tomakehurst.wiremock.common.FileSource fileSource, com.github.tomakehurst.wiremock.extension.Parameters parameters)
    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

    start, stop
  • Field Details

    • logger

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

    • HttpResponseTransformer

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

    • transform

      public final com.github.tomakehurst.wiremock.http.Response transform(com.github.tomakehurst.wiremock.http.Request request, com.github.tomakehurst.wiremock.http.Response response, com.github.tomakehurst.wiremock.common.FileSource fileSource, com.github.tomakehurst.wiremock.extension.Parameters parameters)
      Called by Traffic Parrot. Runs doTransform(com.github.tomakehurst.wiremock.http.Request, com.github.tomakehurst.wiremock.http.Response, com.github.tomakehurst.wiremock.common.FileSource, com.github.tomakehurst.wiremock.extension.Parameters); 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 class com.github.tomakehurst.wiremock.extension.ResponseTransformer
      Parameters:
      request - the request being answered
      response - the response about to be sent
      fileSource - the __files directory of the mappings being served
      parameters - the mapping's transformerParameters
      Returns:
      what doTransform(com.github.tomakehurst.wiremock.http.Request, com.github.tomakehurst.wiremock.http.Response, com.github.tomakehurst.wiremock.common.FileSource, com.github.tomakehurst.wiremock.extension.Parameters) returned, or the 500 response
    • doTransform

      protected abstract com.github.tomakehurst.wiremock.http.Response doTransform(com.github.tomakehurst.wiremock.http.Request request, com.github.tomakehurst.wiremock.http.Response response, com.github.tomakehurst.wiremock.common.FileSource fileSource, com.github.tomakehurst.wiremock.extension.Parameters parameters) throws Exception
      The transformation: returns the response the client gets.
      Parameters:
      request - the request being answered
      response - the response about to be sent: the mapping's, rendered, or the backend's for a proxied mapping, as changed by any transformer that ran before this one
      fileSource - the __files directory of the mappings being served
      parameters - the mapping's transformerParameters. 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. May be null for a mapping that names none and has no parameters
      Returns:
      the response to send; response itself to leave it as it is
      Throws:
      Exception - if the response cannot be transformed; the client then gets a 500 that names this class and the exception's message, and the exception is logged
    • getName

      public String getName()
      The name a mapping puts in its response's transformers to use this transformer. By default the class's simple name with its first letter in lower case.
      Returns:
      the transformer's name
    • applyGlobally

      public boolean applyGlobally()
      Whether the transformer applies to every mapping, or only to those that name it in transformers.
      Overrides:
      applyGlobally in class com.github.tomakehurst.wiremock.extension.AbstractTransformer<com.github.tomakehurst.wiremock.http.Response>
      Returns:
      false, unless overridden: only the mappings that name it