Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

AEM, debug does not catch SlingServletFilter

Avatar

Level 1

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.

1 Accepted Solution

Avatar

Correct answer by
Level 4

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
}
}
}

View solution in original post

2 Replies

Avatar

Community Advisor

@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"
})

Avatar

Correct answer by
Level 4

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
}
}
}