use of javax.script.Bindings in project OpenAM by OpenRock.
the class StandardScriptEvaluator method evaluateScript.
/**
* Evaluates scripts immediately using the configured JSR-223 script engine manager. This implementation should
* be wrapped with a {@link org.forgerock.openam.scripting.ThreadPoolScriptEvaluator} if script interruption or
* timeouts are required.
*
* @param script the script to evaluate.
* @param bindings any additional variable bindings to set before running the script.
* @param <T> the type of result returned from the script.
* @return the result of evaluating the script.
* @throws ScriptException if an error occurs in script execution.
*/
@Override
@SuppressWarnings("unchecked")
public <T> T evaluateScript(final ScriptObject script, final Bindings bindings) throws ScriptException {
Reject.ifNull(script);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Evaluating script: " + script);
}
final ScriptEngine engine = getScriptEngineFor(script);
final Bindings variableBindings = mergeBindings(script.getBindings(), bindings);
final ScriptContext context = buildScriptContext(variableBindings);
return (T) engine.eval(script.getScript(), context);
}
use of javax.script.Bindings in project OpenAM by OpenRock.
the class OidcClaimsExtensionTest method testRequestedClaims.
@Test
public void testRequestedClaims() throws Exception {
// Given
Map<String, Set<String>> requestedClaims = new HashMap<String, Set<String>>();
requestedClaims.put("given_name", asSet("fred"));
requestedClaims.put("family_name", asSet("flintstone"));
Bindings variables = testBindings(asSet("profile"), requestedClaims);
when(identity.getAttribute("cn")).thenReturn(asSet("Joe Bloggs"));
// When
UserInfoClaims result = scriptEvaluator.evaluateScript(script, variables);
// Then
assertThat(result.getValues()).containsOnly(entry("given_name", "fred"), entry("family_name", "flintstone"), entry("name", "Joe Bloggs"));
assertThat(result.getCompositeScopes()).containsOnlyKeys("profile");
ArrayList<String> hashProfile = (ArrayList<String>) result.getCompositeScopes().get("profile");
assertThat(hashProfile).contains("zoneinfo", "name", "locale", "family_name", "given_name");
assertThat(hashProfile).hasSize(5);
verify(identity).getAttribute("cn");
verify(identity).getAttribute("preferredlocale");
verify(identity).getAttribute("preferredtimezone");
verifyNoMoreInteractions(identity);
}
use of javax.script.Bindings in project OpenAM by OpenRock.
the class OidcClaimsExtensionTest method testRequestedClaimsSelect.
@Test
public void testRequestedClaimsSelect() throws Exception {
// Given
Bindings variables = testBindings(asSet("profile"), Collections.singletonMap("given_name", asSet("fred", "george")));
when(identity.getAttribute("cn")).thenReturn(asSet("Joe Bloggs"));
// When/Then
try {
scriptEvaluator.evaluateScript(script, variables);
} catch (Throwable e) {
Throwable last = null;
while (last != e) {
if (e.getClass().equals(RuntimeException.class)) {
break;
}
last = e;
e = e.getCause();
}
assertThat(e.getMessage()).isEqualTo("No selection logic for given_name defined. Values: [george, fred]");
}
}
use of javax.script.Bindings in project OpenAM by OpenRock.
the class OidcClaimsExtensionTest method testRequestedClaimsSelectNoScope.
@Test
public void testRequestedClaimsSelectNoScope() throws Exception {
// Given
Bindings variables = testBindings(asSet("openid"), Collections.singletonMap("given_name", asSet("fred", "george")));
when(identity.getAttribute("cn")).thenReturn(asSet("Joe Bloggs"));
// When/Then
try {
scriptEvaluator.evaluateScript(script, variables);
} catch (Throwable e) {
Throwable last = null;
while (last != e) {
if (e.getClass().equals(RuntimeException.class)) {
break;
}
last = e;
e = e.getCause();
}
assertThat(e.getMessage()).isEqualTo("No selection logic for given_name defined. Values: [george, fred]");
}
}
use of javax.script.Bindings in project OpenAM by OpenRock.
the class ScriptContextScope method getIds.
/**
* {@inheritDoc}
*/
@Override
public Object[] getIds() {
final List<Object> keys = new ArrayList<Object>();
for (int scope : scriptContext.getScopes()) {
final Bindings bindings = scriptContext.getBindings(scope);
keys.addAll(bindings.keySet());
}
return keys.toArray();
}
Aggregations