Hi everyone,
We are trying to add the option of making specific folder paths within our DAM non-indexable. Currently, we are handling via robots.txt but wanted to see the alternatives specifically for assets. We do have the functionality set up for pages but I couldn't seem to find anything for specific DAM content. Any insight would be greatly appreciated. Thank you.
Solved! Go to Solution.
Views
Replies
Total Likes
Hi @asdasdsa
You can use sling filters to add the X-Robots-Tag
header with the value noindex, nofollow
for specific DAM folders
Sling Filter
package com.example.core.filters;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.engine.EngineConstants;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.framework.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@Component(service = Filter.class,
property = {
EngineConstants.SLING_FILTER_SCOPE + "=" + EngineConstants.FILTER_SCOPE_REQUEST,
Constants.SERVICE_RANKING + "=1000"
})
public class XRobotsTagFilter implements Filter {
private static final Logger log = LoggerFactory.getLogger(XRobotsTagFilter.class);
private static final String X_ROBOTS_TAG = "X-Robots-Tag";
private static final String NOINDEX_NOFOLLOW = "noindex, nofollow";
private static final String[] DAM_FOLDERS = {"/content/dam/folder1", "/content/dam/folder2"};
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// Initialization logic, if needed
}
@Override
public void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, FilterChain chain)
throws IOException, ServletException {
if (request instanceof SlingHttpServletRequest && response instanceof SlingHttpServletResponse) {
SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;
SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) response;
ResourceResolver resourceResolver = slingRequest.getResourceResolver();
String requestPath = slingRequest.getRequestURI();
for (String folder : DAM_FOLDERS) {
if (StringUtils.startsWith(requestPath, folder)) {
slingResponse.setHeader(X_ROBOTS_TAG, NOINDEX_NOFOLLOW);
log.debug("Added X-Robots-Tag header to the response for path: {}", requestPath);
break;
}
}
}
// Continue the filter chain
chain.doFilter(request, response);
}
@Override
public void destroy() {
// Cleanup logic, if needed
}
}
Config. , example osgi
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.metatype.annotations.Designate;
import org.osgi.service.metatype.annotations.ObjectClassDefinition;
@Component(service = Filter.class,
property = {
EngineConstants.SLING_FILTER_SCOPE + "=" + EngineConstants.FILTER_SCOPE_REQUEST,
Constants.SERVICE_RANKING + "=1000"
})
@Designate(ocd = XRobotsTagFilter.Config.class)
public class XRobotsTagFilter implements Filter {
@ObjectClassDefinition(name = "X-Robots-Tag Filter Configuration")
public @interface Config {
String[] dam_folders() default {"/content/dam/folder1", "/content/dam/folder2"};
}
private String[] damFolders;
@Activate
protected void activate(Config config) {
this.damFolders = config.dam_folders();
}
// Rest of the code remains the same
}
Hi @asdasdsa
You can use sling filters to add the X-Robots-Tag
header with the value noindex, nofollow
for specific DAM folders
Sling Filter
package com.example.core.filters;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.engine.EngineConstants;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.framework.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@Component(service = Filter.class,
property = {
EngineConstants.SLING_FILTER_SCOPE + "=" + EngineConstants.FILTER_SCOPE_REQUEST,
Constants.SERVICE_RANKING + "=1000"
})
public class XRobotsTagFilter implements Filter {
private static final Logger log = LoggerFactory.getLogger(XRobotsTagFilter.class);
private static final String X_ROBOTS_TAG = "X-Robots-Tag";
private static final String NOINDEX_NOFOLLOW = "noindex, nofollow";
private static final String[] DAM_FOLDERS = {"/content/dam/folder1", "/content/dam/folder2"};
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// Initialization logic, if needed
}
@Override
public void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, FilterChain chain)
throws IOException, ServletException {
if (request instanceof SlingHttpServletRequest && response instanceof SlingHttpServletResponse) {
SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;
SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) response;
ResourceResolver resourceResolver = slingRequest.getResourceResolver();
String requestPath = slingRequest.getRequestURI();
for (String folder : DAM_FOLDERS) {
if (StringUtils.startsWith(requestPath, folder)) {
slingResponse.setHeader(X_ROBOTS_TAG, NOINDEX_NOFOLLOW);
log.debug("Added X-Robots-Tag header to the response for path: {}", requestPath);
break;
}
}
}
// Continue the filter chain
chain.doFilter(request, response);
}
@Override
public void destroy() {
// Cleanup logic, if needed
}
}
Config. , example osgi
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.metatype.annotations.Designate;
import org.osgi.service.metatype.annotations.ObjectClassDefinition;
@Component(service = Filter.class,
property = {
EngineConstants.SLING_FILTER_SCOPE + "=" + EngineConstants.FILTER_SCOPE_REQUEST,
Constants.SERVICE_RANKING + "=1000"
})
@Designate(ocd = XRobotsTagFilter.Config.class)
public class XRobotsTagFilter implements Filter {
@ObjectClassDefinition(name = "X-Robots-Tag Filter Configuration")
public @interface Config {
String[] dam_folders() default {"/content/dam/folder1", "/content/dam/folder2"};
}
private String[] damFolders;
@Activate
protected void activate(Config config) {
this.damFolders = config.dam_folders();
}
// Rest of the code remains the same
}
Works perfectly, thank you very much!
Thanks!
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies