Class HttpFilter

java.lang.Object
org.openremote.container.web.file.HttpFilter
All Implemented Interfaces:
javax.servlet.Filter
Direct Known Subclasses:
CORSFilter, GzipResponseFilter

public abstract class HttpFilter extends Object implements javax.servlet.Filter

The HttpFilter is abstract filter specifically for HTTP requests. It provides a convenient abstract doFilter(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 the doFilter(ServletRequest, ServletResponse, FilterChain) implementation. Also, default implementations of init(FilterConfig) and destroy() 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 barebones Servlet interface.

Usage

To use it, just let your custom filter extend from HttpFilter instead of implement Filter. 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");
         }
     }
 }
 
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    abstract void
    doFilter(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.http.HttpSession session, javax.servlet.FilterChain chain)
    Filter the HTTP request.
    void
    doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain)
     
    protected javax.servlet.FilterConfig
    Returns the filter config.
    protected String
    Returns the value of the filter init parameter associated with the given name.
    protected javax.servlet.ServletContext
    Returns the servlet context.
    void
    Convenience init() method without FilterConfig parameter which will be called by init(FilterConfig).
    void
    init(javax.servlet.FilterConfig filterConfig)
    Called by the servlet container when the filter is about to be placed into service.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • HttpFilter

      public HttpFilter()
  • Method Details

    • init

      public void init(javax.servlet.FilterConfig filterConfig) throws javax.servlet.ServletException
      Called by the servlet container when the filter is about to be placed into service. This implementation stores the FilterConfig object for later use by the getter methods. It's recommended to not override this method. Instead, just use init() method. When overriding this method anyway, don't forget to call super.init(config), otherwise the getter methods will throw an illegal state exception.
      Specified by:
      init in interface javax.servlet.Filter
      Throws:
      javax.servlet.ServletException
    • init

      public void init() throws javax.servlet.ServletException
      Convenience init() method without FilterConfig parameter which will be called by init(FilterConfig).
      Throws:
      javax.servlet.ServletException - When filter's initialization failed.
    • doFilter

      public void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain) throws javax.servlet.ServletException, IOException
      Specified by:
      doFilter in interface javax.servlet.Filter
      Throws:
      javax.servlet.ServletException
      IOException
    • doFilter

      public abstract void doFilter(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.http.HttpSession session, javax.servlet.FilterChain chain) throws javax.servlet.ServletException, IOException
      Filter the HTTP request. The session argument is null if there is no session.
      Parameters:
      request - The HTTP request.
      response - The HTTP response.
      session - The HTTP session, if any, else null.
      chain - The filter chain to continue.
      Throws:
      javax.servlet.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)
    • destroy

      public void destroy()
      Specified by:
      destroy in interface javax.servlet.Filter
    • getFilterConfig

      protected javax.servlet.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 javax.servlet.ServletContext getServletContext()
      Returns the servlet context.
      Returns:
      The servlet context.