KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > abc > NewSimpleFilter


1 /*
2  * NewSimpleFilter.java
3  *
4  * Created on February 19, 2005, 8:30 PM
5  */

6
7 package abc;
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 JavaDoc;
17 import javax.servlet.FilterChain JavaDoc;
18 import javax.servlet.FilterConfig JavaDoc;
19 import javax.servlet.ServletContext JavaDoc;
20 import javax.servlet.ServletException JavaDoc;
21 import javax.servlet.ServletRequest JavaDoc;
22 import javax.servlet.ServletResponse JavaDoc;
23
24 /**
25  *
26  * @author mg116726
27  * @version
28  */

29
30 public class NewSimpleFilter implements Filter JavaDoc {
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 JavaDoc filterConfig = null;
36     
37     public NewSimpleFilter() {
38     }
39     
40     private void doBeforeProcessing(ServletRequest JavaDoc request, ServletResponse JavaDoc response)
41     throws IOException, ServletException JavaDoc {
42         if (debug) log("NewSimpleFilter:DoBeforeProcessing");
43         //
44
// Write code here to process the request and/or response before
45
// the rest of the filter chain is invoked.
46
//
47

48         //
49
// For example, a logging filter might log items on the request object,
50
// such as the parameters.
51
//
52
/*
53          for (Enumeration en = request.getParameterNames(); en.hasMoreElements(); ) {
54              String name = (String)en.nextElement();
55              String values[] = request.getParameterValues(name);
56              int n = values.length;
57              StringBuffer buf = new StringBuffer();
58              buf.append(name);
59              buf.append("=");
60              for(int i=0; i < n; i++) {
61                  buf.append(values[i]);
62                  if (i < n-1)
63                      buf.append(",");
64              }
65              log(buf.toString());
66          }
67          */

68         
69     }
70     
71     private void doAfterProcessing(ServletRequest JavaDoc request, ServletResponse JavaDoc response)
72     throws IOException, ServletException JavaDoc {
73         if (debug) log("NewSimpleFilter:DoAfterProcessing");
74         //
75
// Write code here to process the request and/or response after
76
// the rest of the filter chain is invoked.
77
//
78

79         //
80
// For example, a logging filter might log the attributes on the
81
// request object after the request has been processed.
82
//
83
/*
84         for (Enumeration en = request.getAttributeNames(); en.hasMoreElements(); ) {
85             String name = (String)en.nextElement();
86             Object value = request.getAttribute(name);
87             log("attribute: " + name + "=" + value.toString());
88          
89         }
90          */

91         //
92

93         //
94
// For example, a filter might append something to the response.
95
//
96
/*
97         PrintWriter respOut = new PrintWriter(response.getWriter());
98         respOut.println("<P><B>This has been appended by an intrusive filter.</B>");
99          */

100     }
101     
102     /**
103      *
104      * @param request The servlet request we are processing
105      * @param result The servlet response we are creating
106      * @param chain The filter chain we are processing
107      *
108      * @exception IOException if an input/output error occurs
109      * @exception ServletException if a servlet error occurs
110      */

111     public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response,
112             FilterChain JavaDoc chain)
113             throws IOException, ServletException JavaDoc {
114         
115         if (debug) log("NewSimpleFilter:doFilter()");
116         
117         doBeforeProcessing(request, response);
118         
119         Throwable JavaDoc problem = null;
120         
121         try {
122             chain.doFilter(request, response);
123         }
124 catch(Throwable JavaDoc t) {
125     //
126
// If an exception is thrown somewhere down the filter chain,
127
// we still want to execute our after processing, and then
128
// rethrow the problem after that.
129
//
130
problem = t;
131     t.printStackTrace();
132 }
133         
134         doAfterProcessing(request, response);
135         
136         //
137
// If there was a problem, we want to rethrow it if it is
138
// a known type, otherwise log it.
139
//
140
if (problem != null) {
141             if (problem instanceof ServletException JavaDoc) throw (ServletException JavaDoc)problem;
142             if (problem instanceof IOException) throw (IOException)problem;
143             sendProcessingError(problem, response);
144         }
145     }
146     
147     
148     /**
149      * Return the filter configuration object for this filter.
150      */

151     public FilterConfig JavaDoc getFilterConfig() {
152         return (this.filterConfig);
153     }
154     
155     
156     /**
157      * Set the filter configuration object for this filter.
158      *
159      * @param filterConfig The filter configuration object
160      */

161     public void setFilterConfig(FilterConfig JavaDoc filterConfig) {
162         
163         this.filterConfig = filterConfig;
164     }
165     
166     /**
167      * Destroy method for this filter
168      *
169      */

170     public void destroy() {
171     }
172     
173     
174     /**
175      * Init method for this filter
176      *
177      */

178     public void init(FilterConfig JavaDoc filterConfig) {
179         
180         this.filterConfig = filterConfig;
181         if (filterConfig != null) {
182             if (debug) {
183                 log("NewSimpleFilter:Initializing filter");
184             }
185         }
186     }
187     
188     /**
189      * Return a String representation of this object.
190      */

191     public String JavaDoc toString() {
192         
193         if (filterConfig == null) return ("NewSimpleFilter()");
194         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("NewSimpleFilter(");
195         sb.append(filterConfig);
196         sb.append(")");
197         return (sb.toString());
198         
199     }
200     
201     
202     
203     private void sendProcessingError(Throwable JavaDoc t, ServletResponse JavaDoc response) {
204         
205         String JavaDoc stackTrace = getStackTrace(t);
206         
207         if(stackTrace != null && !stackTrace.equals("")) {
208             
209             try {
210                 
211                 response.setContentType("text/html");
212                 PrintStream ps = new PrintStream(response.getOutputStream());
213                 PrintWriter pw = new PrintWriter(ps);
214                 pw.print("<html>\n<head>\n<title>Error</title>\n</head>\n<body>\n"); //NOI18N
215

216                 // PENDING! Localize this for next official release
217
pw.print("<h1>The resource did not process correctly</h1>\n<pre>\n");
218                 pw.print(stackTrace);
219                 pw.print("</pre></body>\n</html>"); //NOI18N
220
pw.close();
221                 ps.close();
222                 response.getOutputStream().close();;
223             }
224             
225             catch(Exception JavaDoc ex){ }
226         }
227 else {
228             try {
229                 PrintStream ps = new PrintStream(response.getOutputStream());
230                 t.printStackTrace(ps);
231                 ps.close();
232                 response.getOutputStream().close();;
233             }
234 catch(Exception JavaDoc ex){ }
235 }
236     }
237     
238     public static String JavaDoc getStackTrace(Throwable JavaDoc t) {
239         
240         String JavaDoc stackTrace = null;
241         
242         try {
243             StringWriter sw = new StringWriter();
244             PrintWriter pw = new PrintWriter(sw);
245             t.printStackTrace(pw);
246             pw.close();
247             sw.close();
248             stackTrace = sw.getBuffer().toString();
249         }
250 catch(Exception JavaDoc ex) {}
251         return stackTrace;
252     }
253     
254     public void log(String JavaDoc msg) {
255         filterConfig.getServletContext().log(msg);
256     }
257     
258     private static final boolean debug = true;
259 }
260
Popular Tags