I have a servlet which provides JSON for the page. I wanted to get page properties in my code in that servlet. But every time I add page manager or current page to my servlet, the JSON stops loading. And there's no error.
I have the page path. But adapting it to a page is causing issue.
Solved! Go to Solution.
Views
Replies
Total Likes
You can refer below code , I am accessing servlet via resourceType-
package com.aem.demo.core.servlets;
import java.io.IOException;
import java.util.Objects;
import javax.servlet.Servlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.day.cq.wcm.api.Page;
@component(service = Servlet.class, property = { "sling.servlet.resourceTypes=" + "demo/components/page",
"sling.servlet.methods=" + HttpConstants.METHOD_GET })
public class ReadPageInformationServlet extends SlingAllMethodsServlet {
private final Logger logger = LoggerFactory.getLogger(ReadPageInformationServlet.class);
/**
*
*/
private static final long serialVersionUID = 1L;
protected void doGet(SlingHttpServletRequest slingHttpServletRequest,
SlingHttpServletResponse slingHttpServletResponse) {
String requestURI = slingHttpServletRequest.getRequestURI();
String currentpagePath = requestURI.substring(0, requestURI.lastIndexOf("/"));
ResourceResolver resourceResolver = slingHttpServletRequest.getResourceResolver();
Resource resource = resourceResolver.getResource(currentpagePath);
if (Objects.nonNull(resource) && resource.isResourceType("cq:Page")) {
Page page = resource.adaptTo(Page.class);
ValueMap pageproperties = page.getProperties();
try {
slingHttpServletResponse.getWriter().println(pageproperties);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Hi ,
Read the value in servlet by passing the current page pat in ajax-data.
where you consume the servlet - data - URI = ${currentPage.path @CONTEXT= ""}.
Read the URI value in servlet - request.getParameter("URI");
Thanks,
Nani
Hi @Shaheena_Sheikh, I am not sure how you are trying to retrieve page properties, however you can try below code. It assumes that resource resolver from request will have read permissions to the page which page properties you would like to get.
ResourceResolver resourceResolver = request.getResourceResolver();
PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
Page page = pageManager.getPage(pagePath);
ValueMap pageProperties = page.getProperties();
You can refer below code , I am accessing servlet via resourceType-
package com.aem.demo.core.servlets;
import java.io.IOException;
import java.util.Objects;
import javax.servlet.Servlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.day.cq.wcm.api.Page;
@component(service = Servlet.class, property = { "sling.servlet.resourceTypes=" + "demo/components/page",
"sling.servlet.methods=" + HttpConstants.METHOD_GET })
public class ReadPageInformationServlet extends SlingAllMethodsServlet {
private final Logger logger = LoggerFactory.getLogger(ReadPageInformationServlet.class);
/**
*
*/
private static final long serialVersionUID = 1L;
protected void doGet(SlingHttpServletRequest slingHttpServletRequest,
SlingHttpServletResponse slingHttpServletResponse) {
String requestURI = slingHttpServletRequest.getRequestURI();
String currentpagePath = requestURI.substring(0, requestURI.lastIndexOf("/"));
ResourceResolver resourceResolver = slingHttpServletRequest.getResourceResolver();
Resource resource = resourceResolver.getResource(currentpagePath);
if (Objects.nonNull(resource) && resource.isResourceType("cq:Page")) {
Page page = resource.adaptTo(Page.class);
ValueMap pageproperties = page.getProperties();
try {
slingHttpServletResponse.getWriter().println(pageproperties);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Hi,
I tried that code and somehow it returns me null resource and I am not able to get page properties.
I tried with static page path but it doesn't work.
Any help on this would be appreciated.
Thanks
I forgot to mention that I am accessing servlet via path.
Does that make any difference?
As you are trying with servlet path then do one thing just set the debug point at String requestURI = slingHttpServletRequest.getRequestURI(); see requestURI and also check Objects.nonNull(resource) && resource.isResourceType("cq:Page") condition is getting satisfied or not.
I feel you are getting false at if block.
One more suggestion if you are trying to read page properties from both author and publisher both then create ResourceResolver object via service user/system user.