Sling Servlet Filter | Community
Skip to main content
October 18, 2021
Solved

Sling Servlet Filter

  • October 18, 2021
  • 5 replies
  • 10320 views

All, 

 

I have a use case where I need to implement a custom feature in asset share search page. 

Use case : I need to apply search filtering at page loading, if I have a suffix in the URL, for example : en/mediacenter.html/Image/Rolex => filter assets with a tag : Rolex. 

 

The solution I figured out, is to implement a Sling servlet filter, to intercept the request, if I have the suffix then I create a predicate and call the search model otherwise do nothing and proceed with normal processing. 

 

I have two questions : 

  1. what do you think about this solution ? do you have better suggestions ? 
  2. I stated implementing a sling servlet filter, but my request is never intercepted by my filter. 

Here is my code snippet : 

@8220494 @SlingServletFilter( scope = {SlingServletFilterScope.REQUEST}, resourceTypes = {BrandFilterServlet.RESOURCE_TYPE}, pattern = "content/assetshare/.*", extensions = {"html"}, methods = {"GET"}) public class BrandFilterServlet implements Filter { public static final String RESOURCE_TYPE = "asset-share-commons/components/structure/search-page"; private static final Logger log = LoggerFactory.getLogger(RequestLogger.class.getName());

 

Any help please ? 

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 abinaya95

May be you should use the DS annotions for this 

@Component(service = javax.servlet.Filter.class,
name ="Sample Filter",
configurationPid = "org.foo.core.filters.SampleFilter",
property = { "process.label= Sample Filter", 
		EngineConstants.SLING_FILTER_SCOPE + "=" + EngineConstants.FILTER_SCOPE_REQUEST,
		"pattern='/content/foo/en/'",
		"resourceTypes=" + SampleFilter.RESOURCE_TYPE,
		Constants.SERVICE_RANKING+ ":Integer=0"})

 

When I used these annotations the calls started working. 

Reference: https://github.com/Adobe-Marketing-Cloud/aem-guides-wknd/blob/master/core/src/main/java/com/adobe/aem/guides/wknd/core/filters/LoggingFilter.java

 

Hope this helps. 

Thanks 

Abinaya M

5 replies

October 18, 2021

Further information : 

 

Sanjay_Bangar
Community Advisor
Community Advisor
October 18, 2021

Hi @karim_onizuka ,

                            yes, for your use case sling servlet filter is better option  and  go through the below url for latest annotation  to create filter.

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

 

Regards,

Sanjay

 

October 18, 2021

Hello @sanjay_bangar 

Thank you for your replay, I used that link to create the code snipet above 🙂 but it does not work

abinaya95Accepted solution
October 18, 2021

May be you should use the DS annotions for this 

@Component(service = javax.servlet.Filter.class,
name ="Sample Filter",
configurationPid = "org.foo.core.filters.SampleFilter",
property = { "process.label= Sample Filter", 
		EngineConstants.SLING_FILTER_SCOPE + "=" + EngineConstants.FILTER_SCOPE_REQUEST,
		"pattern='/content/foo/en/'",
		"resourceTypes=" + SampleFilter.RESOURCE_TYPE,
		Constants.SERVICE_RANKING+ ":Integer=0"})

 

When I used these annotations the calls started working. 

Reference: https://github.com/Adobe-Marketing-Cloud/aem-guides-wknd/blob/master/core/src/main/java/com/adobe/aem/guides/wknd/core/filters/LoggingFilter.java

 

Hope this helps. 

Thanks 

Abinaya M

Vijayalakshmi_S
October 19, 2021

Hi @karim_onizuka,

Can you try with just the scope and pattern first and then we can refine with other properties. 

In the snippet that you have shared, "/" is missing before content, not sure if its a typo while adding the code here. 

Use the below snippet as is in your local instance and execute any we-retail site page. If it works, use your path pattern first then followed by other properties based on your requirement one by one. (so that we can narrow down accordingly)

 

package com.aem.demoproject.core.filters;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.engine.EngineConstants;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import java.io.IOException;

@Component(service = Filter.class,
        property = {
                EngineConstants.SLING_FILTER_SCOPE + "=" + EngineConstants.FILTER_SCOPE_REQUEST,
                "sling.filter.pattern=" + "/content/we-retail/.*"
        })

public class DemoFilter implements Filter {

    private final Logger logger = LoggerFactory.getLogger(DemoFilter.class);

    @Override
    public void doFilter(final ServletRequest request, final ServletResponse response,
                         final FilterChain filterChain) throws IOException, ServletException {

        final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;
        logger.info("request for {}, with selector {} and suffix={} ", slingRequest
                .getRequestPathInfo().getResourcePath(), slingRequest
                .getRequestPathInfo().getSelectorString(), slingRequest.getRequestPathInfo().getSuffix());

        filterChain.doFilter(request, response);
    }

    @Override
    public void init(FilterConfig filterConfig) {
    }

    @Override
    public void destroy() {
    }

}