AEM Sling Filter

AEM Sling Filter allows to filter request before actually dispatching to the servlet or script for processing. It helps to preprocess data before reaching it to servlet. Before calling authentication Filter.

Filter is an interface belongs to javax.servlet.filter package. It will call doFilter() method every time request reaches out to Server.

Below are the configuration properties available as part of @SlingServletFilter annotation:

sling.filter.pattern used to declare path where filter will get apply.
pattern = “/content/.*”

sling.filter.selectors declare number of selectors to get match.
selectors = {“abc”}

sling.filter.methods is to provide method name as GET and HEAD.
methods = {“GET”,”HEAD”}

sling.filter.resourceTypes map the filter to respective resoourceTypes.
resourceTypes = {“practice/components/page”}

sling.filter.extensions restrict the filter to specific extensions.
extensions = {“html”, “json”, “text”}

service.ranking comes in case of filter chaining to define the order in which filter get call.

sling.filter.suffix.pattern use to map the request URL suffix.
suffix_pattern = “/suffix”

Example

suffix_pattern = "/suffix",
resourceTypes = {"practice/components/page"},
pattern = "/content/.*",
extensions = {"html"},
selectors = {"abc"},
methods = {"GET","HEAD"})

Filter chains

Sling maintains five filter chains: request level, component level, include filters, forward filters and error filters.

REQUEST Filters get call for every request.

INCLUDE Filters are called upon calling the RequestDispatcher.include method after the included resource.

FORWARD Filters are called upon calling the RequestDispatcher.forward method.

ERROR Filters are called upon HttpServletResponse.sendError or any uncaught Throwable before resolving the error.

COMPONENT REQUEST,INCLUDE,FORWARD The COMPONENT scoped filters are present for backwards compatibility with earlier Sling Engine releases. These filters will be called among the INCLUDE and FORWARD filters upon RequestDispatcher.include or RequestDispatcher.forward as well as before calling the request level Servlet or script after the REQUEST filters.

Filter Implementation

Filter can be disable with the help of OSGI configuration and setting invalid value for sling.filter.scope property.

To convert any class to Filter require to implement Filter interface, annotate with @Component and @SlingServletFilter

Implementation of Filter interface will require to override init()doFilter() and destroy() methods.

doFilter() method will get trigger for every GET | HEAD request reaching out to server.

package com.javadoubts.core.filters;

import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.servlets.annotations.SlingServletFilter;
import org.apache.sling.servlets.annotations.SlingServletFilterScope;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import java.io.IOException;

@Component
@SlingServletFilter(scope = {SlingServletFilterScope.REQUEST},
// resourceTypes = {"practice/components/page"},
pattern = "/content/.*",
extensions = {"html","txt","json"},
// selectors = {"foo","bar"},
methods = {"GET","HEAD"})
public class PracticeFilter implements Filter {

private static final Logger log = LoggerFactory.getLogger(PracticeFilter.class);

@Override
public void init(FilterConfig filterConfig) throws ServletException {

}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
SlingHttpServletResponse slingResponse = (SlingHttpServletResponse)response;
log.error(">>>>>>>>>>>>>>>>>>>>>>> PracticeFilter Called ");
chain.doFilter(request, slingResponse);
}

@Override
public void destroy() {

}
}

Hit URL http://localhost:4502/content/practice.html in browser and check for error.log will shown below output.

Explanation:

Above URL(http://localhost:4502/content/practice.html) is passing all the criteria to call PracticeFilter.java class doFilter() method as we are passing all conditions of path starting with /content/, it has html as an extension and it is GET request.

pattern = “/content/.*”,
extensions = {“html”,”txt”,”json”},
methods = {“GET”,”HEAD”})

Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.

0