Giter VIP home page Giter VIP logo

gravitee-policy-groovy's Introduction

Groovy Policy

Github Build status Gitter

Phase

onRequest onResponse OnResponseContent

X

X

X

Description

The Groovy policy lets the user (ie. API publisher) to run groovy scripts during all the stages of the request processing through the gateway.

The following groovy script is an example executed during the OnResponse phase to change HTTP headers:

response.headers.remove 'X-Powered-By'
response.headers.'X-Gravitee-Gateway-Version' = '0.14.0'

Configuration

You can attach a groovy script to all phases of the request processing.

OnRequest / OnResponse

Some variables are automatically bind to the Groovy script to let user to use them and define the policy behavior.

Table 1. List of groovy script variables
Name Description

request

Inbound HTTP request

response

Outbound HTTP response

context

PolicyContext used to access external components such as services, resources, โ€ฆโ€‹

result

Result of the groovy script

You can also break request or response processing by setting the result to FAILURE and by providing an HTTP status code and a message (not mandatory, status is equals to 500 (internal server error) by default).

if (request.headers.containsKey('X-Gravitee-Break')) {
    result.state = FAILURE
    result.code = 500
    result.error = 'Stop request processing due to X-Gravitee-Break header'
} else {
    request.headers.'X-Groovy-Policy' = 'ok'
}

OnResponseContent

You can transform response body content by using a groovy script during the OnResponseContent phase.

Following is an example to show you how to use Groovy policy to transform a JSON content:

Input body content
[
    {
        "age": 32,
        "firstname": "John",
        "lastname": "Doe"
    }
]
Groovy script
import groovy.json.JsonSlurper
import groovy.json.JsonOutput

def jsonSlurper = new JsonSlurper()
def content = jsonSlurper.parseText(response)
content[0].firstname = 'Hacked ' + content[0].firstname
content[0].country = 'US'
return JsonOutput.toJson(content)
Output body content
[
    {
        "age": 32,
        "firstname": "Hacked John",
        "lastname": "Doe",
        "country": "US
    }
]

gravitee-policy-groovy's People

Contributors

brasseld avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.