May 10, 2020
Estimated Post Reading Time ~

Serving Assets with Selectors

In a project, we recently had the requirement to serve assets with a selector. The reason for adding the selector was that all CUG protected assets need to be served from a specific dispatcher farm.

Unfortunately, the extension of assets is part of the node name in AEM. A sample geometrixx asset is stored as node “GeoCube_Datasheet.pdf” under /content/dam/geometrixx/documents .

When we try to access this with a selector /content/dam/geometrixx/documents/GeoCube_Datasheet.secure.pdf, the resource cannot be resolved, resulting in a 404.

After checking the Sling source code, we’ve found a way to get to the document with the selector: add all the selectors after the extension, and then add extension .res.

Why? that’s quite complicated:

adding the selectors after the original extension makes sure that the resource is actually found during resource resolution
rendering of resources needs to be done by the org.apache.sling.servlets.get.impl.helpers.StreamRendererServlet, but the servlet only accepts resources without an extension or with .res
But our troubles are not quite over yet: If we render this as a .res resource, Apache will serve this as content-type application/x‐dtbresource+xml

The following rewrite rule should take care of that for pdf documents:

RewriteRule ^/content/dam/(.*).secure.pdf /content/dam/$1.pdf.secure.res [PT,L,T=application/pdf]

With this rewrite rule, the asset is actually available for end users at the desired url /content/dam/geometrixx/documents/GeoCube_Datasheet.secure.pdf, hiding the .res extension workaround from the end user.


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.