Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Write a sling servlet which will return 10 child pages in json format, when called from any page

Avatar

Level 1
 
1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

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());
	}
}

View solution in original post

2 Replies

Avatar

Correct answer by
Employee Advisor

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());
	}
}