I'm developing a HTTP filter that redirects to another URL when some image is requested, but the other URL that the new image is, needs authentication. I'm using AEM DAM to store all images. So when the code requests an image locally, the code needs to redirect to DAM and log in, but I can't log in using the code as it is implemented:
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
// ==> /images/path/my-image.png
// ==> /path1/path2/image.pngs
String servletPath = req.getServletPath();
// The path to the root directory of the webapp (WebContent)
//String realRootPath = request.getServletContext().getRealPath("");
String realRootPath = "http://localhost:4502/crx/de/index.jsp#/content/dam/";
// Real path of Image.
String imageRealPath = realRootPath + servletPath;
System.out.println("imageRealPath = " + imageRealPath);
File file = new File(imageRealPath);
// Check image exists.
if (file.exists()) {
// Go to the next element (filter or servlet) in chain
chain.doFilter(request, response);
} else if (!servletPath.equals(this.notFoundImage)) {
// Redirect to 'image not found' image.
HttpServletResponse resp = (HttpServletResponse) response;
// ==> /ServletFilterTutorial + /images/image-not-found.png
resp.sendRedirect(req.getContextPath()+ this.notFoundImage);
}
}