The 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 is 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="/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.