Hi All,
I have a one path servlet i need to add extention and selectors in this but i don't have the idea how to add those kindly let me know if any one know how to add extensions and selectors for this below file.
import org.osgi.service.component.annotations.Component; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.day.cq.wcm.api.Page; import com.day.cq.wcm.api.PageManager; @Component(service = Servlet.class) @SlingServletPaths( value = {"/bin/pages","/carrybag/pages"} ) public class PathType extends SlingAllMethodsServlet { private static final Logger LOG = LoggerFactory.getLogger(PathType.class); @Override protected void doGet(final SlingHttpServletRequest req, final SlingHttpServletResponse resp) throws ServletException, IOException { final ResourceResolver resourceResolver = req.getResourceResolver(); Page page = resourceResolver.adaptTo(PageManager.class).getPage("/content/carry-bag/language-masters/en"); JSONArray pagesArray = new JSONArray(); try { Iterator<Page> childPages = page.listChildren(); while (childPages.hasNext()) { Page childPage = childPages.next(); JSONObject pageObject = new JSONObject(); pageObject.put(childPage.getTitle(), childPage.getPath().toString()); pagesArray.put(pageObject); } } catch (JSONException e) { LOG.info("\n ERROR {} ", e.getMessage()); } resp.setContentType("application/json"); resp.getWriter().write(pagesArray.toString()); } }
Thanks
Nandheswara
Solved! Go to Solution.
Views
Replies
Total Likes
Hello @Nandheswara ,
For Path based servlet you can easily add selectors and extensions, Here is a sample code:
import org.apache.sling.api.servlets.ServletResolverConstants;
import org.apache.sling.api.servlets.HttpConstants;
@Component(service = Servlet.class,
property = {
Constants.SERVICE_DESCRIPTION + "=My Path Based Servlet",
ServletResolverConstants.SLING_SERVLET_METHODS + "=" + HttpConstants.METHOD_GET,
ServletResolverConstants.SLING_SERVLET_PATHS + "=/bin/public/my-project/validate",
ServletResolverConstants.SLING_SERVLET_SELECTORS + "=validate",
ServletResolverConstants.SLING_SERVLET_EXTENSIONS + "=html"
})
Hope this is what exactly you want.
@Component(service= Servlet.class, property={ Constants.SERVICE_DESCRIPTION + "=Example Servlet", "sling.servlet.methods=" + HttpConstants.METHOD_GET, "sling.servlet.resourceTypes="+ "my-project/components/general/my-component ", "sling.servlet.extensions=" + "html", "sling.servlet.selectors=" + "simple" })
Hope this helps.
Regards,
Ayush
Is this for Resource type servlet or path type servlet?
We either use Path-Based or Selector-Bases servlets.
For a Path-Based servlets, info should be passed via queryParams. If these queryParams+values are pre-defined, you can by configure dispatcher to ignore them and cache the response.
Hi @Nandheswara
We can not specify selectors and extension with Path Type servlet. The moment you specify the servlet registered by path, selectors and extensions even if you specify do not take effect.
If you want to achive something which requires different selector or extension, you need to register your servlet by resourceType. If the resourceType is not not single or can not be specified, there is default resourceType which you can specify and can work for any path irrespective of any resourceType present or not that is "sling/servlet/default"
@Component(service= Servlet.class,
property={
Constants.SERVICE_DESCRIPTION + "= ResourceType Servlet",
"sling.servlet.methods=" + HttpConstants.METHOD_GET,
"sling.servlet.resourceTypes="+ "sling/servlet/default",
"sling.servlet.extensions=" + "json",
"sling.servlet.selectors=" + "selector"
})
Hope it helps!
Thanks,
Nupur
Hello @Nandheswara ,
For Path based servlet you can easily add selectors and extensions, Here is a sample code:
import org.apache.sling.api.servlets.ServletResolverConstants;
import org.apache.sling.api.servlets.HttpConstants;
@Component(service = Servlet.class,
property = {
Constants.SERVICE_DESCRIPTION + "=My Path Based Servlet",
ServletResolverConstants.SLING_SERVLET_METHODS + "=" + HttpConstants.METHOD_GET,
ServletResolverConstants.SLING_SERVLET_PATHS + "=/bin/public/my-project/validate",
ServletResolverConstants.SLING_SERVLET_SELECTORS + "=validate",
ServletResolverConstants.SLING_SERVLET_EXTENSIONS + "=html"
})
Hope this is what exactly you want.
Hi @Nandheswara ,
For path-based servlet, restriction based on extension would not be possible. If you want to restrict servlet based on extension, then you might need to use resource-type based. Please refer to below link for more detailed explanation.
Apache Sling :: Servlets and Scripts
You can also check out also Sling Servlet path vs resourceType - Adobe Experience League Community - 322039 which will explain about the pros and cons of both approaches.
Thanks,
Nikita Garg
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies