/** * Evaluates the given expression using the given variables as context variables. * * @param <T> The desired type * @param expression The expression to evaluate * @param variables The variables to put into context * @return The result of the evaluation */ public <T> T evaluate(Expression expression, Map<String, Object> variables) { return evaluate(expression, null, variables); } }
/** * Evaluates the given expression string using the given variables as context variables. * * @param <T> The desired type * @param expressionString Expression string to evaluate * @param variables The variables to put into context * @return The result of the evaluation */ public <T> T evaluate(String expressionString, Map<String, Object> variables) { return evaluate(parseExpression(expressionString), variables); }
/** * Evaluates the given expression using the given variables as context variables. * * @param <T> The desired type * @param expression The expression to evaluate * @param variables The variables to put into context * @return The result of the evaluation */ public <T> T evaluate(Expression expression, Map<String, Object> variables) { return evaluate(expression, null, variables); } }
/** * Evaluates the given expression string using the given variables as context variables. * * @param <T> The desired type * @param expressionString Expression string to evaluate * @param variables The variables to put into context * @return The result of the evaluation */ public <T> T evaluate(String expressionString, Map<String, Object> variables) { return evaluate(parseExpression(expressionString), variables); }
/** * Checks whether security is enabled based on SpEL expression defined in environment. * * @param request {@link HttpServletRequest} to determine whether security is enabled. * * @return true if security is enabled, false if disabled. */ public boolean isSecurityEnabled(HttpServletRequest request) { Boolean isSecurityEnabled = true; String enableSecuritySpelExpression = configurationHelper.getProperty(ConfigurationValue.SECURITY_ENABLED_SPEL_EXPRESSION); if (StringUtils.isNotBlank(enableSecuritySpelExpression)) { Map<String, Object> variables = new HashMap<>(); variables.put("request", request); isSecurityEnabled = spelExpressionHelper.evaluate(enableSecuritySpelExpression, Boolean.class, variables); } return isSecurityEnabled; }
/** * Evaluates the given expression string using the given variables as context variables. * * @param <T> The desired type * @param expressionString Expression string to evaluate * @param desiredResultType The desired type of the result. Spring will make best attempt at converting result to this type. * @param variables The variables to put into context * @return The result of the evaluation */ public <T> T evaluate(String expressionString, Class<T> desiredResultType, Map<String, Object> variables) { return evaluate(parseExpression(expressionString), desiredResultType, variables); }
namespaceSecurityHelper.checkPermission(spelExpressionHelper.evaluate(field, Object.class, variables), namespacePermission .permissions());
/** * Evaluates the given expression string using the given variables as context variables. * * @param <T> The desired type * @param expressionString Expression string to evaluate * @param desiredResultType The desired type of the result. Spring will make best attempt at converting result to this type. * @param variables The variables to put into context * @return The result of the evaluation */ public <T> T evaluate(String expressionString, Class<T> desiredResultType, Map<String, Object> variables) { return evaluate(parseExpression(expressionString), desiredResultType, variables); }
namespaceSecurityHelper.checkPermission(spelExpressionHelper.evaluate(field, Object.class, variables), namespacePermission .permissions());
@Test public void evaluateAssertNoErrorsOnValidExpression() { String expressionString = "#foo"; Map<String, Object> variables = new HashMap<>(); variables.put("foo", "bar"); assertEquals("bar", spelExpressionHelper.evaluate(expressionString, variables)); }
@Test public void evaluateAssertIllegalArgumentOnInvalidExpression() { String expressionString = "this is an invalid expression"; try { spelExpressionHelper.evaluate(expressionString, null); fail(); } catch (Exception e) { assertEquals(IllegalArgumentException.class, e.getClass()); assertEquals("Error parsing SpEL \"this is an invalid expression\"", e.getMessage()); } } }