Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.

Sling Filter with multiple resource type and specific query parameter in request

Avatar

Level 2

Hello,

Below are two points I'm trying to cover via my Sling filter but seems not functional properly.

1. I want my filter to be chained on specific resource types e.g.  /apps/a/p1 ,  /apps/a/p2 , /apps/a/p3

2. I want my filter to be chained only on specific query parameters in request (e.g.  https://helloworld.xyz/parent/page-1.html?q=...., https://helloworld.xyz/parent/page-2.html?q=....,   https://helloworld.xyz/parent/page-3.html?q=.... )etc.

 

Below are current state of my filter

1. Filter Scope is of "Request"

2. Tried multiple resource types as below syntax; Assume resource types of page-1 is " /apps/a/p1", resource types of page-2 is " /apps/a/p2"  & resource types of page-3 is " /apps/a/p3".  Also assume content path of pages are as  /content/parent/page-1 etc.

@Component (service = Filter.class, property = {
EngineConstants.SLING_FILTER_SCOPE + "=" + EngineConstants.FILTER_SCOPE_REQUEST,
EngineConstants.SLING_FILTER_METHODS + "=" + "GET",
EngineConstants.SLING_FILTER_EXTENSIONS + "=" + "html",
EngineConstants.SLING_FILTER_PATTERN + "=" + "/content/(parent)/.*",
EngineConstants.SLING_FILTER_RESOURCETYPES + "=['/apps/a/p1','/apps/a/p2',/apps/a/p3']"
})

But I'm unable to register (tried with comma separated values also) and also when i'm using @SlingServletFilter annotation, same issue is taking place.

@SlingServletFilter(scope = { SlingServletFilterScope.REQUEST},
resourceTypes = {"/apps/a/p1","/apps/a/p2","/apps/a/p3"},
pattern = "/content/(parent)/.*",
extensions = {".html"},
methods = {"GET"}) 

 

10 Replies

Avatar

Community Advisor

Hi,

Can you check.

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

 

I have achieve this in the past using regex pattern.



Arun Patidar

Avatar

Level 2

Hi Arun, 

 

I tried with these two patterns for resourceTypes -- i) one with absolute path  "/apps/a/(p1|p2|p3)"  and ii) with relative path "a/(p1|p2|p3)"; Do I need to try something different?

Avatar

Community Advisor

Hi,

Not sure if this will work with resourceType because the resourcetype argument is for component scope. I am not 100% sure but I used this when I need to intercept the component rendering 

e.g. https://github.com/arunpatidar02/aem63app-repo/blob/master/java/XFFilter.java  



Arun Patidar

Avatar

Level 2

Thanks Arun for sharing the input ! seems the resourceType is not considered in request scope (as you mentioned) so it was not working e.g. For the page requests, the resource type seems actually synonymous to primary type of the page node i.e. "cq:Page"..

Avatar

Community Advisor

Yes, all the primary node types also considered as resource type when registered for filter, servlet etc.



Arun Patidar

Avatar

Community Advisor

Hi @DJSarkar 

Is your resourceType getting resolved? Ignore below If not. (resourceType is an relative path just double check)

@Component (service = Filter.class, property = {
EngineConstants.SLING_FILTER_SCOPE + "=" + EngineConstants.FILTER_SCOPE_REQUEST,
EngineConstants.SLING_FILTER_METHODS + "=" + "GET",
EngineConstants.SLING_FILTER_EXTENSIONS + "=" + "html",
EngineConstants.SLING_FILTER_PATTERN + "=" + "/content/(parent)/.*",
EngineConstants.SLING_FILTER_RESOURCETYPES + "=" + "/apps/a/p1",
EngineConstants.SLING_FILTER_RESOURCETYPES + "=" + "/apps/a/p2",
EngineConstants.SLING_FILTER_RESOURCETYPES + "=" + "/apps/a/p3",
})

Then get queryString parameter from the URL to check and filter chain.


Regards,
Santosh

Avatar

Level 2

Hi Santosh, 

Thanks for the response. I tried multiple resource types the way you mentioned. But seems it didn't work. Also I tried with absolute and relative path as well. and at last, I tried with regex pattern as -  "/apps/a/(p1|p2|p3)" but got no luck.

if I just remove the page resourceTypes and keep rest fields, it's working fine.

Avatar

Community Advisor

@DJSarkar 

It seems OOTB, there's no possibility of associating Sling Filters with Sling Resource Types. Found one workaround here to achieve it: https://stackoverflow.com/questions/16886232/does-sling-allow-component-filters-configured-to-target...

 

Hope that Helps!

Avatar

Level 2

Hi Santosh, 

 

Yes! seems the resourceType is not considered in request scope (as Arun mentioned) so it was not working e.g. For the page requests, the resource type seems actually synonymous to primary type of the page node i.e. "cq:Page".

i) COMPONENT SCOPE is working fine with the specific resource types with the format which you mentioned earlier.

ii) But REQUEST SCOPE is working with REQUEST/RESOURCE_PATTERN but seems can not cover resource type.

 

Avatar

Community Advisor

Hi @DJSarkar - 

Have you tried the relative paths with SlingServletFilter annotation like below?

@SlingServletFilter(scope = { SlingServletFilterScope.REQUEST},
resourceTypes = {"a/p1","a/p2","a/p3"},
pattern = "/content/(parent)/.*",
extensions = {".html"},
methods = {"GET"})