Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Custom servlet not getting resolved

Avatar

Level 3

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 {

 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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-... 



Arun Patidar

View solution in original post

6 Replies

Avatar

Level 3

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.

Avatar

Community Advisor

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

Avatar

Level 7

Hi @stiegjo22 

 

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

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

 

Hope this helps.

Avatar

Level 3

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

 

 

Avatar

Correct answer by
Community Advisor

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-... 



Arun Patidar