Liferay hooks are great way of customize the Liferay portal.
Hooks can customize most of the existed portal behaviour. In the each version
of Liferay release they have introduced new types hook plugins to make maximum
avoid of EXT plugins
Liferay 6.1 have come up with new type of hook plugin
called Servlet Filter. Before
Liferay 6.1 If you would like to create new Servlet filer then we had to use EXT
plugin types and as we know that EXT plugins are little difficult to develop and
also its maintenance.
Servlet filter hooks are one of great feature so that
we can create new servlet filters and deploy into portal. From Liferay 6.1 version
onwards these feature available.
Servlet
Filters
Servlet filter is an object that intercept http request to
perform pre or post tasks in web applications before request reach back end or
before response reach client. Some time we might need to perform set of tasks
before request reach backend that is controller. We can also perform post tasks
before response reach client.
We have these filters for different contextual use such
as Authentication Filters, Compression
Filters, Encryption Filters, Image compression filters and Tokenizing filters.
General
pictorial understanding of Servlet Filter
Servlet
Filter Implementation in Web applications
Step:
1
In order to implement servlet filer we must implement javax.servlet.Filter interface and
these interface have following methods so we must implement all methods.
public void doFilter (ServletRequest,
ServletResponse, FilterChain)
For
each time of request doFilter (--) method will be calling and we will perform
actual task in this method.
public void init(FilterConfig
filterConfig)
This
method we will called when servlet container instantiate filter object and in
order to perform some initialization tasks we will use this method.
public void destroy()
When
we stop server or when we undeploy web application then this method will be called.
|
Step:
2
We should register servlet filer in web application web.xml file. In the configuration we
will define name of servlet filter and its implementation class. We will also
define filter matching path so that when we request server with specific URL
path then respective servlet filer will be executed and it will perform the
task.
Example
for Servlet Filter in Web application
Servlet
Implementation class
package
com.liferaysavvy.servletfilter;
import
java.util.Date;
import
javax.servlet.Filter;
import
javax.servlet.FilterChain;
import
javax.servlet.FilterConfig;
import
javax.servlet.ServletException;
import
javax.servlet.ServletRequest;
import
javax.servlet.ServletResponse;
public class SampleServletFilter
implements Filter {
public void
init(FilterConfig config) throws ServletException {
System.out.println("Init
method being called");
/*
*
If we define init parameter in web.xml the following method retrive
*
the configured value
*/
String
intiParamValue = config.getInitParameter("initparam");
System.out.println("initParamValue:
" + initParamValue);
}
public void
doFilter(ServletRequest request, ServletResponse response,
FilterChain
chain) throws java.io.IOException, ServletException {
System.out.println("DoFilter
method being called");
/*
Retrive the IP address of client from where user requested */
String
clientIPAddress = request.getRemoteAddr();
//
Display or save the IP address of requested client.
System.out.println("Requested
Client IP Address" + clientIPAddress
+ ",
Time " + new Date().toString());
/*
Pass request back down the filter chain */
chain.doFilter(request, response);
}
public void
destroy() {
/*
*
This method will be executed when we undeploy application or stop
*
server
*/
System.out.println("Destroy
method being called");
}
}
|
Servlet
filter Configuration in web.xml
<filter>
<filter-name>SampleIPAddresFilter</filter-name>
<filter-class>com.liferaysavvy.servletfilter.SampleServletFilter</filter-class>
<init-param>
<param-name>initparam</param-name>
<param-value>Hello IP Address Filter</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SampleIPAddresFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
|
Note:
URL path we made it * it means Filter will be executed
for every request and we can also make other URL patterns that you desired.
Example:
/user/*
/content/news/*
/login/sso
|
Servlet
filters in Liferay Portal
Liferay is huge portal or its web application and it
consist many servlet filters for different purposes. All these servlet filters
are configured in Liferay portal web.xml and Liferay-web.xml files.
We have many Liferay servlet filters such as
CASFilter, SSOFilter, NTLMFilter and
many more filters are already implemented in Liferay portal.
If we want create new servlet filters in Liferay we
will use Servlet Filter Hook
plugins. Servlet filter hook plugins are the way to implement filters and
deploy into Liferay portal.
Servlet filters implementation in Liferay hook is same
as implementation in general web application. We will use same above steps as we discussed in the above section. Only difference is that we will configure filter definition in liferay-hook.xml
file with different xml tags than web.xml file.
Steps
to Implement Servlet Filters in Liferay Hooks.
- Create Liferay Hook Plugin
- Implement Servlet Filter
- Configure Filter Definition in liferay-hook.xml file
Create
Liferay Hook Plugin
Creating hook using Liferay IDE is very straight forward way and its easy. Follow below articles to create Liferay hook plugin.
Implement
Servlet Filter
Once created the hook using Liferay IDE then create
Servlet Filter and use same steps that we have used in Servlet Filter implementation
for general web applications. I used same implementation in Liferay servlet
filter also.
Liferay Servlet Implementation Java class
package
com.liferaysavvy.servletfilter;
import
java.util.Date;
import
javax.servlet.Filter;
import
javax.servlet.FilterChain;
import
javax.servlet.FilterConfig;
import
javax.servlet.ServletException;
import
javax.servlet.ServletRequest;
import
javax.servlet.ServletResponse;
import
com.liferay.portal.kernel.log.Log;
import
com.liferay.portal.kernel.log.LogFactoryUtil;
public class
LiferayIPAddressServletFilter implements
Filter {
public void
init(FilterConfig config) throws ServletException {
logger.info("Init
method being called");
/*
*
If we define init parameter in web.xml the following method retrive
*
the configured value
*/
String
intiParamValue = config.getInitParameter("initparam");
logger.info("intiParamValue:
" + intiParamValue);
}
public void
doFilter(ServletRequest request, ServletResponse response,
FilterChain
chain) throws java.io.IOException, ServletException {
logger.info("DoFilter
method being called");
/*
Retrive the IP address of client from where user requested */
String
clientIPAddress = request.getRemoteAddr();
//
Display or save the IP address of requested client.
logger.info("Requested
Client IP Address" + clientIPAddress + ",
Time "
+ new
Date().toString());
/*
Pass request back down the filter chain */
chain.doFilter(request, response);
}
public void
destroy() {
/*
*
This method will be executed when we undeploy application or stop
*
server
*/
logger.info("Destroy
method being called");
}
private static final Log
logger = LogFactoryUtil
.getLog(LiferayIPAddressServletFilter.class);
}
|
Configure
Filter Definition in liferay-hook.xml file
Use the following configuration in liferay-hook.xml file
and its slight different than the configuration we used in web.xml file for
general web applications.
<?xml version="1.0"?>
<!DOCTYPE hook PUBLIC "-//Liferay//DTD Hook 6.2.0//EN" "http://www.liferay.com/dtd/liferay-hook_6_2_0.dtd">
<hook>
<servlet-filter>
<servlet-filter-name>SampleIPAddresFilter</servlet-filter-name>
<servlet-filter-impl>com.liferaysavvy.servletfilter.LiferayIPAddressServletFilter</servlet-filter-impl>
<init-param>
<param-name>initparam</param-name>
<param-value>Hi IP
Address Filter</param-value>
</init-param>
</servlet-filter>
<servlet-filter-mapping>
<servlet-filter-name>SampleIPAddresFilter</servlet-filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</servlet-filter-mapping>
</hook>
|
Once finished the implementation then deploy the hook
plugin in Liferay portal server then you can see following output in the
console.
Sample
output after deploy hook plugin in Liferay portal server
12:19:25,228
INFO
[localhost-startStop-5][HookHotDeployListener:693] Registering hook
for LiferayIPAddressFilter-hook
12:19:25,252
INFO
[localhost-startStop-5][LiferayIPAddressServletFilter:19] Init method being called
12:19:25,253
INFO
[localhost-startStop-5][LiferayIPAddressServletFilter:26] intiParamValue: Hi IP Address Filter
12:19:25,253
INFO
[localhost-startStop-5][HookHotDeployListener:821] Hook for
LiferayIPAddressFilter-hook is available for use
12:19:33,393
INFO
[http-bio-8080-exec-37][LiferayIPAddressServletFilter:32] DoFilter method being called
12:19:33,394
INFO
[http-bio-8080-exec-37][LiferayIPAddressServletFilter:38] Requested Client IP Address127.0.0.1,
Time Sun Feb 21 12:19:33 GMT 2016
12:19:33,782
INFO
[http-bio-8080-exec-37][LiferayIPAddressServletFilter:32] DoFilter method being called
12:19:33,811
INFO
[http-bio-8080-exec-37][LiferayIPAddressServletFilter:38] Requested Client IP Address127.0.0.1,
Time Sun Feb 21 12:19:33 GMT 2016
12:19:34,494
INFO
[http-bio-8080-exec-37][LiferayIPAddressServletFilter:32] DoFilter method being called
12:19:34,494 INFO
[http-bio-8080-exec-37][LiferayIPAddressServletFilter:38] Requested Client IP Address127.0.0.1,
Time Sun Feb 21 12:19:34 GMT 2016
|
Get
Source code from following locations.
Author
is it possible to add 2 ervlet-filter entries into one hook?
ReplyDeleteGreat post, thankyou
ReplyDelete