Search in sources :

Example 1 with ParseException

use of org.springframework.expression.ParseException in project spring-framework by spring-projects.

the class AbstractExpressionTests method parseAndCheckError.

/**
	 * Parse the specified expression and ensure the expected message comes out.
	 * The message may have inserts and they will be checked if otherProperties is specified.
	 * The first entry in otherProperties should always be the position.
	 * @param expression the expression to evaluate
	 * @param expectedMessage the expected message
	 * @param otherProperties the expected inserts within the message
	 */
protected void parseAndCheckError(String expression, SpelMessage expectedMessage, Object... otherProperties) {
    try {
        Expression expr = parser.parseExpression(expression);
        SpelUtilities.printAbstractSyntaxTree(System.out, expr);
        fail("Parsing should have failed!");
    } catch (ParseException pe) {
        SpelParseException ex = (SpelParseException) pe;
        if (ex.getMessageCode() != expectedMessage) {
            assertEquals("Failed to get expected message", expectedMessage, ex.getMessageCode());
        }
        if (otherProperties != null && otherProperties.length != 0) {
            // first one is expected position of the error within the string
            int pos = ((Integer) otherProperties[0]).intValue();
            assertEquals("Did not get correct position reported in error ", pos, ex.getPosition());
            if (otherProperties.length > 1) {
                // Check inserts match
                Object[] inserts = ex.getInserts();
                if (inserts == null) {
                    inserts = new Object[0];
                }
                if (inserts.length < otherProperties.length - 1) {
                    fail("Cannot check " + (otherProperties.length - 1) + " properties of the exception, it only has " + inserts.length + " inserts");
                }
                for (int i = 1; i < otherProperties.length; i++) {
                    if (!inserts[i - 1].equals(otherProperties[i])) {
                        fail("Insert does not match, expected '" + otherProperties[i] + "' but insert value was '" + inserts[i - 1] + "'");
                    }
                }
            }
        }
    }
}
Also used : Expression(org.springframework.expression.Expression) ParseException(org.springframework.expression.ParseException)

Example 2 with ParseException

use of org.springframework.expression.ParseException 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 3 with ParseException

use of org.springframework.expression.ParseException in project spring-security by spring-projects.

the class ExpressionBasedAnnotationAttributeFactory method createPreInvocationAttribute.

@Override
public PreInvocationAttribute createPreInvocationAttribute(String preFilterAttribute, String filterObject, String preAuthorizeAttribute) {
    try {
        // TODO: Optimization of permitAll
        ExpressionParser parser = getParser();
        Expression preAuthorizeExpression = (preAuthorizeAttribute != null) ? parser.parseExpression(preAuthorizeAttribute) : parser.parseExpression("permitAll");
        Expression preFilterExpression = (preFilterAttribute != null) ? parser.parseExpression(preFilterAttribute) : null;
        return new PreInvocationExpressionAttribute(preFilterExpression, filterObject, preAuthorizeExpression);
    } catch (ParseException ex) {
        throw new IllegalArgumentException("Failed to parse expression '" + ex.getExpressionString() + "'", ex);
    }
}
Also used : Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) ParseException(org.springframework.expression.ParseException)

Example 4 with ParseException

use of org.springframework.expression.ParseException in project spring-framework by spring-projects.

the class SetValueTests method setValue.

protected void setValue(String expression, Object value) {
    try {
        Expression e = parser.parseExpression(expression);
        assertThat(e).isNotNull();
        if (DEBUG) {
            SpelUtilities.printAbstractSyntaxTree(System.out, e);
        }
        StandardEvaluationContext lContext = TestScenarioCreator.getTestEvaluationContext();
        assertThat(e.isWritable(lContext)).as("Expression is not writeable but should be").isTrue();
        e.setValue(lContext, value);
        assertThat(e.getValue(lContext, value.getClass())).as("Retrieved value was not equal to set value").isEqualTo(value);
    } catch (EvaluationException | ParseException ex) {
        throw new AssertionError("Unexpected Exception: " + ex.getMessage(), ex);
    }
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) EvaluationException(org.springframework.expression.EvaluationException) ParseException(org.springframework.expression.ParseException)

Example 5 with ParseException

use of org.springframework.expression.ParseException in project spring-framework by spring-projects.

the class ExpressionLanguageScenarioTests method testScenario_UsingStandardInfrastructure.

/**
 * Scenario: using the standard infrastructure and running simple expression evaluation.
 */
@Test
public void testScenario_UsingStandardInfrastructure() {
    try {
        // Create a parser
        SpelExpressionParser parser = new SpelExpressionParser();
        // Parse an expression
        Expression expr = parser.parseRaw("new String('hello world')");
        // Evaluate it using a 'standard' context
        Object value = expr.getValue();
        // They are reusable
        value = expr.getValue();
        assertThat(value).isEqualTo("hello world");
        assertThat(value.getClass()).isEqualTo(String.class);
    } catch (EvaluationException | ParseException ex) {
        throw new AssertionError(ex.getMessage(), ex);
    }
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) EvaluationException(org.springframework.expression.EvaluationException) ParseException(org.springframework.expression.ParseException) Test(org.junit.jupiter.api.Test)

Aggregations

ParseException (org.springframework.expression.ParseException)11 Expression (org.springframework.expression.Expression)9 EvaluationException (org.springframework.expression.EvaluationException)4 ExpressionParser (org.springframework.expression.ExpressionParser)3 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)3 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Test (org.junit.jupiter.api.Test)2 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HierarchicalConfiguration (org.apache.commons.configuration2.HierarchicalConfiguration)1 Callback (org.craftercms.commons.lang.Callback)1 ConfigurationException (org.craftercms.engine.exception.ConfigurationException)1 SiteContext (org.craftercms.engine.service.context.SiteContext)1 ModelSyntaxException (org.ow2.proactive.scheduler.common.job.factories.spi.model.exceptions.ModelSyntaxException)1 SpelValidator (org.ow2.proactive.scheduler.common.job.factories.spi.model.validator.SpelValidator)1 ConfigAttribute (org.springframework.security.access.ConfigAttribute)1 FilterInvocation (org.springframework.security.web.FilterInvocation)1