Expand my Community achievements bar.

SOLVED

AEM 6.2 Filters

Avatar

Level 2

We are currently working with an AEM 6.2 (SP1 is not installed) project, and I'm trying to understand and use filters for when an author is in Edit mode in order to restrict the amount of nesting N levels deep for a specific component. Is there any good documentation on Filters? I've seen ACS commons examples, but it doesn't help a whole lot as I'm trying to only trigger the filter on specific URL patterns (such as editor.html/content/{appDir}/*) using the following above my filter:

@Properties({

    @Property(name="service.description", value="A Filter"),

    @Property(name="sling.filter.pattern", value="editor.html/content/informa*")

})

Unable to get the filter to trigger with the above annotations, I decided to try another route by removing the @Properties and check the current WCMMode, only running the validation if it's in EDIT mode. I would prefer the above method work as to not constantly trigger the filter, but if that's not possible, I would expect the below to still work. The below filter will trigger on all requests, and I have logging in my "if" statement that checks for EDIT mode. Now when I make a request to any part of the AEM backend, the log will spit out entries such as:

07.09.2018 15:46:18.468 *DEBUG* [0:0:0:0:0:0:0:1 [1536356778468] GET /libs/granite/csrf/token.json HTTP/1.1] com.informa.filters.ColumnControlLimitFilter ***modeEDIT

07.09.2018 15:46:40.399 *DEBUG* [0:0:0:0:0:0:0:1 [1536356800399] GET /mnt/overlay/granite/ui/content/shell/header/actions/pulse.data.json HTTP/1.1] com.informa.filters.ColumnControlLimitFilter ***modeEDIT

07.09.2018 15:47:25.386 *DEBUG* [0:0:0:0:0:0:0:1 [1536356845386] GET /libs/granite/csrf/token.json HTTP/1.1] com.informa.filters.ColumnControlLimitFilter ***modeEDIT

07.09.2018 15:47:26.335 *DEBUG* [0:0:0:0:0:0:0:1 [1536356846335] GET /libs/granite/csrf/token.json HTTP/1.1] com.informa.filters.ColumnControlLimitFilter ***modeEDIT

07.09.2018 15:47:42.360 *DEBUG* [0:0:0:0:0:0:0:1 [1536356862360] GET /mnt/overlay/granite/ui/content/shell/header/actions/pulse.data.json HTTP/1.1] com.informa.filters.ColumnControlLimitFilter ***modeEDIT

07.09.2018 15:48:44.349 *DEBUG* [0:0:0:0:0:0:0:1 [1536356924349] GET /mnt/overlay/granite/ui/content/shell/header/actions/pulse.data.json HTTP/1.1] com.informa.filters.ColumnControlLimitFilter ***modeEDIT

************* ​Current Code **************

@SlingFilter(

        label = "Filter implementation",

        metatype = true,

        generateComponent = true,

        generateService = true,

        order = 1,

        scope = SlingFilterScope.REQUEST

        )

public class ColumnControlLimitFilter  implements Filter {

  private Logger log = LoggerFactory.getLogger(ColumnControlLimitFilter.class);

  @Override

  public void init(FilterConfig filterConfig) throws ServletException {

  // TODO Auto-generated method stub

  }

  @Override

  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

  WCMMode mode = WCMMode.fromRequest(request);

  if (mode == WCMMode.EDIT) { //Tried mode.equals(WCMMode.EDIT) and same results

    log.debug("***mode" + mode);

  }

  chain.doFilter(request, response);

  }

  @Override

  public void destroy() {

  // TODO Auto-generated method stub

  }

}

Does anybody have any suggestions on how to solve the above problem(s)? Much appreciated in advanced.

Thanks,
Danny

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

For filters the "pattern" property is supported since Sling Engine 2.4.0 (according to [1]). But I don't the correct AEM versions right here to validate, but it's a reasonable assumption that it requires AEM 6.3 or higher.

@Arun: Both styles of annotations are fully supported for all versions of AEM; I don't see any dependencies towards the runtime because both are supposed to generate the same files in a bundle's OSGI-INF folder.

[1] Apache Sling :: Servlet Filter Support

View solution in original post

5 Replies

Avatar

Community Advisor

Hi,

If you want to execute something in filter based on path you can check path using SlingHttpServletRequest

Below is an example of filter which only execute when user login and navigate to welcome screen.

In below I am check path against classic and touch ui welcome screen for particular user to redirect. You can also put checks for path like below:

aem63app-repo/CustomScreenFilter.java at master · arunpatidar02/aem63app-repo · GitHub



Arun Patidar

Avatar

Employee Advisor

The edit mode is implemented as a cookie, which is sent along with every request to AEM. Thus, when it edit mode, every request to the authoring instance will return "edit mode" even if it's not requesting HTML at all.

So what you see is standard behavior.

Jörg

Avatar

Level 2

Thank you Arun and Jörg. It looks like I'll use the approach of checking the request path.

For further clarity, when trying to use the filter pattern on filters like in the example https://github.com/Adobe-Consulting-Services/acs-aem-samples/blob/master/bundle/src/main/java/com/ad...

Is that only available in AEM 6.3 and above?

Thanks,
Danny

Avatar

Community Advisor

Hi,

The above code uses Felix annotation and Felix annotation were suppose to use till 6.2 but compatible to higher version as well. So this code will work in 6.2

But for 6.3 above you should convert this in osgi annotation.

aem63app-repo/CustomScreenFilter.java at master · arunpatidar02/aem63app-repo · GitHub



Arun Patidar

Avatar

Correct answer by
Employee Advisor

For filters the "pattern" property is supported since Sling Engine 2.4.0 (according to [1]). But I don't the correct AEM versions right here to validate, but it's a reasonable assumption that it requires AEM 6.3 or higher.

@Arun: Both styles of annotations are fully supported for all versions of AEM; I don't see any dependencies towards the runtime because both are supposed to generate the same files in a bundle's OSGI-INF folder.

[1] Apache Sling :: Servlet Filter Support