Package org.springframework.expression

Examples of org.springframework.expression.Expression


    }

    @Test
    public void grantsAccessIfExpressionIsTrueDeniesIfFalse() {
        WebExpressionVoter voter = new WebExpressionVoter();
        Expression ex = mock(Expression.class);
        WebExpressionConfigAttribute weca = new WebExpressionConfigAttribute(ex);
        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);

        assertEquals(AccessDecisionVoter.ACCESS_GRANTED, voter.vote(user, fi, attributes));
View Full Code Here


        LinkedHashMap<MessageMatcher<?>, Collection<ConfigAttribute>> matcherToAttrs = new LinkedHashMap<MessageMatcher<?>, Collection<ConfigAttribute>>();

        for(Map.Entry<MessageMatcher<?>,String> entry : matcherToExpression.entrySet()) {
            MessageMatcher<?> matcher = entry.getKey();
            String rawExpression = entry.getValue();
            Expression expression = handler.getExpressionParser().parseExpression(rawExpression);
            ConfigAttribute attribute = new MessageExpressionConfigAttribute(expression);
            matcherToAttrs.put(matcher, Arrays.asList(attribute));
        }
        return new DefaultMessageSecurityMetadataSource(matcherToAttrs);
    }
View Full Code Here

    }

    @Test
    public void canCallMethodsOnVariables() throws Exception {
        ctx.setVariable("var", "somestring");
        Expression e = parser.parseExpression("#var.length() == 10");

        assertTrue(ExpressionUtils.evaluateAsBoolean(e, ctx));
    }
View Full Code Here

        root.setPermissionEvaluator(pe);
        when(pe.hasPermission(eq(user), eq(dummyDomainObject), any(Integer.class))).thenReturn(true)
                .thenReturn(true)
                .thenReturn(false);

        Expression e = parser.parseExpression("hasPermission(#domainObject, 0xA)");
        // evaluator returns true
        assertTrue(ExpressionUtils.evaluateAsBoolean(e, ctx));
        e = parser.parseExpression("hasPermission(#domainObject, 10)");
     // evaluator returns true
        assertTrue(ExpressionUtils.evaluateAsBoolean(e, ctx));
View Full Code Here

        root.setPermissionEvaluator(pe);
        when(pe.hasPermission(user, targetObject, i)).thenReturn(true)
                .thenReturn(false);
        when(pe.hasPermission(user, "x", i)).thenReturn(true);

        Expression e = parser.parseExpression("hasPermission(this, 2)");
        assertTrue(ExpressionUtils.evaluateAsBoolean(e, ctx));
        e = parser.parseExpression("hasPermission(this, 2)");
        assertFalse(ExpressionUtils.evaluateAsBoolean(e, ctx));

        e = parser.parseExpression("hasPermission(this.x, 2)");
View Full Code Here

    @Test
    public void createEvaluationContextCustomTrustResolver() {
        handler.setTrustResolver(trustResolver);

        Expression expression = handler.getExpressionParser().parseExpression("anonymous");
        EvaluationContext context = handler.createEvaluationContext(authentication, methodInvocation);
        expression.getValue(context, Boolean.class);

        verify(trustResolver).isAnonymous(authentication);
    }
View Full Code Here

    @Test
    public void proxyFactoryInterfaceAttributesFound() throws Exception {
        MockMethodInvocation mi = MethodInvocationFactory.createSec2150MethodInvocation();
        Collection<ConfigAttribute> attributes = mds.getAttributes(mi);
        assertThat(attributes.size()).isEqualTo(1);
        Expression expression = (Expression) ReflectionTestUtils.getField(attributes.iterator().next(),"authorizeExpression");
        assertThat(expression.getExpressionString()).isEqualTo("hasRole('ROLE_PERSON')");
    }
View Full Code Here

    @Test
    public void beanNamesAreCorrectlyResolved() throws Exception {
        handler.setApplicationContext(new AnnotationConfigApplicationContext(TestConfiguration.class));

        Expression expression = handler.getExpressionParser().parseExpression("@number10.compareTo(@number20) < 0");
        assertTrue((Boolean) expression.getValue(handler.createEvaluationContext(mock(Authentication.class), new Object())));
    }
View Full Code Here

  }

  private T processInternal(ParametersWrapper parameters) throws Exception {
    HandlerMethod candidate = this.findHandlerMethodForParameters(parameters);
    Assert.notNull(candidate, "No candidate methods found for messages.");
    Expression expression = candidate.getExpression();
    Class<?> expectedType = this.expectedType != null ? this.expectedType : candidate.method.getReturnType();
    try {
      @SuppressWarnings("unchecked")
      T result = (T) this.evaluateExpression(expression, parameters, expectedType);
      if (this.requiresReply) {
View Full Code Here

  public Object evaluate(String value, BeanExpressionContext evalContext) throws BeansException {
    if (!StringUtils.hasLength(value)) {
      return value;
    }
    try {
      Expression expr = this.expressionCache.get(value);
      if (expr == null) {
        expr = this.expressionParser.parseExpression(value, this.beanExpressionParserContext);
        this.expressionCache.put(value, expr);
      }
      StandardEvaluationContext sec = this.evaluationCache.get(evalContext);
      if (sec == null) {
        sec = new StandardEvaluationContext();
        sec.setRootObject(evalContext);
        sec.addPropertyAccessor(new BeanExpressionContextAccessor());
        sec.addPropertyAccessor(new BeanFactoryAccessor());
        sec.addPropertyAccessor(new MapAccessor());
        sec.addPropertyAccessor(new EnvironmentAccessor());
        sec.setBeanResolver(new BeanFactoryResolver(evalContext.getBeanFactory()));
        sec.setTypeLocator(new StandardTypeLocator(evalContext.getBeanFactory().getBeanClassLoader()));
        ConversionService conversionService = evalContext.getBeanFactory().getConversionService();
        if (conversionService != null) {
          sec.setTypeConverter(new StandardTypeConverter(conversionService));
        }
        customizeEvaluationContext(sec);
        this.evaluationCache.put(evalContext, sec);
      }
      return expr.getValue(sec);
    }
    catch (Exception ex) {
      throw new BeanExpressionException("Expression parsing failed", ex);
    }
  }
View Full Code Here

TOP

Related Classes of org.springframework.expression.Expression

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.