Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Sling Filter - Access an asset URL with the RequestDispatcher

julianh55493871
Level 2
Level 2

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

1 Accepted Solution
smacdonald2008
Correct answer by
Level 10
Level 10

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/

View solution in original post

0 Replies
smacdonald2008
Level 10
Level 10

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

julianh55493871
Level 2
Level 2

smacdonald2008 wrote...

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

 

Hello, have you found something about this ?

Thanks and regards!

julianh55493871
Level 2
Level 2

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.

smacdonald2008
Correct answer by
Level 10
Level 10

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/

View solution in original post

Premkarthic-7WP
Level 4
Level 4

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