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