One of the major new feature added in
the Portlet technology by the JSR 286 is the Portlet Filter. A
portlet filter is a Java component that can be used to modify portlet
request and response before/after any lifecycle method of the portlet
is being executed. The concept of Portlet filter is same as that of
servlet filter. The only difference is that a servlet has only one
request handling method i.e. service() method and therefore there is
only one type of the servlet filter. A portlet has four types of
request handling methods and therefore there are 4 different types of
portlet filters. The portlet API defines following interfaces for
creating Portlet filter:
(for serveResource method)
Each of the above filter interface
contains a single doFilter(\*Request, \*Response, FilterChain chain)
method which differs in the type of request and response object. For
example, doFilter() method in ActionFilter takes ActionRequest and
ActionResponse while doFilter() method of RenderFilter takes
RenderRequest and RenderResponse objects. Each of the above filter
interface extends a common base interface called
javax.portlet.filter.PortletFilter. This common base interface
contains init(javax.portlet.filter.FilterConfig filterConfig) and
destroy() methods. The init(...) method makes sure that every Filter
has access to a FilterConfig object from which it can obtain its
initialization parameters, a reference to the PortletContext which it
can use, for example, to load resources needed for filtering tasks.
The init() and destroy() methods of a portlet filter are being called
only once during their lifetime.
A single filter class can provide
filter functionality for more than one lifecycle methods and also a
single filter can provide filter functionality for more than one
portlet. There can be multiple filter associated with one lifecycle
method of a portlet. The javax.portlet.filter.FilterChain class
(created by Portlet container) is used to invoke the sequence of
filters applicable for a particular lifecycle method of a portlet.
The doFilter() method of a portlet filter may create customized
request and response objects by using \*RequestWrapper and
\*ResponseWrapper classes and passing these wrappers to the doFilter()
method of FilterChain.
In order to write a portlet filter, the
developer is required to do the folowing 2 things :
1) Write a filter class - A filter
class should implement one or more of the above mentioned four
interfaces and should provide a no argument public constructor. The
filter class is also required to override init() and destroy() method
of javax.portlet.filter.PortletFilter interface.
2) Add entry of filter in portlet.xml -
After writing a portlet filter class, it can be configured by adding
following 2 xml fragements in portlet.xml.
<value>Filter 1 -
portlet-container 2.0 Filter demo :)</value>
The first xml fragment defines a filter
by providing a logical name to it. It tell portlet container about
the filter class and the lifecycle phases supported by it. You can
specify initialization parameters also.
The Second xml fragment specifies the
portlet(s) for which this filter will be applicable. You can specify
a single portlet name or mapping to a group of portlets using the ‘\*’
as a wildcard.