Sling Filter with multiple resource type and specific query parameter in request | Community
Skip to main content
Level 2
May 31, 2022

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

  • May 31, 2022
  • 3 replies
  • 4332 views

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"}) 

 

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

3 replies

arunpatidar
Community Advisor
Community Advisor
May 31, 2022

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
DJSarkarAuthor
Level 2
June 1, 2022

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?

arunpatidar
Community Advisor
Community Advisor
June 1, 2022

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
SantoshSai
Community Advisor
Community Advisor
May 31, 2022

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

Santosh Sai
DJSarkarAuthor
Level 2
June 1, 2022

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.

SantoshSai
Community Advisor
Community Advisor
June 1, 2022

@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-only-certain-resource-ty#comment24398528_16893230

 

Hope that Helps!

Santosh Sai
Fanindra_Surat
Community Advisor
Community Advisor
June 1, 2022

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"})