Sling filters - what's the modern equivalent to org.apache.felix.scr.annotations.sling.SlingFilter? | Community
Skip to main content
jayv25585659
Level 8
November 24, 2024
Solved

Sling filters - what's the modern equivalent to org.apache.felix.scr.annotations.sling.SlingFilter?

  • November 24, 2024
  • 5 replies
  • 1080 views

as above.

 

I looked around and I found this => org.apache.sling.servlets.annotations.SlingServletFilter. Is this the moderm equivalent?

 

Thanks!

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Tethich

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.

5 replies

Tethich
Community Advisor
TethichCommunity AdvisorAccepted solution
Community Advisor
November 24, 2024

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.

Tethich
Community Advisor
Community Advisor
November 24, 2024

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 { @9944223 public void init(final FilterConfig filterConfig) throws ServletException { } @9944223 public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain filterChain) throws IOException, ServletException { } }
Preetpal_Bindra
Community Advisor
Community Advisor
November 25, 2024

@jayv25585659 

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

 

Ritesh_Mittal
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
November 25, 2024

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

Rohit_Utreja
Community Advisor
Community Advisor
November 25, 2024

@jayv25585659 Please refer below official sling filter documentation.

https://sling.apache.org/documentation/the-sling-engine/filters.html

kautuk_sahni
Community Manager
Community Manager
November 26, 2024

@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!

Kautuk Sahni