Search in sources :

Example 41 with ExpressionParser

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

the class BSFEngineHandler method accepts.

/**
 * Accepts.
 * <p>If the engine doesn't have filter, the method will return true.</p>
 * <p>If the method has a filter, it will be evaluated.</p>
 *
 * @param alarm the alarm
 * @return true, if successful
 */
public boolean accepts(NorthboundAlarm alarm) {
    if (getFilter() != null) {
        StandardEvaluationContext context = new StandardEvaluationContext(alarm);
        ExpressionParser parser = new SpelExpressionParser();
        Expression exp = parser.parseExpression(m_filter);
        boolean passed = false;
        try {
            passed = (Boolean) exp.getValue(context, Boolean.class);
        } catch (Exception e) {
            LOG.warn("accepts: can't evaluate expression {} for alarm {} because: {}", getFilter(), alarm.getUei(), e.getMessage());
        }
        LOG.debug("accepts: checking {} ? {}", m_filter, passed);
        return passed;
    }
    return true;
}
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 42 with ExpressionParser

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

the class VarbindMapping method evaluate.

/**
 * Evaluates a SPEL expression based on a given northbound alarm
 *
 * @param expression the expression
 * @param alarm the northbound alarm
 * @return the string
 */
private String evaluate(String expression, NorthboundAlarm alarm) {
    if (expression == null) {
        return null;
    }
    try {
        StandardEvaluationContext context = new StandardEvaluationContext(alarm);
        ExpressionParser parser = new SpelExpressionParser();
        Expression exp = parser.parseExpression(expression);
        return (String) exp.getValue(context, String.class);
    } catch (Exception e) {
        LOG.warn("Can't evaluate expression {} for alarm {} because: {}", getValue(), alarm.getUei(), e.getMessage());
    }
    return null;
}
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 43 with ExpressionParser

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

the class SyslogEventForwarder method getTranslatedMessage.

/**
 * Gets the translated message.
 *
 * @param event the event
 * @param node the node
 * @param msgFormat the message format
 * @return the translated message
 */
private String getTranslatedMessage(Event event, OnmsNode node, String msgFormat) {
    StandardEvaluationContext context = new StandardEvaluationContext(event);
    if (node != null) {
        context.setVariable("node", node);
    }
    ExpressionParser parser = new SpelExpressionParser();
    Expression exp = parser.parseExpression(msgFormat, new TemplateParserContext("${", "}"));
    try {
        final String msg = (String) exp.getValue(context, String.class);
        LOG.debug("getTranslatedMessage: {} ==> {}", msgFormat, msg);
        return msg;
    } catch (Exception e) {
        LOG.warn("getTranslatedMessage: can't evaluate expression {} for alarm {} because: {}", msgFormat, event.getUei(), e.getMessage());
    }
    return null;
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) TemplateParserContext(org.springframework.expression.common.TemplateParserContext) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) IOException(java.io.IOException) SyslogRuntimeException(org.graylog2.syslog4j.SyslogRuntimeException)

Example 44 with ExpressionParser

use of org.springframework.expression.ExpressionParser in project engine by craftercms.

the class ConfigAwareUrlAccessRestrictionCheckingProcessor method getUrlRestrictions.

@Override
@SuppressWarnings("unchecked")
protected Map<String, Expression> getUrlRestrictions() {
    Callback<Map<String, Expression>> callback = new Callback<Map<String, Expression>>() {

        @Override
        public Map<String, Expression> execute() {
            HierarchicalConfiguration config = ConfigUtils.getCurrentConfig();
            Map<String, Expression> customRestrictions = null;
            if (config != null) {
                List<HierarchicalConfiguration> restrictionsConfig = config.configurationsAt(URL_RESTRICTION_KEY);
                if (CollectionUtils.isNotEmpty(restrictionsConfig)) {
                    customRestrictions = new LinkedHashMap<>(restrictionsConfig.size());
                    ExpressionParser parser = new SpelExpressionParser();
                    for (HierarchicalConfiguration restrictionConfig : restrictionsConfig) {
                        String url = restrictionConfig.getString(URL_RESTRICTION_URL_KEY);
                        String expression = restrictionConfig.getString(URL_RESTRICTION_EXPRESSION_KEY);
                        if (StringUtils.isNotEmpty(url) && StringUtils.isNotEmpty(expression)) {
                            try {
                                customRestrictions.put(url, parser.parseExpression(expression));
                            } catch (ParseException e) {
                                throw new ConfigurationException(expression + " is not a valid SpEL expression", e);
                            }
                        }
                    }
                }
            }
            if (customRestrictions != null) {
                return customRestrictions;
            } else {
                return urlRestrictions;
            }
        }
    };
    SiteContext siteContext = SiteContext.getCurrent();
    if (siteContext != null) {
        return cacheTemplate.getObject(siteContext.getContext(), callback, URL_RESTRICTIONS_CACHE_KEY);
    } else {
        return urlRestrictions;
    }
}
Also used : SiteContext(org.craftercms.engine.service.context.SiteContext) HierarchicalConfiguration(org.apache.commons.configuration2.HierarchicalConfiguration) Callback(org.craftercms.commons.lang.Callback) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) ConfigurationException(org.craftercms.engine.exception.ConfigurationException) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) ParseException(org.springframework.expression.ParseException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 45 with ExpressionParser

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

the class DroolsEngine method accepts.

/**
 * Accepts.
 * <p>If the engine doesn't have filter, the method will return true.</p>
 * <p>If the method has a filter, it will be evaluated.</p>
 *
 * @param alarm the alarm
 * @return true, if successful
 */
public boolean accepts(NorthboundAlarm alarm) {
    if (getFilter() != null) {
        StandardEvaluationContext context = new StandardEvaluationContext(alarm);
        ExpressionParser parser = new SpelExpressionParser();
        Expression exp = parser.parseExpression(m_filter);
        boolean passed = false;
        try {
            passed = (Boolean) exp.getValue(context, Boolean.class);
        } catch (Exception e) {
            LOG.warn("accepts: can't evaluate expression {} for alarm {} because: {}", getFilter(), alarm.getUei(), e.getMessage());
        }
        LOG.debug("accepts: checking {} ? {}", m_filter, passed);
        return passed;
    }
    return true;
}
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)

Aggregations

ExpressionParser (org.springframework.expression.ExpressionParser)92 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)89 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)81 Expression (org.springframework.expression.Expression)78 Test (org.junit.Test)76 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)51 EvaluationContext (org.springframework.expression.EvaluationContext)10 BigInteger (java.math.BigInteger)6 Map (java.util.Map)5 BigDecimal (java.math.BigDecimal)4 HashMap (java.util.HashMap)4 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)3 TreeMap (java.util.TreeMap)3 ParseException (org.springframework.expression.ParseException)3 IOException (java.io.IOException)2 Method (java.lang.reflect.Method)2 List (java.util.List)2 SyslogRuntimeException (org.graylog2.syslog4j.SyslogRuntimeException)2 EvaluationException (org.springframework.expression.EvaluationException)2