sling.servlet.paths don't work in Servlet | Community
Skip to main content
arturl43391132
Level 4
November 5, 2019
Solved

sling.servlet.paths don't work in Servlet

  • November 5, 2019
  • 3 replies
  • 7771 views

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.

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,

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

3 replies

November 5, 2019

Thanks

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
November 5, 2019

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.SlingServletResolver  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
arturl43391132
Level 4
November 12, 2019

Hi, @Arun Patidar
Thanks for your help.

Level 3
November 28, 2023

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);
        }
    }
}