KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > filters > ExampleFilter


1
2
3 /*
4  * The contents of this file are subject to the terms
5  * of the Common Development and Distribution License
6  * (the "License"). You may not use this file except
7  * in compliance with the License.
8  *
9  * You can obtain a copy of the license at
10  * glassfish/bootstrap/legal/CDDLv1.0.txt or
11  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
12  * See the License for the specific language governing
13  * permissions and limitations under the License.
14  *
15  * When distributing Covered Code, include this CDDL
16  * HEADER in each file and include the License file at
17  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
18  * add the following below this CDDL HEADER, with the
19  * fields enclosed by brackets "[]" replaced with your
20  * own identifying information: Portions Copyright [yyyy]
21  * [name of copyright owner]
22  *
23  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24  *
25  * Portions Copyright Apache Software Foundation.
26  */

27
28
29 package filters;
30
31
32 import java.io.IOException JavaDoc;
33 import javax.servlet.Filter JavaDoc;
34 import javax.servlet.FilterChain JavaDoc;
35 import javax.servlet.FilterConfig JavaDoc;
36 import javax.servlet.ServletContext JavaDoc;
37 import javax.servlet.ServletException JavaDoc;
38 import javax.servlet.ServletRequest JavaDoc;
39 import javax.servlet.ServletResponse JavaDoc;
40
41
42 /**
43  * Example filter that can be attached to either an individual servlet
44  * or to a URL pattern. This filter performs the following functions:
45  * <ul>
46  * <li>Attaches itself as a request attribute, under the attribute name
47  * defined by the value of the <code>attribute</code> initialization
48  * parameter.</li>
49  * <li>Calculates the number of milliseconds required to perform the
50  * servlet processing required by this request, including any
51  * subsequently defined filters, and logs the result to the servlet
52  * context log for this application.
53  * </ul>
54  *
55  * @author Craig McClanahan
56  * @version $Revision: 1.2 $ $Date: 2005/12/08 01:13:37 $
57  */

58
59 public final class ExampleFilter implements Filter JavaDoc {
60
61
62     // ----------------------------------------------------- Instance Variables
63

64
65     /**
66      * The request attribute name under which we store a reference to ourself.
67      */

68     private String JavaDoc attribute = null;
69
70
71     /**
72      * The filter configuration object we are associated with. If this value
73      * is null, this filter instance is not currently configured.
74      */

75     private FilterConfig JavaDoc filterConfig = null;
76
77
78     // --------------------------------------------------------- Public Methods
79

80
81     /**
82      * Take this filter out of service.
83      */

84     public void destroy() {
85
86         this.attribute = null;
87         this.filterConfig = null;
88
89     }
90
91
92     /**
93      * Time the processing that is performed by all subsequent filters in the
94      * current filter stack, including the ultimately invoked servlet.
95      *
96      * @param request The servlet request we are processing
97      * @param result The servlet response we are creating
98      * @param chain The filter chain we are processing
99      *
100      * @exception IOException if an input/output error occurs
101      * @exception ServletException if a servlet error occurs
102      */

103     public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response,
104                          FilterChain JavaDoc chain)
105     throws IOException JavaDoc, ServletException JavaDoc {
106
107     // Store ourselves as a request attribute (if requested)
108
if (attribute != null)
109         request.setAttribute(attribute, this);
110
111     // Time and log the subsequent processing
112
long startTime = System.currentTimeMillis();
113         chain.doFilter(request, response);
114     long stopTime = System.currentTimeMillis();
115     filterConfig.getServletContext().log
116         (this.toString() + ": " + (stopTime - startTime) +
117          " milliseconds");
118
119     }
120
121
122     /**
123      * Place this filter into service.
124      *
125      * @param filterConfig The filter configuration object
126      */

127     public void init(FilterConfig JavaDoc filterConfig) throws ServletException JavaDoc {
128
129     this.filterConfig = filterConfig;
130         this.attribute = filterConfig.getInitParameter("attribute");
131
132     }
133
134
135     /**
136      * Return a String representation of this object.
137      */

138     public String JavaDoc toString() {
139
140     if (filterConfig == null)
141         return ("InvokerFilter()");
142     StringBuffer JavaDoc sb = new StringBuffer JavaDoc("InvokerFilter(");
143     sb.append(filterConfig);
144     sb.append(")");
145     return (sb.toString());
146
147     }
148
149
150 }
151
152
Popular Tags