|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ExampleFilter.java | 0% | 0% | 0% | 0% |
|
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 | ||
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.ServletContext; | |
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: 267129 $ $Date: 2004-03-18 08:40:35 -0800 (Thu, 18 Mar 2004) $ | |
46 | */ | |
47 | ||
48 | public final class ExampleFilter implements Filter { | |
49 | ||
50 | ||
51 | // ----------------------------------------------------- Instance Variables | |
52 | ||
53 | ||
54 | /** | |
55 | * The request attribute name under which we store a reference to ourself. | |
56 | */ | |
57 | private String attribute = null; | |
58 | ||
59 | ||
60 | /** | |
61 | * The filter configuration object we are associated with. If this value | |
62 | * is null, this filter instance is not currently configured. | |
63 | */ | |
64 | private FilterConfig filterConfig = null; | |
65 | ||
66 | ||
67 | // --------------------------------------------------------- Public Methods | |
68 | ||
69 | ||
70 | /** | |
71 | * Take this filter out of service. | |
72 | */ | |
73 | 0 | public void destroy() { |
74 | ||
75 | 0 | this.attribute = null; |
76 | 0 | this.filterConfig = null; |
77 | ||
78 | } | |
79 | ||
80 | ||
81 | /** | |
82 | * Time the processing that is performed by all subsequent filters in the | |
83 | * current filter stack, including the ultimately invoked servlet. | |
84 | * | |
85 | * @param request The servlet request we are processing | |
86 | * @param result The servlet response we are creating | |
87 | * @param chain The filter chain we are processing | |
88 | * | |
89 | * @exception IOException if an input/output error occurs | |
90 | * @exception ServletException if a servlet error occurs | |
91 | */ | |
92 | 0 | public void doFilter(ServletRequest request, ServletResponse response, |
93 | FilterChain chain) | |
94 | throws IOException, ServletException { | |
95 | ||
96 | // Store ourselves as a request attribute (if requested) | |
97 | 0 | if (attribute != null) |
98 | 0 | request.setAttribute(attribute, this); |
99 | ||
100 | // Time and log the subsequent processing | |
101 | 0 | long startTime = System.currentTimeMillis(); |
102 | 0 | chain.doFilter(request, response); |
103 | 0 | long stopTime = System.currentTimeMillis(); |
104 | 0 | 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 | 0 | public void init(FilterConfig filterConfig) throws ServletException { |
117 | ||
118 | 0 | this.filterConfig = filterConfig; |
119 | 0 | this.attribute = filterConfig.getInitParameter("attribute"); |
120 | ||
121 | } | |
122 | ||
123 | ||
124 | /** | |
125 | * Return a String representation of this object. | |
126 | */ | |
127 | 0 | public String toString() { |
128 | ||
129 | 0 | if (filterConfig == null) |
130 | 0 | return ("InvokerFilter()"); |
131 | 0 | StringBuffer sb = new StringBuffer("InvokerFilter("); |
132 | 0 | sb.append(filterConfig); |
133 | 0 | sb.append(")"); |
134 | 0 | return (sb.toString()); |
135 | ||
136 | } | |
137 | ||
138 | ||
139 | } | |
140 |
|