Advertisement
navapat

MID_Fillter_loginfillter

Feb 26th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.18 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package Filter;
  7.  
  8. import java.io.IOException;
  9. import java.io.PrintStream;
  10. import java.io.PrintWriter;
  11. import java.io.StringWriter;
  12. import javax.servlet.Filter;
  13. import javax.servlet.FilterChain;
  14. import javax.servlet.FilterConfig;
  15. import javax.servlet.ServletException;
  16. import javax.servlet.ServletRequest;
  17. import javax.servlet.ServletResponse;
  18. import javax.servlet.annotation.WebFilter;
  19. import javax.servlet.http.HttpServletRequest;
  20. import javax.servlet.http.HttpServletResponse;
  21. import javax.servlet.http.HttpSession;
  22.  
  23. /**
  24.  *
  25.  * @author student
  26.  */
  27. @WebFilter(filterName = "LoginFilter", urlPatterns = {"*.do"})
  28. public class LoginFilter implements Filter {
  29.    
  30.     private static final boolean debug = true;
  31.  
  32.     // The filter configuration object we are associated with.  If
  33.     // this value is null, this filter instance is not currently
  34.     // configured.
  35.     private FilterConfig filterConfig = null;
  36.    
  37.     public LoginFilter() {
  38.     }    
  39.    
  40.     private void doBeforeProcessing(ServletRequest request, ServletResponse response)
  41.             throws IOException, ServletException {
  42.         if (debug) {
  43.             log("LoginFilter:DoBeforeProcessing");
  44.         }
  45.  
  46.     // Write code here to process the request and/or response before
  47.         // the rest of the filter chain is invoked.
  48.     // For example, a logging filter might log items on the request object,
  49.         // such as the parameters.
  50.     /*
  51.          for (Enumeration en = request.getParameterNames(); en.hasMoreElements(); ) {
  52.          String name = (String)en.nextElement();
  53.          String values[] = request.getParameterValues(name);
  54.          int n = values.length;
  55.          StringBuffer buf = new StringBuffer();
  56.          buf.append(name);
  57.          buf.append("=");
  58.          for(int i=0; i < n; i++) {
  59.          buf.append(values[i]);
  60.          if (i < n-1)
  61.          buf.append(",");
  62.          }
  63.          log(buf.toString());
  64.          }
  65.          */
  66.     }    
  67.    
  68.     private void doAfterProcessing(ServletRequest request, ServletResponse response)
  69.             throws IOException, ServletException {
  70.         if (debug) {
  71.             log("LoginFilter:DoAfterProcessing");
  72.         }
  73.  
  74.     // Write code here to process the request and/or response after
  75.         // the rest of the filter chain is invoked.
  76.     // For example, a logging filter might log the attributes on the
  77.         // request object after the request has been processed.
  78.     /*
  79.          for (Enumeration en = request.getAttributeNames(); en.hasMoreElements(); ) {
  80.          String name = (String)en.nextElement();
  81.          Object value = request.getAttribute(name);
  82.          log("attribute: " + name + "=" + value.toString());
  83.  
  84.          }
  85.          */
  86.     // For example, a filter might append something to the response.
  87.     /*
  88.          PrintWriter respOut = new PrintWriter(response.getWriter());
  89.          respOut.println("<P><B>This has been appended by an intrusive filter.</B>");
  90.          */
  91.     }
  92.  
  93.     /**
  94.      *
  95.      * @param request The servlet request we are processing
  96.      * @param response The servlet response we are creating
  97.      * @param chain The filter chain we are processing
  98.      *
  99.      * @exception IOException if an input/output error occurs
  100.      * @exception ServletException if a servlet error occurs
  101.      */
  102.     public void doFilter(ServletRequest request, ServletResponse response,
  103.             FilterChain chain)
  104.             throws IOException, ServletException {
  105.        
  106.         if (debug) {
  107.             log("LoginFilter:doFilter()");
  108.         }
  109.        
  110.         doBeforeProcessing(request, response);
  111.         ////////////////////////////////////////////////////////////
  112.         HttpSession session = ((HttpServletRequest)request).getSession();
  113.         Boolean loginFlag = (Boolean) session.getAttribute("loginFlag");
  114.         boolean flag;
  115.         if(loginFlag == null){
  116.             flag = false;
  117.            
  118.         }else {
  119.             flag = loginFlag;          
  120.         }
  121.         if(!flag){
  122.             ((HttpServletResponse)response).sendRedirect("Login.html");
  123.         }
  124.        
  125.         //////////////////////////////////////////////////////////////
  126.        
  127.         Throwable problem = null;
  128.         try {
  129.             chain.doFilter(request, response);
  130.         } catch (Throwable t) {
  131.         // If an exception is thrown somewhere down the filter chain,
  132.             // we still want to execute our after processing, and then
  133.             // rethrow the problem after that.
  134.             problem = t;
  135.             t.printStackTrace();
  136.         }
  137.        
  138.         doAfterProcessing(request, response);
  139.  
  140.     // If there was a problem, we want to rethrow it if it is
  141.         // a known type, otherwise log it.
  142.         if (problem != null) {
  143.             if (problem instanceof ServletException) {
  144.                 throw (ServletException) problem;
  145.             }
  146.             if (problem instanceof IOException) {
  147.                 throw (IOException) problem;
  148.             }
  149.             sendProcessingError(problem, response);
  150.         }
  151.     }
  152.  
  153.     /**
  154.      * Return the filter configuration object for this filter.
  155.      */
  156.     public FilterConfig getFilterConfig() {
  157.         return (this.filterConfig);
  158.     }
  159.  
  160.     /**
  161.      * Set the filter configuration object for this filter.
  162.      *
  163.      * @param filterConfig The filter configuration object
  164.      */
  165.     public void setFilterConfig(FilterConfig filterConfig) {
  166.         this.filterConfig = filterConfig;
  167.     }
  168.  
  169.     /**
  170.      * Destroy method for this filter
  171.      */
  172.     public void destroy() {        
  173.     }
  174.  
  175.     /**
  176.      * Init method for this filter
  177.      */
  178.     public void init(FilterConfig filterConfig) {        
  179.         this.filterConfig = filterConfig;
  180.         if (filterConfig != null) {
  181.             if (debug) {                
  182.                 log("LoginFilter:Initializing filter");
  183.             }
  184.         }
  185.     }
  186.  
  187.     /**
  188.      * Return a String representation of this object.
  189.      */
  190.     @Override
  191.     public String toString() {
  192.         if (filterConfig == null) {
  193.             return ("LoginFilter()");
  194.         }
  195.         StringBuffer sb = new StringBuffer("LoginFilter(");
  196.         sb.append(filterConfig);
  197.         sb.append(")");
  198.         return (sb.toString());
  199.     }
  200.    
  201.     private void sendProcessingError(Throwable t, ServletResponse response) {
  202.         String stackTrace = getStackTrace(t);        
  203.        
  204.         if (stackTrace != null && !stackTrace.equals("")) {
  205.             try {
  206.                 response.setContentType("text/html");
  207.                 PrintStream ps = new PrintStream(response.getOutputStream());
  208.                 PrintWriter pw = new PrintWriter(ps);                
  209.                 pw.print("<html>\n<head>\n<title>Error</title>\n</head>\n<body>\n"); //NOI18N
  210.  
  211.                 // PENDING! Localize this for next official release
  212.                 pw.print("<h1>The resource did not process correctly</h1>\n<pre>\n");                
  213.                 pw.print(stackTrace);                
  214.                 pw.print("</pre></body>\n</html>"); //NOI18N
  215.                 pw.close();
  216.                 ps.close();
  217.                 response.getOutputStream().close();
  218.             } catch (Exception ex) {
  219.             }
  220.         } else {
  221.             try {
  222.                 PrintStream ps = new PrintStream(response.getOutputStream());
  223.                 t.printStackTrace(ps);
  224.                 ps.close();
  225.                 response.getOutputStream().close();
  226.             } catch (Exception ex) {
  227.             }
  228.         }
  229.     }
  230.    
  231.     public static String getStackTrace(Throwable t) {
  232.         String stackTrace = null;
  233.         try {
  234.             StringWriter sw = new StringWriter();
  235.             PrintWriter pw = new PrintWriter(sw);
  236.             t.printStackTrace(pw);
  237.             pw.close();
  238.             sw.close();
  239.             stackTrace = sw.getBuffer().toString();
  240.         } catch (Exception ex) {
  241.         }
  242.         return stackTrace;
  243.     }
  244.    
  245.     public void log(String msg) {
  246.         filterConfig.getServletContext().log(msg);        
  247.     }
  248.    
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement