Remove OGNL expression support

Issue: SWF-1694
This commit is contained in:
Rossen Stoyanchev
2017-01-06 16:02:53 -05:00
parent 4fb1b99a8b
commit b263c1c87a
14 changed files with 22 additions and 980 deletions

View File

@@ -17,8 +17,8 @@ package org.springframework.binding.expression;
/**
* An expression capable of evaluating itself against context objects. Encapsulates the details of a previously parsed
* expression string. Provides a common abstraction for expression evaluation independent of any language like OGNL or
* the Unified EL.
* expression string. Provides a common abstraction for expression evaluation independent of any language like
* Spring EL or the Unified EL.
*
* @author Keith Donald
*/

View File

@@ -1,184 +0,0 @@
/*
* Copyright 2004-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.binding.expression.ognl;
import java.lang.reflect.Member;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import ognl.NoSuchPropertyException;
import ognl.Ognl;
import ognl.OgnlException;
import ognl.TypeConverter;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.BeansException;
import org.springframework.beans.InvalidPropertyException;
import org.springframework.binding.convert.ConversionException;
import org.springframework.binding.convert.ConversionService;
import org.springframework.binding.expression.EvaluationException;
import org.springframework.binding.expression.Expression;
import org.springframework.binding.expression.PropertyNotFoundException;
import org.springframework.binding.expression.ValueCoercionException;
import org.springframework.binding.expression.spel.SpringELExpression;
/**
* Evaluates a parsed Ognl expression.
*
* @author Keith Donald
* @author Scott Andrews
*
* @deprecated in favor of Spring EL, see {@link SpringELExpression}.
*/
class OgnlExpression implements Expression {
private Object expression;
private Map<String, Expression> variableExpressions;
private Class<?> expectedResultType;
private String expressionString;
private ConversionService conversionService;
/**
* Creates a new OGNL expression.
*/
public OgnlExpression(Object expression, Map<String, Expression> variableExpressions, Class<?> expectedResultType,
String expressionString, ConversionService conversionService) {
this.expression = expression;
this.variableExpressions = variableExpressions;
this.expectedResultType = expectedResultType;
this.expressionString = expressionString;
this.conversionService = conversionService;
}
public boolean equals(Object o) {
if (!(o instanceof OgnlExpression)) {
return false;
}
OgnlExpression other = (OgnlExpression) o;
return expressionString.equals(other.expressionString);
}
public int hashCode() {
return expressionString.hashCode();
}
@SuppressWarnings("rawtypes")
public Object getValue(Object context) throws EvaluationException {
try {
Map evaluationContext = Ognl.addDefaultContext(context, getVariables(context));
Ognl.setTypeConverter(evaluationContext, createTypeConverter());
return Ognl.getValue(expression, evaluationContext, context, expectedResultType);
} catch (NoSuchPropertyException e) {
throw new PropertyNotFoundException(context.getClass(), getExpressionString(), e);
} catch (OgnlException e) {
if (e.getReason() instanceof ValueCoercionException) {
throw (ValueCoercionException) e.getReason();
} else {
throw new EvaluationException(context.getClass(), getExpressionString(),
"An OgnlException occurred getting the value for expression '" + getExpressionString()
+ "' on context [" + context.getClass() + "]", causeFor(e));
}
}
}
@SuppressWarnings("rawtypes")
public void setValue(Object context, Object value) {
try {
Map evaluationContext = Ognl.addDefaultContext(context, getVariables(context));
Ognl.setTypeConverter(evaluationContext, createTypeConverter());
Ognl.setValue(expression, evaluationContext, context, value);
} catch (NoSuchPropertyException e) {
throw new PropertyNotFoundException(context.getClass(), getExpressionString(), e);
} catch (OgnlException e) {
if (e.getReason() instanceof ValueCoercionException) {
throw (ValueCoercionException) e.getReason();
} else {
throw new EvaluationException(context.getClass(), getExpressionString(),
"An OgnlException occurred setting the value of expression '" + getExpressionString()
+ "' on context [" + context.getClass() + "] to [" + value + "]", causeFor(e));
}
}
}
public Class<?> getValueType(Object context) {
try {
// OGNL has no native way to get this information
return new BeanWrapperImpl(context).getPropertyType(expressionString);
} catch (InvalidPropertyException e) {
throw new PropertyNotFoundException(context.getClass(), getExpressionString(), e);
} catch (BeansException e) {
throw new EvaluationException(context.getClass(), getExpressionString(),
"An BeansException occurred getting the value type for expression '" + getExpressionString()
+ "' on context [" + context.getClass() + "]", e);
}
}
public String getExpressionString() {
return expressionString;
}
// internal helpers
private Throwable causeFor(OgnlException e) {
if (e.getReason() != null) {
if (e.getCause() == null) {
try {
e.initCause(e.getReason());
} catch (IllegalStateException ex) {
// we tried
}
}
return e;
} else {
return e;
}
}
@SuppressWarnings("rawtypes")
private TypeConverter createTypeConverter() {
return new TypeConverter() {
public Object convertValue(Map context, Object target, Member member, String propertyName, Object value,
Class toType) throws ValueCoercionException {
try {
return conversionService.executeConversion(value, toType);
} catch (ConversionException e) {
throw new ValueCoercionException(context.getClass(), expressionString, value, toType, e);
}
}
};
}
private Map<String, Object> getVariables(Object context) {
if (variableExpressions == null) {
return Collections.emptyMap();
}
Map<String, Object> variables = new HashMap<String, Object>(variableExpressions.size(), 1);
for (Map.Entry<String, Expression> var : variableExpressions.entrySet()) {
Expression valueExpression = var.getValue();
variables.put(var.getKey(), valueExpression.getValue(context));
}
return variables;
}
public String toString() {
return expressionString;
}
}

View File

@@ -1,76 +0,0 @@
/*
* Copyright 2004-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.binding.expression.ognl;
import ognl.Ognl;
import ognl.OgnlException;
import ognl.OgnlRuntime;
import ognl.PropertyAccessor;
import org.springframework.binding.convert.ConversionService;
import org.springframework.binding.convert.service.DefaultConversionService;
import org.springframework.binding.expression.Expression;
import org.springframework.binding.expression.ParserContext;
import org.springframework.binding.expression.ParserException;
import org.springframework.binding.expression.spel.SpringELExpressionParser;
import org.springframework.binding.expression.support.AbstractExpressionParser;
/**
* An expression parser that parses Ognl expressions.
*
* @author Keith Donald
*
* @deprecated in favor of Spring EL, see {@link SpringELExpressionParser}
*/
public class OgnlExpressionParser extends AbstractExpressionParser {
private ConversionService conversionService = new DefaultConversionService();
/**
* The conversion service to use to perform type conversions as needed by the OGNL system. If not specified, the
* default is an instance of {@link DefaultConversionService}.
*/
public ConversionService getConversionService() {
return conversionService;
}
/**
* Sets the conversion service to use to perform type conversions as needed by the OGNL system.
* @param conversionService the conversion service to use
*/
public void setConversionService(ConversionService conversionService) {
this.conversionService = conversionService;
}
/**
* Add a property access strategy for the given class.
* @param clazz the class that contains properties needing access
* @param propertyAccessor the property access strategy
*/
public void addPropertyAccessor(Class<?> clazz, PropertyAccessor propertyAccessor) {
OgnlRuntime.setPropertyAccessor(clazz, propertyAccessor);
}
protected Expression doParseExpression(String expressionString, ParserContext context) throws ParserException {
try {
return new OgnlExpression(Ognl.parseExpression(expressionString),
parseVariableExpressions(context.getExpressionVariables()),
context.getExpectedEvaluationResultType(), expressionString, conversionService);
} catch (OgnlException e) {
throw new ParserException(expressionString, e);
}
}
}

View File

@@ -1,21 +0,0 @@
/*
* Copyright 2004-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Support for the OGNL Expression Language implemented by the OgnlExpressionParser.
*/
package org.springframework.binding.expression.ognl;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2004-2012 the original author or authors.
* Copyright 2004-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -28,9 +28,10 @@ import org.springframework.binding.expression.ParserException;
import org.springframework.util.Assert;
/**
* An expression parser that parses Ognl expressions.
* Abstract base class for parsing ${...} style expressions.
*
* @author Keith Donald
* @see org.springframework.binding.expression.beanwrapper.BeanWrapperExpressionParser
*/
public abstract class AbstractExpressionParser implements ExpressionParser {
@@ -117,14 +118,15 @@ public abstract class AbstractExpressionParser implements ExpressionParser {
if (!allowDelimitedEvalExpressions) {
throw new ParserException(
expressionString,
"The expression '"
+ expressionString
+ "' being parsed is expected be a standard OGNL expression. Do not attempt to enclose such expression strings in ${} delimiters--this is redundant. If you need to parse a template that mixes literal text with evaluatable blocks, set the 'template' parser context attribute to true.",
"The expression '" + expressionString + "' being parsed is expected be an expression. " +
"Do not enclose such expression strings in ${} delimiters as it's redundant. " +
"If you need to parse a template that mixes literal text with evaluatable blocks, " +
"set the 'template' parser context attribute to true.",
null);
} else {
int lastIndex = expressionString.length() - getExpressionSuffix().length();
String ognlExpression = expressionString.substring(getExpressionPrefix().length(), lastIndex);
return doParseExpression(ognlExpression, context);
String expression = expressionString.substring(getExpressionPrefix().length(), lastIndex);
return doParseExpression(expression, context);
}
} else {
return doParseExpression(expressionString, context);

View File

@@ -23,7 +23,6 @@ import org.springframework.binding.convert.service.GenericConversionService;
import org.springframework.binding.expression.Expression;
import org.springframework.binding.expression.ParserException;
import org.springframework.binding.expression.ValueCoercionException;
import org.springframework.binding.expression.ognl.TestBean;
import org.springframework.binding.expression.support.FluentParserContext;
public class BeanWrapperExpressionParserTests extends TestCase {

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.binding.expression.ognl;
package org.springframework.binding.expression.beanwrapper;
import java.util.ArrayList;
import java.util.Date;

View File

@@ -1,243 +0,0 @@
/*
* Copyright 2004-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.binding.expression.ognl;
import junit.framework.TestCase;
import org.springframework.binding.convert.converters.StringToDate;
import org.springframework.binding.convert.service.GenericConversionService;
import org.springframework.binding.expression.EvaluationException;
import org.springframework.binding.expression.Expression;
import org.springframework.binding.expression.ExpressionVariable;
import org.springframework.binding.expression.ParserException;
import org.springframework.binding.expression.ValueCoercionException;
import org.springframework.binding.expression.support.FluentParserContext;
public class OgnlExpressionParserTests extends TestCase {
private OgnlExpressionParser parser = new OgnlExpressionParser();
private TestBean bean = new TestBean();
public void testParseSimple() {
String exp = "flag";
Expression e = parser.parseExpression(exp, null);
assertNotNull(e);
Boolean b = (Boolean) e.getValue(bean);
assertFalse(b);
}
public void testParseSimpleAllowDelimited() {
parser.setAllowDelimitedEvalExpressions(true);
String exp = "${flag}";
Expression e = parser.parseExpression(exp, null);
assertNotNull(e);
Boolean b = (Boolean) e.getValue(bean);
assertFalse(b);
}
public void testParseSimpleDelimitedNotAllowed() {
String exp = "${flag}";
try {
parser.parseExpression(exp, null);
fail("should have failed");
} catch (ParserException e) {
}
}
public void testParseTemplateSimpleLiteral() {
String exp = "flag";
Expression e = parser.parseExpression(exp, new FluentParserContext().template());
assertNotNull(e);
assertEquals("flag", e.getValue(bean));
}
public void testParseTemplateEmpty() {
Expression e = parser.parseExpression("", new FluentParserContext().template());
assertNotNull(e);
assertEquals("", e.getValue(bean));
}
public void testParseTemplateComposite() {
String exp = "hello ${flag} ${flag} ${flag}";
Expression e = parser.parseExpression(exp, new FluentParserContext().template());
assertNotNull(e);
String str = (String) e.getValue(bean);
assertEquals("hello false false false", str);
}
public void testTemplateEnclosedCompositeNotSupported() {
String exp = "${hello ${flag} ${flag} ${flag}}";
try {
parser.parseExpression(exp, new FluentParserContext().template());
fail("Should've failed - not intended use");
} catch (ParserException e) {
}
}
public void testSyntaxError1() {
try {
parser.parseExpression("${", new FluentParserContext().template());
fail();
} catch (ParserException e) {
}
try {
String exp = "hello ${flag} ${abcd defg";
parser.parseExpression(exp, null);
fail("Should've failed - not intended use");
} catch (ParserException e) {
}
}
public void testSyntaxError2() {
try {
parser.parseExpression("${}", new FluentParserContext().template());
fail("Should've failed - not intended use");
} catch (ParserException e) {
}
try {
String exp = "hello ${flag} ${}";
parser.parseExpression(exp, null);
fail("Should've failed - not intended use");
} catch (ParserException e) {
}
}
public void testCollectionConstructionSyntax() {
// lists
parser.parseExpression("name in {null, \"Untitled\"}", null);
parser.parseExpression("${name in {null, \"Untitled\"}}", new FluentParserContext().template());
// native arrays
parser.parseExpression("new int[] {1, 2, 3}", null);
parser.parseExpression("${new int[] {1, 2, 3}}", new FluentParserContext().template());
// maps
parser.parseExpression("#{ 'foo' : 'foo value', 'bar' : 'bar value' }", null);
parser.parseExpression("${#{ 'foo' : 'foo value', 'bar' : 'bar value' }}", new FluentParserContext().template());
parser.parseExpression("#@java.util.LinkedHashMap@{ 'foo' : 'foo value', 'bar' : 'bar value' }", null);
parser.parseExpression("${#@java.util.LinkedHashMap@{ 'foo' : 'foo value', 'bar' : 'bar value' }}",
new FluentParserContext().template());
// complex examples
parser.parseExpression("b,#{1:2}", null);
parser.parseExpression("${b,#{1:2}}", new FluentParserContext().template());
parser.parseExpression("a${b,#{1:2},e}f${g,#{3:4},j}k", new FluentParserContext().template());
}
public void testVariables() {
Expression exp = parser.parseExpression("#var",
new FluentParserContext().variable(new ExpressionVariable("var", "flag")));
assertFalse((Boolean) exp.getValue(bean));
}
public void testVariablesWithCoersion() {
Expression exp = parser.parseExpression("#var", new FluentParserContext().variable(new ExpressionVariable(
"var", "number", new FluentParserContext().expectResult(Long.class))));
assertEquals(new Long(0), exp.getValue(bean));
}
public void testNestedVariablesWithTemplates() {
Expression exp = parser.parseExpression("#var", new FluentParserContext().variable(new ExpressionVariable(
"var", "${flag}${#var}", new FluentParserContext().template().variable(
new ExpressionVariable("var", "number")))));
assertEquals("false0", exp.getValue(bean));
}
public void testGetExpressionString() {
String expressionString = "maximum";
Expression exp = parser.parseExpression(expressionString, null);
assertEquals("maximum", exp.getExpressionString());
}
public void testGetValueType() {
String exp = "flag";
Expression e = parser.parseExpression(exp, null);
assertEquals(boolean.class, e.getValueType(bean));
}
public void testGetValueTypeNullCollectionValue() {
String exp = "list[0]";
Expression e = parser.parseExpression(exp, null);
assertEquals(null, e.getValueType(bean));
}
public void testGetValueWithCoersion() {
String expressionString = "number";
Expression exp = parser.parseExpression(expressionString, new FluentParserContext().expectResult(String.class));
TestBean context = new TestBean();
assertEquals("0", exp.getValue(context));
}
public void testGetValueCoersionError() {
String expressionString = "number";
Expression exp = parser.parseExpression(expressionString,
new FluentParserContext().expectResult(TestBean.class));
TestBean context = new TestBean();
try {
exp.getValue(context);
fail("Should have failed with coersion");
} catch (ValueCoercionException e) {
}
}
public void testSetValue() {
String expressionString = "number";
Expression exp = parser.parseExpression(expressionString, null);
TestBean context = new TestBean();
exp.setValue(context, 5);
assertEquals(5, context.getNumber());
}
public void testSetValueWithCoersion() {
GenericConversionService cs = (GenericConversionService) parser.getConversionService();
StringToDate converter = new StringToDate();
converter.setPattern("yyyy-MM-dd");
cs.addConverter(converter);
Expression e = parser.parseExpression("date", null);
e.setValue(bean, "2008-9-15");
}
public void testSetBogusValueWithCoersion() {
Expression e = parser.parseExpression("date", null);
try {
e.setValue(bean, "bogus");
fail("Should have failed tme");
} catch (ValueCoercionException ex) {
}
}
public void testReasonCauseLinkingGetValue() {
String exp = "getException()";
Expression e = parser.parseExpression(exp, null);
try {
e.getValue(bean);
} catch (EvaluationException ex) {
assertTrue(ex.getCause().getCause() instanceof IllegalStateException);
}
}
public void testReasonCauseLinkingSetValue() {
String exp = "exceptionProperty";
Expression e = parser.parseExpression(exp, null);
try {
e.setValue(bean, "does not matter");
} catch (EvaluationException ex) {
assertTrue(ex.getCause().getCause() instanceof IllegalStateException);
}
}
}