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