Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

sling.servlet.paths don't work in Servlet

Avatar

Level 4

Hello, AEM Community

I have the following issue:

I want to create a servlet which works with the following properties:

@Component(

   immediate = true,
   service = Servlet.class,
   property = {

   "sling.servlet.extensions=json",
   "sling.servlet.extensions=js",
   "sling.servlet.resourceTypes=sling/servlet/default",
   "sling.servlet.methods=GET",
   "sling.servlet.methods=HEAD",
   "sling.servlet.paths=/content/unified"
   }

)

public class UnifiedSettingsJSONServlet extends SlingSafeMethodsServlet {

...

}

But it activates on all paths, not only specified. If I delete "sling.servlet.resourceTypes=sling/servlet/default", it doesn't work at all. So, I think "sling.servlet.paths" is ignored and I don't know why. Do you have any ideas?

Use:

org.osgi.service.component.annotations.Component

AEM 6.3.

Thanks.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi,

You should not use extension if you are registering servlet by path.

The servlet is working with "sling.servlet.resourceTypes=sling/servlet/default", because you registered as default servlet.

There is a issue with path as well, you need to specify Execution path at http://localhost:4502/system/console/configMgr/org.apache.sling.servlets.resolver.SlingServletResolv...  if you are registering servlet by path

"sling.servlet.paths=/content/unified"

Please check sample servlet aem63app-repo/SimpleGetGroup.java at master · arunpatidar02/aem63app-repo · GitHub



Arun Patidar

View solution in original post

4 Replies

Avatar

Correct answer by
Community Advisor

Hi,

You should not use extension if you are registering servlet by path.

The servlet is working with "sling.servlet.resourceTypes=sling/servlet/default", because you registered as default servlet.

There is a issue with path as well, you need to specify Execution path at http://localhost:4502/system/console/configMgr/org.apache.sling.servlets.resolver.SlingServletResolv...  if you are registering servlet by path

"sling.servlet.paths=/content/unified"

Please check sample servlet aem63app-repo/SimpleGetGroup.java at master · arunpatidar02/aem63app-repo · GitHub



Arun Patidar

Avatar

Level 4

Hi, arturl43391132 and  Arun Patidar,

below is my servelt but when i am using http://localhost:4502/content/dam/html-content/test.html  this url it is not calling servelt i have added the path "/content/dam/html-content" in Apache Sling Servlet Configuration

FYI , We have a business requirement to host static webpages from AEM. Our AEM version is the latest AEM as a Cloud Service - 6.5.


I did try
1. Unchecked "Enable For All Resource Paths" option in the Apache Sling Content
Disposition Filter;
2. Removed the blacklisted "text/html" configuration and unchecked Allow unknown mime types from SafeBinaryGetServlet(DamContentDispositionFilter)

but nothing worked.
let me know if you have better solution

@Component(immediate = true, service = { javax.servlet.Servlet.class }, property = {
        "sling.servlet.methods=GET",
        "sling.servlet.paths=/content/dam/html-content",
        "sling.servlet.methods=HEAD",
        "sling.servlet.resourceTypes=sling/servlet/default",
})

public class CustomDAMSlingServlet extends SlingSafeMethodsServlet {
    private static final Logger log = LoggerFactory.getLogger(CustomDAMSlingServlet.class);

    @Activate
    protected void activate() {
        log.info("CustomDAMSlingServlet activated");
    }

    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
            throws ServletException, IOException {
        try {
            log.info("In the servlet");

            response.getWriter().write("Hello, this is MyCustomServlet for /content/dam/html-content/test.html!");

            log.info("CustomDAMSlingServlet: doGet method started");
            // Get the path of the requested HTML file
            String path = request.getRequestPathInfo().getResourcePath();

            log.info("pathhhhhh: {}");

            response.setHeader("Content-Disposition", "inline; filename=" + path);

            // Read the content of the HTML file from DAM
            InputStream htmlStream = request.getResource().adaptTo(InputStream.class);

            // Set the content type
            response.setContentType("text/html");

            log.info("CustomDAMSlingServletttttt");

            // Copy the content to the response
            IOUtils.copy(htmlStream, response.getOutputStream());

            log.info("CustomDAMSlingServlet: doGet method completed");

        } catch (Exception e) {
            log.error("Error in CustomDAMSlingServlet: " + e.getMessage());
            response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
    }
}