Expand my Community achievements bar.

Radically easy to access on brand approved content for distribution and omnichannel performant delivery. AEM Assets Content Hub and Dynamic Media with OpenAPI capabilities is now GA.
SOLVED

"Invalid name" when using node.orderBefore

Avatar

Level 2

I'm attempting to pull a property out of an existing node and use it to create a new titlecore node just before the source node and I'm having an issue with moving the newly-created nodes around within a container, it's throwing an IllegalArgumentException saying that the srcChildRelPath has an invalid name. The code executing is 

 

 

Property globalTitle = ref.getProperty( "title" );
    if ( globalTitle != null ) {
        Property size = ref.getProperty( "headingStyle" );
        Node globalTitleNode = createTitle( session, container, hash, globalTitle.getValue().toString(), size != null ? size.getValue().toString() : null );
                        
        container.orderBefore( globalTitleNode.getPath(), ref.getPath() );
}

 

with the container.orderBefore line being the one that throws the exception. I know the createTitle method returns a valid node, I have the session save in that method and I see the node created as a child of container in CRX/DE. I get the same results if I manually regrab the titlecore node from container before using orderBefore. 

 

The exception I get is 

 

10.01.2024 14:06:44.191 *ERROR* [[0:0:0:0:0:0:0:1] [1704913574756] GET [execution-path] HTTP/1.1] [project-file-path] Invalid name: [page-path]/jcr:content/root/responsivegrid/titlecore_2093688976
java.lang.IllegalArgumentException: Invalid name: [page-path]/jcr:content/root/responsivegrid/titlecore_2093688976
	at org.apache.jackrabbit.oak.spi.state.AbstractNodeState.checkValidName(AbstractNodeState.java:62) [org.apache.jackrabbit.oak-store-spi:1.22.15]
	at org.apache.jackrabbit.oak.plugins.memory.MemoryNodeBuilder.getChildNode(MemoryNodeBuilder.java:329) [org.apache.jackrabbit.oak-store-spi:1.22.15]
	at org.apache.jackrabbit.oak.core.SecureNodeBuilder.<init>(SecureNodeBuilder.java:111) [org.apache.jackrabbit.oak-core:1.22.15]
	at org.apache.jackrabbit.oak.core.SecureNodeBuilder.getChildNode(SecureNodeBuilder.java:328) [org.apache.jackrabbit.oak-core:1.22.15]
	at org.apache.jackrabbit.oak.core.MutableTree.createChild(MutableTree.java:91) [org.apache.jackrabbit.oak-core:1.22.15]
	at org.apache.jackrabbit.oak.core.MutableTree.createChild(MutableTree.java:35) [org.apache.jackrabbit.oak-core:1.22.15]
	at org.apache.jackrabbit.oak.plugins.tree.impl.AbstractTree.getChild(AbstractTree.java:240) [org.apache.jackrabbit.oak-core:1.22.15]
	at org.apache.jackrabbit.oak.core.MutableTree.getChild(MutableTree.java:159) [org.apache.jackrabbit.oak-core:1.22.15]
	at org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate.orderBefore(NodeDelegate.java:374) [org.apache.jackrabbit.oak-jcr:1.22.15]
	at org.apache.jackrabbit.oak.jcr.session.NodeImpl$6.performVoid(NodeImpl.java:346) [org.apache.jackrabbit.oak-jcr:1.22.15]
	at org.apache.jackrabbit.oak.jcr.delegate.SessionDelegate.performVoid(SessionDelegate.java:273) [org.apache.jackrabbit.oak-jcr:1.22.15]
	at org.apache.jackrabbit.oak.jcr.session.NodeImpl.orderBefore(NodeImpl.java:337) [org.apache.jackrabbit.oak-jcr:1.22.15]
	at [project-file-path].[method-name]([project-file].java:122) [project-package]

 

 

I can't find any documentation on exactly what qualifies as valid JCR node names, but this name is exactly in line with AEM-generated nodes for titlecore if authored normally. I've also copied the full path directly from the exception and pasted it in CRX/DE and it correctly points directly to the node in question. No other logs are giving me any additional information that would point me towards a solution that I've found.

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @benorosz,

The problem is that you are using absolute paths as an argument of orderBefore method. Base on documentation you should use relative path which essentially is a name plus possible index. For example, for given structure:

 

/content/samples/page
                    /page-b
                    /page-a

 

to reorder page-b (/content/samples/page/page-b) and page-a (/content/samples/page/page-a) you should use following code:

 

// getting parent node
Node parentNode = session.getNode("/content/samples/page");

// changing order of child nodes, putting page-a before page-b
parentNode.orderBefore("page-a", "page-b");

 

So instead of getPath() you can try to use getName() method.

Regarding node name generation, I would recommend to use createValidName method from JcrUtil.

View solution in original post

5 Replies

Avatar

Community Advisor

Please check this thread: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/javax-jcr-node-how-to-use-... And keep in mind that the "container" node should have the created node available at the time the "orderBefore" method is executed, you may need a session.save() or session.refresh(true)



Esteban Bustamante

Avatar

Level 2

I originally had this as a reply to that thread but decided to pull it out into a new one. lukasz-m caught my actual issue with using getPath() rather than getName().

Avatar

Correct answer by
Community Advisor

Hi @benorosz,

The problem is that you are using absolute paths as an argument of orderBefore method. Base on documentation you should use relative path which essentially is a name plus possible index. For example, for given structure:

 

/content/samples/page
                    /page-b
                    /page-a

 

to reorder page-b (/content/samples/page/page-b) and page-a (/content/samples/page/page-a) you should use following code:

 

// getting parent node
Node parentNode = session.getNode("/content/samples/page");

// changing order of child nodes, putting page-a before page-b
parentNode.orderBefore("page-a", "page-b");

 

So instead of getPath() you can try to use getName() method.

Regarding node name generation, I would recommend to use createValidName method from JcrUtil.

Avatar

Level 2

That was exactly it, and looking at previous code that was working I see now that getName() was used, not getPath() like I was using, thank you.

 

And as far as the name generation, I'm using hashes of the paths to salt the node names so there's very little chance of collision, but I'll be sure to check out that createValidName in the future.

Avatar

Community Advisor

@benorosz Can you try saving the state of the newly created node and then try to invoke the orderBefore operation. 

resolver.commit or a session.save() should do the trick.