anotherdave
anotherdave

Reputation: 6744

Does Sling allow Component Filters configured to target only certain resource types?

I've found Sling's ability to associate Servlets with certain resource types, selectors and extentions, methods really useful in component development.

Now I'm starting to look into the ComponentFilterChain & would like to create filters that only register against certain resource types, in the same way as Servlets above.

From the Example filters on the Sling project, I see that there's a pattern property that you can apply for particular paths, though it feels like this limits the benefit of having components.

Really what I'm looking for is an equivalent property to sling.servlet.resourceType that I can annotate my Filter with so that only certain components enter this filter as part of the component filter chain, rather than having to check the component resourceType/superResourceType within the Filter.

Is this possible with Sling filters? Or is there an equivalent approach that can be used?

Upvotes: 2

Views: 776

Answers (2)

toniedzwiedz
toniedzwiedz

Reputation: 18553

As of Apache Sling 2.6.14, there is an option to associate Sling Filters with resource types.

The property you need to add to your OSGi service to achieve this is sling.filter.resourceTypes.

There's a set of annotations available that make the job easier.

From the Sling Documentation

   //...
   import org.apache.sling.servlets.annotations.SlingServletFilter;
   import org.apache.sling.servlets.annotations.SlingServletFilterScope;
   import org.osgi.service.component.annotations.Component;

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

       // ... implementation
   }

Upvotes: 1

Bertrand Delacretaz
Bertrand Delacretaz

Reputation: 6100

Out of the box, there's no way to associate servlet Filters with Sling resource types. Composing OSGi services, maybe using sling:resourceType values set as service properties, should allow you to provide similar functionality.

Upvotes: 3

Related Questions