How to specify the order of filters in AEM? | Community
Skip to main content
Level 8
August 18, 2022
Solved

How to specify the order of filters in AEM?

  • August 18, 2022
  • 4 replies
  • 3834 views

We define several filters like this:

 

@8220494

@SlingServletFilter(

  scope = { SlingServletFilterScope.REQUEST },

  pattern = "/bin/api/.*",

  methods = { "GET", "POST" }

)

@Designate(ocd = CorrelationLoggingFilter.Config.class)

public class CorrelationLoggingFilter implements Filter {

 

The question is, how do we specify the order the filters are executed in?

 

Is this documented anywhere?

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

I found the solution, and it seems to work:

 

@component

@SlingServletFilter(

  scope = { SlingServletFilterScope.REQUEST },

  pattern = "/bin/api/.*",

  methods = { "GET", "POST" }

)

@ServiceRanking(-200)

@Designate(ocd = CorrelationLoggingFilter.Config.class)

 

The next question is which fitler is run first, lower or higher order/priority/ranking number?

4 replies

Adobe Employee
August 18, 2022

Two service properties are relevant to define a Sling filter :

  • sling.filter.scope – Indicates the filter chain the filter should be part of. Required! Sling won’t pick up your filter if it isn’t set.
  • service.ranking – Indicates the priority of the filter in the chain. The higher the number the earlier the filter will be invoked. (You should use this)

 

service.ranking Integer 0 (Default) Any Integer value Indication of where to place the filter in the filter chain. The higher the number the earlier in the filter chain. This value may span the whole range of integer values. Two filters with equal service.ranking property value (explicitly set or default value of zero) will be ordered according to their service.id service property as described in section 5.2.5, Service Properties, of the OSGi Core Specification R 4.2.

 

For more information refer - https://sling.apache.org/documentation/the-sling-engine/filters.html

 

 

 

TB3dockAuthor
Level 8
August 18, 2022

FYI, we are not using this type of annotation, as its not clear how we convert form the annotations we are using to these different ones, and they appear to be incompatible (we dont know how to combine the two types).  We are looking for a solution we an add to our exsting code.

HeenaMadan
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
August 18, 2022

@tb3dock you can use 

@ServiceRanking(500) to provide ranking. try this.

HeenaMadan
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
August 18, 2022

Order of filters is handled by sling ranking. Indication of where to place the filter in the filter chain. The higher the number the earlier in the filter chain. This value may span the whole range of integer values. Two filters with equal service.ranking property value (explicitly set or default value of zero) will be ordered according to their service.id. Syntax:

@Component(property=Constants.SERVICE_RANKING+":Integer=5000")

Refer https://sling.apache.org/documentation/the-sling-engine/filters.html for more details.

TB3dockAuthor
Level 8
August 18, 2022

We have found this annotation: 

@ServiceRanking

 

So we are guessing this might work:

 

@SlingServletFilter(

  scope = { SlingServletFilterScope.REQUEST },

  pattern = "/bin/api/.*",

  methods = { "GET", "POST" }

)

@ServiceRanking( value = 1000)

@Designate(ocd = CorrelationLoggingFilter.Config.class)

public class CorrelationLoggingFilter implements Filter {

 

The question is, how do we see the order of the existing servlets which dont have rankings?  is there some sort of tool, or do we need to add a lot of debug statements and look at the logs?

TB3dockAuthorAccepted solution
Level 8
August 18, 2022

I found the solution, and it seems to work:

 

@component

@SlingServletFilter(

  scope = { SlingServletFilterScope.REQUEST },

  pattern = "/bin/api/.*",

  methods = { "GET", "POST" }

)

@ServiceRanking(-200)

@Designate(ocd = CorrelationLoggingFilter.Config.class)

 

The next question is which fitler is run first, lower or higher order/priority/ranking number?