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: package org.apache.catalina.valves;
019:
020: import java.io.IOException;
021: import java.util.Enumeration;
022:
023: import javax.servlet.ServletException;
024: import javax.servlet.http.Cookie;
025:
026: import org.apache.catalina.connector.Request;
027: import org.apache.catalina.connector.Response;
028: import org.apache.catalina.util.StringManager;
029: import org.apache.juli.logging.Log;
030:
031: /**
032: * <p>Implementation of a Valve that logs interesting contents from the
033: * specified Request (before processing) and the corresponding Response
034: * (after processing). It is especially useful in debugging problems
035: * related to headers and cookies.</p>
036: *
037: * <p>This Valve may be attached to any Container, depending on the granularity
038: * of the logging you wish to perform.</p>
039: *
040: * @author Craig R. McClanahan
041: * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
042: */
043:
044: public class RequestDumperValve extends ValveBase {
045:
046: // ----------------------------------------------------- Instance Variables
047:
048: /**
049: * The descriptive information related to this implementation.
050: */
051: private static final String info = "org.apache.catalina.valves.RequestDumperValve/1.0";
052:
053: /**
054: * The StringManager for this package.
055: */
056: protected static StringManager sm = StringManager
057: .getManager(Constants.Package);
058:
059: // ------------------------------------------------------------- Properties
060:
061: /**
062: * Return descriptive information about this Valve implementation.
063: */
064: public String getInfo() {
065:
066: return (info);
067:
068: }
069:
070: // --------------------------------------------------------- Public Methods
071:
072: /**
073: * Log the interesting request parameters, invoke the next Valve in the
074: * sequence, and log the interesting response parameters.
075: *
076: * @param request The servlet request to be processed
077: * @param response The servlet response to be created
078: *
079: * @exception IOException if an input/output error occurs
080: * @exception ServletException if a servlet error occurs
081: */
082: public void invoke(Request request, Response response)
083: throws IOException, ServletException {
084:
085: Log log = container.getLogger();
086:
087: // Log pre-service information
088: log.info("REQUEST URI =" + request.getRequestURI());
089: log.info(" authType=" + request.getAuthType());
090: log
091: .info(" characterEncoding="
092: + request.getCharacterEncoding());
093: log.info(" contentLength=" + request.getContentLength());
094: log.info(" contentType=" + request.getContentType());
095: log.info(" contextPath=" + request.getContextPath());
096: Cookie cookies[] = request.getCookies();
097: if (cookies != null) {
098: for (int i = 0; i < cookies.length; i++)
099: log.info(" cookie=" + cookies[i].getName()
100: + "=" + cookies[i].getValue());
101: }
102: Enumeration hnames = request.getHeaderNames();
103: while (hnames.hasMoreElements()) {
104: String hname = (String) hnames.nextElement();
105: Enumeration hvalues = request.getHeaders(hname);
106: while (hvalues.hasMoreElements()) {
107: String hvalue = (String) hvalues.nextElement();
108: log.info(" header=" + hname + "=" + hvalue);
109: }
110: }
111: log.info(" locale=" + request.getLocale());
112: log.info(" method=" + request.getMethod());
113: Enumeration pnames = request.getParameterNames();
114: while (pnames.hasMoreElements()) {
115: String pname = (String) pnames.nextElement();
116: String pvalues[] = request.getParameterValues(pname);
117: StringBuffer result = new StringBuffer(pname);
118: result.append('=');
119: for (int i = 0; i < pvalues.length; i++) {
120: if (i > 0)
121: result.append(", ");
122: result.append(pvalues[i]);
123: }
124: log.info(" parameter=" + result.toString());
125: }
126: log.info(" pathInfo=" + request.getPathInfo());
127: log.info(" protocol=" + request.getProtocol());
128: log.info(" queryString=" + request.getQueryString());
129: log.info(" remoteAddr=" + request.getRemoteAddr());
130: log.info(" remoteHost=" + request.getRemoteHost());
131: log.info(" remoteUser=" + request.getRemoteUser());
132: log.info("requestedSessionId="
133: + request.getRequestedSessionId());
134: log.info(" scheme=" + request.getScheme());
135: log.info(" serverName=" + request.getServerName());
136: log.info(" serverPort=" + request.getServerPort());
137: log.info(" servletPath=" + request.getServletPath());
138: log.info(" isSecure=" + request.isSecure());
139: log
140: .info("---------------------------------------------------------------");
141:
142: // Perform the request
143: getNext().invoke(request, response);
144:
145: // Log post-service information
146: log
147: .info("---------------------------------------------------------------");
148: log.info(" authType=" + request.getAuthType());
149: log.info(" contentLength=" + response.getContentLength());
150: log.info(" contentType=" + response.getContentType());
151: Cookie rcookies[] = response.getCookies();
152: for (int i = 0; i < rcookies.length; i++) {
153: log.info(" cookie=" + rcookies[i].getName()
154: + "=" + rcookies[i].getValue() + "; domain="
155: + rcookies[i].getDomain() + "; path="
156: + rcookies[i].getPath());
157: }
158: String rhnames[] = response.getHeaderNames();
159: for (int i = 0; i < rhnames.length; i++) {
160: String rhvalues[] = response.getHeaderValues(rhnames[i]);
161: for (int j = 0; j < rhvalues.length; j++)
162: log.info(" header=" + rhnames[i] + "="
163: + rhvalues[j]);
164: }
165: log.info(" message=" + response.getMessage());
166: log.info(" remoteUser=" + request.getRemoteUser());
167: log.info(" status=" + response.getStatus());
168: log
169: .info("===============================================================");
170:
171: }
172:
173: /**
174: * Return a String rendering of this object.
175: */
176: public String toString() {
177:
178: StringBuffer sb = new StringBuffer("RequestDumperValve[");
179: if (container != null)
180: sb.append(container.getName());
181: sb.append("]");
182: return (sb.toString());
183:
184: }
185:
186: }
|