001: /*
002: * Copyright 1999-2001,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 org.apache.catalina.valves;
018:
019: import java.io.IOException;
020: import java.util.Enumeration;
021:
022: import javax.servlet.ServletException;
023: import javax.servlet.http.Cookie;
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.http.HttpServletResponse;
026:
027: import org.apache.catalina.HttpRequest;
028: import org.apache.catalina.HttpResponse;
029: import org.apache.catalina.Logger;
030: import org.apache.catalina.Request;
031: import org.apache.catalina.Response;
032: import org.apache.catalina.ValveContext;
033: import org.apache.catalina.util.StringManager;
034:
035: /**
036: * <p>Implementation of a Valve that logs interesting contents from the
037: * specified Request (before processing) and the corresponding Response
038: * (after processing). It is especially useful in debugging problems
039: * related to headers and cookies.</p>
040: *
041: * <p>This Valve may be attached to any Container, depending on the granularity
042: * of the logging you wish to perform.</p>
043: *
044: * @author Craig R. McClanahan
045: * @version $Revision: 1.4 $ $Date: 2004/02/27 14:58:52 $
046: */
047:
048: public class RequestDumperValve extends ValveBase {
049:
050: // ----------------------------------------------------- Instance Variables
051:
052: /**
053: * The descriptive information related to this implementation.
054: */
055: private static final String info = "org.apache.catalina.valves.RequestDumperValve/1.0";
056:
057: /**
058: * The StringManager for this package.
059: */
060: protected static StringManager sm = StringManager
061: .getManager(Constants.Package);
062:
063: // ------------------------------------------------------------- Properties
064:
065: /**
066: * Return descriptive information about this Valve implementation.
067: */
068: public String getInfo() {
069:
070: return (info);
071:
072: }
073:
074: // --------------------------------------------------------- Public Methods
075:
076: /**
077: * Log the interesting request parameters, invoke the next Valve in the
078: * sequence, and log the interesting response parameters.
079: *
080: * @param request The servlet request to be processed
081: * @param response The servlet response to be created
082: * @param context The valve context used to invoke the next valve
083: * in the current processing pipeline
084: *
085: * @exception IOException if an input/output error occurs
086: * @exception ServletException if a servlet error occurs
087: */
088: public void invoke(Request request, Response response,
089: ValveContext context) throws IOException, ServletException {
090:
091: // Skip logging for non-HTTP requests and responses
092: if (!(request instanceof HttpRequest)
093: || !(response instanceof HttpResponse)) {
094: context.invokeNext(request, response);
095: return;
096: }
097: HttpRequest hrequest = (HttpRequest) request;
098: HttpResponse hresponse = (HttpResponse) response;
099: HttpServletRequest hreq = (HttpServletRequest) hrequest
100: .getRequest();
101: HttpServletResponse hres = (HttpServletResponse) hresponse
102: .getResponse();
103:
104: // Log pre-service information
105: log("REQUEST URI =" + hreq.getRequestURI());
106: log(" authType=" + hreq.getAuthType());
107: log(" characterEncoding=" + hreq.getCharacterEncoding());
108: log(" contentLength=" + hreq.getContentLength());
109: log(" contentType=" + hreq.getContentType());
110: log(" contextPath=" + hreq.getContextPath());
111: Cookie cookies[] = hreq.getCookies();
112: if (cookies != null) {
113: for (int i = 0; i < cookies.length; i++)
114: log(" cookie=" + cookies[i].getName() + "="
115: + cookies[i].getValue());
116: }
117: Enumeration hnames = hreq.getHeaderNames();
118: while (hnames.hasMoreElements()) {
119: String hname = (String) hnames.nextElement();
120: Enumeration hvalues = hreq.getHeaders(hname);
121: while (hvalues.hasMoreElements()) {
122: String hvalue = (String) hvalues.nextElement();
123: log(" header=" + hname + "=" + hvalue);
124: }
125: }
126: log(" locale=" + hreq.getLocale());
127: log(" method=" + hreq.getMethod());
128: Enumeration pnames = hreq.getParameterNames();
129: while (pnames.hasMoreElements()) {
130: String pname = (String) pnames.nextElement();
131: String pvalues[] = hreq.getParameterValues(pname);
132: StringBuffer result = new StringBuffer(pname);
133: result.append('=');
134: for (int i = 0; i < pvalues.length; i++) {
135: if (i > 0)
136: result.append(", ");
137: result.append(pvalues[i]);
138: }
139: log(" parameter=" + result.toString());
140: }
141: log(" pathInfo=" + hreq.getPathInfo());
142: log(" protocol=" + hreq.getProtocol());
143: log(" queryString=" + hreq.getQueryString());
144: log(" remoteAddr=" + hreq.getRemoteAddr());
145: log(" remoteHost=" + hreq.getRemoteHost());
146: log(" remoteUser=" + hreq.getRemoteUser());
147: log("requestedSessionId=" + hreq.getRequestedSessionId());
148: log(" scheme=" + hreq.getScheme());
149: log(" serverName=" + hreq.getServerName());
150: log(" serverPort=" + hreq.getServerPort());
151: log(" servletPath=" + hreq.getServletPath());
152: log(" isSecure=" + hreq.isSecure());
153: log("---------------------------------------------------------------");
154:
155: // Perform the request
156: context.invokeNext(request, response);
157:
158: // Log post-service information
159: log("---------------------------------------------------------------");
160: log(" authType=" + hreq.getAuthType());
161: log(" contentLength=" + hresponse.getContentLength());
162: log(" contentType=" + hresponse.getContentType());
163: Cookie rcookies[] = hresponse.getCookies();
164: for (int i = 0; i < rcookies.length; i++) {
165: log(" cookie=" + rcookies[i].getName() + "="
166: + rcookies[i].getValue() + "; domain="
167: + rcookies[i].getDomain() + "; path="
168: + rcookies[i].getPath());
169: }
170: String rhnames[] = hresponse.getHeaderNames();
171: for (int i = 0; i < rhnames.length; i++) {
172: String rhvalues[] = hresponse.getHeaderValues(rhnames[i]);
173: for (int j = 0; j < rhvalues.length; j++)
174: log(" header=" + rhnames[i] + "="
175: + rhvalues[j]);
176: }
177: log(" message=" + hresponse.getMessage());
178: log(" remoteUser=" + hreq.getRemoteUser());
179: log(" status=" + hresponse.getStatus());
180: log("===============================================================");
181:
182: }
183:
184: /**
185: * Return a String rendering of this object.
186: */
187: public String toString() {
188:
189: StringBuffer sb = new StringBuffer("RequestDumperValve[");
190: if (container != null)
191: sb.append(container.getName());
192: sb.append("]");
193: return (sb.toString());
194:
195: }
196:
197: // ------------------------------------------------------ Protected Methods
198:
199: /**
200: * Log a message on the Logger associated with our Container (if any).
201: *
202: * @param message Message to be logged
203: */
204: protected void log(String message) {
205:
206: Logger logger = container.getLogger();
207: if (logger != null)
208: logger.log(this .toString() + ": " + message);
209: else
210: System.out.println(this .toString() + ": " + message);
211:
212: }
213:
214: /**
215: * Log a message on the Logger associated with our Container (if any).
216: *
217: * @param message Message to be logged
218: * @param throwable Associated exception
219: */
220: protected void log(String message, Throwable throwable) {
221:
222: Logger logger = container.getLogger();
223: if (logger != null)
224: logger.log(this .toString() + ": " + message, throwable);
225: else {
226: System.out.println(this .toString() + ": " + message);
227: throwable.printStackTrace(System.out);
228: }
229:
230: }
231:
232: }
|