Clover coverage report - Maven Clover report
Coverage timestamp: Thu Aug 24 2006 01:18:17 PDT
file stats: LOC: 201   Methods: 4
NCLOC: 108   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
RequestDumperFilter.java 0% 0% 0% 0%
coverage
 1    /*
 2    * Copyright 2004 The Apache Software Foundation
 3    *
 4    * Licensed under the Apache License, Version 2.0 (the "License");
 5    * you may not use this file except in compliance with the License.
 6    * You may obtain a copy of the License at
 7    *
 8    * http://www.apache.org/licenses/LICENSE-2.0
 9    *
 10    * Unless required by applicable law or agreed to in writing, software
 11    * distributed under the License is distributed on an "AS IS" BASIS,
 12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13    * See the License for the specific language governing permissions and
 14    * limitations under the License.
 15    */
 16   
 17   
 18   
 19    package filters;
 20   
 21   
 22    import java.io.IOException;
 23    import java.io.PrintWriter;
 24    import java.io.StringWriter;
 25    import java.sql.Timestamp;
 26    import java.util.Enumeration;
 27    import java.util.Locale;
 28    import javax.servlet.Filter;
 29    import javax.servlet.FilterChain;
 30    import javax.servlet.FilterConfig;
 31    import javax.servlet.ServletContext;
 32    import javax.servlet.ServletException;
 33    import javax.servlet.ServletRequest;
 34    import javax.servlet.ServletResponse;
 35    import javax.servlet.http.Cookie;
 36    import javax.servlet.http.HttpServletRequest;
 37   
 38   
 39    /**
 40    * Example filter that dumps interesting state information about a request
 41    * to the associated servlet context log file, before allowing the servlet
 42    * to process the request in the usual way. This can be installed as needed
 43    * to assist in debugging problems.
 44    *
 45    * @author Craig McClanahan
 46    * @version $Revision: 267129 $ $Date: 2004-03-18 08:40:35 -0800 (Thu, 18 Mar 2004) $
 47    */
 48   
 49    public final class RequestDumperFilter implements Filter {
 50   
 51   
 52    // ----------------------------------------------------- Instance Variables
 53   
 54   
 55    /**
 56    * The filter configuration object we are associated with. If this value
 57    * is null, this filter instance is not currently configured.
 58    */
 59    private FilterConfig filterConfig = null;
 60   
 61   
 62    // --------------------------------------------------------- Public Methods
 63   
 64   
 65    /**
 66    * Take this filter out of service.
 67    */
 68  0 public void destroy() {
 69   
 70  0 this.filterConfig = null;
 71   
 72    }
 73   
 74   
 75    /**
 76    * Time the processing that is performed by all subsequent filters in the
 77    * current filter stack, including the ultimately invoked servlet.
 78    *
 79    * @param request The servlet request we are processing
 80    * @param result The servlet response we are creating
 81    * @param chain The filter chain we are processing
 82    *
 83    * @exception IOException if an input/output error occurs
 84    * @exception ServletException if a servlet error occurs
 85    */
 86  0 public void doFilter(ServletRequest request, ServletResponse response,
 87    FilterChain chain)
 88    throws IOException, ServletException {
 89   
 90  0 if (filterConfig == null)
 91  0 return;
 92   
 93    // Render the generic servlet request properties
 94  0 StringWriter sw = new StringWriter();
 95  0 PrintWriter writer = new PrintWriter(sw);
 96  0 writer.println("Request Received at " +
 97    (new Timestamp(System.currentTimeMillis())));
 98  0 writer.println(" characterEncoding=" + request.getCharacterEncoding());
 99  0 writer.println(" contentLength=" + request.getContentLength());
 100  0 writer.println(" contentType=" + request.getContentType());
 101  0 writer.println(" locale=" + request.getLocale());
 102  0 writer.print(" locales=");
 103  0 Enumeration locales = request.getLocales();
 104  0 boolean first = true;
 105  0 while (locales.hasMoreElements()) {
 106  0 Locale locale = (Locale) locales.nextElement();
 107  0 if (first)
 108  0 first = false;
 109    else
 110  0 writer.print(", ");
 111  0 writer.print(locale.toString());
 112    }
 113  0 writer.println();
 114  0 Enumeration names = request.getParameterNames();
 115  0 while (names.hasMoreElements()) {
 116  0 String name = (String) names.nextElement();
 117  0 writer.print(" parameter=" + name + "=");
 118  0 String values[] = request.getParameterValues(name);
 119  0 for (int i = 0; i < values.length; i++) {
 120  0 if (i > 0)
 121  0 writer.print(", ");
 122  0 writer.print(values[i]);
 123    }
 124  0 writer.println();
 125    }
 126  0 writer.println(" protocol=" + request.getProtocol());
 127  0 writer.println(" remoteAddr=" + request.getRemoteAddr());
 128  0 writer.println(" remoteHost=" + request.getRemoteHost());
 129  0 writer.println(" scheme=" + request.getScheme());
 130  0 writer.println(" serverName=" + request.getServerName());
 131  0 writer.println(" serverPort=" + request.getServerPort());
 132  0 writer.println(" isSecure=" + request.isSecure());
 133   
 134    // Render the HTTP servlet request properties
 135  0 if (request instanceof HttpServletRequest) {
 136  0 writer.println("---------------------------------------------");
 137  0 HttpServletRequest hrequest = (HttpServletRequest) request;
 138  0 writer.println(" contextPath=" + hrequest.getContextPath());
 139  0 Cookie cookies[] = hrequest.getCookies();
 140  0 if (cookies == null)
 141  0 cookies = new Cookie[0];
 142  0 for (int i = 0; i < cookies.length; i++) {
 143  0 writer.println(" cookie=" + cookies[i].getName() +
 144    "=" + cookies[i].getValue());
 145    }
 146  0 names = hrequest.getHeaderNames();
 147  0 while (names.hasMoreElements()) {
 148  0 String name = (String) names.nextElement();
 149  0 String value = hrequest.getHeader(name);
 150  0 writer.println(" header=" + name + "=" + value);
 151    }
 152  0 writer.println(" method=" + hrequest.getMethod());
 153  0 writer.println(" pathInfo=" + hrequest.getPathInfo());
 154  0 writer.println(" queryString=" + hrequest.getQueryString());
 155  0 writer.println(" remoteUser=" + hrequest.getRemoteUser());
 156  0 writer.println("requestedSessionId=" +
 157    hrequest.getRequestedSessionId());
 158  0 writer.println(" requestURI=" + hrequest.getRequestURI());
 159  0 writer.println(" servletPath=" + hrequest.getServletPath());
 160    }
 161  0 writer.println("=============================================");
 162   
 163    // Log the resulting string
 164  0 writer.flush();
 165  0 filterConfig.getServletContext().log(sw.getBuffer().toString());
 166   
 167    // Pass control on to the next filter
 168  0 chain.doFilter(request, response);
 169   
 170    }
 171   
 172   
 173    /**
 174    * Place this filter into service.
 175    *
 176    * @param filterConfig The filter configuration object
 177    */
 178  0 public void init(FilterConfig filterConfig) throws ServletException {
 179   
 180  0 this.filterConfig = filterConfig;
 181   
 182    }
 183   
 184   
 185    /**
 186    * Return a String representation of this object.
 187    */
 188  0 public String toString() {
 189   
 190  0 if (filterConfig == null)
 191  0 return ("RequestDumperFilter()");
 192  0 StringBuffer sb = new StringBuffer("RequestDumperFilter(");
 193  0 sb.append(filterConfig);
 194  0 sb.append(")");
 195  0 return (sb.toString());
 196   
 197    }
 198   
 199   
 200    }
 201