001: /*
002: * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/valves/RequestDumperValve.java,v 1.5 2002/02/19 22:11:26 remm Exp $
003: * $Revision: 1.5 $
004: * $Date: 2002/02/19 22:11:26 $
005: *
006: * ====================================================================
007: *
008: * The Apache Software License, Version 1.1
009: *
010: * Copyright (c) 1999-2001 The Apache Software Foundation. All rights
011: * reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions
015: * are met:
016: *
017: * 1. Redistributions of source code must retain the above copyright
018: * notice, this list of conditions and the following disclaimer.
019: *
020: * 2. Redistributions in binary form must reproduce the above copyright
021: * notice, this list of conditions and the following disclaimer in
022: * the documentation and/or other materials provided with the
023: * distribution.
024: *
025: * 3. The end-user documentation included with the redistribution, if
026: * any, must include the following acknowlegement:
027: * "This product includes software developed by the
028: * Apache Software Foundation (http://www.apache.org/)."
029: * Alternately, this acknowlegement may appear in the software itself,
030: * if and wherever such third-party acknowlegements normally appear.
031: *
032: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
033: * Foundation" must not be used to endorse or promote products derived
034: * from this software without prior written permission. For written
035: * permission, please contact apache@apache.org.
036: *
037: * 5. Products derived from this software may not be called "Apache"
038: * nor may "Apache" appear in their names without prior written
039: * permission of the Apache Group.
040: *
041: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
042: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
043: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
044: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
045: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
046: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
047: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
048: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
049: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
050: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
051: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
052: * SUCH DAMAGE.
053: * ====================================================================
054: *
055: * This software consists of voluntary contributions made by many
056: * individuals on behalf of the Apache Software Foundation. For more
057: * information on the Apache Software Foundation, please see
058: * <http://www.apache.org/>.
059: *
060: * [Additional notices, if required by prior licensing conditions]
061: *
062: */
063:
064: package org.apache.catalina.valves;
065:
066: import java.io.IOException;
067: import java.util.ArrayList;
068: import java.util.Enumeration;
069: import java.util.Iterator;
070: import javax.servlet.ServletException;
071: import javax.servlet.ServletRequest;
072: import javax.servlet.ServletResponse;
073: import javax.servlet.http.Cookie;
074: import javax.servlet.http.HttpServletRequest;
075: import javax.servlet.http.HttpServletResponse;
076: import org.apache.catalina.Container;
077: import org.apache.catalina.HttpRequest;
078: import org.apache.catalina.HttpResponse;
079: import org.apache.catalina.Logger;
080: import org.apache.catalina.Request;
081: import org.apache.catalina.Response;
082: import org.apache.catalina.Valve;
083: import org.apache.catalina.ValveContext;
084: import org.apache.catalina.connector.HttpResponseWrapper;
085: import org.apache.catalina.util.StringManager;
086:
087: /**
088: * <p>Implementation of a Valve that logs interesting contents from the
089: * specified Request (before processing) and the corresponding Response
090: * (after processing). It is especially useful in debugging problems
091: * related to headers and cookies.</p>
092: *
093: * <p>This Valve may be attached to any Container, depending on the granularity
094: * of the logging you wish to perform.</p>
095: *
096: * @author Craig R. McClanahan
097: * @version $Revision: 1.5 $ $Date: 2002/02/19 22:11:26 $
098: */
099:
100: public class RequestDumperValve extends ValveBase {
101:
102: // ----------------------------------------------------- Instance Variables
103:
104: /**
105: * The descriptive information related to this implementation.
106: */
107: private static final String info = "org.apache.catalina.valves.RequestDumperValve/1.0";
108:
109: /**
110: * The StringManager for this package.
111: */
112: protected static StringManager sm = StringManager
113: .getManager(Constants.Package);
114:
115: // ------------------------------------------------------------- Properties
116:
117: /**
118: * Return descriptive information about this Valve implementation.
119: */
120: public String getInfo() {
121:
122: return (info);
123:
124: }
125:
126: // --------------------------------------------------------- Public Methods
127:
128: /**
129: * Log the interesting request parameters, invoke the next Valve in the
130: * sequence, and log the interesting response parameters.
131: *
132: * @param request The servlet request to be processed
133: * @param response The servlet response to be created
134: * @param context The valve context used to invoke the next valve
135: * in the current processing pipeline
136: *
137: * @exception IOException if an input/output error occurs
138: * @exception ServletException if a servlet error occurs
139: */
140: public void invoke(Request request, Response response,
141: ValveContext context) throws IOException, ServletException {
142:
143: // Skip logging for non-HTTP requests and responses
144: if (!(request instanceof HttpRequest)
145: || !(response instanceof HttpResponse)) {
146: context.invokeNext(request, response);
147: return;
148: }
149: HttpRequest hrequest = (HttpRequest) request;
150: HttpResponse hresponse = (HttpResponse) response;
151: HttpServletRequest hreq = (HttpServletRequest) hrequest
152: .getRequest();
153: HttpServletResponse hres = (HttpServletResponse) hresponse
154: .getResponse();
155:
156: // Log pre-service information
157: log("REQUEST URI =" + hreq.getRequestURI());
158: log(" authType=" + hreq.getAuthType());
159: log(" characterEncoding=" + hreq.getCharacterEncoding());
160: log(" contentLength=" + hreq.getContentLength());
161: log(" contentType=" + hreq.getContentType());
162: log(" contextPath=" + hreq.getContextPath());
163: Cookie cookies[] = hreq.getCookies();
164: if (cookies != null) {
165: for (int i = 0; i < cookies.length; i++)
166: log(" cookie=" + cookies[i].getName() + "="
167: + cookies[i].getValue());
168: }
169: Enumeration hnames = hreq.getHeaderNames();
170: while (hnames.hasMoreElements()) {
171: String hname = (String) hnames.nextElement();
172: Enumeration hvalues = hreq.getHeaders(hname);
173: while (hvalues.hasMoreElements()) {
174: String hvalue = (String) hvalues.nextElement();
175: log(" header=" + hname + "=" + hvalue);
176: }
177: }
178: log(" locale=" + hreq.getLocale());
179: log(" method=" + hreq.getMethod());
180: Enumeration pnames = hreq.getParameterNames();
181: while (pnames.hasMoreElements()) {
182: String pname = (String) pnames.nextElement();
183: String pvalues[] = hreq.getParameterValues(pname);
184: StringBuffer result = new StringBuffer(pname);
185: result.append('=');
186: for (int i = 0; i < pvalues.length; i++) {
187: if (i > 0)
188: result.append(", ");
189: result.append(pvalues[i]);
190: }
191: log(" parameter=" + result.toString());
192: }
193: log(" pathInfo=" + hreq.getPathInfo());
194: log(" protocol=" + hreq.getProtocol());
195: log(" queryString=" + hreq.getQueryString());
196: log(" remoteAddr=" + hreq.getRemoteAddr());
197: log(" remoteHost=" + hreq.getRemoteHost());
198: log(" remoteUser=" + hreq.getRemoteUser());
199: log("requestedSessionId=" + hreq.getRequestedSessionId());
200: log(" scheme=" + hreq.getScheme());
201: log(" serverName=" + hreq.getServerName());
202: log(" serverPort=" + hreq.getServerPort());
203: log(" servletPath=" + hreq.getServletPath());
204: log(" isSecure=" + hreq.isSecure());
205: log("---------------------------------------------------------------");
206:
207: // Perform the request
208: context.invokeNext(request, response);
209:
210: // Log post-service information
211: log("---------------------------------------------------------------");
212: log(" authType=" + hreq.getAuthType());
213: log(" contentLength=" + hresponse.getContentLength());
214: log(" contentType=" + hresponse.getContentType());
215: Cookie rcookies[] = hresponse.getCookies();
216: for (int i = 0; i < rcookies.length; i++) {
217: log(" cookie=" + rcookies[i].getName() + "="
218: + rcookies[i].getValue() + "; domain="
219: + rcookies[i].getDomain() + "; path="
220: + rcookies[i].getPath());
221: }
222: String rhnames[] = hresponse.getHeaderNames();
223: for (int i = 0; i < rhnames.length; i++) {
224: String rhvalues[] = hresponse.getHeaderValues(rhnames[i]);
225: for (int j = 0; j < rhvalues.length; j++)
226: log(" header=" + rhnames[i] + "="
227: + rhvalues[j]);
228: }
229: log(" message=" + hresponse.getMessage());
230: log(" remoteUser=" + hreq.getRemoteUser());
231: log(" status=" + hresponse.getStatus());
232: log("===============================================================");
233:
234: }
235:
236: /**
237: * Return a String rendering of this object.
238: */
239: public String toString() {
240:
241: StringBuffer sb = new StringBuffer("RequestDumperValve[");
242: if (container != null)
243: sb.append(container.getName());
244: sb.append("]");
245: return (sb.toString());
246:
247: }
248:
249: // ------------------------------------------------------ Protected Methods
250:
251: /**
252: * Log a message on the Logger associated with our Container (if any).
253: *
254: * @param message Message to be logged
255: */
256: protected void log(String message) {
257:
258: Logger logger = container.getLogger();
259: if (logger != null)
260: logger.log(this .toString() + ": " + message);
261: else
262: System.out.println(this .toString() + ": " + message);
263:
264: }
265:
266: /**
267: * Log a message on the Logger associated with our Container (if any).
268: *
269: * @param message Message to be logged
270: * @param throwable Associated exception
271: */
272: protected void log(String message, Throwable throwable) {
273:
274: Logger logger = container.getLogger();
275: if (logger != null)
276: logger.log(this .toString() + ": " + message, throwable);
277: else {
278: System.out.println(this .toString() + ": " + message);
279: throwable.printStackTrace(System.out);
280: }
281:
282: }
283:
284: }
|