how to add more than one pattern to slingServletFilter? | Community
Skip to main content
Level 8
April 21, 2023
Solved

how to add more than one pattern to slingServletFilter?

  • April 21, 2023
  • 3 replies
  • 1858 views

We want a logging filter to support multiple different groups of endpoints, e.g. /bin/proxy/.*, /bin/someApi/v2.* etc.  We could try to create a regexp that matches each pattern we want to add, or is there a way to add multiple patterns?  Or we could create a filter for each pattern, and point to some common code.

 

@SlingServletFilter(scope = { SlingServletFilterScope.REQUEST }, pattern = "/bin/someApi/v2/.*", methods = { "GET",
"POST" })

 

Best answer by lukasz-m

Hi @tb3dock,

Base on documentation:

Pattern attribute accepts single String value.

Answering your question multiple patters are not supported, to achieve your goal you should follow one of the option you already mentioned:

  • combine multiple patterns into one - this option will be good if you have exactly the same logic for each patter
  • create separate filter for each pattern, and extract common part to some external/abstract class - this will be an alternative to the option #1
  • create separate filter for each pattern - this is obvious choice when you would like to handle requests differently depending on the pattern it matches

3 replies

Siva_Sogalapalli
Community Advisor
Community Advisor
April 21, 2023
Rohit_Utreja
Community Advisor
Community Advisor
April 21, 2023

It can be done by using attribute 'sling.filter.pattern'. Please have a look at the below URL for more details.

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

lukasz-m
Community Advisor
lukasz-mCommunity AdvisorAccepted solution
Community Advisor
April 25, 2023

Hi @tb3dock,

Base on documentation:

Pattern attribute accepts single String value.

Answering your question multiple patters are not supported, to achieve your goal you should follow one of the option you already mentioned:

  • combine multiple patterns into one - this option will be good if you have exactly the same logic for each patter
  • create separate filter for each pattern, and extract common part to some external/abstract class - this will be an alternative to the option #1
  • create separate filter for each pattern - this is obvious choice when you would like to handle requests differently depending on the pattern it matches