This section provides information on portlet filters in WebSphere Portal. A portlet filter enables the administrator of a portal to intercept and modify the output of a portlet before it is aggregated to the entire portal page. Thus it is possible to support different languages and markups other than those for which the portlet was originally designed. Portlet filters are also used for adding additional information to the portlet output (for example, a copyright statement), deleting unimportant or restricted content, and for parsing destructive JavaScript.
To use portlet filters, portlet filtering must be enabled for the portal,
the portlet filters must be defined and activated in a properties file, and
the filters must be assigned to the portlet.
The usage of portlet filters is enabled by the legacy.portlet.enable.filtering property in the PortletContainerService.properties file. To enable portlet filtering for the portal, set this property to true:
legacy.portlet.enable.filtering = true
The default value is true.
Before a filter can be used (assigned to a portlet), it must be registered in the PortletFilterService.properties file. The following example shows the declaration of a sample filter, MonitorFilter. This is a sample implementation and it is contained in the classes of WebSphere Portal. The line numbers shown are for explanation purposes only; they are not used in an actual definition.
1: # Example: 2: filtername1 = MonitorFilter 3: MonitorFilter.classname = com.ibm.wps.portletcontainer.portletfilter.PortletMonitorFilter 4: MonitorFilter.heading = Start Monitor 5: MonitorFilter.ending = End Monitor 6: MonitorFilter.doFilter = false 7: # methods to be filtered 8: MonitorFilter.method.1 = service 9: MonitorFilter.method.2 = doTitle 10: # FromMarkup:ToMarkup 11: MonitorFilter.transcodeMarkup.1 = html->vxml 12: MonitorFilter.transcodeMarkup.2 = html->wml
The portlet filter MonitorFilter is defined as follows:
The information in lines 8 and 9 is used to call the filter only for the specified methods to improve the performance. The possible methods for the filter are: login, beginPage, service, endPage, doTitle, ActionEvent, MessageEvent, and WindowEvent. If the filter is to be called for every method, it does not need to define any method in the properties file.
The information in lines 11 and 12 is used to check if the portlet with the filter is able to support the requested markup. If the requested markup cannot be generated by the filter or is not supported by the portlet natively, the portlet will not be rendered. WebSphere Portal supports HTML, WML, iMode, and VXML (to manage supported markups, use the administration portlet for markups management).
The following are examples of a portlet in the context of different markups and the sample filter. If filtering is enabled, the filter will be called even if the portlet could not generate the requested markup (see Example 2). It is the task of the filter to analyze whether or not filtering is necessary.
Example 1:
The client is a voice server and requests VXML. Because the portlet has specified in the portlet.xml file that it supports HTML and the filter can transcode HTML to VXML, the portlet will be rendered.Example 2:
The client is an HTML browser. Because the portlet supports HTML natively, the rendering process will be started.Example 3:
The client is an iMode device. Because the portlet is not generating cHTML and the filter cannot provide it, the rendering of this portlet will be skipped.
When a portlet filter is registered in the properties file, it can be assigned to a portlet. Multiple portlet filters can be assigned to a portlet by defining the portlet setting "FilterChain" and delivering as a value a list of filters separated by comma or semicolon.
To add this setting to the relevant portlet during runtime, use Modify Parameters in the Manage Portlets portlet.
The request will pass to the filters in the list from the left to the right and then proceed to the portlet. The portlet response will then traverse back through all filters from right to left.
Example:
FilterChain = MyTranscodingFilter, MyAdStripper
In this filter chain example, the filters do the following:
The following processing steps are taken:
Multiple filters, which are capable of transcoding from one markup to another, are not allowed in a single filter chain.
All filters have the following stages:
Init
The filter will be initialized. To get information about the specific filter
settings and portal information, the FilterConfig is given to the init method
of the filter. After the call of the init method, the filter will switch
to the use mode.
Use
Filter requests are handled by the doFilter method.
Calls to the portlet that do not have a request attached are not available to the portlet filter.
The following calls to the portlet are available to the filter:
The following calls to the portlet are not available to the filter:
To quickly enable the usage of portlet filters, WebSphere Portal provides a predefined set of wrapping objects. These can be used to modify the standard behavior of the wrapped components. Wrappers are defined for the following objects:
Object
|
Predefined wrapper
|
---|---|
PortletRequest | PortletRequestWrapper |
PortletResponse | PortletResponseWrapper |
Client | ClientWrapper |
A transcoding filter for markups (for example, from HTML to WML ) will need to use all of these wrappers to emulate the HTML environment that an HTML portlet assumes.
On server startup, all portlet filters that are registered in the PortletFilterService.properties file are initialized and are made available for filter registration. As soon as a portlet is dispatched, the portlet container gets the needed filters from the filter registration and calls these filters successively in the specified order. The portlet request and portlet response is forwarded through this chain of portlet filters before the portlet is rendered. Afterwards, the portlet container passes the request and response back through the chain of filters in the reverse order.
If a filter is to manipulate the output of a portlet, it must exchange the
actual writer in the portlet response with one that stores data for later changes.
This can be implemented using the wrapper classes delivered with the portal.
After the portlet is called and the response is returned, the filter can then
manipulate the output of the portlet and write the resulting output to the original
writer.