You can use the groovy policy to execute Groovy scripts at any stage of request processing within the Gateway.
This policy is applicable to the following API types:
- v2 APIs
- v4 HTTP proxy APIs
- v4 message APIs
- v4 LLM proxy APIs
- v4 MCP proxy APIs
- v4 A2A proxy APIs
- v4 Native Kafka APIs
Note: The Groovy policy is not supported by v4 TCP APIs.
Several variables are automatically bound to the Groovy script. These let you read, and potentially modify, their values to define the behavior of the policy.
| Variable | Description |
|---|---|
request |
Inbound HTTP request |
response |
Outbound HTTP response |
message |
Message transiting the Gateway |
context |
Context usable to access external components such as services and resources |
result |
Object to return to alter the outcome of the request/response |
See the Usage section for object attributes and methods.
| Variable | Description |
|---|---|
request.content |
When "Read content" is enabled |
request.contentAsByteArray |
When "Read content" is enabled |
request.contentAsBase64 |
When "Read content" is enabled |
response.content |
When "Read content" is enabled |
response.contentAsByteArray |
When "Read content" is enabled |
response.contentAsBase64 |
When "Read content" is enabled |
message.content |
Always available |
On v4 Native Kafka APIs the policy runs Groovy scripts across the Kafka flow phases:
- INTERACT — runs on each Kafka request/response at the connection level (e.g. Produce, Fetch).
- PUBLISH — runs on each individual Kafka message being produced.
- SUBSCRIBE — runs on each individual Kafka message being consumed.
The context, result, and (for PUBLISH/SUBSCRIBE) message variables are bound to the script. Note that context.metrics is not available in Kafka contexts, and result.code, result.key, and result.contentType are not forwarded to the client — Kafka uses its own protocol-level error codes.
To change the outcome of the request or response to access the result object, use the following properties in your script:
| Attribute | Type | Description |
|---|---|---|
state |
PolicyResult.State | To indicate a failure |
code |
integer | An HTTP status code |
error |
string | The error message |
key |
string | The key of a response template |
Here is an example on the request phase:
import io.gravitee.policy.groovy.PolicyResult.State
if (request.headers.containsKey('X-Gravitee-Break')) {
result.key = 'RESPONSE_TEMPLATE_KEY'
result.state = State.FAILURE
result.code = 500
result.error = 'Stop request processing due to X-Gravitee-Break header'
} else {
request.headers.'X-Groovy-Policy' = 'ok'
}To override content in a proxy API, you must enable "Override content." "Override content" is always enabled for message APIs.
To override existing content, make your script the expected content.
Input body content
{
"age": 32,
"firstname": "John",
"lastname": "Doe"
}Groovy script
You must enable "Read content" for this to work
import groovy.json.JsonSlurper
import groovy.json.JsonOutput
def jsonSlurper = new JsonSlurper()
def content = jsonSlurper.parseText(response.content)
content.firstname = 'Hacked ' + content.firstname
content.country = 'US'
return JsonOutput.toJson(content)Output body content
{
"age": 32,
"firstname": "Hacked John",
"lastname": "Doe",
"country": "US"
}In HTTP proxy APIs, with "Override content" enabled, a request or response script may return a String, a byte[], or a Gravitee Buffer. A String is written as text, which is the behavior existing scripts rely on. A byte[] or Buffer is written as those bytes, including bytes at 0x80 and above. Any other return value is written with toString().
Message and native Kafka scripts still write the returned value with toString(), so return a String there. v2 APIs on the v4 emulation engine accept a byte[] or Buffer. v2 APIs running on the legacy v3 execution engine still require the script to return a String.
The script can also set the response status and headers. This decodes an AWS Lambda proxy response:
import groovy.json.JsonSlurper
def envelope = new JsonSlurper().parseText(response.content)
response.status(envelope.statusCode)
response.headers.'Content-Type' = envelope.headers.'Content-Type'
return envelope.body.decodeBase64()request.<property>
| Property | Type | Mutable | Description |
|---|---|---|---|
content |
String |
Body of the request. Requires "Read content". | |
contentAsByteArray |
byte[] |
Body bytes. Requires "Read content". | |
contentAsBase64 |
String |
Body bytes as a basic base64 string. Requires "Read content". | |
transactionId |
String |
Unique identifier for the transaction. | |
clientIdentifier |
String |
Identifies the client that made the request. | |
uri |
String |
The complete request URI. | |
host |
String |
Host from the incoming request. | |
originalHost |
String |
Host as originally received before any internal rewriting. | |
contextPath |
String |
API context path. | |
pathInfo |
String |
Path beyond the context path. | |
path |
String |
The full path component of the request URI. | |
parameters |
Map<String, <List<String>> |
✅️ | Query parameters as a multi-value map. For methods, refer to Multimap methods. |
pathParameters |
Map<String, <List<String>> |
Parameters extracted from path templates. For methods, refer to Multimap methods. Note that altering method are useless in this context. | |
headers |
Map<String, String> |
✅ | HTTP headers. For methods, refer to Headers methods. |
method |
HttpMethod (enum) |
HTTP method used in the request (e.g., GET, POST). | |
scheme |
String |
The scheme (HTTP or HTTPS) used by the request. | |
version |
HttpVersion (enum) |
HTTP protocol version: HTTP_1_0, HTTP_1_1, HTTP_2. |
|
timestamp |
long |
Epoch timestamp of when the request was received. | |
remoteAddress |
String |
IP address of the client. | |
localAddress |
String |
Local IP address of the server handling the request. |
response.<property>
| Property | Type | Mutable | Description |
|---|---|---|---|
content |
String |
Body of the response. Requires "Read content". | |
contentAsByteArray |
byte[] |
Body bytes. Requires "Read content". | |
contentAsBase64 |
String |
Body bytes as a basic base64 string. Requires "Read content". | |
status |
int |
Response status code. | |
reason |
String |
Reason for the status. | |
headers |
Map<String, String> |
✅ | HTTP headers wrapped in a bindable object. For methods, refer to Headers methods. |
message.<property>
| Property | Type | Mutable | Description |
|---|---|---|---|
correlationId |
String | Correlation ID to track the message. | |
parentCorrelationId |
String | Parent correlation ID. | |
timestamp |
long | Epoch (ms) timestamp. | |
error |
boolean | Message is an error message. | |
metadata |
Map<String, Object> | ✅ | Message metadata. Dependent on the messaging system. |
headers |
Map<String, String> | ✅ | Message headers. For methods, refer to Headers methods. |
content |
String | Message body as a string. | |
contentAsBase64 |
String | Message body bytes as a basic base64 string. | |
contentAsByteArray |
byte[] | Message body bytes. | |
attributes |
Map<String, Object> | ✅ | Message attributes wrapped in a bindable object. For methods, see below. |
Message attributes methods
message.attributes.<method>
| Method | Arguments (type) | Return type | Description |
|---|---|---|---|
remove |
key (Object) |
Remove an attribute. | |
containsKey |
key (Object) |
boolean |
Check if an attribute exists. |
containsValue |
key (Object) |
boolean |
Check if one of the attributes contains the value. |
empty |
boolean |
true when the attribute exists. |
|
size |
int |
Return an attribute count. | |
keySet |
Set<String> |
All attribute names. |
| Property | Type | Mutable | Description |
|---|---|---|---|
attributes |
Map<String, Object> | ✅ | Context attributes as a map. |
attributeNames |
Set | All attribute names. | |
attributeAsList |
List | All attribute values. |
Context attributes methods
context.attributes.<method>
Use a prefix to get an attribute or verify that it exists.
For example, context.attributes.'my-specific-attribute' returns an attribute map entry with the key gravitee.attribute.my-specific-attribute.
Refer to the Gravitee documentation for available attributes.
Note: This rule only applies when reading an attribute. You must explicitly add gravitee.attribute.<key> to alter an attribute.
| Method | Arguments (type) | Return type | Description |
|---|---|---|---|
get |
key (Object) |
Object |
Get a Gravitee attribute starting with gravitee.attribute.. Works with Groovy context.attributes.'key' syntax using the same prefix rule. |
containsKey |
key (Object) |
boolean |
Check if a Gravitee attribute starting with gravitee.attribute. exists. |
put |
key (String) value (Object) |
Equivalent of context.attributes.'key' = value. |
Other methods of java.util.Map are accessible, such as remove and size.
Applicable to request.headers, response.headers, message.headers.
| Method | Arguments (type) | Return type | Description |
|---|---|---|---|
remove |
key (Object) |
Remove a header. | |
containsKey |
key (Object) |
boolean |
Check if a header exists. |
clear |
Remove all headers. | ||
empty |
boolean |
true when header exists. |
|
size |
int |
Return header count. | |
keySet |
Set<String> |
All header names. |
Multimap lets you use several values for a single map entry without pre-initializing a collection.
Multimap is applicable to request.parameters and request.pathParameters.
All methods of the Gravitee MultiValueMap implementation: getFirst, add, set, setAll, toSingleValueMap, containsAllKeys.
All java.util.Map are also available.
These templates are defined at the API level, in the "Entrypoint" section for v4 APIs, or in "Response Templates" for v2 APIs. The error keys sent by this policy are as follows:
| Key |
|---|
| GROOVY_EXECUTION_FAILURE |
The groovy policy can be applied to the following API types and flow phases.
PROXYMESSAGENATIVE KAFKAMCP PROXYLLM PROXYA2A PROXY
- Interact
- Publish
- Subscribe
- Request
- Response
Strikethrough text indicates that a version is deprecated.
| Plugin version | APIM | AM | Java version |
|---|---|---|---|
| 5.x | 4.9.x and above | 4.12.x and above | 21 |
| 4.x | 4.9.x and above | - | 21 |
| 3.x | 4.6.x to 4.8.x | - | 17 |
| 2.x | 4.5.x and below | 4.0.x to 4.11.x | 17 |
The groovy policy includes a native sandbox feature, which lets you safely run Groovy scripts. The sandbox is based on a predefined list of allowed methods, fields, constructors, and annotations.
The complete whitelist can be found here: gravitee groovy whitelist.
This whitelist should address the majority of possible use cases. If you have specific needs which are not satisfied by the built-in whitelist, you can extend, or even replace, the list with your own declarations. To modify the whitelist, configure the gravitee.yml file to specify:
groovy.whitelist.mode:appendorreplace. This lets you append whitelisted definitions to the built-in list, or completely replace it. We recommend selectingappendto avoid unintended behaviors.groovy.whitelist.list: This lets you declare other methods, constructors, fields, or annotations in the whitelist.- Start with
methodto allow a specific method (complete signature) - Start with
classto allow a complete class. All methods, constructors, and fields of the class are then accessible. - Start with
newto allow a specific constructor (complete signature) - Start with
fieldto allow access to a specific field of a class - Start with
annotationto allow use of a specific annotation
- Start with
gravitee.yml
groovy:
whitelist:
mode: append
list:
- method com.acme.common.Strings toTitleCase java.lang.String
- class com.acme.common.StringsEnvironment variables (Helm)
gateway:
env:
- name: GRAVITEE_GROOVY_WHITELIST_MODE
value: append
- name: GRAVITEE_GROOVY_WHITELIST_LIST_0
value: "method com.acme.common.Strings toTitleCase java.lang.String"
- name: GRAVITEE_GROOVY_WHITELIST_LIST_1
value: "class com.acme.common.Strings"Security implications
Exercise care when using classes or methods. In some cases, giving access to all methods of a class may make unwanted methods accessible via transitivity and risk security breaches.
Groovy scripts are interrupted if they run longer than the configured timeout, to protect the gateway from long-running or never-ending scripts. By default, the timeout instrumentation skips interfaces so that scripts declaring Groovy interfaces (or annotations) keep compiling; the strictExecutionTimeout option of the policy rejects such scripts instead, guaranteeing that no part of the script can escape the timeout.
| System property | Default | Description |
|---|---|---|
gravitee.policy.groovy.script.timeout.seconds |
5 |
Maximum script execution time in seconds, clamped between 1 and 30. |
Example:
-Dgravitee.policy.groovy.script.timeout.seconds=10
Name json name |
Type constraint |
Mandatory | Default | Description |
|---|---|---|---|---|
Override contentoverrideContent |
boolean | Enable to override the content of the request or response with the value returned by your script. For HTTP proxy APIs, the script can return a String, a byte[] or a Buffer. | ||
Read contentreadContent |
boolean | Enable if your script needs to access the content of the HTTP request or response in your script. | ||
Scriptscript |
string | Groovy script to evaluate. | ||
Strict execution timeoutstrictExecutionTimeout |
boolean | Rejects scripts that cannot be fully covered by the execution timeout protection, such as scripts declaring Groovy interfaces or annotations. Enabling this option improves security by guaranteeing that no part of the script can escape the execution timeout. Recommended if your script does not declare interfaces. |
Proxy API on Request phase
{
"api": {
"definitionVersion": "V4",
"type": "PROXY",
"name": "Groovy example API",
"flows": [
{
"name": "Common Flow",
"enabled": true,
"selectors": [
{
"type": "HTTP",
"path": "/",
"pathOperator": "STARTS_WITH"
}
],
"request": [
{
"name": "Groovy",
"enabled": true,
"policy": "groovy",
"configuration":
{
"readContent": false,
"overrideContent": false,
"script": "response.headers.remove 'X-Powered-By'\nresponse.headers.'X-Gravitee-Gateway-Version' = '0.14.0'"
}
}
]
}
]
}
}
Message API CRD
apiVersion: "gravitee.io/v1alpha1"
kind: "ApiV4Definition"
metadata:
name: "groovy-message-api-crd"
spec:
name: "Groovy example"
type: "MESSAGE"
flows:
- name: "Common Flow"
enabled: true
selectors:
matchRequired: false
mode: "DEFAULT"
request:
- name: "Groovy"
enabled: true
policy: "groovy"
configuration:
overrideContent: false
readContent: false
script: |-
response.headers.remove 'X-Powered-By'
response.headers.'X-Gravitee-Gateway-Version' = '0.14.0'
5.2.0 (2026-11-19)
5.1.3 (2026-11-19)
- sandbox: restrict XmlSlurper to safe members (06f0d9f)
5.1.2 (2026-11-05)
- sandbox: harden whitelist against asType and MethodClosure bypass (dfce1e4)
5.1.1 (2026-09-04)
- sandbox: intercept the typed for-each implicit cast (SECURITY-3792) (c76bf18)
5.1.0 (2026-08-31)
- sandbox: apply the environment given to a later initialize() (136468f)
- sandbox: cache denied property lookups like denied methods (d72859c)
- sandbox: key the decision cache by class identity (3b4f4d4)
- sandbox: key the whitelist by signature instead of class identity (87483a9)
- sandbox: publish the whitelist safely and stop destroying it in use (2a22adb)
- sandbox: instrument whitelist denials for diagnosis (d2e84ef)
5.0.5 (2026-08-12)
- remove default value for deprecated scope field in Groovy policy schema (2fb67a5)
5.0.4 (2026-07-13)
- groovy: reintroduce script execution timeout with interface-safe default (17e9f3d)
5.0.3 (2026-07-10)
- Revert "fix(groovy): limit script execution time and always run scripts off the event loop" (50ae720)
5.0.2 (2026-07-07)
- declare default schema explicitly in plugin manifest (3b62772)
5.0.1 (2026-06-26)
- adapt whitelist for groovy 4 (c779d30)
- exclude groovy2 from the dependencies (c3ebdf3)
- relay on PropertyExpression constructor to avoid NoSuchMethod exception (a3f095e)
5.0.0 (2026-06-24)
- upgrade to Groovy 4 to support Java 25 (c5f86ef)
- Groovy 4 is required (enables Java 25 support).
Also includes the sandbox super-call fix (onSuperCall now dispatches via senderType, since Groovy 4 made the super-call meta method index strict and groovy-sandbox failed to resolve inherited methods) and the XmlSlurper package move (groovy.util -> groovy.xml).
4.3.4 (2026-06-09)
4.3.3 (2026-05-20)
- groovy: limit script execution time and always run scripts off the event loop (ad10d44)
4.3.2 (2026-05-06)
4.3.1 (2026-04-23)
4.3.0 (2026-03-17)
4.2.0 (2026-02-19)
- enable for A2A proxy (4075422)
4.1.0 (2025-11-14)
- enable for LLM & MCP Proxy API (4f67dc6)
4.0.0 (2025-09-18)
- include cause throwable in the execution failure (1cf1409)
- requires APIM version 4.9.0 or later
3.0.3 (2025-08-08)
- deps: update dependency org.apache.commons:commons-lang3 to v3.18.0 [security] (7913dc6)
3.0.2 (2025-06-18)
- doc gen (24bde45)
3.0.1 (2025-01-15)
- deps: upgrade groovy-sandbox to 1.30 (32fba8f)
3.0.0 (2024-12-30)
- deps: bump apim version (4eb775a)
- use latest node and apim version (fb3706b)
- warning messages (5b3b334)
- support new OpenTelemetry feature (14ca260)
- Tracer interface is not more available through tracer-api module
- tracer and components are no longer allowed for groovy context
3.0.0-alpha.3 (2024-12-30)
- deps: bump apim version (4eb775a)
3.0.0-alpha.2 (2024-12-16)
- warning messages (5b3b334)
3.0.0-alpha.1 (2024-11-06)
- use latest node and apim version (fb3706b)
- support new OpenTelemetry feature (14ca260)
- Tracer interface is not more available through tracer-api module
- tracer and components are no longer allowed for groovy context
2.6.3 (2024-12-13)
- warning messages in logs for groovy classes (612f554)
2.6.2 (2024-10-02)
- avoid blocking eventloop when compiling (5bccbe2)
2.6.1 (2024-08-21)
2.6.0 (2024-06-03)
- add methods for binary content of messages (707519e)
2.5.2 (2023-11-09)
- do not write body if onRequest/Response only (62f692c)
2.5.1 (2023-10-05)
- add request and response to message phases (154db98)
2.5.0 (2023-10-02)
- add message level support to policy (632813e)
2.4.2 (2023-07-20)
- update policy description (b0e00a0)
2.4.1 (2023-06-27)
- add policy result key to readme (f8b6774)
2.4.0 (2023-06-27)
- allow to add response template key in policy result (b26046e)
2.3.0 (2023-04-12)
- properly return the scheme with
scheme()andgetScheme()methods (2a827b9)
- add a
getHost()method so that "request.host" expression is correctly resolved (12a3a04)
2.2.2 (2022-06-24)
- whitelist gateway-api HttpHeaders (f4bd528)
2.2.1 (2022-06-15)
- make header accessor return an iterable instead of a string (46774f2)
2.2.0 (2022-03-17)
- resolve GStringImpl.trim() (4ff3390)
- resolve iteration on map (d8fd8e5), closes gravitee-io/issues#7302
- add EncodingGroovyMethods to whitelist (2ba4f27)
2.1.0 (2022-01-24)
- headers: Internal rework and introduce HTTP Headers API (3a3aa33), closes gravitee-io/issues#6772
- perf: adapt policy for new classloader system (08c3aea), closes gravitee-io/issues#6758