Search in sources :

Example 41 with SimpleBindings

use of javax.script.SimpleBindings in project OpenAM by OpenRock.

the class BatchResource method actionCollection.

@Override
public Promise<ActionResponse, ResourceException> actionCollection(Context serverContext, ActionRequest actionRequest) {
    if (!actionRequest.getAction().equals(BATCH)) {
        final String msg = "Action '" + actionRequest.getAction() + "' not implemented for this resource";
        final NotSupportedException exception = new NotSupportedException(msg);
        debug.error(msg, exception);
        return exception.asPromise();
    }
    String scriptId = null;
    try {
        JsonValue scriptIdValue = actionRequest.getContent().get(SCRIPT_ID);
        if (scriptIdValue == null) {
            if (debug.errorEnabled()) {
                debug.error("BatchResource :: actionCollection - ScriptId null. Default scripts not implemented.");
            }
            return new BadRequestException().asPromise();
        } else {
            scriptId = scriptIdValue.asString();
        }
        final JsonValue requests = actionRequest.getContent().get(REQUESTS);
        final String realm = getRealm(serverContext);
        final ScriptConfiguration scriptConfig = scriptingServiceFactory.create(SubjectUtils.createSuperAdminSubject(), realm).get(scriptId);
        final ScriptObject script = new ScriptObject(scriptConfig.getName(), scriptConfig.getScript(), scriptConfig.getLanguage());
        final ScriptResponse response = new ScriptResponse();
        final Bindings bindings = new SimpleBindings();
        bindings.put(PAYLOAD, requests);
        bindings.put(CONTEXT, serverContext);
        bindings.put(LOGGER, debug);
        bindings.put(REQUESTER, requester);
        bindings.put(RESPONSE, response);
        return newResultPromise(newActionResponse((JsonValue) scriptEvaluator.evaluateScript(script, bindings)));
    } catch (ScriptException e) {
        debug.error("BatchResource :: actionCollection - Error running script : {}", scriptId);
        return exceptionMappingHandler.handleError(serverContext, actionRequest, e).asPromise();
    } catch (javax.script.ScriptException e) {
        debug.error("BatchResource :: actionCollection - Error running script : {}", scriptId);
        return new InternalServerErrorException().asPromise();
    }
}
Also used : ScriptObject(org.forgerock.openam.scripting.ScriptObject) JsonValue(org.forgerock.json.JsonValue) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) ScriptException(org.forgerock.openam.scripting.ScriptException) SimpleBindings(javax.script.SimpleBindings) BadRequestException(org.forgerock.json.resource.BadRequestException) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) ScriptConfiguration(org.forgerock.openam.scripting.service.ScriptConfiguration) NotSupportedException(org.forgerock.json.resource.NotSupportedException) ScriptResponse(org.forgerock.openam.scripting.rest.batch.helpers.ScriptResponse)

Example 42 with SimpleBindings

use of javax.script.SimpleBindings in project OpenAM by OpenRock.

the class OidcClaimsExtensionTest method testBindings.

private <T> Bindings testBindings(Set<String> scopes, Map<String, Set<T>> requestedClaims) {
    Bindings scriptVariables = new SimpleBindings();
    scriptVariables.put("logger", logger);
    scriptVariables.put("claims", new HashMap<String, Object>());
    scriptVariables.put("accessToken", accessToken);
    scriptVariables.put("session", ssoToken);
    scriptVariables.put("identity", identity);
    scriptVariables.put("scopes", scopes);
    scriptVariables.put("requestedClaims", requestedClaims);
    return scriptVariables;
}
Also used : SimpleBindings(javax.script.SimpleBindings) ScriptObject(org.forgerock.openam.scripting.ScriptObject) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings)

Example 43 with SimpleBindings

use of javax.script.SimpleBindings in project OpenAM by OpenRock.

the class AbstractSandboxTests method shouldNotBeAllowedToAccessBlackListedMember.

@Test
public void shouldNotBeAllowedToAccessBlackListedMember() throws Exception {
    // Given
    Allowed allowed = new Allowed();
    Bindings bindings = new SimpleBindings();
    bindings.put("allowed", allowed);
    // When
    try {
        eval(bindings, "allowed.forbiddenFruit.setDirty()");
        fail("Sandbox failed to protect access to black-listed member");
    } catch (ScriptException ex) {
        // Then
        assertThat(allowed.fruit.dirty).isFalse();
    }
}
Also used : ScriptException(javax.script.ScriptException) SimpleBindings(javax.script.SimpleBindings) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) Test(org.testng.annotations.Test)

Example 44 with SimpleBindings

use of javax.script.SimpleBindings in project OpenAM by OpenRock.

the class StandardScriptEvaluatorTest method shouldSupportJSONParsingInGroovy.

@Test
public void shouldSupportJSONParsingInGroovy() throws Exception {
    ScriptObject script = getGroovyScript("import groovy.json.JsonSlurper", "def slurper = new JsonSlurper()", "def json = slurper.parseText(x)", "json.a");
    Bindings scope = new SimpleBindings();
    scope.put("x", "{\"a\" : 12}");
    Object result = testEvaluator.evaluateScript(script, scope);
    assertThat(result).isEqualTo(12);
}
Also used : SimpleBindings(javax.script.SimpleBindings) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) Test(org.testng.annotations.Test)

Example 45 with SimpleBindings

use of javax.script.SimpleBindings in project OpenAM by OpenRock.

the class ThreadPoolScriptEvaluatorTest method shouldDelegateToConfiguredScriptEvaluator.

@Test
public void shouldDelegateToConfiguredScriptEvaluator() throws Exception {
    testEvaluator = new ThreadPoolScriptEvaluator(scriptEngineManager, Executors.newSingleThreadExecutor(), mockEvaluator);
    ScriptObject testScript = getGroovyScript("x + 1");
    Bindings bindings = new SimpleBindings();
    bindings.put("x", 3);
    int expectedResult = 42;
    given(mockEvaluator.evaluateScript(testScript, bindings)).willReturn(expectedResult);
    // When
    Number result = testEvaluator.evaluateScript(testScript, bindings);
    // Then
    verify(mockEvaluator).evaluateScript(testScript, bindings);
    assertThat(result.intValue()).isEqualTo(expectedResult);
}
Also used : SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) Test(org.testng.annotations.Test)

Aggregations

SimpleBindings (javax.script.SimpleBindings)67 Bindings (javax.script.Bindings)47 Test (org.junit.Test)20 ScriptContext (javax.script.ScriptContext)14 SimpleScriptContext (javax.script.SimpleScriptContext)12 Test (org.testng.annotations.Test)11 ScriptException (javax.script.ScriptException)8 SlingBindings (org.apache.sling.api.scripting.SlingBindings)7 StringWriter (java.io.StringWriter)5 HashMap (java.util.HashMap)5 ScriptEngine (javax.script.ScriptEngine)5 PrintWriter (java.io.PrintWriter)4 CompiledScript (javax.script.CompiledScript)4 RenderContext (org.apache.sling.scripting.sightly.render.RenderContext)4 StringReader (java.io.StringReader)3 Map (java.util.Map)3 ScriptEngineManager (javax.script.ScriptEngineManager)3 Resource (org.apache.sling.api.resource.Resource)3 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)3 ProtectedBindings (org.apache.sling.scripting.core.impl.helper.ProtectedBindings)3