Search in sources :

Example 6 with SimpleBindings

use of javax.script.SimpleBindings in project es6draft by anba.

the class ScriptEngineScopeTest method scopeInteractionNewContextSimpleBindingsGlobalAssignment.

@Test
public void scopeInteractionNewContextSimpleBindingsGlobalAssignment() throws ScriptException {
    ScriptContext context = new SimpleScriptContext();
    context.setBindings(engine.getBindings(ScriptContext.GLOBAL_SCOPE), ScriptContext.GLOBAL_SCOPE);
    context.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE);
    context.setAttribute("value", "Phecda", ScriptContext.ENGINE_SCOPE);
    context.setAttribute("value", "Scheat", ScriptContext.GLOBAL_SCOPE);
    engine.eval("value = 'Aludra'", context);
    assertThat(engine.eval("this.value", context), nullValue());
    assertThat(engine.eval("value", context), instanceOfWith(String.class, is("Aludra")));
    assertThat(context.getAttribute("value", ScriptContext.ENGINE_SCOPE), instanceOfWith(String.class, is("Aludra")));
    assertThat(context.getAttribute("value", ScriptContext.GLOBAL_SCOPE), instanceOfWith(String.class, is("Scheat")));
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) SimpleBindings(javax.script.SimpleBindings) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Test(org.junit.Test)

Example 7 with SimpleBindings

use of javax.script.SimpleBindings in project es6draft by anba.

the class ScriptEngineScopeTest method isolatedBindingsWithContextAndSimpleBindings.

@Test
public void isolatedBindingsWithContextAndSimpleBindings() throws ScriptException {
    ScriptContext context = new SimpleScriptContext();
    Bindings binding = new SimpleBindings();
    context.setBindings(binding, ScriptContext.ENGINE_SCOPE);
    engine.eval("var value = 'Zeta Scorpii'", context);
    assertThat(engine.eval("value", context), instanceOfWith(String.class, is("Zeta Scorpii")));
    context.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE);
    assertThat(engine.eval("typeof value", context), instanceOfWith(String.class, is("undefined")));
    context.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE);
    assertThat(engine.eval("typeof value", context), instanceOfWith(String.class, is("undefined")));
    context.setBindings(binding, ScriptContext.ENGINE_SCOPE);
    assertThat(engine.eval("value", context), instanceOfWith(String.class, is("Zeta Scorpii")));
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) SimpleBindings(javax.script.SimpleBindings) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) Test(org.junit.Test)

Example 8 with SimpleBindings

use of javax.script.SimpleBindings in project es6draft by anba.

the class ScriptEngineScopeTest method scopeInteractionNewContextSimpleBindingsPropertyAssignment.

@Test
public void scopeInteractionNewContextSimpleBindingsPropertyAssignment() throws ScriptException {
    ScriptContext context = new SimpleScriptContext();
    context.setBindings(engine.getBindings(ScriptContext.GLOBAL_SCOPE), ScriptContext.GLOBAL_SCOPE);
    context.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE);
    context.setAttribute("value", "Phecda", ScriptContext.ENGINE_SCOPE);
    context.setAttribute("value", "Scheat", ScriptContext.GLOBAL_SCOPE);
    engine.eval("this.value = 'Aludra'", context);
    assertThat(engine.eval("this.value", context), nullValue());
    assertThat(engine.eval("value", context), instanceOfWith(String.class, is("Phecda")));
    assertThat(context.getAttribute("value", ScriptContext.ENGINE_SCOPE), instanceOfWith(String.class, is("Phecda")));
    assertThat(context.getAttribute("value", ScriptContext.GLOBAL_SCOPE), instanceOfWith(String.class, is("Scheat")));
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) SimpleBindings(javax.script.SimpleBindings) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Test(org.junit.Test)

Example 9 with SimpleBindings

use of javax.script.SimpleBindings in project es6draft by anba.

the class ScriptEngineScopeTest method globalScopeAccessWithOverrideNewContextSimpleBindings.

@Test
public void globalScopeAccessWithOverrideNewContextSimpleBindings() throws ScriptException {
    Bindings globalScope = engine.getBindings(ScriptContext.GLOBAL_SCOPE);
    globalScope.put("globalVar", "Becrux");
    ScriptContext context = new SimpleScriptContext();
    context.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE);
    context.setBindings(globalScope, ScriptContext.GLOBAL_SCOPE);
    context.getBindings(ScriptContext.ENGINE_SCOPE).put("globalVar", "Antares");
    assertThat(engine.eval("globalVar", context), instanceOfWith(String.class, is("Antares")));
    engine.getBindings(ScriptContext.ENGINE_SCOPE).remove("globalVar");
    assertThat(engine.eval("globalVar"), instanceOfWith(String.class, is("Becrux")));
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) SimpleBindings(javax.script.SimpleBindings) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) Test(org.junit.Test)

Example 10 with SimpleBindings

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

the class Scripted method process.

/**
     * {@inheritDoc}
     */
@Override
public int process(Callback[] callbacks, int state) throws LoginException {
    switch(state) {
        case ISAuthConstants.LOGIN_START:
            substituteUIStrings();
            return STATE_RUN_SCRIPT;
        case STATE_RUN_SCRIPT:
            Bindings scriptVariables = new SimpleBindings();
            scriptVariables.put(REQUEST_DATA_VARIABLE_NAME, getScriptHttpRequestWrapper());
            String clientScriptOutputData = getClientScriptOutputData(callbacks);
            scriptVariables.put(CLIENT_SCRIPT_OUTPUT_DATA_VARIABLE_NAME, clientScriptOutputData);
            scriptVariables.put(LOGGER_VARIABLE_NAME, DEBUG);
            scriptVariables.put(STATE_VARIABLE_NAME, state);
            scriptVariables.put(SHARED_STATE, sharedState);
            scriptVariables.put(USERNAME_VARIABLE_NAME, userName);
            scriptVariables.put(SUCCESS_ATTR_NAME, SUCCESS_VALUE);
            scriptVariables.put(FAILED_ATTR_NAME, FAILURE_VALUE);
            scriptVariables.put(HTTP_CLIENT_VARIABLE_NAME, httpClient);
            scriptVariables.put(IDENTITY_REPOSITORY, identityRepository);
            try {
                scriptEvaluator.evaluateScript(getServerSideScript(), scriptVariables);
            } catch (ScriptException e) {
                DEBUG.message("Error running server side scripts", e);
                throw new AuthLoginException("Error running script", e);
            }
            state = ((Number) scriptVariables.get(STATE_VARIABLE_NAME)).intValue();
            userName = (String) scriptVariables.get(USERNAME_VARIABLE_NAME);
            sharedState.put(CLIENT_SCRIPT_OUTPUT_DATA_VARIABLE_NAME, clientScriptOutputData);
            if (state != SUCCESS_VALUE) {
                throw new AuthLoginException("Authentication failed");
            }
            return state;
        default:
            throw new AuthLoginException("Invalid state");
    }
}
Also used : ScriptException(javax.script.ScriptException) SimpleBindings(javax.script.SimpleBindings) AuthLoginException(com.sun.identity.authentication.spi.AuthLoginException) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings)

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