Search in sources :

Example 21 with Bindings

use of javax.script.Bindings in project hazelcast by hazelcast.

the class HazelcastOSGiScriptEngineManagerTest method bindingsGetAndSetSuccessfully.

@Test
public void bindingsGetAndSetSuccessfully() {
    ScriptEngineManager scriptEngineManager = ScriptEngineManagerContext.getScriptEngineManager();
    assertNotNull(scriptEngineManager.getBindings());
    Bindings mockBindings = mock(Bindings.class);
    scriptEngineManager.setBindings(mockBindings);
    assertEquals(mockBindings, scriptEngineManager.getBindings());
}
Also used : ScriptEngineManager(javax.script.ScriptEngineManager) Bindings(javax.script.Bindings) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 22 with Bindings

use of javax.script.Bindings in project hazelcast by hazelcast.

the class HazelcastOSGiScriptEngineTest method verifyThatBindingsGetAndSetSuccessfully.

private void verifyThatBindingsGetAndSetSuccessfully(ScriptEngine scriptEngine) {
    assertNotNull(scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE));
    Bindings bindings = scriptEngine.createBindings();
    assertNotEquals(bindings, scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE));
    scriptEngine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
    assertEquals(bindings, scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE));
}
Also used : Bindings(javax.script.Bindings)

Example 23 with Bindings

use of javax.script.Bindings in project midpoint by Evolveum.

the class Jsr223ScriptEvaluator method evaluate.

@Override
public <T, V extends PrismValue> List<V> evaluate(ScriptExpressionEvaluatorType expressionType, ExpressionVariables variables, ItemDefinition outputDefinition, Function<Object, Object> additionalConvertor, ScriptExpressionReturnTypeType suggestedReturnType, ObjectResolver objectResolver, Collection<FunctionLibrary> functions, String contextDescription, Task task, OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, ExpressionSyntaxException {
    Bindings bindings = convertToBindings(variables, objectResolver, functions, contextDescription, task, result);
    String codeString = expressionType.getCode();
    if (codeString == null) {
        throw new ExpressionEvaluationException("No script code in " + contextDescription);
    }
    boolean allowEmptyValues = false;
    if (expressionType.isAllowEmptyValues() != null) {
        allowEmptyValues = expressionType.isAllowEmptyValues();
    }
    CompiledScript compiledScript = createCompiledScript(codeString, contextDescription);
    Object evalRawResult;
    try {
        InternalMonitor.recordScriptExecution();
        evalRawResult = compiledScript.eval(bindings);
    } catch (Throwable e) {
        throw new ExpressionEvaluationException(e.getMessage() + " in " + contextDescription, e);
    }
    if (outputDefinition == null) {
        // No outputDefinition means "void" return type, we can return right now
        return null;
    }
    QName xsdReturnType = outputDefinition.getTypeName();
    Class<T> javaReturnType = XsdTypeMapper.toJavaType(xsdReturnType);
    if (javaReturnType == null) {
        javaReturnType = prismContext.getSchemaRegistry().getCompileTimeClass(xsdReturnType);
    }
    if (javaReturnType == null) {
        // TODO quick and dirty hack - because this could be because of enums defined in schema extension (MID-2399)
        // ...and enums (xsd:simpleType) are not parsed into ComplexTypeDefinitions
        javaReturnType = (Class) String.class;
    }
    List<V> pvals = new ArrayList<V>();
    // PrismProperty?
    if (evalRawResult instanceof Collection) {
        for (Object evalRawResultElement : (Collection) evalRawResult) {
            T evalResult = convertScalarResult(javaReturnType, additionalConvertor, evalRawResultElement, contextDescription);
            if (allowEmptyValues || !ExpressionUtil.isEmpty(evalResult)) {
                pvals.add((V) ExpressionUtil.convertToPrismValue(evalResult, outputDefinition, contextDescription, prismContext));
            }
        }
    } else if (evalRawResult instanceof PrismProperty<?>) {
        pvals.addAll((Collection<? extends V>) PrismPropertyValue.cloneCollection(((PrismProperty<T>) evalRawResult).getValues()));
    } else {
        T evalResult = convertScalarResult(javaReturnType, additionalConvertor, evalRawResult, contextDescription);
        if (allowEmptyValues || !ExpressionUtil.isEmpty(evalResult)) {
            pvals.add((V) ExpressionUtil.convertToPrismValue(evalResult, outputDefinition, contextDescription, prismContext));
        }
    }
    return pvals;
}
Also used : CompiledScript(javax.script.CompiledScript) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) Bindings(javax.script.Bindings) PrismProperty(com.evolveum.midpoint.prism.PrismProperty) Collection(java.util.Collection)

Example 24 with Bindings

use of javax.script.Bindings 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)

Example 25 with Bindings

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

the class OidcClaimsExtensionTest method testRequestedClaimsNoScope.

@Test
public void testRequestedClaimsNoScope() 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("openid"), requestedClaims);
    // When
    UserInfoClaims result = scriptEvaluator.evaluateScript(script, variables);
    // Then
    assertThat(result.getValues()).containsOnly(entry("given_name", "fred"), entry("family_name", "flintstone"));
}
Also used : Set(java.util.Set) HashMap(java.util.HashMap) UserInfoClaims(org.forgerock.oauth2.core.UserInfoClaims) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) Test(org.testng.annotations.Test)

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