Reading a page property inside Sling servlet doGet() | Community
Skip to main content
Level 2
May 19, 2023
Solved

Reading a page property inside Sling servlet doGet()

  • May 19, 2023
  • 4 replies
  • 2815 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by arunpatidar

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.

 

4 replies

Community Advisor
May 19, 2023

@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);

 

 

mewan86Author
Level 2
May 22, 2023

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? 

Community Advisor
May 22, 2023

@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

 

Ritesh_Mittal
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
May 19, 2023

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>");

 

 

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
May 19, 2023

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.

 

Arun Patidar
joerghoh
Adobe Employee
Adobe Employee
May 20, 2023

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?