Skip to content

Commit 33e19ad

Browse files
committed
Properly encode JCR paths in XPath query
Keep wildcards unencoded, though (https://issues.apache.org/jira/browse/JCR-5051) This closes #688
1 parent 40fb69c commit 33e19ad

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

accesscontroltool-bundle/src/main/java/biz/netcentric/cq/tools/actool/configreader/YamlConfigReader.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import javax.jcr.query.InvalidQueryException;
2828

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

299300
if (result.isEmpty()) {
@@ -316,6 +317,19 @@ protected void handleWildcards(final Session session,
316317
}
317318
}
318319

320+
/**
321+
* Create an XPath query for the given JCR path (which may contain wildcards ({@code *}) which should be preserved).
322+
* @param jcrPathWithWildcard a valid JCR path where some names/elements might be wildcards.
323+
*
324+
* @see <a href="https://jackrabbit.apache.org/archive/wiki/JCR/EncodingAndEscaping_115513396.html#EncodingAndEscaping-Encodingpathinqueries">Encoding Path In XPath Queries</a>
325+
* @see <a href="https://issues.apache.org/jira/browse/JCR-5051">JCR-5051</a>
326+
*/
327+
static final String createXPathQueryForPathWithWildcards(String jcrPathWithWildcard) {
328+
final String query = "/jcr:root" + ISO9075.encodePath(jcrPathWithWildcard);
329+
// decode wildcards again (https://issues.apache.org/jira/browse/JCR-5051)
330+
return query.replace("_x002a_", "*");
331+
}
332+
319333
protected AceBean getNewAceBean() {
320334
return new AceBean();
321335
}

accesscontroltool-bundle/src/test/java/biz/netcentric/cq/tools/actool/configreader/YamlConfigReaderTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,12 @@ public void testInvalidKeys() throws IOException, AcConfigBeanValidationExceptio
154154
() -> yamlConfigReader.getUserConfigurationBeans(yamlList, null));
155155
}
156156

157+
@Test
158+
public void testCreateXPathQueryForPathWithWildcards() {
159+
assertEquals("/jcr:root/var/test", YamlConfigReader.createXPathQueryForPathWithWildcards("/var/test"));
160+
assertEquals("/jcr:root/var/*/_x0031_2node/*", YamlConfigReader.createXPathQueryForPathWithWildcards("/var/*/12node/*"));
161+
}
162+
157163
static List<Map> getYamlList(final String filename) throws IOException {
158164
final String configString = getTestConfigAsString(filename);
159165

0 commit comments

Comments
 (0)