Class HttpFilter
- java.lang.Object
-
- org.omnifaces.filter.HttpFilter
-
- All Implemented Interfaces:
Filter
- Direct Known Subclasses:
CacheControlFilter
,CharacterEncodingFilter
,FacesExceptionFilter
,FacesViewsForwardingFilter
,GzipResponseFilter
,MutableRequestFilter
,OnDemandResponseBufferFilter
public abstract class HttpFilter extends Object implements Filter
The
HttpFilter
is abstract filter specifically for HTTP requests. It provides a convenient abstractdoFilter(HttpServletRequest, HttpServletResponse, HttpSession, FilterChain)
method directly providing the HTTP servlet request, response and session, so that there's no need to cast them everytime in thedoFilter(ServletRequest, ServletResponse, FilterChain)
implementation. Also, default implementations ofinit(FilterConfig)
anddestroy()
are provided, so that there's no need to implement them every time even when not really needed.It's a bit the idea of using the convenient
HttpServlet
abstract servlet class instead of the barebonesServlet
interface.Usage
To use it, just let your custom filter extend from
HttpFilter
instead of implementFilter
. For example:@WebFilter("/app/*") public class LoginFilter extends HttpFilter { @Override public void doFilter(HttpServletRequest request, HttpServletResponse response, HttpSession session, FilterChain chain) throws ServletException, IOException { if (session != null && session.getAttribute("user") != null) { chain.doFilter(request, response); } else { Servlets.facesRedirect(request, response, "login.xhtml"); } } }
- Author:
- Arjan Tijms, Bauke Scholtz
-
-
Constructor Summary
Constructors Constructor Description HttpFilter()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
destroy()
abstract void
doFilter(HttpServletRequest request, HttpServletResponse response, HttpSession session, FilterChain chain)
Filter the HTTP request.void
doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
protected FilterConfig
getFilterConfig()
Returns the filter config.protected String
getInitParameter(String name)
Returns the value of the filter init parameter associated with the given name.protected ServletContext
getServletContext()
Returns the servlet context.void
init()
Convenience init() method without FilterConfig parameter which will be called by init(FilterConfig).void
init(FilterConfig filterConfig)
Called by the servlet container when the filter is about to be placed into service.
-
-
-
Method Detail
-
init
public void init(FilterConfig filterConfig) throws ServletException
Called by the servlet container when the filter is about to be placed into service. This implementation stores theFilterConfig
object for later use by the getter methods. It's recommended to not override this method. Instead, just useinit()
method. When overriding this method anyway, don't forget to callsuper.init(config)
, otherwise the getter methods will throw an illegal state exception.- Specified by:
init
in interfaceFilter
- Throws:
ServletException
-
init
public void init() throws ServletException
Convenience init() method without FilterConfig parameter which will be called by init(FilterConfig).- Throws:
ServletException
- When filter's initialization failed.
-
doFilter
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException
- Specified by:
doFilter
in interfaceFilter
- Throws:
ServletException
IOException
-
doFilter
public abstract void doFilter(HttpServletRequest request, HttpServletResponse response, HttpSession session, FilterChain chain) throws ServletException, IOException
Filter the HTTP request. The session argument isnull
if there is no session.- Parameters:
request
- The HTTP request.response
- The HTTP response.session
- The HTTP session, if any, elsenull
.chain
- The filter chain to continue.- Throws:
ServletException
- As wrapper exception when something fails in the request processing.IOException
- Whenever something fails at I/O level.- See Also:
Filter.doFilter(ServletRequest, ServletResponse, FilterChain)
-
getFilterConfig
protected FilterConfig getFilterConfig()
Returns the filter config.- Returns:
- The filter config.
-
getInitParameter
protected String getInitParameter(String name)
Returns the value of the filter init parameter associated with the given name.- Parameters:
name
- The filter init parameter name to return the associated value for.- Returns:
- The value of the filter init parameter associated with the given name.
-
getServletContext
protected ServletContext getServletContext()
Returns the servlet context.- Returns:
- The servlet context.
-
-