Sling Filter - Access an asset URL with the RequestDispatcher
Hello!
I am working in a feature about Sling Filters in AEM. I have to intercept a specific URL in AEM, e.g /en-us/abc/folder1 and without modify that URL, forward an asset URL from the DAM folder. The asset exist in the DAM folder at this location: /content/dam/myApp/test.pdf. So here is an approach of this implementation:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (!(request instanceof SlingHttpServletRequest) || !(response instanceof SlingHttpServletResponse)) { // Not a SlingHttpServletRequest/Response, so ignore. chain.doFilter(request, response); // This line would let you // proceed to the rest of the // filters. return; } final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request; final SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) response; String tempURI = ""; //some impl here if (matchFound) { tempURI = "/content/dam/app/test.pdf"; RequestDispatcher dispatch = slingRequest.getRequestDispatcher(tempURI); dispatch.forward(slingRequest, slingResponse); return; } chain.doFilter(request, response); }But the problem is that I get an empty or blank pdf so the asset is not loaded correctly. I tried instead of forwarding an DAM asset go to a URL page and is working good in that case. So, I wonder if the forward method only works for pages (contentType="text/html") and how can I do this by retrieving the asset with that filter ?
Any help will be appreciated!
Thanks