How to sanitize JCR node names ? | Community
Skip to main content
Mario248
Level 7
January 26, 2023
Solved

How to sanitize JCR node names ?

  • January 26, 2023
  • 2 replies
  • 1221 views

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-assets.html?lang=en#filename-handling-bulkimport

 

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 ?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Kiran_Vedantam

Hi @mario248 

 

Can you try this: 

JcrUtil.createValidName(String)

Hope this helps!

 

Thanks,

Kiran Vedantam.

2 replies

Anudeep_Garnepudi
Community Advisor
Community Advisor
January 26, 2023

@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);
AG
Kiran_Vedantam
Community Advisor
Kiran_VedantamCommunity AdvisorAccepted solution
Community Advisor
January 26, 2023

Hi @mario248 

 

Can you try this: 

JcrUtil.createValidName(String)

Hope this helps!

 

Thanks,

Kiran Vedantam.