Skip to content

Commit

Permalink
Use JXPathFilter based on the jxpath.allow.class system property
Browse files Browse the repository at this point in the history
This has the additional benefit of not breaking the binary
compatibility of the Functions interface
  • Loading branch information
kyakdan committed Oct 19, 2022
1 parent 021b8ce commit 4ae80cb
Show file tree
Hide file tree
Showing 11 changed files with 210 additions and 256 deletions.
13 changes: 2 additions & 11 deletions src/main/java/org/apache/commons/jxpath/ClassFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,8 @@ public Function getFunction(
final String namespace,
final String name,
Object[] parameters) {
return getFunction(namespace, name, parameters, new JXPathFilter());
}

public Function getFunction(
String namespace,
String name,
Object[] parameters,
JXPathFilter jxPathFilter) {

// give chance to ClassFilter to filter out, if present
if (jxPathFilter != null && !jxPathFilter.exposeToXPath(functionClass.getName())) {
JXPathFilter jxPathFilter = new JXPathFilter();
if (!jxPathFilter.exposeToXPath(functionClass.getName())) {
throw new JXPathException(
"Extension function is not allowed: " + (namespace != null ? namespace + ":" + name : name)
+ " (in " + functionClass.getName() + ")");
Expand Down
27 changes: 3 additions & 24 deletions src/main/java/org/apache/commons/jxpath/FunctionLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
*/
package org.apache.commons.jxpath;

import org.apache.commons.jxpath.ri.JXPathFilter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -78,30 +76,12 @@ public Set getUsedNamespaces() {
@Override
public Function getFunction(final String namespace, final String name,
final Object[] parameters) {
return getFunction(namespace, name, parameters, new JXPathFilter());
}

/**
* Returns a Function, if any, for the specified namespace,
* name and parameter types.
* @param namespace function namespace
* @param name function name
* @param parameters parameters
* @param jxPathFilter the XPath filter
* @return Function found
*/
public Function getFunction(
final String namespace,
final String name,
final Object[] parameters,
final JXPathFilter jxPathFilter) {
Object candidates = functionCache().get(namespace);
final Object candidates = functionCache().get(namespace);
if (candidates instanceof Functions) {
return ((Functions) candidates).getFunction(
namespace,
name,
parameters,
jxPathFilter);
parameters);
}
if (candidates instanceof List) {
final List list = (List) candidates;
Expand All @@ -111,8 +91,7 @@ public Function getFunction(
((Functions) list.get(i)).getFunction(
namespace,
name,
parameters,
jxPathFilter);
parameters);
if (function != null) {
return function;
}
Expand Down
11 changes: 0 additions & 11 deletions src/main/java/org/apache/commons/jxpath/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,4 @@ public interface Functions {
* @return Function
*/
Function getFunction(String namespace, String name, Object[] parameters);

/**
* Returns a Function, if any, for the specified namespace,
* name and parameter types.
* @param namespace ns
* @param name function name
* @param parameters Object[]
* @param jxPathFilter the XPath filter
* @return Function
*/
Function getFunction(String namespace, String name, Object[] parameters, JXPathFilter jxPathFilter);
}
32 changes: 1 addition & 31 deletions src/main/java/org/apache/commons/jxpath/PackageFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,36 +117,6 @@ public Function getFunction(
final String namespace,
final String name,
Object[] parameters) {
return getFunction(namespace, name, parameters, new JXPathFilter());
}

/**
* Returns a {@link Function}, if found, for the specified namespace,
* name and parameter types.
* <p>
* @param namespace - if it is not the same as specified in the
* construction, this method returns null
* @param name - name of the method, which can one these forms:
* <ul>
* <li><b>methodname</b>, if invoking a method on an object passed as the
* first parameter</li>
* <li><b>Classname.new</b>, if looking for a constructor</li>
* <li><b>subpackage.subpackage.Classname.new</b>, if looking for a
* constructor in a subpackage</li>
* <li><b>Classname.methodname</b>, if looking for a static method</li>
* <li><b>subpackage.subpackage.Classname.methodname</b>, if looking for a
* static method of a class in a subpackage</li>
* </ul>
* @param parameters Object[] of parameters
* @param jxPathFilter the XPath filter
* @return a MethodFunction, a ConstructorFunction or null if no function
* is found
*/
public Function getFunction(
final String namespace,
final String name,
Object[] parameters,
final JXPathFilter jxPathFilter) {
if (!Objects.equals(this.namespace, namespace)) {
return null;
}
Expand Down Expand Up @@ -216,7 +186,7 @@ public Function getFunction(

Class functionClass;
try {
functionClass = ClassLoaderUtil.getClass(className, true, jxPathFilter);
functionClass = ClassLoaderUtil.getClass(className, true);
}
catch (final ClassNotFoundException ex) {
throw new JXPathException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ public Function getFunction(final QName functionName, final Object[] parameters)
while (funcCtx != null) {
funcs = funcCtx.getFunctions();
if (funcs != null) {
func = funcs.getFunction(namespace, name, parameters, new JXPathFilter());
func = funcs.getFunction(namespace, name, parameters);
if (func != null) {
return func;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static Class getClass(final ClassLoader classLoader, final String classNa
Class clazz;

// give chance to ClassFilter to filter out, if present
if (jxPathFilter != null && !jxPathFilter.exposeToXPath(className)) {
if (jxPathFilter == null || !jxPathFilter.exposeToXPath(className)) {
throw new ClassNotFoundException(className);
}

Expand All @@ -106,7 +106,7 @@ public static Class getClass(final ClassLoader classLoader, final String classNa
*/
public static Class getClass(ClassLoader classLoader, String className, boolean initialize)
throws ClassNotFoundException {
return getClass(classLoader, className, initialize, null);
return getClass(classLoader, className, initialize, new JXPathFilter());
}

/**
Expand Down Expand Up @@ -181,7 +181,7 @@ public static Class getClass(String className, JXPathFilter jxPathFilter) throws
* @throws ClassNotFoundException if the class is not found
*/
public static Class getClass(final String className, final boolean initialize) throws ClassNotFoundException {
return getClass(className, initialize, null);
return getClass(className, initialize, new JXPathFilter());
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/org/apache/commons/jxpath/BasicNodeSetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,17 @@ public class BasicNodeSetTest extends JXPathTestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
System.setProperty("jxpath.class.allow", "*");
context = JXPathContext.newContext(new TestMixedModelBean());
nodeSet = new BasicNodeSet();
}

@Override
public void tearDown() throws Exception {
System.clearProperty("jxpath.class.allow");
super.tearDown();
}

/**
* Add the pointers for the specified path to <code>nodeSet</code>.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ public static TestSuite suite()
return new TestSuite(JXPath172DynamicTest.class);
}

@Override
public void setUp() throws Exception {
super.setUp();
System.setProperty("jxpath.class.allow", "*");
}

@Override
public void tearDown() throws Exception {
System.clearProperty("jxpath.class.allow");
super.tearDown();
}

public void testIssue172_propertyExistAndIsNotNull()
{
final JXPathContext context = getContext("ciao", false);
Expand Down
Loading

0 comments on commit 4ae80cb

Please sign in to comment.