mirror of
https://github.com/apache/struts.git
synced 2026-08-31 11:24:28 +00:00
Makes OgnlUtil more immutable
This commit is contained in:
@@ -61,14 +61,36 @@ public class OgnlUtil {
|
||||
private boolean enableExpressionCache = true;
|
||||
private boolean enableEvalExpression;
|
||||
|
||||
private Set<Class<?>> excludedClasses = Collections.emptySet();
|
||||
private Set<Pattern> excludedPackageNamePatterns = Collections.emptySet();
|
||||
private Set<String> excludedPackageNames = Collections.emptySet();
|
||||
private Set<Class<?>> excludedClasses;
|
||||
private Set<Pattern> excludedPackageNamePatterns;
|
||||
private Set<String> excludedPackageNames;
|
||||
|
||||
private Container container;
|
||||
private boolean allowStaticMethodAccess;
|
||||
private boolean disallowProxyMemberAccess;
|
||||
|
||||
public OgnlUtil(
|
||||
@Inject(value = XWorkConstants.OGNL_EXCLUDED_CLASSES, required = false)
|
||||
String commaDelimitedClasses,
|
||||
@Inject(value = XWorkConstants.OGNL_EXCLUDED_PACKAGE_NAME_PATTERNS, required = false)
|
||||
String commaDelimitedPackagePatterns,
|
||||
@Inject(value = XWorkConstants.OGNL_EXCLUDED_PACKAGE_NAMES, required = false)
|
||||
String commaDelimitedPackageNames
|
||||
) {
|
||||
excludedClasses = Collections.unmodifiableSet(parseExcludedClasses(commaDelimitedClasses));
|
||||
excludedPackageNamePatterns = Collections.unmodifiableSet(parseExcludedPackageNamePatterns(commaDelimitedPackagePatterns));
|
||||
excludedPackageNames = Collections.unmodifiableSet(parseExcludedPackageNames(commaDelimitedPackageNames));
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor used by internal DI
|
||||
*/
|
||||
public OgnlUtil() {
|
||||
excludedClasses = Collections.emptySet();
|
||||
excludedPackageNamePatterns = Collections.emptySet();
|
||||
excludedPackageNames = Collections.emptySet();
|
||||
}
|
||||
|
||||
@Inject
|
||||
public void setXWorkConverter(XWorkConverter conv) {
|
||||
this.defaultConverter = new OgnlTypeConverterWrapper(conv);
|
||||
@@ -93,8 +115,7 @@ public class OgnlUtil {
|
||||
}
|
||||
}
|
||||
|
||||
@Inject(value = StrutsConstants.STRUTS_EXCLUDED_CLASSES, required = false)
|
||||
public void setExcludedClasses(String commaDelimitedClasses) {
|
||||
private Set<Class<?>> parseExcludedClasses(String commaDelimitedClasses) {
|
||||
Set<String> classNames = TextParseUtil.commaDelimitedStringToSet(commaDelimitedClasses);
|
||||
Set<Class<?>> classes = new HashSet<>();
|
||||
|
||||
@@ -106,11 +127,11 @@ public class OgnlUtil {
|
||||
}
|
||||
}
|
||||
|
||||
excludedClasses = Collections.unmodifiableSet(classes);
|
||||
return classes;
|
||||
}
|
||||
|
||||
@Inject(value = StrutsConstants.STRUTS_EXCLUDED_PACKAGE_NAME_PATTERNS, required = false)
|
||||
public void setExcludedPackageNamePatterns(String commaDelimitedPackagePatterns) {
|
||||
|
||||
private Set<Pattern> parseExcludedPackageNamePatterns(String commaDelimitedPackagePatterns) {
|
||||
Set<String> packagePatterns = TextParseUtil.commaDelimitedStringToSet(commaDelimitedPackagePatterns);
|
||||
Set<Pattern> packageNamePatterns = new HashSet<>();
|
||||
|
||||
@@ -118,12 +139,11 @@ public class OgnlUtil {
|
||||
packageNamePatterns.add(Pattern.compile(pattern));
|
||||
}
|
||||
|
||||
excludedPackageNamePatterns = Collections.unmodifiableSet(packageNamePatterns);
|
||||
return packageNamePatterns;
|
||||
}
|
||||
|
||||
@Inject(value = StrutsConstants.STRUTS_EXCLUDED_PACKAGE_NAMES, required = false)
|
||||
public void setExcludedPackageNames(String commaDelimitedPackageNames) {
|
||||
excludedPackageNames = Collections.unmodifiableSet(TextParseUtil.commaDelimitedStringToSet(commaDelimitedPackageNames));
|
||||
private Set<String> parseExcludedPackageNames(String commaDelimitedPackageNames) {
|
||||
return TextParseUtil.commaDelimitedStringToSet(commaDelimitedPackageNames);
|
||||
}
|
||||
|
||||
public Set<Class<?>> getExcludedClasses() {
|
||||
@@ -679,6 +699,13 @@ public class OgnlUtil {
|
||||
return Ognl.createDefaultContext(root, memberAccess, resolver, defaultConverter);
|
||||
}
|
||||
|
||||
protected void addExcludedClasses(String commaDelimitedClasses) {
|
||||
Set<Class<?>> existingClasses = new HashSet<>(excludedClasses);
|
||||
existingClasses.addAll(parseExcludedClasses(commaDelimitedClasses));
|
||||
|
||||
excludedClasses = Collections.unmodifiableSet(existingClasses);
|
||||
}
|
||||
|
||||
private interface OgnlTask<T> {
|
||||
T execute(Object tree) throws OgnlException;
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ public class OgnlUtilTest extends XWorkTestCase {
|
||||
ognlUtil = container.getInstance(OgnlUtil.class);
|
||||
}
|
||||
|
||||
public void testCanSetADependentObject() throws Exception {
|
||||
public void testCanSetADependentObject() {
|
||||
String dogName = "fido";
|
||||
|
||||
OgnlRuntime.setNullHandler(Owner.class, new NullHandler() {
|
||||
@@ -653,7 +653,7 @@ public class OgnlUtilTest extends XWorkTestCase {
|
||||
|
||||
Exception expected = null;
|
||||
try {
|
||||
ognlUtil.setExcludedClasses(Object.class.getName());
|
||||
ognlUtil.addExcludedClasses(Object.class.getName());
|
||||
ognlUtil.setValue("class.classLoader.defaultAssertionStatus", ognlUtil.createDefaultContext(foo), foo, true);
|
||||
fail();
|
||||
} catch (OgnlException e) {
|
||||
@@ -669,7 +669,7 @@ public class OgnlUtilTest extends XWorkTestCase {
|
||||
|
||||
Exception expected = null;
|
||||
try {
|
||||
ognlUtil.setExcludedClasses(Object.class.getName());
|
||||
ognlUtil.addExcludedClasses(Object.class.getName());
|
||||
ognlUtil.setValue("Class.ClassLoader.DefaultAssertionStatus", ognlUtil.createDefaultContext(foo), foo, true);
|
||||
fail();
|
||||
} catch (OgnlException e) {
|
||||
@@ -685,7 +685,7 @@ public class OgnlUtilTest extends XWorkTestCase {
|
||||
|
||||
Exception expected = null;
|
||||
try {
|
||||
ognlUtil.setExcludedClasses(Object.class.getName());
|
||||
ognlUtil.addExcludedClasses(Object.class.getName());
|
||||
ognlUtil.setValue("class['classLoader']['defaultAssertionStatus']", ognlUtil.createDefaultContext(foo), foo, true);
|
||||
fail();
|
||||
} catch (OgnlException e) {
|
||||
@@ -716,7 +716,7 @@ public class OgnlUtilTest extends XWorkTestCase {
|
||||
|
||||
Exception expected = null;
|
||||
try {
|
||||
ognlUtil.setExcludedClasses(Object.class.getName());
|
||||
ognlUtil.addExcludedClasses(Object.class.getName());
|
||||
ognlUtil.setValue("class[\"classLoader\"]['defaultAssertionStatus']", ognlUtil.createDefaultContext(foo), foo, true);
|
||||
fail();
|
||||
} catch (OgnlException e) {
|
||||
@@ -762,7 +762,7 @@ public class OgnlUtilTest extends XWorkTestCase {
|
||||
|
||||
Exception expected = null;
|
||||
try {
|
||||
ognlUtil.setExcludedClasses(Runtime.class.getName());
|
||||
ognlUtil.addExcludedClasses(Runtime.class.getName());
|
||||
ognlUtil.setValue("@java.lang.Runtime@getRuntime().exec('mate')", ognlUtil.createDefaultContext(foo), foo, true);
|
||||
fail();
|
||||
} catch (OgnlException e) {
|
||||
|
||||
Reference in New Issue
Block a user