KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > filters > RequestDumperFilter


1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */

16
17
18 package filters;
19
20
21 import java.io.IOException JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23 import java.io.StringWriter JavaDoc;
24 import java.sql.Timestamp JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.Locale JavaDoc;
27 import javax.servlet.Filter JavaDoc;
28 import javax.servlet.FilterChain JavaDoc;
29 import javax.servlet.FilterConfig JavaDoc;
30 import javax.servlet.ServletContext JavaDoc;
31 import javax.servlet.ServletException JavaDoc;
32 import javax.servlet.ServletRequest JavaDoc;
33 import javax.servlet.ServletResponse JavaDoc;
34 import javax.servlet.http.Cookie JavaDoc;
35 import javax.servlet.http.HttpServletRequest JavaDoc;
36
37
38 /**
39  * Example filter that dumps interesting state information about a request
40  * to the associated servlet context log file, before allowing the servlet
41  * to process the request in the usual way. This can be installed as needed
42  * to assist in debugging problems.
43  *
44  * @author Craig McClanahan
45  * @version $Revision: 1.2 $ $Date: 2004/03/18 16:40:33 $
46  */

47
48 public final class RequestDumperFilter implements Filter JavaDoc {
49
50
51     // ----------------------------------------------------- Instance Variables
52

53
54     /**
55      * The filter configuration object we are associated with. If this value
56      * is null, this filter instance is not currently configured.
57      */

58     private FilterConfig JavaDoc filterConfig = null;
59
60
61     // --------------------------------------------------------- Public Methods
62

63
64     /**
65      * Take this filter out of service.
66      */

67     public void destroy() {
68
69         this.filterConfig = null;
70
71     }
72
73
74     /**
75      * Time the processing that is performed by all subsequent filters in the
76      * current filter stack, including the ultimately invoked servlet.
77      *
78      * @param request The servlet request we are processing
79      * @param result The servlet response we are creating
80      * @param chain The filter chain we are processing
81      *
82      * @exception IOException if an input/output error occurs
83      * @exception ServletException if a servlet error occurs
84      */

85     public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response,
86                          FilterChain JavaDoc chain)
87     throws IOException JavaDoc, ServletException JavaDoc {
88
89         if (filterConfig == null)
90         return;
91
92     // Render the generic servlet request properties
93
StringWriter JavaDoc sw = new StringWriter JavaDoc();
94     PrintWriter JavaDoc writer = new PrintWriter JavaDoc(sw);
95     writer.println("Request Received at " +
96                (new Timestamp JavaDoc(System.currentTimeMillis())));
97     writer.println(" characterEncoding=" + request.getCharacterEncoding());
98     writer.println(" contentLength=" + request.getContentLength());
99     writer.println(" contentType=" + request.getContentType());
100     writer.println(" locale=" + request.getLocale());
101     writer.print(" locales=");
102     Enumeration JavaDoc locales = request.getLocales();
103     boolean first = true;
104     while (locales.hasMoreElements()) {
105         Locale JavaDoc locale = (Locale JavaDoc) locales.nextElement();
106         if (first)
107             first = false;
108         else
109             writer.print(", ");
110         writer.print(locale.toString());
111     }
112     writer.println();
113     Enumeration JavaDoc names = request.getParameterNames();
114     while (names.hasMoreElements()) {
115         String JavaDoc name = (String JavaDoc) names.nextElement();
116         writer.print(" parameter=" + name + "=");
117         String JavaDoc values[] = request.getParameterValues(name);
118         for (int i = 0; i < values.length; i++) {
119             if (i > 0)
120             writer.print(", ");
121         writer.print(values[i]);
122         }
123         writer.println();
124     }
125     writer.println(" protocol=" + request.getProtocol());
126     writer.println(" remoteAddr=" + request.getRemoteAddr());
127     writer.println(" remoteHost=" + request.getRemoteHost());
128     writer.println(" scheme=" + request.getScheme());
129     writer.println(" serverName=" + request.getServerName());
130     writer.println(" serverPort=" + request.getServerPort());
131     writer.println(" isSecure=" + request.isSecure());
132
133     // Render the HTTP servlet request properties
134
if (request instanceof HttpServletRequest JavaDoc) {
135         writer.println("---------------------------------------------");
136         HttpServletRequest JavaDoc hrequest = (HttpServletRequest JavaDoc) request;
137         writer.println(" contextPath=" + hrequest.getContextPath());
138         Cookie JavaDoc cookies[] = hrequest.getCookies();
139             if (cookies == null)
140                 cookies = new Cookie JavaDoc[0];
141         for (int i = 0; i < cookies.length; i++) {
142             writer.println(" cookie=" + cookies[i].getName() +
143                    "=" + cookies[i].getValue());
144         }
145         names = hrequest.getHeaderNames();
146         while (names.hasMoreElements()) {
147             String JavaDoc name = (String JavaDoc) names.nextElement();
148         String JavaDoc value = hrequest.getHeader(name);
149             writer.println(" header=" + name + "=" + value);
150         }
151         writer.println(" method=" + hrequest.getMethod());
152         writer.println(" pathInfo=" + hrequest.getPathInfo());
153         writer.println(" queryString=" + hrequest.getQueryString());
154         writer.println(" remoteUser=" + hrequest.getRemoteUser());
155         writer.println("requestedSessionId=" +
156                hrequest.getRequestedSessionId());
157         writer.println(" requestURI=" + hrequest.getRequestURI());
158         writer.println(" servletPath=" + hrequest.getServletPath());
159     }
160     writer.println("=============================================");
161
162     // Log the resulting string
163
writer.flush();
164     filterConfig.getServletContext().log(sw.getBuffer().toString());
165
166     // Pass control on to the next filter
167
chain.doFilter(request, response);
168
169     }
170
171
172     /**
173      * Place this filter into service.
174      *
175      * @param filterConfig The filter configuration object
176      */

177     public void init(FilterConfig JavaDoc filterConfig) throws ServletException JavaDoc {
178
179     this.filterConfig = filterConfig;
180
181     }
182
183
184     /**
185      * Return a String representation of this object.
186      */

187     public String JavaDoc toString() {
188
189     if (filterConfig == null)
190         return ("RequestDumperFilter()");
191     StringBuffer JavaDoc sb = new StringBuffer JavaDoc("RequestDumperFilter(");
192     sb.append(filterConfig);
193     sb.append(")");
194     return (sb.toString());
195
196     }
197
198
199 }
200
201
Popular Tags