Hi,
I am new to AEM development.
I have a sling servlet with the below code and I am trying to read a page property. This code snippet works in Author mode, but not in the publisher.
ResourceResolver resourceResolver = request.getResourceResolver();
PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
String currentpagePath = new URI(request.getHeader("referer")).getPath();
Page page = pageManager.getPage(currentpagePath.substring(0, currentpagePath.lastIndexOf(".")));
ValueMap pageProperties = page.getProperties();
excludeBefore = pageProperties.get("opendayEventDate", String.class);
LOG.info(ERROR_MSG_PREFIX + "page property exclude before "+ excludeBefore);
Did a bit of debugging, and it seems the page path comes in different structure when the code runs in publisher. Can someone please advise how to make it work in both Publisher and Author? Can you please suggest the best practice here?
I ve tried the below code and it does not seem to work either (reason being the resource object is null):
// try with resources block to manage resource resolver life cycle
try (ResourceResolver serviceRes = resourceResolverFactory.getServiceResourceResolver(authenticationInfo)) {
PageManager pm = serviceRes.adaptTo(PageManager.class);
Resource resource = request.getResource();
Page containingPage = pm.getContainingPage(resource);
ValueMap pageProperties = containingPage.getProperties();
excludeBefore = pageProperties.get("opendayEventDate", String.class);
LOG.info(ERROR_MSG_PREFIX + "page property exclude before "+ excludeBefore);
} catch (LoginException e) {
LOG.error(ERROR_MSG_PREFIX + "Login Exception occurred " + e.getMessage());
}
Solved! Go to Solution.
you should not rely on referer header,
Your servlet must have this path as a payload. Either you can pass complete path or just a trimmed one and later converted to the actual path using resources resolver or with custom logic.
@mewan86 In part 1, you are request.getHeader("referer") to get page path, please check if it unavailable on publisher. If yes this is causing your code to break - Not recommended
Coming to the code you have later, please check if the userId for resourceResolver and check if it has permission to read the resource you are trying to fetch the path for.
You may try to get the currentPage path using request URI like below, Hope this helps!
String resourcePath = request.getRequestURI();
Page currentPage = pageManager.getPage(resourcePath);
Thanks,
String resourcePath = request.getRequestURI();
Does this return the actual resource path? ?
I just printed the return value to the log and it seems this returns the path of the sling servlet (defined in @SlingServlet annotation)
How can I get the path of the page to read the page property?
@mewan86 , Register your servlet by resource type of page component. Use the current page path to call your servlet, this would give you the current page path. And, using this way request.getResource() will also not return null. From what I understand, you may have registered your servlet by path.
@Component(service = { Servlet.class })
@SlingServletResourceTypes(
resourceTypes="<Path of the page component of your project example "/apps/project/components/page>",
methods= "GET",
extensions="html",
selectors="something")
public class MyServlet extends SlingSafeMethodsServlet {
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
...
//Here either use request.getResource()(recommended) or request.getRequestURI()
}
//Call your servlet like localhost:4502/<current page path>.something.html
Ref: Apache Sling :: Servlets and Scripts
Thanks @shubhanshu_singh
You are right, I registered the servlet by path and invoking it through a Javascript on page level.
I will try your suggestion.
Hi @mewan86 ,
I see you already have resource resolver then why you are trying to get resource from request, you can simply use something like below-
Resource resource = serviceRes .getResource("<PATH of CONTENT>");
you should not rely on referer header,
Your servlet must have this path as a payload. Either you can pass complete path or just a trimmed one and later converted to the actual path using resources resolver or with custom logic.
Please do not implement this.
* What if the referer is empty? What if it contains a hostname as well?
* What if I tamper with that referer header? Because then I can make you read this information from basically any page.
* What if this request is served from a CDN/dispatcher cache?
Views
Likes
Replies