Expand my Community achievements bar.

SOLVED

How to specify the order of filters in AEM?

Avatar

Level 9

We define several filters like this:

 

@component

@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?

1 Accepted Solution

Avatar

Correct answer by
Level 9

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?

View solution in original post

6 Replies

Avatar

Level 7

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

 

 

 

Avatar

Level 9

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.

Avatar

Community Advisor

@TB3dock you can use 

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

Avatar

Community Advisor

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.

Avatar

Level 9

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?

Avatar

Correct answer by
Level 9

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?