Accessing Response object in ScriptableHandler scripts in ForgeRock Identity Gateway

Hi All,

Trying to create a scriptable handler in ForgeRock IG which would create a response object based on some parameters in the current response object.

However, when I use the “response” object in the script, it comes up null.

logger.error("Response object: ${response}")

I also tried sending response.status.code (as an example) as argument to the script. But that comes up as null too. But I have used the same object earlier in the script, and that workes as expected. Please find my route snippet below

{
        "name": "SwitchFilter-1",
        "type": "SwitchFilter",
        "config": {
                "onResponse" : [
                        {
                                "condition": "${response.status.code == 404}",
                                "handler": "ScriptableHandler-1"
                        }]
        }

        },{
                "name": "ScriptableHandler-1",
                "type": "ScriptableHandler",
                "config": {
                        "type": "application/x-groovy",
                        "file": "/app/forgerock/installnew/ig-config/scripts/groovy/errorMessageHandler.groovy",
                        "args": {"responseEntity" : "${response.status.code}"}
                }
        }

What’s the right way to access the response object in a ScriptableHandler?

Hi @anishetty,

Config is handled at start time, there is no global response object, this is entirely static. So you need to save the response code locally in the route with the attributes binding, to be reused downstream by the scriptable handler. You might want to replace the SwitchFilter with a ScriptableFilter, and store the value in the attributes object. Then in the ScriptableHandler, pass in ${attributes.responseEntity}. So the scriptable filter script would be something like :

return next.handle(context, request) { response ->
       if (response.status.code == 404) {
              attributes.responseEntity = response.entity
       }
       return response
}

Regards
Patrick

1 Like