Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session
SOLVED

how to add more than one pattern to slingServletFilter?

Avatar

Level 9

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

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

View solution in original post

3 Replies

Avatar

Community Advisor

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

Avatar

Correct answer by
Community Advisor

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