Servlet for fetching data from one node and using that data elsewhere | Community
Skip to main content
New Participant
February 24, 2023
Solved

Servlet for fetching data from one node and using that data elsewhere

  • February 24, 2023
  • 2 replies
  • 3036 views

Say I have multiple pages under /content/practice/articles. Now in /content/practice/articles/firstpage/jcr:content there is a property named myImage. I want to read and fetch that value from myImage property which is a path basically.

 

Now I want to navigate to /content/practice/articles/firstpage/jcr:content/root/container/detailed_page and replace the properties (mainImage & smallImage) value with the data fetched from myImage property.

 

Can someone help me with the servlet code that I need to write in order to fetch the same?

 

Note : There are 500 pages under /content/practice/articles and I would have to do the above for all of them.

 

@nitin_laad had helped me out previously with the following code snippet, but since I'm still a newbie I'm unable to make the code run-worthy. Can someone please help me out with the full code snippet (say enclose the following with appropriate try catch block and other necessary statements). Thanks!

 

sample code - @component(service = Servlet.class, property = { Constants.SERVICE_DESCRIPTION + "= Sample servlet to copy property value", "sling.servlet.methods=" + HttpConstants.METHOD_POST, "sling.servlet.paths=" + "/bin/copy-property" }) public class CopyPropertyServlet extends SlingAllMethodsServlet { private static final Logger LOG = LoggerFactory.getLogger(CopyPropertyServlet.class); @Override protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { ResourceResolver resourceResolver = request.getResourceResolver(); Session session = resourceResolver.adaptTo(Session.class); // Get the source property value Node sourceNode = session.getNode("/content/practice/articles/jcr:content"); Property sourceProperty = sourceNode.getProperty("myImage"); String sourcePropertyValue = sourceProperty.getString(); // Get the destination property and set the value Node destinationNode = session.getNode("/content/practice/articles/jcr:content/root/container/main_page"); destinationNode.setProperty("mainImage", sourcePropertyValue); // Save the changes session.save(); response.getWriter().write("Property value copied successfully"); } }
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 Saravanan_Dharmaraj

I have updated the same code below to run for all the articles with try catch block , please run it in browser and see. First run it for fewer set of pages before run it for all.

 

 

import org.apache.felix.scr.annotations.Component; import org.apache.felix.scr.annotations.Properties; import org.apache.felix.scr.annotations.Reference; import org.apache.felix.scr.annotations.Service; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.api.servlets.SlingAllMethodsServlet; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.jcr.Node; import javax.jcr.Property; import javax.jcr.RepositoryException; import javax.jcr.Session; import javax.servlet.Servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.*; @Component(metatype = false) @Service({ Servlet.class }) @Properties({ @org.apache.felix.scr.annotations.Property(name = "sling.servlet.paths", value = {"/bin/copy-property" }), }) public class CopyPropertyServlet extends SlingAllMethodsServlet { private static final long serialVersionUID = -29612592807014205L; private final Logger log = LoggerFactory.getLogger(CopyPropertyServlet.class); private final String PAGE_PATH = "/content/practice/articles"; public void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException { response.setContentType("text/html"); ResourceResolver resourceResolver = request.getResourceResolver(); Session session = resourceResolver.adaptTo(Session.class); // Get the source property value try { Resource parentRes = resourceResolver.getResource(PAGE_PATH); Iterator<Resource> resourceIterator = parentRes.listChildren(); while (resourceIterator.hasNext()) { Resource articleRes = resourceIterator.next(); Node sourceNode = session.getNode(articleRes.getPath() + "/jcr:content"); Property sourceProperty = sourceNode.getProperty("myImage"); String sourcePropertyValue = sourceProperty.getString(); // Get the destination property and set the value Node destinationNode = session.getNode(articleRes.getPath() + "/jcr:content/root/container/detailed_page"); destinationNode.setProperty("mainImage", sourcePropertyValue); destinationNode.setProperty("smallImage", sourcePropertyValue); // Save the changes session.save(); } } catch (RepositoryException e) { log.error("CopyPropertyServlet : Reading and setting error "); } response.getWriter().write("Property value copied successfully"); } }

 

2 replies

Saravanan_Dharmaraj
Saravanan_DharmarajAccepted solution
New Participant
February 27, 2023

I have updated the same code below to run for all the articles with try catch block , please run it in browser and see. First run it for fewer set of pages before run it for all.

 

 

import org.apache.felix.scr.annotations.Component; import org.apache.felix.scr.annotations.Properties; import org.apache.felix.scr.annotations.Reference; import org.apache.felix.scr.annotations.Service; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.api.servlets.SlingAllMethodsServlet; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.jcr.Node; import javax.jcr.Property; import javax.jcr.RepositoryException; import javax.jcr.Session; import javax.servlet.Servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.*; @Component(metatype = false) @Service({ Servlet.class }) @Properties({ @org.apache.felix.scr.annotations.Property(name = "sling.servlet.paths", value = {"/bin/copy-property" }), }) public class CopyPropertyServlet extends SlingAllMethodsServlet { private static final long serialVersionUID = -29612592807014205L; private final Logger log = LoggerFactory.getLogger(CopyPropertyServlet.class); private final String PAGE_PATH = "/content/practice/articles"; public void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException { response.setContentType("text/html"); ResourceResolver resourceResolver = request.getResourceResolver(); Session session = resourceResolver.adaptTo(Session.class); // Get the source property value try { Resource parentRes = resourceResolver.getResource(PAGE_PATH); Iterator<Resource> resourceIterator = parentRes.listChildren(); while (resourceIterator.hasNext()) { Resource articleRes = resourceIterator.next(); Node sourceNode = session.getNode(articleRes.getPath() + "/jcr:content"); Property sourceProperty = sourceNode.getProperty("myImage"); String sourcePropertyValue = sourceProperty.getString(); // Get the destination property and set the value Node destinationNode = session.getNode(articleRes.getPath() + "/jcr:content/root/container/detailed_page"); destinationNode.setProperty("mainImage", sourcePropertyValue); destinationNode.setProperty("smallImage", sourcePropertyValue); // Save the changes session.save(); } } catch (RepositoryException e) { log.error("CopyPropertyServlet : Reading and setting error "); } response.getWriter().write("Property value copied successfully"); } }

 

Saravanan_Dharmaraj
New Participant
February 27, 2023

Please see i didnt run this code in my local since i dont have these pages created in my local. You may need to modify the PAGE_PATH to test for other path.

Hope it helps.

New Participant
February 27, 2023

Thanks a lot 🙂

New Participant
February 24, 2023