Advertisement
Guest User

TomcatRequestDumperFilter

a guest
Jul 21st, 2011
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.82 KB | None | 0 0
  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 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 $Id: RequestDumperFilter.java 939541 2010-04-30 01:43:56Z kkolinko $
  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.     public void destroy() {
  69.  
  70.         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.     public void doFilter(ServletRequest request, ServletResponse response,
  87.                          FilterChain chain)
  88.     throws IOException, ServletException {
  89.  
  90.         if (filterConfig == null)
  91.         return;
  92.  
  93.     // Render the generic servlet request properties
  94.     StringWriter sw = new StringWriter();
  95.     PrintWriter writer = new PrintWriter(sw);
  96.     writer.println("Request Received at " +
  97.                (new Timestamp(System.currentTimeMillis())));
  98.     writer.println(" characterEncoding=" + request.getCharacterEncoding());
  99.     writer.println("     contentLength=" + request.getContentLength());
  100.     writer.println("       contentType=" + request.getContentType());
  101.     writer.println("            locale=" + request.getLocale());
  102.     writer.print("           locales=");
  103.     Enumeration locales = request.getLocales();
  104.     boolean first = true;
  105.     while (locales.hasMoreElements()) {
  106.         Locale locale = (Locale) locales.nextElement();
  107.         if (first)
  108.             first = false;
  109.         else
  110.             writer.print(", ");
  111.         writer.print(locale.toString());
  112.     }
  113.     writer.println();
  114.     Enumeration names = request.getParameterNames();
  115.     while (names.hasMoreElements()) {
  116.         String name = (String) names.nextElement();
  117.         writer.print("         parameter=" + name + "=");
  118.         String values[] = request.getParameterValues(name);
  119.         for (int i = 0; i < values.length; i++) {
  120.             if (i > 0)
  121.             writer.print(", ");
  122.         writer.print(values[i]);
  123.         }
  124.         writer.println();
  125.     }
  126.     writer.println("          protocol=" + request.getProtocol());
  127.     writer.println("        remoteAddr=" + request.getRemoteAddr());
  128.     writer.println("        remoteHost=" + request.getRemoteHost());
  129.     writer.println("            scheme=" + request.getScheme());
  130.     writer.println("        serverName=" + request.getServerName());
  131.     writer.println("        serverPort=" + request.getServerPort());
  132.     writer.println("          isSecure=" + request.isSecure());
  133.  
  134.     // Render the HTTP servlet request properties
  135.     if (request instanceof HttpServletRequest) {
  136.         writer.println("---------------------------------------------");
  137.         HttpServletRequest hrequest = (HttpServletRequest) request;
  138.         writer.println("       contextPath=" + hrequest.getContextPath());
  139.         Cookie cookies[] = hrequest.getCookies();
  140.             if (cookies == null)
  141.                 cookies = new Cookie[0];
  142.         for (int i = 0; i < cookies.length; i++) {
  143.             writer.println("            cookie=" + cookies[i].getName() +
  144.                    "=" + cookies[i].getValue());
  145.         }
  146.         names = hrequest.getHeaderNames();
  147.         while (names.hasMoreElements()) {
  148.             String name = (String) names.nextElement();
  149.         String value = hrequest.getHeader(name);
  150.             writer.println("            header=" + name + "=" + value);
  151.         }
  152.         writer.println("            method=" + hrequest.getMethod());
  153.         writer.println("          pathInfo=" + hrequest.getPathInfo());
  154.         writer.println("       queryString=" + hrequest.getQueryString());
  155.         writer.println("        remoteUser=" + hrequest.getRemoteUser());
  156.         writer.println("requestedSessionId=" +
  157.                hrequest.getRequestedSessionId());
  158.         writer.println("        requestURI=" + hrequest.getRequestURI());
  159.         writer.println("       servletPath=" + hrequest.getServletPath());
  160.     }
  161.     writer.println("=============================================");
  162.  
  163.     // Log the resulting string
  164.     writer.flush();
  165.     filterConfig.getServletContext().log(sw.getBuffer().toString());
  166.  
  167.     // Pass control on to the next filter
  168.         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.     public void init(FilterConfig filterConfig) throws ServletException {
  179.  
  180.     this.filterConfig = filterConfig;
  181.  
  182.     }
  183.  
  184.  
  185.     /**
  186.      * Return a String representation of this object.
  187.      */
  188.     public String toString() {
  189.  
  190.     if (filterConfig == null)
  191.         return ("RequestDumperFilter()");
  192.     StringBuffer sb = new StringBuffer("RequestDumperFilter(");
  193.     sb.append(filterConfig);
  194.     sb.append(")");
  195.     return (sb.toString());
  196.  
  197.     }
  198.  
  199.  
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement