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 | import javax.servlet.Filter; |
---|
23 | import javax.servlet.FilterChain; |
---|
24 | import javax.servlet.FilterConfig; |
---|
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: 500674 $ $Date: 2007-01-28 00:15:00 +0100 (Sun, 28 Jan 2007) $ |
---|
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 | |
---|