Search in sources :

Example 26 with Expression

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);
}
Also used : SecurityExpressionHandler(org.springframework.security.access.expression.SecurityExpressionHandler) Expression(org.springframework.expression.Expression) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ArrayList(java.util.ArrayList) EvaluationContext(org.springframework.expression.EvaluationContext) FilterInvocation(org.springframework.security.web.FilterInvocation) Test(org.junit.Test)

Example 27 with Expression

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();
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) ArrayList(java.util.ArrayList) List(java.util.List) MapAccessor(org.springframework.context.expression.MapAccessor) LinkedHashMap(java.util.LinkedHashMap) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 28 with Expression

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;
}
Also used : Expression(org.springframework.expression.Expression) TemplateParserContext(org.springframework.expression.common.TemplateParserContext) ParserContext(org.springframework.expression.ParserContext) TemplateParserContext(org.springframework.expression.common.TemplateParserContext)

Example 29 with 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);
    }
}
Also used : ExpressionEvaluationException(org.apache.camel.ExpressionEvaluationException) Expression(org.springframework.expression.Expression) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) EvaluationContext(org.springframework.expression.EvaluationContext) ExpressionEvaluationException(org.apache.camel.ExpressionEvaluationException)

Example 30 with Expression

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;
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) Parameter(org.opennms.netmgt.collection.api.Parameter) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser)

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