1 | /* |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
---|
3 | * contributor license agreements. See the NOTICE file distributed with |
---|
4 | * this work for additional information regarding copyright ownership. |
---|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
---|
6 | * (the "License"); you may not use this file except in compliance with |
---|
7 | * the License. You may obtain a copy of the License at |
---|
8 | * |
---|
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
10 | * |
---|
11 | * Unless required by applicable law or agreed to in writing, software |
---|
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
14 | * See the License for the specific language governing permissions and |
---|
15 | * limitations under the License. |
---|
16 | */ |
---|
17 | |
---|
18 | |
---|
19 | package filters; |
---|
20 | |
---|
21 | |
---|
22 | import java.io.IOException; |
---|
23 | import java.io.PrintWriter; |
---|
24 | import java.io.StringWriter; |
---|
25 | import java.sql.Timestamp; |
---|
26 | import java.util.Enumeration; |
---|
27 | import java.util.Locale; |
---|
28 | import javax.servlet.Filter; |
---|
29 | import javax.servlet.FilterChain; |
---|
30 | import javax.servlet.FilterConfig; |
---|
31 | import javax.servlet.ServletException; |
---|
32 | import javax.servlet.ServletRequest; |
---|
33 | import javax.servlet.ServletResponse; |
---|
34 | import javax.servlet.http.Cookie; |
---|
35 | import javax.servlet.http.HttpServletRequest; |
---|
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: 500674 $ $Date: 2007-01-28 00:15:00 +0100 (Sun, 28 Jan 2007) $ |
---|
46 | */ |
---|
47 | |
---|
48 | public final class RequestDumperFilter implements Filter { |
---|
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 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 request, ServletResponse response, |
---|
86 | FilterChain chain) |
---|
87 | throws IOException, ServletException { |
---|
88 | |
---|
89 | if (filterConfig == null) |
---|
90 | return; |
---|
91 | |
---|
92 | // Render the generic servlet request properties |
---|
93 | StringWriter sw = new StringWriter(); |
---|
94 | PrintWriter writer = new PrintWriter(sw); |
---|
95 | writer.println("Request Received at " + |
---|
96 | (new Timestamp(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 locales = request.getLocales(); |
---|
103 | boolean first = true; |
---|
104 | while (locales.hasMoreElements()) { |
---|
105 | Locale locale = (Locale) locales.nextElement(); |
---|
106 | if (first) |
---|
107 | first = false; |
---|
108 | else |
---|
109 | writer.print(", "); |
---|
110 | writer.print(locale.toString()); |
---|
111 | } |
---|
112 | writer.println(); |
---|
113 | Enumeration names = request.getParameterNames(); |
---|
114 | while (names.hasMoreElements()) { |
---|
115 | String name = (String) names.nextElement(); |
---|
116 | writer.print(" parameter=" + name + "="); |
---|
117 | String 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) { |
---|
135 | writer.println("---------------------------------------------"); |
---|
136 | HttpServletRequest hrequest = (HttpServletRequest) request; |
---|
137 | writer.println(" contextPath=" + hrequest.getContextPath()); |
---|
138 | Cookie cookies[] = hrequest.getCookies(); |
---|
139 | if (cookies == null) |
---|
140 | cookies = new Cookie[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 name = (String) names.nextElement(); |
---|
148 | String 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 filterConfig) throws ServletException { |
---|
178 | |
---|
179 | this.filterConfig = filterConfig; |
---|
180 | |
---|
181 | } |
---|
182 | |
---|
183 | |
---|
184 | /** |
---|
185 | * Return a String representation of this object. |
---|
186 | */ |
---|
187 | public String toString() { |
---|
188 | |
---|
189 | if (filterConfig == null) |
---|
190 | return ("RequestDumperFilter()"); |
---|
191 | StringBuffer sb = new StringBuffer("RequestDumperFilter("); |
---|
192 | sb.append(filterConfig); |
---|
193 | sb.append(")"); |
---|
194 | return (sb.toString()); |
---|
195 | |
---|
196 | } |
---|
197 | |
---|
198 | |
---|
199 | } |
---|
200 | |
---|