1 16 17 18 package filters; 19 20 21 import java.io.IOException ; 22 import java.io.PrintWriter ; 23 import java.io.StringWriter ; 24 import java.sql.Timestamp ; 25 import java.util.Enumeration ; 26 import java.util.Locale ; 27 import javax.servlet.Filter ; 28 import javax.servlet.FilterChain ; 29 import javax.servlet.FilterConfig ; 30 import javax.servlet.ServletContext ; 31 import javax.servlet.ServletException ; 32 import javax.servlet.ServletRequest ; 33 import javax.servlet.ServletResponse ; 34 import javax.servlet.http.Cookie ; 35 import javax.servlet.http.HttpServletRequest ; 36 37 38 47 48 public final class RequestDumperFilter implements Filter { 49 50 51 53 54 58 private FilterConfig filterConfig = null; 59 60 61 63 64 67 public void destroy() { 68 69 this.filterConfig = null; 70 71 } 72 73 74 85 public void doFilter(ServletRequest request, ServletResponse response, 86 FilterChain chain) 87 throws IOException , ServletException { 88 89 if (filterConfig == null) 90 return; 91 92 StringWriter sw = new StringWriter (); 94 PrintWriter writer = new PrintWriter (sw); 95 writer.println("Request Received at " + 96 (new Timestamp (System.currentTimeMillis()))); 97 writer.println(" characterEncoding=" + request.getCharacterEncoding()); 98 writer.println(" contentLength=" + request.getContentLength()); 99 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 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 writer.flush(); 164 filterConfig.getServletContext().log(sw.getBuffer().toString()); 165 166 chain.doFilter(request, response); 168 169 } 170 171 172 177 public void init(FilterConfig filterConfig) throws ServletException { 178 179 this.filterConfig = filterConfig; 180 181 } 182 183 184 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 201 | Popular Tags |