Custom servlet not getting resolved | Community
Skip to main content
stiegjo22
Level 4
February 4, 2024
Solved

Custom servlet not getting resolved

  • February 4, 2024
  • 4 replies
  • 1732 views

I'm developing a custom servlet to prevent users not logged into our site from downloading certain content in the DAM. I can see my servlet active in components but I don't see it resolving in the Servlet Resolver Test (system/console/servletresolver) Thanks for any advice.

 

(service = Servlet.class, immediate = true, property = { ServletResolverConstants.SLING_SERVLET_PATHS + "=/content/dam/advisor/*", ServletResolverConstants.SLING_SERVLET_EXTENSIONS + "=pdf,jpg,jpeg", ServletResolverConstants.SLING_SERVLET_SELECTORS + "=download", ServletResolverConstants.SLING_SERVLET_METHODS + "=GET", "service.ranking:Integer=100" } ) public class AnonymousAccessServlet extends SlingAllMethodsServlet {

 

 

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

Hi @stiegjo22 
You can't register a servlet with path pattern, it has to be a absolute path

 ServletResolverConstants.SLING_SERVLET_PATHS + "=/content/dam/advisor/*",

https://sling.apache.org/documentation/the-sling-engine/servlets.html#servlet-registration-1 

 

You should register by resourcetype, example

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/call-aem-servlet-based-on-asset-path/td-p/606605 

4 replies

kapil_rajoria
Community Advisor
Community Advisor
February 5, 2024

Hi @stiegjo22 , did you check these things?
1. Visit system/console/components to see if your servlet is successfully registered.
2. Visit system/console/bundles to check your OSGI Bundle containing the servlet is active.

3. Use debugging statements to check the error in logs.

4. Use a debugger to see up to which point is your servlet is working.

5. Make sure you are testing a correct path with correct selectors and extensions. Double-check that the resource paths you're testing with actually exist in the DAM and are accessible to the servlet.

6. If multiple servlets could handle the same requests, change the service.ranking property to ensure your servlet takes precedence.

7. If your servlet is registered as a component, ensure the component is correctly defined and activated.

8. Check your code is correct.

9. Try restarting your instance.

arunpatidar
Community Advisor
Community Advisor
February 5, 2024

Hi @stiegjo22 
Please check the extensions declaration

ServletResolverConstants.SLING_SERVLET_EXTENSIONS + "=pdf,jpg,jpeg",

 it should be 

 

ServletResolverConstants.SLING_SERVLET_EXTENSIONS + "=pdf", ServletResolverConstants.SLING_SERVLET_EXTENSIONS + "=jpg", ServletResolverConstants.SLING_SERVLET_EXTENSIONS + "=jpeg",
Arun Patidar
iamnjain
Community Advisor
Community Advisor
February 5, 2024

Hi @stiegjo22 

 

Try changing the servlet extensions declaration as array of items as mentioned below.

sling.servlet.extensions = ["html", "txt", "json"]

 

Hope this helps.

stiegjo22
stiegjo22Author
Level 4
February 5, 2024

I updated the extensions declaration in the Component section to: 

 

ServletResolverConstants.SLING_SERVLET_EXTENSIONS + "=pdf", ServletResolverConstants.SLING_SERVLET_EXTENSIONS + "=jpg", ServletResolverConstants.SLING_SERVLET_EXTENSIONS + "=jpeg"

 

However now I see this message when I check a valid pdf resource in the Servlet Resolver Test:

 

The resource given by path ' /content/dam/advisor/market-intelligence' does not exist. Therefore no resource type could be determined! Candidate servlets and scripts in order of preference for method GET: com.day.cq.commons.servlets.NonExistingDispatcherServlet (OptingServlet) org.apache.sling.servlets.get.impl.DefaultGetServlet org.apache.sling.jcr.webdav.impl.servlets.SlingWebDavServlet

 

 

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
February 6, 2024

Hi @stiegjo22 
You can't register a servlet with path pattern, it has to be a absolute path

 ServletResolverConstants.SLING_SERVLET_PATHS + "=/content/dam/advisor/*",

https://sling.apache.org/documentation/the-sling-engine/servlets.html#servlet-registration-1 

 

You should register by resourcetype, example

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/call-aem-servlet-based-on-asset-path/td-p/606605 

Arun Patidar
stiegjo22
stiegjo22Author
Level 4
February 6, 2024

Thanks Arun.