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 javax.servlet.Filter; 024 import javax.servlet.FilterChain; 025 import javax.servlet.FilterConfig; 026 import javax.servlet.ServletException; 027 import javax.servlet.ServletRequest; 028 import javax.servlet.ServletResponse; 029 030 031 /** 032 * Example filter that can be attached to either an individual servlet 033 * or to a URL pattern. This filter performs the following functions: 034 * <ul> 035 * <li>Attaches itself as a request attribute, under the attribute name 036 * defined by the value of the <code>attribute</code> initialization 037 * parameter.</li> 038 * <li>Calculates the number of milliseconds required to perform the 039 * servlet processing required by this request, including any 040 * subsequently defined filters, and logs the result to the servlet 041 * context log for this application. 042 * </ul> 043 * 044 * @author Craig McClanahan 045 * @version $Revision: 514091 $ $Date: 2007-03-03 01:26:39 -0500 (Sat, 03 Mar 2007) $ 046 */ 047 048 public final class ExampleFilter implements Filter { 049 050 051 // ----------------------------------------------------- Instance Variables 052 053 054 /** 055 * The request attribute name under which we store a reference to ourself. 056 */ 057 private String attribute = null; 058 059 060 /** 061 * The filter configuration object we are associated with. If this value 062 * is null, this filter instance is not currently configured. 063 */ 064 private FilterConfig filterConfig = null; 065 066 067 // --------------------------------------------------------- Public Methods 068 069 070 /** 071 * Take this filter out of service. 072 */ 073 public void destroy() { 074 075 this.attribute = null; 076 this.filterConfig = null; 077 078 } 079 080 081 /** 082 * Time the processing that is performed by all subsequent filters in the 083 * current filter stack, including the ultimately invoked servlet. 084 * 085 * @param request The servlet request we are processing 086 * @param result The servlet response we are creating 087 * @param chain The filter chain we are processing 088 * 089 * @exception IOException if an input/output error occurs 090 * @exception ServletException if a servlet error occurs 091 */ 092 public void doFilter(ServletRequest request, ServletResponse response, 093 FilterChain chain) 094 throws IOException, ServletException { 095 096 // Store ourselves as a request attribute (if requested) 097 if (attribute != null) 098 request.setAttribute(attribute, this); 099 100 // Time and log the subsequent processing 101 long startTime = System.currentTimeMillis(); 102 chain.doFilter(request, response); 103 long stopTime = System.currentTimeMillis(); 104 filterConfig.getServletContext().log 105 (this.toString() + ": " + (stopTime - startTime) + 106 " milliseconds"); 107 108 } 109 110 111 /** 112 * Place this filter into service. 113 * 114 * @param filterConfig The filter configuration object 115 */ 116 public void init(FilterConfig filterConfig) throws ServletException { 117 118 this.filterConfig = filterConfig; 119 this.attribute = filterConfig.getInitParameter("attribute"); 120 121 } 122 123 124 /** 125 * Return a String representation of this object. 126 */ 127 public String toString() { 128 129 if (filterConfig == null) 130 return ("InvokerFilter()"); 131 StringBuffer sb = new StringBuffer("InvokerFilter("); 132 sb.append(filterConfig); 133 sb.append(")"); 134 return (sb.toString()); 135 136 } 137 138 139 } 140