Package com.trafficparrot.sdk.http
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
FieldsModifier and TypeFieldDescriptionprotected final org.slf4j.LoggerA logger named afterHttpResponseTransformer, not after the extension's own class. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanWhether the transformer applies to every mapping, or only to those that name it intransformers.protected abstract com.github.tomakehurst.wiremock.http.ResponsedoTransform(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.getName()The name a mapping puts in its response'stransformersto use this transformer.final com.github.tomakehurst.wiremock.http.Responsetransform(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, waitMethods inherited from interface com.github.tomakehurst.wiremock.extension.Extension
start, stop
-
Field Details
-
logger
protected final org.slf4j.Logger loggerA logger named afterHttpResponseTransformer, not after the extension's own class.
-
-
Constructor Details
-
HttpResponseTransformer
protected HttpResponseTransformer()Creates the transformer with itslogger.
-
-
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. RunsdoTransform(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:
transformin classcom.github.tomakehurst.wiremock.extension.ResponseTransformer- Parameters:
request- the request being answeredresponse- the response about to be sentfileSource- the__filesdirectory of the mappings being servedparameters- the mapping'stransformerParameters- 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 answeredresponse- 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 onefileSource- the__filesdirectory of the mappings being servedparameters- the mapping'stransformerParameters. 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 withdataDirectory,databaseConnectionsFileandscenarioDirectory. May be null for a mapping that names none and has no parameters- Returns:
- the response to send;
responseitself 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
The name a mapping puts in its response'stransformersto 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 intransformers.- Overrides:
applyGloballyin classcom.github.tomakehurst.wiremock.extension.AbstractTransformer<com.github.tomakehurst.wiremock.http.Response>- Returns:
- false, unless overridden: only the mappings that name it
-