I am trying to implement a Sling Servlet Filter which aims to be invoked when I upload any .json file into filtertest folder, following the tutorial on https://sling.apache.org/documentation/the-sling-engine/filters.html
Here is the annotations and attributes in it:
@SlingServletFilter(scope= {SlingServletFilterScope.REQUEST},
pattern = "/content/dam/filtertest/.*",
extensions = {"json"},
methods = {"GET", "HEAD", "PUT"})
However, debug does not catch the breakpoint in this class' doFilter method.
What is the problem in here? I could not figure it out.
Solved! Go to Solution.
Hi ,
Please check the status of the filter .
http://localhost:4502/system/console/status-slingfilter
Try checking the servlet filter component in OSGI components console and check if it is enabled and also reload once.
sample code:
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
SlingHttpServletRequest request = (SlingHttpServletRequest) req;
SlingHttpServletResponse response = (SlingHttpServletResponse) res;
WCMMode mode = WCMMode.fromRequest(request);
if(WCMMode.EDIT!=mode) {
String requestURL = request.getRequestURI(); //page url
String extensions = request.getRequestPathInfo().getExtension(); //page extensions
if(extensions.contains("json")) {
//your logic here
}
}
}
@coderninja Just update your Service class with following code
@Component(
service = Filter.class,
immediate = true,
name = "Sample Filter for page request",
property = {
Constants.SERVICE_DESCRIPTION + "=Demo to filter incoming requests",
EngineConstants.SLING_FILTER_SCOPE + "=" + EngineConstants.FILTER_SCOPE_REQUEST,
Constants.SERVICE_RANKING + ":Integer=-100",
EngineConstants.SLING_FILTER_REQUEST_PATTERN + "=/content/dam/.*",
})
@SlingServletFilter(
resourceTypes = {
"dam:Asset"
})
Hi ,
Please check the status of the filter .
http://localhost:4502/system/console/status-slingfilter
Try checking the servlet filter component in OSGI components console and check if it is enabled and also reload once.
sample code:
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
SlingHttpServletRequest request = (SlingHttpServletRequest) req;
SlingHttpServletResponse response = (SlingHttpServletResponse) res;
WCMMode mode = WCMMode.fromRequest(request);
if(WCMMode.EDIT!=mode) {
String requestURL = request.getRequestURI(); //page url
String extensions = request.getRequestPathInfo().getExtension(); //page extensions
if(extensions.contains("json")) {
//your logic here
}
}
}