Expand my Community achievements bar.

SOLVED

Host static webpages

Avatar

Level 4

Hi,
We have a business requirement to host static webpages from AEM. Our AEM version is the latest AEM as a Cloud Service - 6.5.
for that i have created a sling servelt but when i am clicking on below url:
http://localhost:4502/content/dam/html-content/test.html (which i get when i click on html file , the url:

http://localhost:4502/assetdetails.html/content/dam/html-content/test.html  )  the servelt code is unreachable it is not calling the servelt code Can someone help me ?

Below is my servelt code:

 
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.Servlet;
import javax.servlet.ServletException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.commons.io.IOUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;

@Component(service = Servlet.class, property = {
        "sling.servlet.methods=" + HttpConstants.METHOD_GET,
        "sling.servlet.extensions=html",
        "sling.servlet.paths=/content/dam/html-content"
})

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

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

 

 

1 Accepted Solution

Avatar

Correct answer by
Level 9

@tatrived, Please make sure that the path which you are using to hit the servlet should be part of the allowed paths in "Apache Sling Servlet Configuration".

sravs_0-1701092243119.png

 

As per the best practices please try to create the servlets using resource types. Refer to this for more information about Servlet using resourceType vs path based https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/sling-servlet-path-vs-reso... 

 

View solution in original post

9 Replies

Avatar

Level 9

@tatrived , 

 

As per your code, your servlet path is '/content/dam/html-content' with an extension of 'html'. But you are trying to call the servlet in the child resource. Can you please try hitting http://localhost:4502/content/dam/html-content.html and see if your servlet is getting invoked or not?

Avatar

Correct answer by
Level 9

@tatrived, Please make sure that the path which you are using to hit the servlet should be part of the allowed paths in "Apache Sling Servlet Configuration".

sravs_0-1701092243119.png

 

As per the best practices please try to create the servlets using resource types. Refer to this for more information about Servlet using resourceType vs path based https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/sling-servlet-path-vs-reso... 

 

Avatar

Community Advisor

Your servlet is not correctly registered that's why you cannot invoke it. You cannot combine a servlet registered by path and try to use an extension. Please see this thread, which has some good examples of how to properly register a servlet: link.

 

If you want to host simple HTML files, you can just upload them into the DAM and make sure to adjust the "Content-Disposition" header through the OSGI config. You can take a look at these threads for more information: 
https://experienceleague.adobe.com/docs/experience-manager-65/administering/security/content-disposi... 
http://www.sgaemsolutions.com/2019/10/aempdf-is-getting-downloded-instead-of.html 

 

Hope this helps.



Esteban Bustamante

Avatar

Level 4

Hi EstebanBustamante,


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

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 = trueservice = { 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 requestSlingHttpServletResponse response)
            throws ServletExceptionIOException {
        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(htmlStreamresponse.getOutputStream());

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

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

Avatar

Community Advisor

@tatrived  can you try below code in a AEM instance and then update logic accordingly 

 

package com.example.core.servlets;

import java.io.IOException;

import javax.servlet.ServletException;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.osgi.service.component.annotations.Component;

@Component(
service = {javax.servlet.Servlet.class},
property = {
"sling.servlet.methods=GET",
"sling.servlet.paths=/content/dam/html-content",
"sling.servlet.extensions=html",
"sling.servlet.selectors=test"
}
)
public class MyCustomServlet extends SlingSafeMethodsServlet {

@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws ServletException, IOException {
// Your servlet logic goes here
response.getWriter().write("Hello, this is MyCustomServlet for /content/dam/html-content/test.html!");
}
}

 

 

With these properties, the servlet will respond to GET requests with the URL pattern http://localhost:4502/content/dam/html-content/test.html

 

Avatar

Level 3

Wondering why you need a servlet for that? 

you can simply create a rewrite rule at dispatcher (apache httpd) to server static html file from a desired dam location.

From the URLs seems you are trying to access "static" html files at author instance, right? 

Do you want to edit those files at author instance? 

Avatar

Administrator

@tatrived Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Kautuk Sahni