Search in sources :

Example 56 with SimpleBindings

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);
    }
}
Also used : ScriptObject(org.forgerock.openam.scripting.ScriptObject) SSOToken(com.iplanet.sso.SSOToken) HashMap(java.util.HashMap) IdRepoException(com.sun.identity.idm.IdRepoException) SSOException(com.iplanet.sso.SSOException) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) EntitlementException(com.sun.identity.entitlement.EntitlementException) ScriptException(org.forgerock.openam.scripting.ScriptException) SimpleBindings(javax.script.SimpleBindings) ScriptConfiguration(org.forgerock.openam.scripting.service.ScriptConfiguration) List(java.util.List) ScriptedSession(org.forgerock.openam.scripting.api.ScriptedSession) ScriptedIdentity(org.forgerock.openam.scripting.api.ScriptedIdentity)

Example 57 with SimpleBindings

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;
}
Also used : SimpleBindings(javax.script.SimpleBindings) MeasurementsWrapper(org.opennms.features.topology.api.info.MeasurementsWrapper)

Example 58 with SimpleBindings

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);
}
Also used : ScriptException(javax.script.ScriptException) StringWriter(java.io.StringWriter) SimpleScriptContext(javax.script.SimpleScriptContext) SimpleBindings(javax.script.SimpleBindings) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext)

Example 59 with SimpleBindings

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;
}
Also used : SimpleBindings(javax.script.SimpleBindings) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage)

Example 60 with SimpleBindings

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;
}
Also used : SimpleBindings(javax.script.SimpleBindings)

Aggregations

SimpleBindings (javax.script.SimpleBindings)102 Bindings (javax.script.Bindings)65 ScriptEngine (javax.script.ScriptEngine)23 Test (org.junit.Test)22 ScriptContext (javax.script.ScriptContext)17 ScriptException (javax.script.ScriptException)17 ScriptEngineManager (javax.script.ScriptEngineManager)16 SimpleScriptContext (javax.script.SimpleScriptContext)15 Test (org.testng.annotations.Test)11 CompiledScript (javax.script.CompiledScript)10 PrintWriter (java.io.PrintWriter)9 HashMap (java.util.HashMap)8 Map (java.util.Map)8 SlingBindings (org.apache.sling.api.scripting.SlingBindings)7 File (java.io.File)6 StringWriter (java.io.StringWriter)5 ArrayList (java.util.ArrayList)4 XMLEventReader (javax.xml.stream.XMLEventReader)4 XMLEventWriter (javax.xml.stream.XMLEventWriter)4 XMLInputFactory (javax.xml.stream.XMLInputFactory)4