Solved! Go to Solution.
Views
Replies
Total Likes
Use the sample code below. You will have to create a system user to get the resource resolver and restrict the number to 10.
import java.io.IOException; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.servlet.Servlet; import javax.servlet.ServletException; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; import org.apache.sling.api.resource.LoginException; import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.api.resource.ResourceResolverFactory; import org.apache.sling.api.servlets.SlingSafeMethodsServlet; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; import org.osgi.service.component.propertytypes.ServiceDescription; import com.day.cq.wcm.api.Page; import com.google.gson.JsonArray; @Component(service = { Servlet.class }, property={"sling.servlet.methods=get", "sling.servlet.path=/bin/MyServlet"}) @ServiceDescription("Simple Demo Servlet") public class MyServlet extends SlingSafeMethodsServlet { @Reference ResourceResolverFactory resourceResolverFactory; ResourceResolver resourceResolver; List<String> children; private static final long serialVersionUID = 1L; @Override protected void doGet(final SlingHttpServletRequest req, final SlingHttpServletResponse resp) throws ServletException, IOException { Map<String, Object> param = new HashMap<String, Object>(); param.put(ResourceResolverFactory.SUBSERVICE, "getResourceResolver"); JsonArray array = new JsonArray(); try { resourceResolver = resourceResolverFactory.getServiceResourceResolver(param); Page sourcePage = resourceResolver.getResource("/content/we-retail").adaptTo(Page.class); Iterator<Page> listChildren = sourcePage.listChildren(null, true); while (listChildren.hasNext()) { array.add(listChildren.next().getPath()); } } catch (LoginException e) { } resp.setContentType("application/json"); resp.setCharacterEncoding("UTF-8"); resp.getWriter().print(array.toString()); } }
@prasadb026 Please refer below community thread:
Use the sample code below. You will have to create a system user to get the resource resolver and restrict the number to 10.
import java.io.IOException; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.servlet.Servlet; import javax.servlet.ServletException; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; import org.apache.sling.api.resource.LoginException; import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.api.resource.ResourceResolverFactory; import org.apache.sling.api.servlets.SlingSafeMethodsServlet; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; import org.osgi.service.component.propertytypes.ServiceDescription; import com.day.cq.wcm.api.Page; import com.google.gson.JsonArray; @Component(service = { Servlet.class }, property={"sling.servlet.methods=get", "sling.servlet.path=/bin/MyServlet"}) @ServiceDescription("Simple Demo Servlet") public class MyServlet extends SlingSafeMethodsServlet { @Reference ResourceResolverFactory resourceResolverFactory; ResourceResolver resourceResolver; List<String> children; private static final long serialVersionUID = 1L; @Override protected void doGet(final SlingHttpServletRequest req, final SlingHttpServletResponse resp) throws ServletException, IOException { Map<String, Object> param = new HashMap<String, Object>(); param.put(ResourceResolverFactory.SUBSERVICE, "getResourceResolver"); JsonArray array = new JsonArray(); try { resourceResolver = resourceResolverFactory.getServiceResourceResolver(param); Page sourcePage = resourceResolver.getResource("/content/we-retail").adaptTo(Page.class); Iterator<Page> listChildren = sourcePage.listChildren(null, true); while (listChildren.hasNext()) { array.add(listChildren.next().getPath()); } } catch (LoginException e) { } resp.setContentType("application/json"); resp.setCharacterEncoding("UTF-8"); resp.getWriter().print(array.toString()); } }