use of javax.script.SimpleBindings in project OpenAM by OpenRock.
the class ScriptCondition method evaluate.
@Override
public ConditionDecision evaluate(String realm, Subject subject, String resourceName, Map<String, Set<String>> environment) throws EntitlementException {
try {
ScriptConfiguration configuration = getScriptConfiguration(realm);
if (configuration == null) {
throw new EntitlementException(EntitlementException.INVALID_SCRIPT_ID, scriptId);
}
ScriptObject script = new ScriptObject(configuration.getName(), configuration.getScript(), configuration.getLanguage());
Map<String, List<String>> advice = new HashMap<>();
Map<String, List<String>> responseAttributes = new HashMap<>();
Bindings scriptVariables = new SimpleBindings();
scriptVariables.put("logger", PolicyConstants.DEBUG);
scriptVariables.put("username", SubjectUtils.getPrincipalId(subject));
scriptVariables.put("resourceURI", resourceName);
scriptVariables.put("environment", environment);
scriptVariables.put("advice", advice);
scriptVariables.put("responseAttributes", responseAttributes);
scriptVariables.put("httpClient", getHttpClient(configuration.getLanguage()));
scriptVariables.put("authorized", Boolean.FALSE);
scriptVariables.put("ttl", Long.MAX_VALUE);
SSOToken ssoToken = SubjectUtils.getSSOToken(subject);
if (ssoToken != null) {
// If a token is present include the corresponding identity and session objects.
scriptVariables.put("identity", new ScriptedIdentity(coreWrapper.getIdentity(ssoToken)));
scriptVariables.put("session", new ScriptedSession(ssoToken));
}
evaluator.evaluateScript(script, scriptVariables);
boolean authorized = (Boolean) scriptVariables.get("authorized");
if (!authorized) {
return ConditionDecision.newFailureBuilder().setAdvice(transformMap(advice, LIST_TO_SET)).setResponseAttributes(transformMap(responseAttributes, LIST_TO_SET)).build();
}
long ttl = ((Number) scriptVariables.get("ttl")).longValue();
return ConditionDecision.newSuccessBuilder().setResponseAttributes(transformMap(responseAttributes, LIST_TO_SET)).setTimeToLive(ttl).build();
} catch (ScriptException | javax.script.ScriptException | IdRepoException | SSOException ex) {
throw new EntitlementException(EntitlementException.CONDITION_EVALUATION_FAILED, ex);
}
}
use of javax.script.SimpleBindings in project opennms by OpenNMS.
the class GraphMLEdgeStatusProvider method createBindings.
private SimpleBindings createBindings(GraphMLEdge edge) {
SimpleBindings bindings = new SimpleBindings();
bindings.put("edge", edge);
bindings.put("sourceNode", getNodeForEdgeVertexConnector(edge.getSource()));
bindings.put("targetNode", getNodeForEdgeVertexConnector(edge.getTarget()));
bindings.put("measurements", new MeasurementsWrapper(serviceAccessor.getMeasurementsService()));
bindings.put("nodeDao", serviceAccessor.getNodeDao());
bindings.put("snmpInterfaceDao", serviceAccessor.getSnmpInterfaceDao());
return bindings;
}
use of javax.script.SimpleBindings in project opennms by OpenNMS.
the class GraphMLEdgeStatusProvider method computeEdgeStatus.
private GraphMLEdgeStatus computeEdgeStatus(final List<StatusScript> scripts, final GraphMLEdge edge) {
return scripts.stream().flatMap(script -> {
final SimpleBindings bindings = createBindings(edge);
final StringWriter writer = new StringWriter();
final ScriptContext context = new SimpleScriptContext();
context.setWriter(writer);
context.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);
try {
LOG.debug("Executing script: {}", script);
final GraphMLEdgeStatus status = script.eval(context);
if (status != null) {
return Stream.of(status);
} else {
return Stream.empty();
}
} catch (final ScriptException e) {
LOG.error("Failed to execute script: {}", e);
return Stream.empty();
} finally {
LOG.info(writer.toString());
}
}).reduce(GraphMLEdgeStatus::merge).orElse(null);
}
use of javax.script.SimpleBindings in project logging-log4j2 by apache.
the class ScriptFilter method filter.
@Override
public Result filter(final Logger logger, final Level level, final Marker marker, final String msg, final Object... params) {
final SimpleBindings bindings = new SimpleBindings();
bindings.put("logger", logger);
bindings.put("level", level);
bindings.put("marker", marker);
bindings.put("message", new SimpleMessage(msg));
bindings.put("parameters", params);
bindings.put("throwable", null);
bindings.putAll(configuration.getProperties());
bindings.put("substitutor", configuration.getStrSubstitutor());
final Object object = configuration.getScriptManager().execute(script.getName(), bindings);
return object == null || !Boolean.TRUE.equals(object) ? onMismatch : onMatch;
}
use of javax.script.SimpleBindings in project logging-log4j2 by apache.
the class ScriptFilter method filter.
@Override
public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg, final Throwable t) {
final SimpleBindings bindings = new SimpleBindings();
bindings.put("logger", logger);
bindings.put("level", level);
bindings.put("marker", marker);
bindings.put("message", msg);
bindings.put("parameters", null);
bindings.put("throwable", t);
bindings.putAll(configuration.getProperties());
bindings.put("substitutor", configuration.getStrSubstitutor());
final Object object = configuration.getScriptManager().execute(script.getName(), bindings);
return object == null || !Boolean.TRUE.equals(object) ? onMismatch : onMatch;
}
Aggregations