use of org.springframework.expression.Expression in project spring-security by spring-projects.
the class WebExpressionVoterTests method grantsAccessIfExpressionIsTrueDeniesIfFalse.
@Test
public void grantsAccessIfExpressionIsTrueDeniesIfFalse() {
WebExpressionVoter voter = new WebExpressionVoter();
Expression ex = mock(Expression.class);
EvaluationContextPostProcessor postProcessor = mock(EvaluationContextPostProcessor.class);
when(postProcessor.postProcess(any(EvaluationContext.class), any(FilterInvocation.class))).thenAnswer(new Answer<EvaluationContext>() {
public EvaluationContext answer(InvocationOnMock invocation) throws Throwable {
return invocation.getArgumentAt(0, EvaluationContext.class);
}
});
WebExpressionConfigAttribute weca = new WebExpressionConfigAttribute(ex, postProcessor);
EvaluationContext ctx = mock(EvaluationContext.class);
SecurityExpressionHandler eh = mock(SecurityExpressionHandler.class);
FilterInvocation fi = new FilterInvocation("/path", "GET");
voter.setExpressionHandler(eh);
when(eh.createEvaluationContext(user, fi)).thenReturn(ctx);
when(ex.getValue(ctx, Boolean.class)).thenReturn(Boolean.TRUE).thenReturn(Boolean.FALSE);
ArrayList attributes = new ArrayList();
attributes.addAll(SecurityConfig.createList("A", "B", "C"));
attributes.add(weca);
assertThat(voter.vote(user, fi, attributes)).isEqualTo(AccessDecisionVoter.ACCESS_GRANTED);
// Second time false
assertThat(voter.vote(user, fi, attributes)).isEqualTo(AccessDecisionVoter.ACCESS_DENIED);
}
use of org.springframework.expression.Expression in project spring-boot by spring-projects.
the class BindingPreparationTests method testExpressionLists.
@Test
@Ignore("Work in progress")
public void testExpressionLists() throws Exception {
TargetWithNestedMapOfListOfString target = new TargetWithNestedMapOfListOfString();
LinkedHashMap<String, List<String>> map = new LinkedHashMap<>();
// map.put("foo", Arrays.asList("bar"));
target.setNested(map);
SpelExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext(target);
context.addPropertyAccessor(new MapAccessor());
Expression expression = parser.parseExpression("nested.foo");
assertThat(expression.getValue(context)).isNotNull();
}
use of org.springframework.expression.Expression in project camel by apache.
the class SpelExpression method parseExpression.
private Expression parseExpression() {
// Support template parsing with #{ } delimiters
ParserContext parserContext = new TemplateParserContext();
Expression expression = expressionParser.parseExpression(expressionString, parserContext);
return expression;
}
use of org.springframework.expression.Expression in project camel by apache.
the class SpelExpression method evaluate.
public <T> T evaluate(Exchange exchange, Class<T> tClass) {
try {
Expression expression = parseExpression();
EvaluationContext evaluationContext = createEvaluationContext(exchange);
Object value = expression.getValue(evaluationContext);
// Let Camel handle the type conversion
return exchange.getContext().getTypeConverter().convertTo(tClass, value);
} catch (Exception e) {
throw new ExpressionEvaluationException(this, exchange, e);
}
}
use of org.springframework.expression.Expression in project opennms by OpenNMS.
the class PersistRegexSelectorStrategy method shouldPersist.
@Override
public boolean shouldPersist(CollectionResource resource) {
LOG.debug("shouldPersist: checking resource {}", resource);
if (m_parameterCollection == null) {
LOG.warn("shouldPersist: no parameters defined; the resource will be persisted.");
return true;
}
EvaluatorContextVisitor visitor = new EvaluatorContextVisitor();
resource.visit(visitor);
ExpressionParser parser = new SpelExpressionParser();
for (Parameter param : m_parameterCollection) {
if (param.getKey().equals(MATCH_EXPRESSION)) {
Expression exp = parser.parseExpression(param.getValue());
boolean shouldPersist = false;
try {
shouldPersist = (Boolean) exp.getValue(visitor.getEvaluationContext(), Boolean.class);
} catch (Exception e) {
LOG.warn("shouldPersist: can't evaluate expression {} for resource {} because: {}", param.getValue(), resource, e.getMessage());
}
LOG.debug("shouldPersist: checking {} ? {}", param.getValue(), shouldPersist);
if (shouldPersist)
return true;
}
}
return false;
}
Aggregations