Search in sources :

Example 96 with Bindings

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);
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine)

Example 97 with Bindings

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);
}
Also used : Set(java.util.Set) HashMap(java.util.HashMap) UserInfoClaims(org.forgerock.oauth2.core.UserInfoClaims) ArrayList(java.util.ArrayList) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) Test(org.testng.annotations.Test)

Example 98 with Bindings

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]");
    }
}
Also used : Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) Test(org.testng.annotations.Test)

Example 99 with Bindings

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]");
    }
}
Also used : Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) Test(org.testng.annotations.Test)

Example 100 with Bindings

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();
}
Also used : ArrayList(java.util.ArrayList) NativeObject(org.mozilla.javascript.NativeObject) Bindings(javax.script.Bindings)

Aggregations

Bindings (javax.script.Bindings)142 SimpleBindings (javax.script.SimpleBindings)74 Test (org.junit.Test)36 ScriptException (javax.script.ScriptException)27 ScriptContext (javax.script.ScriptContext)24 SimpleScriptContext (javax.script.SimpleScriptContext)21 SlingBindings (org.apache.sling.api.scripting.SlingBindings)18 Test (org.testng.annotations.Test)17 ScriptEngine (javax.script.ScriptEngine)15 CompiledScript (javax.script.CompiledScript)14 Resource (org.apache.sling.api.resource.Resource)11 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)10 SlingScriptHelper (org.apache.sling.api.scripting.SlingScriptHelper)10 HashMap (java.util.HashMap)9 Map (java.util.Map)9 IOException (java.io.IOException)8 StringWriter (java.io.StringWriter)7 ArrayList (java.util.ArrayList)7 PrintWriter (java.io.PrintWriter)6 ScriptEngineManager (javax.script.ScriptEngineManager)6