Filters has been edited by Mike Mueller (Apr 23, 2009).

(View changes)

Content:

Servlet Filter Support

Sling supports filter processing by applying filter chains to the requests before actually dispatching to the servlet or script for processing. Filters to be used in such filter processing are plain OSGi services for the type javax.servlet.Filter which of course means that the services implement this interface.

For Sling to pick up a javax.servlet.Filter service for filter processing two service registration properties are inspected:

Property Type Default Value Valid Values Description
filter.scope String request component, resource Indication of which chain the filter should be added to. By default a filter is added to the request filter chain. The default value also applies if the value of this property is any other value than one of the valid values.
filter.order Integer Integer.MAX_VALUE Any Integer value Indication of where to place the filter in the filter chain. The smaller the number the earlier in the filter chain. This value may span the whole range of integer values. The default value causes the filter to just be appended to the filter chain.

Filter Chains

Sling maintains two filter chains: the request filter chain and the component filter chain.

The filters on the request filter chain are only called once for the complete request processing. The request level filter chain is comparable to the set of filters configured as <dispatcher>REQUEST</dispatcher> in Servlet API 2.5 web applications (see section SRV.6.2.5 Filters and the RequestDispatcher).

The filters on the component filter chain are processed for each included resource initiated through the RequestDispatcher. The component level filter chain is comparable to the set of filters configured as <dispatcher>INCLUDE,FORWARD,ERROR</dispatcher> in Servlet API 2.5 web applications (see section SRV.6.2.5 Filters and the RequestDispatcher). Note, that it is currently not possible to call filters only on include, forward, or error or any combination.

Filter Processing

Filter processing is part of the Sling request processing, which may be sketched as follows:

Request Level
Authentication
Resource Resolution
Servlet/Script Resolution
Request Level Filter Processing
Component Level

The first step of request processing is the Request Level processing which is concerned with resolving the resource, finding the appropriate servlet and calling into the request level filter chain. The next step is the Component Level processing, calling into the component level filters before finally calling the servlet or script:

Component Level
Component Level Filter Processing
Call Servlet or Script

When a servlet or script is including another resource for processing through the RequestDispatcher (or any JSP tag or other language feature ultimately using a RequestDispatcher) the following Dispatch processing takes place:

Dispatch
Resolve the resource to dispatch to if not already defined when getting the RequestDispatcher
Servlet/Script resolution
Component Level

Note, that after ensuring the resource to be included (or forwarded to) and resolving the servlet or script to handle the included (or forwarded) resource, the Component Level processing takes place as for the general request processing.

As a consequence, request level filters will be called at most once during request processing (they may not be called at all if a filter earlier in the filter chain decides to terminate the request) while the component level filters may be called multiple times while processing a request.

Reply via email to