View Javadoc

1   package org.appfuse.webapp.filter;
2   
3   import javax.servlet.Filter;
4   import javax.servlet.FilterChain;
5   import javax.servlet.FilterConfig;
6   import javax.servlet.ServletException;
7   import javax.servlet.ServletRequest;
8   import javax.servlet.ServletResponse;
9   import javax.servlet.http.HttpServletRequest;
10  import java.io.IOException;
11  
12  /**
13   * Filter to remove messages form the session and put them in the request
14   * - to solve the redirect after post issue.
15   * 
16   * <p><a href="MessageFilter.java.html"><i>View Source</i></a></p>
17   *
18   * @author  Matt Raible
19   */
20  public class MessageFilter implements Filter {
21      public void doFilter(ServletRequest req, ServletResponse res,
22                           FilterChain chain)
23      throws IOException, ServletException {
24          HttpServletRequest request = (HttpServletRequest) req;
25  
26          // grab messages from the session and put them into request
27          // this is so they're not lost in a redirect
28          Object messages = request.getSession().getAttribute("messages");
29  
30          if (messages != null) {
31              request.setAttribute("messages", messages);
32              request.getSession().removeAttribute("messages");
33          }
34          
35          // grab errors from the session and put them into request
36          // this is so they're not lost in a redirect
37          Object errors = request.getSession().getAttribute("errors");
38  
39          if (errors != null) {
40              request.setAttribute("errors", errors);
41              request.getSession().removeAttribute("errors");
42          }
43  
44          chain.doFilter(req, res);
45      }
46  
47      public void init(FilterConfig filterConfig) {
48      }
49  
50      public void destroy() {
51      }
52  }