Why am I getting a JS Exception when accessing one of the context variables

I’m getting the following exception when I try to loop through the collection returned by the request.queryparams.names variable using javascript:

"Javascript runtime error: "Access to Java class "java.util.HashSet" is prohibited” Here’s a sample code snippet where the failure point is at line 2.

var queryFieldsCollection = context.getVariable('request.queryparams.names');
for (var k in queryFieldsCollection) {
    print(queryFieldsCollection[k]);
}

How can I access this collection?

1 Like

We use compiled JavaScript written in Java, and there’s a limitation on accessing Collections. It’s not possible to iterate directly through the collection…this is a limitation in the library we use. To get around that, convert the collection to a string, tweak the string, then split it into an array, and loop through that array. Here’s a sample.

//Convert Collection to string
var queryFieldsCollection = context.getVariable('request.queryparams.names') + '';

//Remove square brackets
queryFieldsCollection = queryFieldsCollection.substr(1, queryFieldsCollection.length - 2);

//Split string into an array
var queryFieldsArray = queryFieldsCollection.split(", ");

//Loop through Array and get value of queryparam
for (var i = 0; i < queryFieldsArray.length; i++) {
    print( queryFieldsArray[i] + ":" + context.getVariable('request.queryparam.' + queryFieldsArray[i]) );
}

1 Like

Could use another context variable ‘request.queryparams.names.string’ as below skipping few lines of code from the above accepted solution :

// Retrieve the query param names as String
var queryFields = context.getVariable('request.queryparams.names.string');

// Split string into an array and loop to get queryparam names
queryFields.split(",").forEach(function(queryparam) {
    print(queryparam);
});
1 Like

Hello @yogesh-techy , welcome to the Apigee community!

Thank you for sharing this alternative approach - your contribution is valuable to this discussion and hope it’ll be helpful to others facing similar challenges.

We encourage you to continue sharing your expertise and knowledge in other threats as well, we are excited to see more of your contributions in the community.

Thanks again :slightly_smiling_face:

But it should be possible to whitelist the java.util.HashSet class in Rhino’s ClassShutter, right? Since flow variables are already accessible from JavaScript, it could be worth considering interaction with collections directly as a feature request in the future.

yes, this variable was added at some point in the past couple years, … years after the initial question had been asked. So this ^^ (use the new variable) is a much better approach at this point. I think this may not work on OPDK, but you’d have to check.

yes, it would be possible for Apigee to expose this within the JS engine, but… the engineering team haven’t done that. The feature request is on the backlog, but hasn’t been prioritized, since there are “reasonable” workarounds.