Skip to content

Latest commit

 

History

History
158 lines (113 loc) · 6.07 KB

File metadata and controls

158 lines (113 loc) · 6.07 KB
description
This page provides the technical details of the Assign Attributes policy

Assign Attributes

Overview

You can use the assign-attributes policy to set variables such as request attributes and other execution context attributes.

You can use it to retrieve initial request attributes after Transform headers or Transform query parameters policies and reuse them in other policies (Dynamic routing, for example).

Functional and implementation information for the assign-attributes policy is organized into the following sections:

Examples

{% hint style="warning" %} This policy can be applied to all Gravitee APIs: v2 APIs, v4 proxy APIs, and v4 message APIs. {% endhint %}

{% tabs %} {% tab title="Proxy API example" %} To inject an attribute that will dynamically determine if the content is in JSON format:

"assign-attributes": {
    "attributes": [
        {
            "name": "isJson,
            "value": "'application/json'.equals(#request.headers['Content-Type'])"
        }
    ]
}

To extract the request attribute and get the format of the content you can use the following syntax:

{#context.attributes['isJson']}

Request objects

You can also be more general and inject complex objects into the context attributes:

"assign-attributes": {
    "attributes": [
        {
            "name": "initialRequest,
            "value": "{#request}"
        }
    ]
}

To extract request attributes and get the Content-Type header you can use the following syntax:

{#context.attributes['initialRequest'].headers['Content-Type']}

{% endtab %}

{% tab title="Message API example" %} To inject an attribute that will dynamically determine if the content is in JSON format:

"assign-attributes": {
    "attributes": [
        {
            "name": "isJson,
            "value": "'application/json'.equals(#message.headers['Content-Type'])"
        }
    ]
}

To extract the message attribute and get the format of the content you can use the following syntax:

{#message.attributes['isJson']}

Message objects

You can also be more general and inject complex objects into the message attributes:

"assign-attributes": {
    "attributes": [
        {
            "name": "initialMessage,
            "value": "{#message}"
        }
    ]
}

To extract message attributes and get the Content-Type header you can use the following syntax:

{#message.attributes['initialMessage'].headers['Content-Type']}

To assign an attribute to the content of a message:

"assign-attributes": {
    "attributes": [
        {
            "name": "messageContent,
            "value": "{#message.content}"
        }
    ]
}

{% endtab %} {% endtabs %}

Configuration

Phases

The phases checked below are supported by the assign-attributes policy:

v2 PhasesCompatible?v4 PhasesCompatible?
onRequesttrueonRequesttrue
onResponsetrueonResponsetrue
onRequestContenttrueonMessageRequesttrue
onResponseContenttrueonMessageResponsetrue

Options

You can configure the assign-attributes policy with the following options:

PropertyRequiredDescriptionTypeDefault
scopeonly for v4 proxy APIsThe execution scope (request or response)stringREQUEST
attributesXList of attributesSee table below

Attributes

You can configure the assign-attributes policy with the following attributes:

PropertyRequiredDescriptionTypeDefault
nameXAttribute namestring
valueXAttribute value (can be EL)string

Compatibility matrix

The following is the compatibility matrix for APIM and the assign-attributes policy:

Plugin VersionSupported APIM versions
Up to 1.xAll
From 2.x4.0+

Errors

PhaseHTTP status codeError template key
onRequest500An error occurred while setting request attributes in the execution context
onResponse500An error occurred while setting request attributes in the execution context
onRequestContent500An error occurred while setting request attributes in the execution context
onResponseContent500An error occurred while setting request attributes in the execution context
onMessageRequest500An error occurred while setting request attributes in the execution context
onMessageResponse500An error occurred while setting request attributes in the execution context

Changelogs

{% @github-files/github-code-block url="https://github.com/gravitee-io/gravitee-policy-assign-attributes/blob/master/CHANGELOG.md" %}