Sling Filter Restring scope to resources | Community
Skip to main content
karthikb1706130
New Participant
Jun 18, 2020 2:38 PM UTC
Solved

Sling Filter Restring scope to resources

Hi,

I want to reggister a filter using a resourceTypes , as part of my code i have implemented the following

@8220494(service = Filter.class, property = {SLING_FILTER_SCOPE_REQUEST, SERVICE_RANKING_MAX,
    SLING_FILTER_RESOURCETYPES + "= testproject/components/page/myDetailsPage"})

 

when i open an page with the above resourcetype, the above filter is not getting triggered, can anyone help me how to overcome this issue. 
also how to register the filter using multiple resourceTypes
 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
AlbinIs1Accepted solution
Level 2
Jun 18, 2020 3:32 PM UTC

try with

@8220494(service = Filter.class, name = "Custom Logging Filter", property = {
SLING_FILTER_SCOPE + "=" + FILTER_SCOPE_REQUEST, Constants.SERVICE_RANKING + ":Integer=1",
SLING_FILTER_RESOURCETYPES + "testproject/components/page/myDetailsPage" })

add SLING_FILTER_RESOURCETYPES multiple times to support different resource types

@8220494(service = Filter.class, name = "Custom Logging Filter", property = {
SLING_FILTER_SCOPE + "=" + FILTER_SCOPE_REQUEST, Constants.SERVICE_RANKING + ":Integer=1",
SLING_FILTER_RESOURCETYPES + "testproject/components/page/myDetailsPage",

SLING_FILTER_RESOURCETYPES + "testproject/components/page/myDetailsPage1"})

 

import static org.apache.sling.engine.EngineConstants.*; for properties

 

Regards

Albin

4 replies

antoniom5495929
Level 7
Jun 19, 2020 12:43 PM UTC

Hi @karthikb1706130 ,

If I understood correctly you want to create a filter which is able to get all the request that comes from a specific resourcetype or list of resourcetype.

If this is your purpose, I think that it's better for you to use a servlet instead of a filter. In this way you can use also multiple resourcetype without problems [0].

 

A suggestion, don't use a filter if it's not required since this filter will be executed every time for each request (also the AEM default request) and then will evaluate the conditions.  

 

[0]https://helpx.adobe.com/experience-manager/using/resourcetypes.html

 

Thanks,

Antonio

Translate
kautuk_sahni
Employee
Jun 23, 2020 8:11 AM UTC
Wonderfully summarized, great response.
Translate
Kautuk Sahni
Vijayalakshmi_S
Level 10
Jun 18, 2020 4:25 PM UTC

Hi @karthikb1706130,

Looks like the resource type to be mentioned in sling.filter.resourceTypes is jcr:primaryType of the resource and not sling:resourceType

For your case, if the pages created using myDetailsPage is created/resides under one hierarchy, then we can restrict the filter using sling.filter.pattern property.

Sample below will apply the filter to pages under /content/learnings

dam:Asset type will not take effect given the pattern. Have just added to showcase the multiple resource types (similar to what @albinis1 said)

 

@Component(service = Filter.class, property = { EngineConstants.SLING_FILTER_SCOPE + "=" + EngineConstants.FILTER_SCOPE_REQUEST, EngineConstants.SLING_FILTER_PATTERN + "=/content/learnings/(.*)", EngineConstants.SLING_FILTER_RESOURCETYPES + "=cq:Page", EngineConstants.SLING_FILTER_RESOURCETYPES + "=dam:Asset", Constants.SERVICE_RANKING + "=500", })

 

 

 

Translate
AlbinIs1Accepted solution
Level 2
Jun 18, 2020 3:32 PM UTC

try with

@8220494(service = Filter.class, name = "Custom Logging Filter", property = {
SLING_FILTER_SCOPE + "=" + FILTER_SCOPE_REQUEST, Constants.SERVICE_RANKING + ":Integer=1",
SLING_FILTER_RESOURCETYPES + "testproject/components/page/myDetailsPage" })

add SLING_FILTER_RESOURCETYPES multiple times to support different resource types

@8220494(service = Filter.class, name = "Custom Logging Filter", property = {
SLING_FILTER_SCOPE + "=" + FILTER_SCOPE_REQUEST, Constants.SERVICE_RANKING + ":Integer=1",
SLING_FILTER_RESOURCETYPES + "testproject/components/page/myDetailsPage",

SLING_FILTER_RESOURCETYPES + "testproject/components/page/myDetailsPage1"})

 

import static org.apache.sling.engine.EngineConstants.*; for properties

 

Regards

Albin

Translate