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