Expand my Community achievements bar.

SOLVED

How to sanitize JCR node names ?

Avatar

Level 9

I want to create a node but I get Invalid name or path exception while creating a node, below is the exception

 

javax.jcr.RepositoryException: Invalid name or path: parentCategoty/tco[a].jpg

 

 

Hence I am using JcrUtil.escapeIllegalJcrChars to sanitize the path before creating but it seems not working. 

 

Reference - https://experienceleague.adobe.com/docs/experience-manager-cloud-service/content/assets/manage/add-a...

 

JcrUtil.createPath(JcrUtil.escapeIllegalJcrChars("parentCategoty/tco[a].jpg"),JcrConstants.NT_UNSTRUCTURED, session);

 

Can you tell me what could be the problem ? Also is there any other API that will check the path give whether the path is valid or not, basically I don't want to get the exceltopn instead i want to put all invalid path in a collection. Is this possible ?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @Mario248 

 

Can you try this: 

JcrUtil.createValidName(String)

Hope this helps!

 

Thanks,

Kiran Vedantam.

View solution in original post

2 Replies

Avatar

Community Advisor

@Mario248 

Do not escape the entire path, just escape the name of file here i.e. tco[a].jpg

 

JcrUtil.createPath("parentCategoty/" + JcrUtil.escapeIllegalJcrChars("tco[a].jpg"),JcrConstants.NT_UNSTRUCTURED, session);

 

If you want to validate each node name in path then

String[] nodesArray = path.split("/");
String escapedPath = "";
for (String nodeName : nodesArray) {
	if (nodeName.isEmpty())
		continue;
	escapedPath += "/" + (JcrUtil.isValidName(nodeName)? nodeName : JcrUtil.escapeIllegalJcrChars(nodeName));
}
JcrUtil.createPath(escapedPath, JcrConstants.NT_UNSTRUCTURED, jcrSession);

Avatar

Correct answer by
Community Advisor

Hi @Mario248 

 

Can you try this: 

JcrUtil.createValidName(String)

Hope this helps!

 

Thanks,

Kiran Vedantam.