Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Sling Servlet Filter

Avatar

Level 2

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 : 

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

1 Accepted Solution

Avatar

Correct answer by
Level 1

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/ae...

 

Hope this helps. 

Thanks 

Abinaya M

View solution in original post

6 Replies

Avatar

Community Advisor

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

 

Avatar

Level 2

Hello @Sanjay_Bangar 

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

Avatar

Correct answer by
Level 1

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/ae...

 

Hope this helps. 

Thanks 

Abinaya M

Avatar

Community Advisor

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() {
    }

}