Skip to content

Commit

Permalink
Properly encode JCR paths in XPath query
Browse files Browse the repository at this point in the history
Keep wildcards unencoded, though
(https://issues.apache.org/jira/browse/JCR-5051)

This closes #688
  • Loading branch information
kwin committed May 7, 2024
1 parent 40fb69c commit 33e19ad
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import javax.jcr.query.InvalidQueryException;

import org.apache.commons.lang3.StringUtils;
import org.apache.jackrabbit.util.ISO9075;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
Expand Down Expand Up @@ -293,7 +294,7 @@ protected void handleWildcards(final Session session,
final AceBean tmpAclBean) throws InvalidQueryException,
RepositoryException {
// perform query using the path containing wildcards
final String query = "/jcr:root" + tmpAclBean.getJcrPath();
final String query = createXPathQueryForPathWithWildcards(tmpAclBean.getJcrPath());
final Set<String> result = QueryHelper.getNodePathsFromQuery(session, query);

if (result.isEmpty()) {
Expand All @@ -316,6 +317,19 @@ protected void handleWildcards(final Session session,
}
}

/**
* Create an XPath query for the given JCR path (which may contain wildcards ({@code *}) which should be preserved).
* @param jcrPathWithWildcard a valid JCR path where some names/elements might be wildcards.
*
* @see <a href="https://jackrabbit.apache.org/archive/wiki/JCR/EncodingAndEscaping_115513396.html#EncodingAndEscaping-Encodingpathinqueries">Encoding Path In XPath Queries</a>
* @see <a href="https://issues.apache.org/jira/browse/JCR-5051">JCR-5051</a>
*/
static final String createXPathQueryForPathWithWildcards(String jcrPathWithWildcard) {
final String query = "/jcr:root" + ISO9075.encodePath(jcrPathWithWildcard);
// decode wildcards again (https://issues.apache.org/jira/browse/JCR-5051)
return query.replace("_x002a_", "*");
}

protected AceBean getNewAceBean() {
return new AceBean();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ public void testInvalidKeys() throws IOException, AcConfigBeanValidationExceptio
() -> yamlConfigReader.getUserConfigurationBeans(yamlList, null));
}

@Test
public void testCreateXPathQueryForPathWithWildcards() {
assertEquals("/jcr:root/var/test", YamlConfigReader.createXPathQueryForPathWithWildcards("/var/test"));
assertEquals("/jcr:root/var/*/_x0031_2node/*", YamlConfigReader.createXPathQueryForPathWithWildcards("/var/*/12node/*"));
}

static List<Map> getYamlList(final String filename) throws IOException {
final String configString = getTestConfigAsString(filename);

Expand Down

0 comments on commit 33e19ad

Please sign in to comment.