Class HttpResponseDefinitionTransformer
- All Implemented Interfaces:
com.github.tomakehurst.wiremock.extension.Extension,com.github.tomakehurst.wiremock.extension.ResponseDefinitionTransformerV2
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
FieldsModifier and TypeFieldDescriptionprotected final org.slf4j.LoggerA logger named afterHttpResponseDefinitionTransformer, not after the extension's own class. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncom.github.tomakehurst.wiremock.http.ResponseDefinitiondoTransform(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.ResponseDefinitiondoTransform(com.github.tomakehurst.wiremock.stubbing.ServeEvent serveEvent, com.github.tomakehurst.wiremock.extension.Parameters parameters) The transformation: override this one.com.github.tomakehurst.wiremock.http.ResponseDefinitiontransform(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, waitMethods inherited from interface com.github.tomakehurst.wiremock.extension.Extension
getName, start, stopMethods inherited from interface com.github.tomakehurst.wiremock.extension.ResponseDefinitionTransformerV2
applyGlobally
-
Field Details
-
logger
protected final org.slf4j.Logger loggerA logger named afterHttpResponseDefinitionTransformer, not after the extension's own class.
-
-
Constructor Details
-
HttpResponseDefinitionTransformer
protected HttpResponseDefinitionTransformer()Creates the transformer with itslogger.
-
-
Method Details
-
transform
public com.github.tomakehurst.wiremock.http.ResponseDefinition transform(com.github.tomakehurst.wiremock.stubbing.ServeEvent serveEvent) Called by Traffic Parrot. RunsdoTransform(ServeEvent, Parameters)with the response definition'stransformerParameters; 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 interfacecom.github.tomakehurst.wiremock.extension.ResponseDefinitionTransformerV2- Parameters:
serveEvent- the request and the response definition so far- Returns:
- what
doTransformreturned, 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 andserveEvent.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 farparameters- the response definition'stransformerParameters; 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 withdataDirectory,databaseConnectionsFileandscenarioDirectory- Returns:
- the response definition for Traffic Parrot to render; return a
TransformedRequestDefinitionto 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.OverridedoTransform(ServeEvent, Parameters)instead.The transformation as extensions written for earlier versions declare it. It is called only bydoTransform(ServeEvent, Parameters), when that is not overridden.- Parameters:
request- the request being answeredresponseDefinition- the response definition so farfileSource- always nullparameters- the response definition'stransformerParameters; empty, never null, when it has none- Returns:
- the response definition for Traffic Parrot to render
- Throws:
Exception- if the response definition cannot be transformedUnsupportedOperationException- if the extension overrides neitherdoTransformmethod
-
doTransform(ServeEvent, Parameters)instead.