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