1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
19 package org.apache.catalina.valves;
20
21
22 import java.io.IOException;
23 import java.util.Enumeration;
24
25 import javax.servlet.ServletException;
26 import javax.servlet.http.Cookie;
27
28 import org.apache.catalina.connector.Request;
29 import org.apache.catalina.connector.Response;
30 import org.apache.catalina.util.StringManager;
31 import org.apache.juli.logging.Log;
32
33
34 /**
35 * <p>Implementation of a Valve that logs interesting contents from the
36 * specified Request (before processing) and the corresponding Response
37 * (after processing). It is especially useful in debugging problems
38 * related to headers and cookies.</p>
39 *
40 * <p>This Valve may be attached to any Container, depending on the granularity
41 * of the logging you wish to perform.</p>
42 *
43 * @author Craig R. McClanahan
44 * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
45 */
46
47 public class RequestDumperValve
48 extends ValveBase {
49
50
51 // ----------------------------------------------------- Instance Variables
52
53
54 /**
55 * The descriptive information related to this implementation.
56 */
57 private static final String info =
58 "org.apache.catalina.valves.RequestDumperValve/1.0";
59
60
61 /**
62 * The StringManager for this package.
63 */
64 protected static StringManager sm =
65 StringManager.getManager(Constants.Package);
66
67
68 // ------------------------------------------------------------- Properties
69
70
71 /**
72 * Return descriptive information about this Valve implementation.
73 */
74 public String getInfo() {
75
76 return (info);
77
78 }
79
80
81 // --------------------------------------------------------- Public Methods
82
83
84 /**
85 * Log the interesting request parameters, invoke the next Valve in the
86 * sequence, and log the interesting response parameters.
87 *
88 * @param request The servlet request to be processed
89 * @param response The servlet response to be created
90 *
91 * @exception IOException if an input/output error occurs
92 * @exception ServletException if a servlet error occurs
93 */
94 public void invoke(Request request, Response response)
95 throws IOException, ServletException {
96
97 Log log = container.getLogger();
98
99 // Log pre-service information
100 log.info("REQUEST URI =" + request.getRequestURI());
101 log.info(" authType=" + request.getAuthType());
102 log.info(" characterEncoding=" + request.getCharacterEncoding());
103 log.info(" contentLength=" + request.getContentLength());
104 log.info(" contentType=" + request.getContentType());
105 log.info(" contextPath=" + request.getContextPath());
106 Cookie cookies[] = request.getCookies();
107 if (cookies != null) {
108 for (int i = 0; i < cookies.length; i++)
109 log.info(" cookie=" + cookies[i].getName() + "=" +
110 cookies[i].getValue());
111 }
112 Enumeration hnames = request.getHeaderNames();
113 while (hnames.hasMoreElements()) {
114 String hname = (String) hnames.nextElement();
115 Enumeration hvalues = request.getHeaders(hname);
116 while (hvalues.hasMoreElements()) {
117 String hvalue = (String) hvalues.nextElement();
118 log.info(" header=" + hname + "=" + hvalue);
119 }
120 }
121 log.info(" locale=" + request.getLocale());
122 log.info(" method=" + request.getMethod());
123 Enumeration pnames = request.getParameterNames();
124 while (pnames.hasMoreElements()) {
125 String pname = (String) pnames.nextElement();
126 String pvalues[] = request.getParameterValues(pname);
127 StringBuffer result = new StringBuffer(pname);
128 result.append('=');
129 for (int i = 0; i < pvalues.length; i++) {
130 if (i > 0)
131 result.append(", ");
132 result.append(pvalues[i]);
133 }
134 log.info(" parameter=" + result.toString());
135 }
136 log.info(" pathInfo=" + request.getPathInfo());
137 log.info(" protocol=" + request.getProtocol());
138 log.info(" queryString=" + request.getQueryString());
139 log.info(" remoteAddr=" + request.getRemoteAddr());
140 log.info(" remoteHost=" + request.getRemoteHost());
141 log.info(" remoteUser=" + request.getRemoteUser());
142 log.info("requestedSessionId=" + request.getRequestedSessionId());
143 log.info(" scheme=" + request.getScheme());
144 log.info(" serverName=" + request.getServerName());
145 log.info(" serverPort=" + request.getServerPort());
146 log.info(" servletPath=" + request.getServletPath());
147 log.info(" isSecure=" + request.isSecure());
148 log.info("---------------------------------------------------------------");
149
150 // Perform the request
151 getNext().invoke(request, response);
152
153 // Log post-service information
154 log.info("---------------------------------------------------------------");
155 log.info(" authType=" + request.getAuthType());
156 log.info(" contentLength=" + response.getContentLength());
157 log.info(" contentType=" + response.getContentType());
158 Cookie rcookies[] = response.getCookies();
159 for (int i = 0; i < rcookies.length; i++) {
160 log.info(" cookie=" + rcookies[i].getName() + "=" +
161 rcookies[i].getValue() + "; domain=" +
162 rcookies[i].getDomain() + "; path=" + rcookies[i].getPath());
163 }
164 String rhnames[] = response.getHeaderNames();
165 for (int i = 0; i < rhnames.length; i++) {
166 String rhvalues[] = response.getHeaderValues(rhnames[i]);
167 for (int j = 0; j < rhvalues.length; j++)
168 log.info(" header=" + rhnames[i] + "=" + rhvalues[j]);
169 }
170 log.info(" message=" + response.getMessage());
171 log.info(" remoteUser=" + request.getRemoteUser());
172 log.info(" status=" + response.getStatus());
173 log.info("===============================================================");
174
175 }
176
177
178 /**
179 * Return a String rendering of this object.
180 */
181 public String toString() {
182
183 StringBuffer sb = new StringBuffer("RequestDumperValve[");
184 if (container != null)
185 sb.append(container.getName());
186 sb.append("]");
187 return (sb.toString());
188
189 }
190
191
192 }