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    
018    package filters;
019    
020    
021    import java.io.IOException;
022    import javax.servlet.Filter;
023    import javax.servlet.FilterChain;
024    import javax.servlet.FilterConfig;
025    import javax.servlet.ServletContext;
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: 267129 $ $Date: 2004-03-18 08:40:35 -0800 (Thu, 18 Mar 2004) $
046     */
047    
048    public final class ExampleFilter implements Filter {
049    
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    
067        // --------------------------------------------------------- Public Methods
068    
069    
070        /**
071         * Take this filter out of service.
072         */
073        public void destroy() {
074    
075            this.attribute = null;
076            this.filterConfig = null;
077    
078        }
079    
080    
081        /**
082         * Time the processing that is performed by all subsequent filters in the
083         * current filter stack, including the ultimately invoked servlet.
084         *
085         * @param request The servlet request we are processing
086         * @param result The servlet response we are creating
087         * @param chain The filter chain we are processing
088         *
089         * @exception IOException if an input/output error occurs
090         * @exception ServletException if a servlet error occurs
091         */
092        public void doFilter(ServletRequest request, ServletResponse response,
093                             FilterChain chain)
094            throws IOException, ServletException {
095    
096            // Store ourselves as a request attribute (if requested)
097            if (attribute != null)
098                request.setAttribute(attribute, this);
099    
100            // Time and log the subsequent processing
101            long startTime = System.currentTimeMillis();
102            chain.doFilter(request, response);
103            long stopTime = System.currentTimeMillis();
104            filterConfig.getServletContext().log
105                (this.toString() + ": " + (stopTime - startTime) +
106                 " milliseconds");
107    
108        }
109    
110    
111        /**
112         * Place this filter into service.
113         *
114         * @param filterConfig The filter configuration object
115         */
116        public void init(FilterConfig filterConfig) throws ServletException {
117    
118            this.filterConfig = filterConfig;
119            this.attribute = filterConfig.getInitParameter("attribute");
120    
121        }
122    
123    
124        /**
125         * Return a String representation of this object.
126         */
127        public String toString() {
128    
129            if (filterConfig == null)
130                return ("InvokerFilter()");
131            StringBuffer sb = new StringBuffer("InvokerFilter(");
132            sb.append(filterConfig);
133            sb.append(")");
134            return (sb.toString());
135    
136        }
137    
138    
139    }
140