View Javadoc

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