In the context of “Retrieving the Current Page in an AEM Servlet via ResourceType”, it is great to understand how this is done. This guide provides a simplified walkthrough on how you can adeptly achieve this. In the code example, we will associating the servlet with a resourceType.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | package com.example.aem.servlets; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; import org.apache.sling.api.servlets.SlingAllMethodsServlet; import org.apache.sling.models.annotations.injectorspecific.Self; import org.osgi.service.component.annotations.Component; import org.apache.sling.api.resource.ResourceResolver; import com.day.cq.wcm.api.Page; import com.day.cq.wcm.api.PageManager; import javax.servlet.Servlet; import java.io.IOException; import java.util.Optional; @Component(service = Servlet.class) @SlingServletResourceTypes( resourceTypes="example/components/content/mycomponent", methods="GET", extensions="html" ) public class ResourceTypeServlet extends SlingAllMethodsServlet { @Self private SlingHttpServletRequest request; @Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException { Page currentPage = Optional.ofNullable(request.getResourceResolver().adaptTo(PageManager.class)) .map(pm -> pm.getContainingPage(request.getResource())) .orElse(null); if(currentPage != null) { response.getWriter().write("Gotcha! Current page is: " + currentPage.getPath()); } else { response.getWriter().write("Oops, couldn't find the current page."); } } } |
Understanding the AEM Code Snippet
1. request.getResourceResolver().adaptTo(PageManager.class):
request.getResourceResolver(): This fetches a ResourceResolver from the servlet’s request. A ResourceResolver in AEM assists in translating resource paths to actual resources.
.adaptTo(PageManager.class): This method tries to adapt or convert the ResourceResolver into another type, specifically a PageManager. The PageManager in AEM offers utilities for working with pages, such as fetching, creating, or deleting pages.
2. Optional.ofNullable(…)
This wraps the value returned by the preceding code (which might be a PageManager object or null if the adaptation failed) within an Optional object. Optional is a container that may or may not contain a value.
3. .map(pm -> pm.getContainingPage(request.getResource()))
If the Optional contains a non-null PageManager value, the .map method applies the given function to it. Here, it uses the PageManager (denoted as pm in the lambda) to retrieve the containing page of the present resource.
pm.getContainingPage(request.getResource()): This extracts the AEM Page that encompasses the current resource.
4. .orElse(null)
Lastly, if any prior steps don’t succeed (i.e., if the adaptation to PageManager was unsuccessful or if no containing page for the resource exists), the orElse method ensures currentPage is set to null.