Coverage details for org.chwf.servlet.filter.FilterChainImpl

LineHitsSource
1 /*
2 Chrysalis Web Framework [http://chrysalis.sourceforge.net]
3 Copyright (c) 2002, 2003, 2004, Paul Strack
4  
5 All rights reserved.
6  
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9  
10 1. Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12  
13 2. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16  
17 3. Neither the name of the copyright holder nor the names of its contributors
18 may be used to endorse or promote products derived from this software without
19 specific prior written permission.
20  
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
25 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
28 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32  
33 package org.chwf.servlet.filter;
34  
35 import java.io.IOException;
36  
37 import javax.servlet.Filter;
38 import javax.servlet.FilterChain;
39 import javax.servlet.ServletException;
40 import javax.servlet.ServletRequest;
41 import javax.servlet.ServletResponse;
42  
43 /**
44  * <p>Implementation of {@link javax.servlet.FilterChain}.</p>
45  *
46  * <p>This class duplicates the filter functionality of the Servlet 2.3 API.</p>
47  *
48  * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a>
49  */
501public class FilterChainImpl implements FilterChain {
51  
52   /** Attribute key for storing filter iterator in request. */
5315  private final String key =
54     FilterIterator.class.getName() + ":" + this.toString();
55  
56   /** The filters in the chain. */
57   private final Filter[] filters;
58  
59   /**
60    * Constructor.
61    *
62    * @param filters The filters in the chain.
63    */
6415  public FilterChainImpl(Filter[] filters) {
6515    this.filters = filters;
6615  }
67  
68   /**
69    * Execute the chain. When the chain is complete, the terminator filter is
70    * invoked. This terminator filter should invoke the resource at the end
71    * of the chain.
72    *
73    * @param request The request to pass along the chain.
74    * @param response The response to pass along the chain.
75    * @param terminator A filter that invokes the final resource invoked once
76    * the chain is complete.
77    * @throws IOException For I/O errors.
78    * @throws ServletException For servlet errors.
79    */
80   public void doChain(
81     ServletRequest request,
82     ServletResponse response,
83     Filter terminator)
84     throws IOException, ServletException {
8515    FilterIterator iterator = new FilterIterator(this.filters, terminator);
8615    request.setAttribute(key, iterator);
8715    iterator.nextFilter().doFilter(request, response, this);
8814  }
89  
90   /**
91    * Causes the next filter in the chain to be invoked, or if the calling
92    * filter is the last filter in the chain, causes the resource at
93    * the end of the chain to be invoked.
94    *
95    * @param request The request to pass along the chain.
96    * @param response The response to pass along the chain.
97    * @throws IOException For IO errors.
98    * @throws ServletException For servlet errors.
99    */
100   public void doFilter(ServletRequest request, ServletResponse response)
101     throws IOException, ServletException {
10230    getIterator(request).nextFilter().doFilter(request, response, this);
10328  }
104  
105   /**
106    * Get the filter iterator from the request.
107    *
108    * @param request The request.
109    * @return The filter iterator.
110    */
111   private FilterIterator getIterator(ServletRequest request) {
11230    return (FilterIterator) request.getAttribute(key);
113   }
114  
115   /**
116    * Iterator for stepping through the filter chain.
117    *
118    * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a>
119    */
120   private static class FilterIterator {
121     
122     /** The filters. */
123     private final Filter[] filters;
124     
125     /** The terminator filter. */
126     private final Filter terminator;
127     
128     /** The counter. */
129     private int count;
130  
131     /**
132      * Constructor.
133      *
134      * @param filters The filters in the chain.
135      * @param terminator The terminator filter.
136      */
137     FilterIterator(Filter[] filters, Filter terminator) {
138       this.filters = filters;
139       this.terminator = terminator;
140     }
141  
142     /**
143      * The next filter, or the terminator filter if all filters in the chain
144      * have been executed.
145      *
146      * @return The next filter or the terminator filter.
147      */
148     Filter nextFilter() {
149       if (count < filters.length) {
150         return this.filters[count++];
151       } else {
152         return this.terminator;
153       }
154     }
155   }
156 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.