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    
019    package filters;
020    
021    
022    import java.io.IOException;
023    
024    import javax.servlet.Filter;
025    import javax.servlet.FilterChain;
026    import javax.servlet.FilterConfig;
027    import javax.servlet.ServletException;
028    import javax.servlet.ServletRequest;
029    import javax.servlet.ServletResponse;
030    
031    
032    /**
033     * Example filter that can be attached to either an individual servlet
034     * or to a URL pattern.  This filter performs the following functions:
035     * <ul>
036     * <li>Attaches itself as a request attribute, under the attribute name
037     * defined by the value of the <code>attribute</code> initialization
038     * parameter.</li>
039     * <li>Calculates the number of milliseconds required to perform the
040     * servlet processing required by this request, including any
041     * subsequently defined filters, and logs the result to the servlet
042     * context log for this application.
043     * </ul>
044     *
045     * @author Craig McClanahan
046     * @version $Revision: 664175 $ $Date: 2008-06-06 18:43:44 -0400 (Fri, 06 Jun 2008) $
047     */
048    
049    public final class ExampleFilter implements Filter {
050    
051        // ----------------------------------------------------- Instance Variables
052    
053    
054        /**
055         * The request attribute name under which we store a reference to ourself.
056         */
057        private String attribute = null;
058    
059    
060        /**
061         * The filter configuration object we are associated with.  If this value
062         * is null, this filter instance is not currently configured.
063         */
064        private FilterConfig filterConfig = null;
065    
066        // --------------------------------------------------------- Public Methods
067    
068    
069        /**
070         * Take this filter out of service.
071         */
072        public void destroy() {
073    
074            this.attribute = null;
075            this.filterConfig = null;
076    
077        }
078    
079    
080        /**
081         * Time the processing that is performed by all subsequent filters in the
082         * current filter stack, including the ultimately invoked servlet.
083         *
084         * @param request The servlet request we are processing
085         * @param result  The servlet response we are creating
086         * @param chain   The filter chain we are processing
087         * @throws IOException      if an input/output error occurs
088         * @throws ServletException if a servlet error occurs
089         */
090        public void doFilter(ServletRequest request, ServletResponse response,
091                             FilterChain chain)
092                throws IOException, ServletException {
093    
094            // Store ourselves as a request attribute (if requested)
095            if (attribute != null)
096                request.setAttribute(attribute, this);
097    
098            // Time and log the subsequent processing
099            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