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());
}
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));
}
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;
}
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");
}
}
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"));
}
Aggregations