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);
// @580286
// 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);
}
}
}