AEM 6.3 using Filter, does not work

Viewed 1356

I want to catch all requests inside a filter in AEM 6.3. The filter is the following:

@Component(
        property = {
                "pattern=/.*",
                "service.ranking=" + TryFilter.SERVICE_RANKING
        }
)
public class TryFilter implements Filter {

    public static final int SERVICE_RANKING = 2147483647;

    private static final Logger LOG = LoggerFactory.getLogger(TryFilter.class);
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        LOG.debug("Begin Filter");
        LOG.debug("End Filter");
    }

    @Override
    public void destroy() {
    }

The problem is the filter does not catch requests like http://localhost:4502/aem/start.html Especially requests that end in .html. Do you have any idea about what can be the problem? I checked the patternUrl for the filter, but for http://localhost:4502/crx/de/index.jsp it works.

1 Answers

To sumarize, you should register your filter service as:

@Service
@Component(metatype = true,
    immediate = true,
    label = "An example filter for html requests.",
    description = "This filter will handle html requests. Actual logic performed inside doFilter method.")
@Properties({ 
    @Property(name = EngineConstants.SLING_FILTER_SCOPE, value = { EngineConstants.FILTER_SCOPE_REQUEST }, propertyPrivate = true),
    @Property(name = Constants.SERVICE_RANKING, intValue = TryFilter.SERVICE_RANKING, propertyPrivate = true),
    @Property(name = EngineConstants.SLING_FILTER_PATTERN,  value = "*.html", propertyPrivate = true)
    })

Beside I would recommend adding a log inside the activate method beside one inside init

/**
 * Handle OSGi activation.
 *
 * @param context osgi component context
 */
@Activate
protected void activate(final ComponentContext context) {
    if (LOG.isInfoEnabled()) {
        TryFilter.LOG.info("activate " + getClass());
    }
}

UPDATE

  • Prior to AEM 6.3

Felix Filters were registered with only the pattern property, as in your example, which defined path pattern for which the filter responds to.

@Component(immediate=true)
@Property(
    name = "pattern",
    value = ".*"
)
@Service
public class TryFilter implements Filter { .. }
  • Starting AEM 6.3

Felix Filters must provide the osgi.http.whiteboard.filter.regex and osgi.http.whiteboard.context.select properties to be properly registered.

@Component
@Properties({
        @Property( name = HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_REGEX,
                value = "/"
        ),
        @Property( name = HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT,
                value = ("(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=org.osgi.service.http)")
        )
})
@Service
public class TryFilter extends Filter { ... }

The following dependencies are required to be updated in order for the annotations to be processed properly.

<dependency>
    <groupId>org.osgi</groupId>
    <artifactId>osgi.core</artifactId>
    <version>6.0.0</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.osgi</groupId>
    <artifactId>osgi.cmpn</artifactId>
    <version>6.0.0</version>
    <scope>provided</scope>
</dependency>
Related