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 package filters; 19 20 21 import java.io.IOException; 22 23 import javax.servlet.Filter; 24 import javax.servlet.FilterChain; 25 import javax.servlet.FilterConfig; 26 import javax.servlet.ServletException; 27 import javax.servlet.ServletRequest; 28 import javax.servlet.ServletResponse; 29 30 31 /** 32 * Example filter that can be attached to either an individual servlet 33 * or to a URL pattern. This filter performs the following functions: 34 * <ul> 35 * <li>Attaches itself as a request attribute, under the attribute name 36 * defined by the value of the <code>attribute</code> initialization 37 * parameter.</li> 38 * <li>Calculates the number of milliseconds required to perform the 39 * servlet processing required by this request, including any 40 * subsequently defined filters, and logs the result to the servlet 41 * context log for this application. 42 * </ul> 43 * 44 * @author Craig McClanahan 45 * @version $Revision: 664175 $ $Date: 2008-06-06 18:43:44 -0400 (Fri, 06 Jun 2008) $ 46 */ 47 48 public final class ExampleFilter implements Filter { 49 50 // ----------------------------------------------------- Instance Variables 51 52 53 /** 54 * The request attribute name under which we store a reference to ourself. 55 */ 56 private String attribute = null; 57 58 59 /** 60 * The filter configuration object we are associated with. If this value 61 * is null, this filter instance is not currently configured. 62 */ 63 private FilterConfig filterConfig = null; 64 65 // --------------------------------------------------------- Public Methods 66 67 68 /** 69 * Take this filter out of service. 70 */ 71 public void destroy() { 72 73 this.attribute = null; 74 this.filterConfig = null; 75 76 } 77 78 79 /** 80 * Time the processing that is performed by all subsequent filters in the 81 * current filter stack, including the ultimately invoked servlet. 82 * 83 * @param request The servlet request we are processing 84 * @param result The servlet response we are creating 85 * @param chain The filter chain we are processing 86 * @throws IOException if an input/output error occurs 87 * @throws ServletException if a servlet error occurs 88 */ 89 public void doFilter(ServletRequest request, ServletResponse response, 90 FilterChain chain) 91 throws IOException, ServletException { 92 93 // Store ourselves as a request attribute (if requested) 94 if (attribute != null) 95 request.setAttribute(attribute, this); 96 97 // Time and log the subsequent processing 98 long startTime = System.currentTimeMillis(); 99 chain.doFilter(request, response); 100 long stopTime = System.currentTimeMillis(); 101 filterConfig.getServletContext().log 102 (this.toString() + ": " + (stopTime - startTime) + 103 " milliseconds"); 104 105 } 106 107 108 /** 109 * Place this filter into service. 110 * 111 * @param filterConfig The filter configuration object 112 */ 113 public void init(FilterConfig filterConfig) throws ServletException { 114 115 this.filterConfig = filterConfig; 116 this.attribute = filterConfig.getInitParameter("attribute"); 117 118 } 119 120 121 /** 122 * Return a String representation of this object. 123 */ 124 public String toString() { 125 126 if (filterConfig == null) 127 return ("InvokerFilter()"); 128 StringBuffer sb = new StringBuffer("InvokerFilter("); 129 sb.append(filterConfig); 130 sb.append(")"); 131 return (sb.toString()); 132 133 } 134 135 136 } 137