Search in sources :

Example 31 with Expression

use of org.springframework.expression.Expression in project spring-framework by spring-projects.

the class SetValueTests method setValue.

/**
	 * For use when coercion is happening during a setValue().  The expectedValue should be
	 * the coerced form of the value.
	 */
protected void setValue(String expression, Object value, Object expectedValue) {
    try {
        Expression e = parser.parseExpression(expression);
        if (e == null) {
            fail("Parser returned null for expression");
        }
        if (DEBUG) {
            SpelUtilities.printAbstractSyntaxTree(System.out, e);
        }
        StandardEvaluationContext lContext = TestScenarioCreator.getTestEvaluationContext();
        assertTrue("Expression is not writeable but should be", e.isWritable(lContext));
        e.setValue(lContext, value);
        Object a = expectedValue;
        Object b = e.getValue(lContext);
        if (!a.equals(b)) {
            fail("Not the same: [" + a + "] type=" + a.getClass() + "  [" + b + "] type=" + b.getClass());
        //				assertEquals("Retrieved value was not equal to set value", expectedValue, e.getValue(lContext));
        }
    } catch (EvaluationException ee) {
        ee.printStackTrace();
        fail("Unexpected Exception: " + ee.getMessage());
    } catch (ParseException pe) {
        pe.printStackTrace();
        fail("Unexpected Exception: " + pe.getMessage());
    }
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) EvaluationException(org.springframework.expression.EvaluationException) ParseException(org.springframework.expression.ParseException)

Example 32 with Expression

use of org.springframework.expression.Expression in project opennms by OpenNMS.

the class ResponseHandlingUtils method matchesFilter.

public static boolean matchesFilter(String spelFilter, ListMultimap<String, String> valuesByName) {
    // Compile the expression
    final ExpressionParser parser = new SpelExpressionParser();
    final Expression exp = parser.parseExpression(spelFilter);
    // Build the context with the first values for all of the attributes
    final StandardEvaluationContext context = new StandardEvaluationContext();
    for (String name : valuesByName.keySet()) {
        final List<String> values = valuesByName.get(name);
        if (values.size() > 0) {
            context.setVariable(name, values.get(0));
        }
    }
    // Evaluate our expression
    try {
        return exp.getValue(context, Boolean.class);
    } catch (Exception e) {
        LOG.error("Failed to evaluate expression {}. Assuming match is negative. Msg: {}", exp.getExpressionString(), e.getMessage());
        throw Throwables.propagate(e);
    }
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) NoSuchElementException(java.util.NoSuchElementException)

Example 33 with Expression

use of org.springframework.expression.Expression in project opennms by OpenNMS.

the class ResponseHandlingUtils method getMatchingIndex.

public static int getMatchingIndex(String spelExpression, ListMultimap<String, String> values) throws NoSuchElementException {
    // Compile the expression
    final ExpressionParser parser = new SpelExpressionParser();
    final Expression exp = parser.parseExpression(spelExpression);
    // Recursively check all levels, defaulting to the first element
    return getMatchingIndex(exp, values, 0);
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser)

Example 34 with Expression

use of org.springframework.expression.Expression in project opennms by OpenNMS.

the class WSManDataCollectionConfigDaoJaxb method isAgentSupportedBySystemDefinition.

public static boolean isAgentSupportedBySystemDefinition(SystemDefinition sysDef, CollectionAgent agent, WsmanAgentConfig agentConfig, OnmsNode node) {
    // Determine the effective values for the productVendor and productVersion:
    // The detected values are stored in the assets table, we allow these
    // to be overridden by the agent specific configuration
    String productVendor;
    if (agentConfig.getProductVendor() != null) {
        productVendor = agentConfig.getProductVendor();
    } else {
        productVendor = node.getAssetRecord().getVendor();
        // Guarantee that the values are non-null
        productVendor = Strings.nullToEmpty(productVendor);
    }
    String productVersion;
    if (agentConfig.getProductVersion() != null) {
        productVersion = agentConfig.getProductVersion();
    } else {
        productVersion = node.getAssetRecord().getModelNumber();
        // Guarantee that the values are non-null
        productVersion = Strings.nullToEmpty(productVersion);
    }
    // Build the evaluation context
    StandardEvaluationContext context = new StandardEvaluationContext(node);
    // Add the agent, so that the rule can determine the IP address in question, if required
    context.setVariable("agent", agent);
    context.setVariable("productVendor", productVendor);
    context.setVariable("productVersion", productVersion);
    // Evaluate the rules. Multiple rules are logically ORed.
    for (String rule : sysDef.getRule()) {
        ExpressionParser parser = new SpelExpressionParser();
        Expression exp = parser.parseExpression(rule);
        boolean passed = false;
        try {
            passed = exp.getValue(context, Boolean.class);
        } catch (Exception e) {
            LOG.error("Failed to evaluate expression {} for agent {} with context {}. System defintion with name {} will not be used. Msg: {}", rule, agent, context, sysDef.getName(), e.getMessage());
        }
        LOG.debug("Rule '{}' on {} passed? {}", rule, agent, passed);
        if (passed) {
            return true;
        }
    }
    return false;
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser)

Example 35 with Expression

use of org.springframework.expression.Expression in project uPortal by Jasig.

the class StaticTransformerConfigurationSource method setParameterExpressions.

public void setParameterExpressions(Map<String, String> parameterExpressions) {
    final Map<String, Expression> parameterExpressionsBuilder = new LinkedHashMap<String, Expression>();
    for (final Map.Entry<String, String> expressionEntry : parameterExpressions.entrySet()) {
        final String string = expressionEntry.getValue();
        final Expression expression = this.portalSpELService.parseExpression(string);
        parameterExpressionsBuilder.put(expressionEntry.getKey(), expression);
    }
    this.parameterExpressions = parameterExpressionsBuilder;
}
Also used : Expression(org.springframework.expression.Expression) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

Expression (org.springframework.expression.Expression)299 Test (org.junit.Test)228 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)179 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)159 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)114 ExpressionParser (org.springframework.expression.ExpressionParser)78 EvaluationContext (org.springframework.expression.EvaluationContext)53 ArrayList (java.util.ArrayList)32 HashMap (java.util.HashMap)19 CompoundExpression (org.springframework.expression.spel.ast.CompoundExpression)18 EvaluationException (org.springframework.expression.EvaluationException)17 Authentication (org.springframework.security.core.Authentication)16 Map (java.util.Map)14 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)14 List (java.util.List)13 LinkedHashMap (java.util.LinkedHashMap)11 ParseException (org.springframework.expression.ParseException)11 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)10 MethodInvocation (org.aopalliance.intercept.MethodInvocation)9 SimpleMethodInvocation (org.springframework.security.util.SimpleMethodInvocation)9