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] + "'");
}
}
}
}
}
}
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;
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations