001    /*
002    * Licensed to the Apache Software Foundation (ASF) under one or more
003    * contributor license agreements.  See the NOTICE file distributed with
004    * this work for additional information regarding copyright ownership.
005    * The ASF licenses this file to You under the Apache License, Version 2.0
006    * (the "License"); you may not use this file except in compliance with
007    * the License.  You may obtain a copy of the License at
008    *
009    *     http://www.apache.org/licenses/LICENSE-2.0
010    *
011    * Unless required by applicable law or agreed to in writing, software
012    * distributed under the License is distributed on an "AS IS" BASIS,
013    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014    * See the License for the specific language governing permissions and
015    * limitations under the License.
016    */
017    
018    package filters;
019    
020    
021    import java.io.IOException;
022    
023    import javax.servlet.Filter;
024    import javax.servlet.FilterChain;
025    import javax.servlet.FilterConfig;
026    import javax.servlet.ServletException;
027    import javax.servlet.ServletRequest;
028    import javax.servlet.ServletResponse;
029    
030    
031    /**
032     * Example filter that can be attached to either an individual servlet
033     * or to a URL pattern.  This filter performs the following functions:
034     * <ul>
035     * <li>Attaches itself as a request attribute, under the attribute name
036     * defined by the value of the <code>attribute</code> initialization
037     * parameter.</li>
038     * <li>Calculates the number of milliseconds required to perform the
039     * servlet processing required by this request, including any
040     * subsequently defined filters, and logs the result to the servlet
041     * context log for this application.
042     * </ul>
043     *
044     * @author Craig McClanahan
045     * @version $Revision: 664175 $ $Date: 2008-06-06 18:43:44 -0400 (Fri, 06 Jun 2008) $
046     */
047    
048    public final class ExampleFilter implements Filter {
049    
050        // ----------------------------------------------------- Instance Variables
051    
052    
053        /**
054         * The request attribute name under which we store a reference to ourself.
055         */
056        private String attribute = null;
057    
058    
059        /**
060         * The filter configuration object we are associated with.  If this value
061         * is null, this filter instance is not currently configured.
062         */
063        private FilterConfig filterConfig = null;
064    
065        // --------------------------------------------------------- Public Methods
066    
067    
068        /**
069         * Take this filter out of service.
070         */
071        public void destroy() {
072    
073            this.attribute = null;
074            this.filterConfig = null;
075    
076        }
077    
078    
079        /**
080         * Time the processing that is performed by all subsequent filters in the
081         * current filter stack, including the ultimately invoked servlet.
082         *
083         * @param request The servlet request we are processing
084         * @param result  The servlet response we are creating
085         * @param chain   The filter chain we are processing
086         * @throws IOException      if an input/output error occurs
087         * @throws ServletException if a servlet error occurs
088         */
089        public void doFilter(ServletRequest request, ServletResponse response,
090                             FilterChain chain)
091                throws IOException, ServletException {
092    
093            // Store ourselves as a request attribute (if requested)
094            if (attribute != null)
095                request.setAttribute(attribute, this);
096    
097            // Time and log the subsequent processing
098            long startTime = System.currentTimeMillis();
099            chain.doFilter(request, response);
100            long stopTime = System.currentTimeMillis();
101            filterConfig.getServletContext().log
102                    (this.toString() + ": " + (stopTime - startTime) +
103                            " milliseconds");
104    
105        }
106    
107    
108        /**
109         * Place this filter into service.
110         *
111         * @param filterConfig The filter configuration object
112         */
113        public void init(FilterConfig filterConfig) throws ServletException {
114    
115            this.filterConfig = filterConfig;
116            this.attribute = filterConfig.getInitParameter("attribute");
117    
118        }
119    
120    
121        /**
122         * Return a String representation of this object.
123         */
124        public String toString() {
125    
126            if (filterConfig == null)
127                return ("InvokerFilter()");
128            StringBuffer sb = new StringBuffer("InvokerFilter(");
129            sb.append(filterConfig);
130            sb.append(")");
131            return (sb.toString());
132    
133        }
134    
135    
136    }
137