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: import java.io.IOException;
020: import javax.servlet.Filter;
021: import javax.servlet.FilterChain;
022: import javax.servlet.FilterConfig;
023: import javax.servlet.ServletContext;
024: import javax.servlet.ServletException;
025: import javax.servlet.ServletRequest;
026: import javax.servlet.ServletResponse;
027:
028: /**
029: * Example filter that can be attached to either an individual servlet
030: * or to a URL pattern. This filter performs the following functions:
031: * <ul>
032: * <li>Attaches itself as a request attribute, under the attribute name
033: * defined by the value of the <code>attribute</code> initialization
034: * parameter.</li>
035: * <li>Calculates the number of milliseconds required to perform the
036: * servlet processing required by this request, including any
037: * subsequently defined filters, and logs the result to the servlet
038: * context log for this application.
039: * </ul>
040: *
041: * @author Craig McClanahan
042: * @version $Revision: 1.2 $ $Date: 2004/03/18 16:40:33 $
043: */
044:
045: public final class ExampleFilter implements Filter {
046:
047: // ----------------------------------------------------- Instance Variables
048:
049: /**
050: * The request attribute name under which we store a reference to ourself.
051: */
052: private String attribute = null;
053:
054: /**
055: * The filter configuration object we are associated with. If this value
056: * is null, this filter instance is not currently configured.
057: */
058: private FilterConfig filterConfig = null;
059:
060: // --------------------------------------------------------- Public Methods
061:
062: /**
063: * Take this filter out of service.
064: */
065: public void destroy() {
066:
067: this .attribute = null;
068: this .filterConfig = null;
069:
070: }
071:
072: /**
073: * Time the processing that is performed by all subsequent filters in the
074: * current filter stack, including the ultimately invoked servlet.
075: *
076: * @param request The servlet request we are processing
077: * @param result The servlet response we are creating
078: * @param chain The filter chain we are processing
079: *
080: * @exception IOException if an input/output error occurs
081: * @exception ServletException if a servlet error occurs
082: */
083: public void doFilter(ServletRequest request,
084: ServletResponse response, FilterChain chain)
085: throws IOException, ServletException {
086:
087: // Store ourselves as a request attribute (if requested)
088: if (attribute != null)
089: request.setAttribute(attribute, this );
090:
091: // Time and log the subsequent processing
092: long startTime = System.currentTimeMillis();
093: chain.doFilter(request, response);
094: long stopTime = System.currentTimeMillis();
095: filterConfig.getServletContext().log(
096: this .toString() + ": " + (stopTime - startTime)
097: + " milliseconds");
098:
099: }
100:
101: /**
102: * Place this filter into service.
103: *
104: * @param filterConfig The filter configuration object
105: */
106: public void init(FilterConfig filterConfig) throws ServletException {
107:
108: this .filterConfig = filterConfig;
109: this .attribute = filterConfig.getInitParameter("attribute");
110:
111: }
112:
113: /**
114: * Return a String representation of this object.
115: */
116: public String toString() {
117:
118: if (filterConfig == null)
119: return ("InvokerFilter()");
120: StringBuffer sb = new StringBuffer("InvokerFilter(");
121: sb.append(filterConfig);
122: sb.append(")");
123: return (sb.toString());
124:
125: }
126:
127: }
|