Sling Filter - Access an asset URL with the RequestDispatcher | Community
Skip to main content
julianhs2024
New Participant
November 10, 2016
Solved

Sling Filter - Access an asset URL with the RequestDispatcher

  • November 10, 2016
  • 5 replies
  • 8441 views

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

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 smacdonald2008

I checked with some internal Adobe ppl - this was their response: 

There is some DAM Asset filter that prevents Forwards from occurring. You *might* be able to bump that up to a Felix Servlet Filter and do something up there (might need to wrap the Request and change the URI, not sure)

Check the ACS AEM Samples page for a Felix Servlet Filter if you want to try it out.

ACS AEM Samples -- http://adobe-consulting-services.github.io/acs-aem-samples/

5 replies

Premkarthic-7WP
New Participant
November 18, 2016

Can you try with order = -700, // The smaller the number, the earlier in the Filter chain (can go negative);

Its worked for me.

@SlingFilter(generateComponent = true, generateService = true, order = -700, scope = SlingFilterScope.REQUEST)
public class SampleFilter implements Filter {

    private Logger logger = LoggerFactory.getLogger(SampleFilter.class);

    public void init(FilterConfig filterConfig) throws ServletException {
    }

    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

        logger.info("Inside sample filter {}", slingRequest
                .getRequestPathInfo().getResourcePath());

        boolean matchFound = false;

        if (slingRequest.getRequestPathInfo().getResourcePath()
                .contains("/en-us/abc/folder1")) {

            matchFound = true;
        }

        if (matchFound) {

            tempURI = "/content/dam/geometrixx/documents/GeoPyramid_Datasheet.pdf";

            RequestDispatcher dispatch = slingRequest
                    .getRequestDispatcher(tempURI);
            dispatch.forward(slingRequest, slingResponse);
            return;
        }

        chain.doFilter(request, response);
    }

    public void destroy() {
    }

}

 

Thanks

Prem

smacdonald2008
smacdonald2008Accepted solution
New Participant
November 16, 2016

I checked with some internal Adobe ppl - this was their response: 

There is some DAM Asset filter that prevents Forwards from occurring. You *might* be able to bump that up to a Felix Servlet Filter and do something up there (might need to wrap the Request and change the URI, not sure)

Check the ACS AEM Samples page for a Felix Servlet Filter if you want to try it out.

ACS AEM Samples -- http://adobe-consulting-services.github.io/acs-aem-samples/

julianhs2024
New Participant
November 15, 2016

Hello, I tried another scenario to get an idea about the issue that I am facing.  And when I do a forward to a image or text URL e.g:  en-us/folder1/folder2/test.jpg . I get an "You don't have permission to access to this page. Please contact the Administrator" message, so I wonder if maybe there is something else that is executing before my custom filter. I tried putting a breakpoint and debugging but after the call to 

dispatch.forward(slingRequest, slingResponse);

I get always the same error.

julianhs2024
New Participant
November 14, 2016

smacdonald2008 wrote...

We will look at getting an example of this on mon. 

 

Hello, have you found something about this ?

Thanks and regards!

smacdonald2008
New Participant
November 12, 2016

We will look at getting an example of this on mon.