Skip to content

Instantly share code, notes, and snippets.

@techforum-repo
Created October 12, 2020 00:54
Show Gist options
  • Save techforum-repo/57960d67f822bc23183694a14acb7121 to your computer and use it in GitHub Desktop.
Save techforum-repo/57960d67f822bc23183694a14acb7121 to your computer and use it in GitHub Desktop.
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.resource.filter.ResourceFilterStream;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.propertytypes.ServiceDescription;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.IOException;
@Component(service = Servlet.class,
property = { "sling.servlet.methods=" + HttpConstants.METHOD_GET,
"sling.servlet.paths=" + "/bin/resourcefilterdemo" })
@ServiceDescription("SlingResourceFilterDemoServlet")
public class SlingResourceFilterDemoServlet extends SlingSafeMethodsServlet {
private static final long serialVersionUID = 1L;
private String type = "weretail/components/structure/page";
@Override
protected void doGet(final SlingHttpServletRequest req, final SlingHttpServletResponse resp)
throws ServletException, IOException {
Resource resource = req.getResourceResolver().getResource("/content/we-retail/us");
resp.getWriter().println("===========getChildResources===========\n");
getChildResourcesThroughTraversal(resource, resp);
resp.getWriter().println("==========================================\n\n\n");
resp.getWriter().println("===========getChildResourcesFilter===========\n");
getChildResourcesThroughResourceFilter(resource, resp);
resp.getWriter().println("==========================================\n\n\n");
resp.getWriter().println("===========getChildResourcesThroughResourceFilterAnd===========\n");
getChildResourcesThroughResourceFilterAnd(resource, resp);
resp.getWriter().println("==========================================\n\n\n");
}
private void getChildResourcesThroughTraversal(final Resource resource, final SlingHttpServletResponse resp) throws IOException {
for (Resource child : resource.getChildren()) {
if ("cq:Page".equals(child.getValueMap().get("jcr:primaryType", ""))) {
Resource content = child.getChild("jcr:content");
if (content != null && type.equals(content.getValueMap().get("sling:resourceType", ""))) {
resp.getWriter().println("Path: " + child.getPath());
getChildResourcesThroughTraversal(child, resp);
}
}
}
}
private void getChildResourcesThroughResourceFilter(final Resource resource, final SlingHttpServletResponse resp)
throws IOException {
ResourceFilterStream resourceStream = resource.adaptTo(ResourceFilterStream.class);
resourceStream.setBranchSelector("[jcr:primaryType] == 'cq:Page'")
.setChildSelector("[jcr:content/sling:resourceType] == $type").addParam("type", type).stream()
.forEach(r -> {
try {
resp.getWriter().println("Path: " + r.getPath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
}
private void getChildResourcesThroughResourceFilterAnd(final Resource resource, final SlingHttpServletResponse resp)
throws IOException {
ResourceFilterStream resourceStream = resource.adaptTo(ResourceFilterStream.class);
resourceStream.setBranchSelector("[jcr:primaryType] == 'cq:Page'")
.setChildSelector("[jcr:content/sling:resourceType] == $type and [jcr:content/test] like 'test'").addParam("type", type).stream()
.forEach(r -> {
try {
resp.getWriter().println("Path: " + r.getPath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment