Programmatically Deleting Nodes - ConstraintViolationException No matching property definition

Mat_Barreto 13-02-2017

Hello guys, how are you?

I'm trying to programmatically delete nodes in a given path using a Java Bundle.

My bundle code is:

public boolean deleteNodes(String path){ try { //Create a connection to the CQ repository running on local host Repository repository = JcrUtils.getRepository("http://localhost:4502/crx/server"); //Create a Session javax.jcr.Session session = repository.login( new SimpleCredentials("admin", "admin".toCharArray())); //Create a node that represents the root node Node root = session.getRootNode(); // Retrieve content Node node = root.getNode(path); for (NodeIterator ni = node.getNodes(); ni.hasNext(); ) { Node childNode = ni.nextNode(); childNode.remove(); } // Save the session changes and log out session.save(); session.logout(); return true; } catch(Exception e){ e.printStackTrace(); } return false; }

And on my AEM, I've created a Template/Component that has the following code:

 

<html> <head> <title>Node Delete</title> </head> <body> <form action="deletenodeimpl.jsp" method="POST"> <input type="text" id="path" name="path" /> <input type="submit" value="Delete Child Nodes" /> </form> </body> </html>

My deletenodeimpl.jsp is on the same folder (the component deletenode) of the previous jsp file and the code is:

<% String path = request.getParameter("path"); DeleteNodesImpl deleteNodes = new DeleteNodesImpl(); if(deleteNodes.deleteNodes(path){ alert("success"); }else{ alert("error"); } %>

I do import my bundle (that is successfully builded), but upon clicking the submit, I get an "Error while processing /content/portal/deletenodeimpl.jsp" page, and my error.log file logs:

13.02.2017 16:09:47.827 *ERROR* [0:0:0:0:0:0:0:1 [1487009387825] POST /content/portal/deletenodeimpl.jsp HTTP/1.1] org.apache.sling.servlets.post.impl.operations.ModifyOperation Exception during response processing. javax.jcr.nodetype.ConstraintViolationException: No matching property definition: path = /apps/portal/testfolder

- /apps/portal/testfolder is the path I'm inputing on my page, and it exists on my CRX.

 

So, can anyone tell me whats wrong or if there is a better way to supply child node deletion upon path input? 

 

Thanks a lot.

Mark Solution

Are these answers useful?
Help other community members by marking useful answers as accepted.

Accepted Solutions (0)

Answers (1)

Answers (1)

daniel_henriqu1 22-02-2017

Hi.

I'm not sure that is possible to directly invoke a .jsp. After all, Sling is a content centric webapp framework.

I think the Sling POST Servlet is trying to modify your jsp (a nt:file) instead of interpreting/running it.

Have you tried to create a resource and POST to it? Or maybe POST directly to the resource created from your component?

The second option would be something like:

Let's say that your page component is under /apps/portal/components/structure/mypagemteplate and that you have a page under /content/portal/test/my-page, created from that template/component.

  1. Create a jsp called POST.jsp under /apps/portal/components/structure/mypagemteplate;
  2. Add some code to it;
  3. Change your form action to "/content/portal/test/my-page"

Be aware that this is an untested code!

You may need to adjust some things because:

  • I don't recall what the script naming convention is for POST;
  • If the servlet/script that handles cq:Page would allow you to POST to a page template without changing the redirect.jsp file.

Either way, if cq:Page doesn't allow it, you can try to POST to /content/portal/test/my-page/jcr:content instead or to a regular component at your page.

A last resource would be to implement the first option: a nt:unstructure node with a specific resource type + POST.jsp script for that type. 

 

Best regards,

Daniel.