Expand my Community achievements bar.

SOLVED

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

Avatar

Level 9

as above.

 

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

 

Thanks!

1 Accepted Solution

Avatar

Correct answer by
Level 8

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.

View solution in original post

6 Replies

Avatar

Correct answer by
Level 8

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.

Avatar

Level 8

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

Avatar

Community Advisor

@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

 

Avatar

Community Advisor

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

Avatar

Administrator

@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