001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018
019
020 package filters;
021
022
023 import java.io.IOException;
024 import java.io.PrintWriter;
025 import java.io.StringWriter;
026 import java.sql.Timestamp;
027 import java.util.Enumeration;
028 import java.util.Locale;
029 import javax.servlet.Filter;
030 import javax.servlet.FilterChain;
031 import javax.servlet.FilterConfig;
032 import javax.servlet.ServletException;
033 import javax.servlet.ServletRequest;
034 import javax.servlet.ServletResponse;
035 import javax.servlet.http.Cookie;
036 import javax.servlet.http.HttpServletRequest;
037
038
039 /**
040 * Example filter that dumps interesting state information about a request
041 * to the associated servlet context log file, before allowing the servlet
042 * to process the request in the usual way. This can be installed as needed
043 * to assist in debugging problems.
044 *
045 * @author Craig McClanahan
046 * @version $Revision: 514091 $ $Date: 2007-03-03 01:26:39 -0500 (Sat, 03 Mar 2007) $
047 */
048
049 public final class RequestDumperFilter implements Filter {
050
051
052 // ----------------------------------------------------- Instance Variables
053
054
055 /**
056 * The filter configuration object we are associated with. If this value
057 * is null, this filter instance is not currently configured.
058 */
059 private FilterConfig filterConfig = null;
060
061
062 // --------------------------------------------------------- Public Methods
063
064
065 /**
066 * Take this filter out of service.
067 */
068 public void destroy() {
069
070 this.filterConfig = null;
071
072 }
073
074
075 /**
076 * Time the processing that is performed by all subsequent filters in the
077 * current filter stack, including the ultimately invoked servlet.
078 *
079 * @param request The servlet request we are processing
080 * @param result The servlet response we are creating
081 * @param chain The filter chain we are processing
082 *
083 * @exception IOException if an input/output error occurs
084 * @exception ServletException if a servlet error occurs
085 */
086 public void doFilter(ServletRequest request, ServletResponse response,
087 FilterChain chain)
088 throws IOException, ServletException {
089
090 if (filterConfig == null)
091 return;
092
093 // Render the generic servlet request properties
094 StringWriter sw = new StringWriter();
095 PrintWriter writer = new PrintWriter(sw);
096 writer.println("Request Received at " +
097 (new Timestamp(System.currentTimeMillis())));
098 writer.println(" characterEncoding=" + request.getCharacterEncoding());
099 writer.println(" contentLength=" + request.getContentLength());
100 writer.println(" contentType=" + request.getContentType());
101 writer.println(" locale=" + request.getLocale());
102 writer.print(" locales=");
103 Enumeration locales = request.getLocales();
104 boolean first = true;
105 while (locales.hasMoreElements()) {
106 Locale locale = (Locale) locales.nextElement();
107 if (first)
108 first = false;
109 else
110 writer.print(", ");
111 writer.print(locale.toString());
112 }
113 writer.println();
114 Enumeration names = request.getParameterNames();
115 while (names.hasMoreElements()) {
116 String name = (String) names.nextElement();
117 writer.print(" parameter=" + name + "=");
118 String values[] = request.getParameterValues(name);
119 for (int i = 0; i < values.length; i++) {
120 if (i > 0)
121 writer.print(", ");
122 writer.print(values[i]);
123 }
124 writer.println();
125 }
126 writer.println(" protocol=" + request.getProtocol());
127 writer.println(" remoteAddr=" + request.getRemoteAddr());
128 writer.println(" remoteHost=" + request.getRemoteHost());
129 writer.println(" scheme=" + request.getScheme());
130 writer.println(" serverName=" + request.getServerName());
131 writer.println(" serverPort=" + request.getServerPort());
132 writer.println(" isSecure=" + request.isSecure());
133
134 // Render the HTTP servlet request properties
135 if (request instanceof HttpServletRequest) {
136 writer.println("---------------------------------------------");
137 HttpServletRequest hrequest = (HttpServletRequest) request;
138 writer.println(" contextPath=" + hrequest.getContextPath());
139 Cookie cookies[] = hrequest.getCookies();
140 if (cookies == null)
141 cookies = new Cookie[0];
142 for (int i = 0; i < cookies.length; i++) {
143 writer.println(" cookie=" + cookies[i].getName() +
144 "=" + cookies[i].getValue());
145 }
146 names = hrequest.getHeaderNames();
147 while (names.hasMoreElements()) {
148 String name = (String) names.nextElement();
149 String value = hrequest.getHeader(name);
150 writer.println(" header=" + name + "=" + value);
151 }
152 writer.println(" method=" + hrequest.getMethod());
153 writer.println(" pathInfo=" + hrequest.getPathInfo());
154 writer.println(" queryString=" + hrequest.getQueryString());
155 writer.println(" remoteUser=" + hrequest.getRemoteUser());
156 writer.println("requestedSessionId=" +
157 hrequest.getRequestedSessionId());
158 writer.println(" requestURI=" + hrequest.getRequestURI());
159 writer.println(" servletPath=" + hrequest.getServletPath());
160 }
161 writer.println("=============================================");
162
163 // Log the resulting string
164 writer.flush();
165 filterConfig.getServletContext().log(sw.getBuffer().toString());
166
167 // Pass control on to the next filter
168 chain.doFilter(request, response);
169
170 }
171
172
173 /**
174 * Place this filter into service.
175 *
176 * @param filterConfig The filter configuration object
177 */
178 public void init(FilterConfig filterConfig) throws ServletException {
179
180 this.filterConfig = filterConfig;
181
182 }
183
184
185 /**
186 * Return a String representation of this object.
187 */
188 public String toString() {
189
190 if (filterConfig == null)
191 return ("RequestDumperFilter()");
192 StringBuffer sb = new StringBuffer("RequestDumperFilter(");
193 sb.append(filterConfig);
194 sb.append(")");
195 return (sb.toString());
196
197 }
198
199
200 }
201