001    /*
002    * Copyright 2004 The Apache Software Foundation
003    *
004    * Licensed under the Apache License, Version 2.0 (the "License");
005    * you may not use this file except in compliance with the License.
006    * You may obtain a copy of the License at
007    *
008    *     http://www.apache.org/licenses/LICENSE-2.0
009    *
010    * Unless required by applicable law or agreed to in writing, software
011    * distributed under the License is distributed on an "AS IS" BASIS,
012    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013    * See the License for the specific language governing permissions and
014    * limitations under the License.
015    */
016    
017    package filters;
018    
019    
020    import java.io.IOException;
021    import javax.servlet.Filter;
022    import javax.servlet.FilterChain;
023    import javax.servlet.FilterConfig;
024    import javax.servlet.ServletContext;
025    import javax.servlet.ServletException;
026    import javax.servlet.ServletRequest;
027    import javax.servlet.ServletResponse;
028    
029    
030    /**
031     * Example filter that can be attached to either an individual servlet
032     * or to a URL pattern.  This filter performs the following functions:
033     * <ul>
034     * <li>Attaches itself as a request attribute, under the attribute name
035     *     defined by the value of the <code>attribute</code> initialization
036     *     parameter.</li>
037     * <li>Calculates the number of milliseconds required to perform the
038     *     servlet processing required by this request, including any
039     *     subsequently defined filters, and logs the result to the servlet
040     *     context log for this application.
041     * </ul>
042     *
043     * @author Craig McClanahan
044     * @version $Revision: 267129 $ $Date: 2004-03-18 08:40:35 -0800 (Thu, 18 Mar 2004) $
045     */
046    
047    public final class ExampleFilter implements Filter {
048    
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    
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         *
088         * @exception IOException if an input/output error occurs
089         * @exception ServletException if a servlet error occurs
090         */
091        public void doFilter(ServletRequest request, ServletResponse response,
092                             FilterChain chain)
093            throws IOException, ServletException {
094    
095            // Store ourselves as a request attribute (if requested)
096            if (attribute != null)
097                request.setAttribute(attribute, this);
098    
099            // Time and log the subsequent processing
100            long startTime = System.currentTimeMillis();
101            chain.doFilter(request, response);
102            long stopTime = System.currentTimeMillis();
103            filterConfig.getServletContext().log
104                (this.toString() + ": " + (stopTime - startTime) +
105                 " milliseconds");
106    
107        }
108    
109    
110        /**
111         * Place this filter into service.
112         *
113         * @param filterConfig The filter configuration object
114         */
115        public void init(FilterConfig filterConfig) throws ServletException {
116    
117            this.filterConfig = filterConfig;
118            this.attribute = filterConfig.getInitParameter("attribute");
119    
120        }
121    
122    
123        /**
124         * Return a String representation of this object.
125         */
126        public String toString() {
127    
128            if (filterConfig == null)
129                return ("InvokerFilter()");
130            StringBuffer sb = new StringBuffer("InvokerFilter(");
131            sb.append(filterConfig);
132            sb.append(")");
133            return (sb.toString());
134    
135        }
136    
137    
138    }
139