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

Adobe Summit 2023 [19th to 23rd March, Las Vegas and Virtual] | Complete AEM Session & Lab list

AEM filter fails to redirect when using "download" attribute in <a> tag

Avatar

Level 1

Problem to solve :  to implement gated content on documents or (assets) tagged with a specific tag,if a tag is used and a self generated cookie in not present i have to redirect the user to a form to complete and on the successful submission of the form a cookie will be created. then the user can download the asset

Ref: How to achieve gated content

html download element :

  <a href="https://forums.adobe.com/content/dam/test.pdf" target="_self" download="doc">

</a>

Filter Implementation :

@SlingFilter(order = -700, scope = SlingFilterScope.REQUEST)

public class GatedContentFilter implements Filter {

   private final Logger logger = LoggerFactory.getLogger(getClass());
  private final String TAGSELECTOR = "cq:tags";
  private final String FORMPAGE = "/content/form.html";
  private final String TAGNAME = "derby";

  private final String COOKIE = "TEST"; // eg : cookie name

   @Override

   public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain filterChain) throws IOException, ServletException {

   final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;
  final SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) response;

   String path = slingRequest.getRequestPathInfo().getResourcePath();
  if(!path.isEmpty() && path !=null){

  Resource resource = slingRequest.getResourceResolver().getResource(path);
  if(resource != null){

  Asset asset = resource.adaptTo(Asset.class);
  if(asset != null){

  String assetTags = asset.getMetadataValue(TAGSELECTOR);
  if(assetTags != null){

  String[] assetTagsArray = assetTags.split(",");
  if(assetTagsArray.length > 0){

   for(String tag : assetTagsArray){

   if(tag.toLowerCase().contains(TAGNAME)  ) { //and cookie is not present

  slingResponse.sendRedirect(FORMPAGE);
  return;
   }

  }

  }else{

  logger.info("No tags associated with the asset at: " + path);
   }

  }

  }

  }

  }

  filterChain.doFilter(request, response);
   }

  @Override

   public void init(FilterConfig filterConfig) {

  }

  @Override

   public void destroy() {

  }

}

if this is implemented without the download attribute works fine but i need to have the download attribute.

Thanks.

1 Reply

Avatar

Community Advisor

HI,

the HTML spec's download attribute to a elements. When used, this attribute signifies that the resource it points to should be downloaded by the browser rather than navigating to it. That means response header contain content-type as download that's why the filter is not working.

You can remove the download attribute and check the url and if valid send response as download.