Created
October 12, 2020 00:54
-
-
Save techforum-repo/57960d67f822bc23183694a14acb7121 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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