View Javadoc

1   /*
2    * GeneralFilter.java
3    *
4    * Created on 29 de Abril de 2005, 06:17
5    */
6   
7   package org.thema.scriptware.controller;
8   
9   import java.io.*;
10  import java.net.*;
11  import java.util.*;
12  import java.text.*;
13  import javax.servlet.*;
14  import javax.servlet.http.*;
15  
16  import javax.servlet.Filter;
17  import javax.servlet.FilterChain;
18  import javax.servlet.FilterConfig;
19  import javax.servlet.ServletContext;
20  import javax.servlet.ServletException;
21  import javax.servlet.ServletRequest;
22  import javax.servlet.ServletResponse;
23  
24  /***
25   *
26   * @author  Eduardo M . Sasso
27   * @version
28   */
29  
30  public class GeneralFilter implements Filter {
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 GeneralFilter() {
38      }
39      
40      private void doBeforeProcessing(ServletRequest request, ServletResponse response)
41      throws IOException, ServletException {
42          if (debug) log("GeneralFilter:DoBeforeProcessing");
43          
44          HttpSession session = ((HttpServletRequest)request).getSession();
45          if (session.getAttribute("connection") == null && request.getParameter("acao") == null &&
46              session.getAttribute("datasources") == null) {
47              ((HttpServletResponse)response).sendRedirect("datasource");
48              //RequestDispatcher dispatcher = request.getRequestDispatcher("datasource");
49              //dispatcher.forward(request,response);
50          }
51      }
52      
53      private void doAfterProcessing(ServletRequest request, ServletResponse response)
54      throws IOException, ServletException {
55          if (debug) log("GeneralFilter:DoAfterProcessing");
56          //
57          // Write code here to process the request and/or response after
58          // the rest of the filter chain is invoked.
59          //
60          
61          //
62          // For example, a logging filter might log the attributes on the
63          // request object after the request has been processed.
64          //
65          /*
66          for (Enumeration en = request.getAttributeNames(); en.hasMoreElements(); ) {
67              String name = (String)en.nextElement();
68              Object value = request.getAttribute(name);
69              log("attribute: " + name + "=" + value.toString());
70           
71          }
72           */
73          //
74          
75          //
76          // For example, a filter might append something to the response.
77          //
78          /*
79          PrintWriter respOut = new PrintWriter(response.getWriter());
80          respOut.println("<P><B>This has been appended by an intrusive filter.</B>");
81           */
82      }
83      
84      /***
85       *
86       * @param request The servlet request we are processing
87       * @param result The servlet response we are creating
88       * @param chain The filter chain we are processing
89       *
90       * @exception IOException if an input/output error occurs
91       * @exception ServletException if a servlet error occurs
92       */
93      public void doFilter(ServletRequest request, ServletResponse response,
94              FilterChain chain)
95              throws IOException, ServletException {
96          
97          if (debug) log("GeneralFilter:doFilter()");
98          
99          doBeforeProcessing(request, response);
100         
101         Throwable problem = null;
102         
103         try {
104             chain.doFilter(request, response);
105         } catch(Throwable t) {
106             //
107             // If an exception is thrown somewhere down the filter chain,
108             // we still want to execute our after processing, and then
109             // rethrow the problem after that.
110             //
111             problem = t;
112             t.printStackTrace();
113         }
114         
115         doAfterProcessing(request, response);
116         
117         //
118         // If there was a problem, we want to rethrow it if it is
119         // a known type, otherwise log it.
120         //
121         if (problem != null) {
122             if (problem instanceof ServletException) throw (ServletException)problem;
123             if (problem instanceof IOException) throw (IOException)problem;
124             sendProcessingError(problem, response);
125         }
126     }
127         
128     
129     private void sendProcessingError(Throwable t, ServletResponse response) {
130         
131         String stackTrace = getStackTrace(t);
132         
133         if(stackTrace != null && !stackTrace.equals("")) {
134             
135             try {
136                 
137                 response.setContentType("text/html");
138                 PrintStream ps = new PrintStream(response.getOutputStream());
139                 PrintWriter pw = new PrintWriter(ps);
140                 pw.print("<html>\n<head>\n<title>Error</title>\n</head>\n<body>\n"); //NOI18N
141                 
142                 // PENDING! Localize this for next official release
143                 pw.print("<h1>The resource did not process correctly</h1>\n<pre>\n");
144                 pw.print(stackTrace);
145                 pw.print("</pre></body>\n</html>"); //NOI18N
146                 pw.close();
147                 ps.close();
148                 response.getOutputStream().close();;
149             }
150             
151             catch(Exception ex){ }
152         } else {
153             try {
154                 PrintStream ps = new PrintStream(response.getOutputStream());
155                 t.printStackTrace(ps);
156                 ps.close();
157                 response.getOutputStream().close();;
158             } catch(Exception ex){ }
159         }
160     }
161     
162     public static String getStackTrace(Throwable t) {
163         
164         String stackTrace = null;
165         
166         try {
167             StringWriter sw = new StringWriter();
168             PrintWriter pw = new PrintWriter(sw);
169             t.printStackTrace(pw);
170             pw.close();
171             sw.close();
172             stackTrace = sw.getBuffer().toString();
173         } catch(Exception ex) {}
174         return stackTrace;
175     }
176     
177     public void log(String msg) {
178         filterConfig.getServletContext().log(msg);
179     }
180     
181     private static final boolean debug = false;
182 
183     public void init(FilterConfig filterConfig) throws ServletException {
184     }
185 
186     public void destroy() {
187     }
188 
189  
190 }