as above.
I looked around and I found this => org.apache.sling.servlets.annotations.SlingServletFilter. Is this the moderm equivalent?
Thanks!
Views
Replies
Total Likes
Hi @jayv25585659
Yes it is. You can find the full story here: https://medium.com/globant/declarative-services-from-felix-scr-to-osgi-4770509f1965
Long story short, AEM moved from SCR to OSGi annotations, which is exactly the case you raised.
And a basic such modern Filter implementation would look something like this:
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
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;
@Slf4j
@Component(service = Filter.class, immediate = true)
@SlingServletFilter(scope = SlingServletFilterScope.COMPONENT, resourceTypes = "path/to/resource")
public class MyFilter implements Filter {
@Override
public void init(final FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain filterChain)
throws IOException, ServletException {
}
}
You can also check the official Sling documentation. It has the latest syntax not only for this section but additional recommendations.
if you have additional questions feel free to ask. Thank you.
Sling official Documentation:
https://sling.apache.org/documentation/the-sling-engine/servlets.html
Hi @jayv25585659 ,
The modern equivalent to org.apache.felix.scr.annotations.sling.SlingFilter in AEM and Sling applications is typically using OSGi annotations with @Designate and @Activate or configuring filters via the Sling HTTP Filter API (org.apache.sling.api.filter.SlingFilter) alongside OSGi Declarative Services (DS) annotations. Use @Designate to bind filters to configurations.
SCR annotations were used in old Sling versions. From Sling 11 and AEM 6.5+ onwards, OSGi DS annotations and the Sling Filter API are the preferred approach.
Thanks,
Ritesh Mittal
@jayv25585659 Please refer below official sling filter documentation.
https://sling.apache.org/documentation/the-sling-engine/filters.html
@jayv25585659 Did you find the suggestions helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!
Views
Replies
Total Likes