| Line | Hits | Source |
|---|---|---|
| 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 | import java.util.Iterator; | |
| 37 | ||
| 38 | import javax.servlet.Filter; | |
| 39 | import javax.servlet.FilterChain; | |
| 40 | import javax.servlet.FilterConfig; | |
| 41 | import javax.servlet.ServletContext; | |
| 42 | import javax.servlet.ServletException; | |
| 43 | import javax.servlet.ServletRequest; | |
| 44 | import javax.servlet.ServletResponse; | |
| 45 | import javax.servlet.http.HttpServletRequest; | |
| 46 | ||
| 47 | import org.xml.sax.InputSource; | |
| 48 | ||
| 49 | /** | |
| 50 | * <p>Filter that composes a set of other filters, to simplify filter | |
| 51 | * configuration. When the composite filter is invoked, it invokes all | |
| 52 | * the composed filters in turn. When the composite filter chain is complete, | |
| 53 | * it continues with the original filter chain.</p> | |
| 54 | ||
| 55 | * <p>The configuration file for the composite filter is loaded from the | |
| 56 | * classpath. The file name is always <i>ClassName</i>-filter.xml. It | |
| 57 | * uses the same configuration as the FilterMicroContainer, but URL | |
| 58 | * patterns are ignored; all filters are always invoked.</p> | |
| 59 | * | |
| 60 | * | |
| 61 | * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a> | |
| 62 | */ | |
| 63 | 19 | public class CompositeFilter implements Filter { |
| 64 | ||
| 65 | /** The composite filter set. */ | |
| 66 | private FilterSet filterSet; | |
| 67 | ||
| 68 | /** | |
| 69 | * Initializes the composed filters, using the <i>ClassName</i>-filter.xml | |
| 70 | * configuration file. | |
| 71 | * | |
| 72 | * @param config The filter config. | |
| 73 | * @throws ServletException If composed filters cannot be initialized. | |
| 74 | */ | |
| 75 | public void init(FilterConfig config) throws ServletException { | |
| 76 | 19 | Class cls = this.getClass(); |
| 77 | 19 | String configFile = "/" + cls.getName().replace('.', '/') + "-filter.xml"; |
| 78 | 19 | InputSource xml = new InputSource(cls.getResourceAsStream(configFile)); |
| 79 | 19 | ServletContext context = config.getServletContext(); |
| 80 | 19 | AllFilterConfig allFilterConfig = new AllFilterConfig(context, xml); |
| 81 | 19 | this.filterSet = new CompositeFilterSet(allFilterConfig); |
| 82 | 19 | } |
| 83 | ||
| 84 | /** | |
| 85 | * Invokes all the composite filters in order, then continues with the | |
| 86 | * original chain. | |
| 87 | * | |
| 88 | * @param request The request. | |
| 89 | * @param response The response. | |
| 90 | * @param chain The original filter chain. | |
| 91 | * @throws ServletException For servlet errors. | |
| 92 | * @throws IOException For I/O errors. | |
| 93 | */ | |
| 94 | public void doFilter( | |
| 95 | ServletRequest request, | |
| 96 | ServletResponse response, | |
| 97 | FilterChain chain) | |
| 98 | throws IOException, ServletException { | |
| 99 | ||
| 100 | 9 | HttpServletRequest httpRequest = (HttpServletRequest) request; |
| 101 | 9 | Filter terminator = new OriginalChainInvoker(chain); |
| 102 | 9 | filterSet.doChain(httpRequest, response, terminator); |
| 103 | 8 | } |
| 104 | ||
| 105 | /** Destroys all composite filters. */ | |
| 106 | public void destroy() { | |
| 107 | 2 | this.filterSet.destroy(); |
| 108 | 2 | } |
| 109 | ||
| 110 | /** Terminator filter that invokes the original chain. */ | |
| 111 | private static class OriginalChainInvoker extends FilterSupport { | |
| 112 | ||
| 113 | /** The original filter chain. */ | |
| 114 | private final FilterChain originalChain; | |
| 115 | ||
| 116 | /** | |
| 117 | * Constructor. | |
| 118 | * | |
| 119 | * @param originalChain The original filter chain. | |
| 120 | */ | |
| 121 | public OriginalChainInvoker(FilterChain originalChain) { | |
| 122 | this.originalChain = originalChain; | |
| 123 | } | |
| 124 | ||
| 125 | /** | |
| 126 | * Invoke the original chain. | |
| 127 | * | |
| 128 | * @param request The request. | |
| 129 | * @param response The response. | |
| 130 | * @param chain The filter chain (ignored). | |
| 131 | * @throws ServletException For servlet errors. | |
| 132 | * @throws IOException For I/O errors. | |
| 133 | */ | |
| 134 | public void doFilter( | |
| 135 | ServletRequest request, | |
| 136 | ServletResponse response, | |
| 137 | FilterChain chain) | |
| 138 | throws IOException, ServletException { | |
| 139 | ||
| 140 | this.originalChain.doFilter(request, response); | |
| 141 | } | |
| 142 | } | |
| 143 | ||
| 144 | /** | |
| 145 | * Contains a set of filters. Optimized for composite filters. In particular, | |
| 146 | * ignores URI patterns, putting all filters into the chain. | |
| 147 | * | |
| 148 | * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a> | |
| 149 | */ | |
| 150 | private static class CompositeFilterSet extends FilterSet { | |
| 151 | ||
| 152 | /** All filters. */ | |
| 153 | private final Filter[] filters; | |
| 154 | ||
| 155 | /** | |
| 156 | * Constructor. | |
| 157 | * | |
| 158 | * @param allFilterConfig Filter configuration. | |
| 159 | * @throws ServletException If filters cannot be initialized. | |
| 160 | */ | |
| 161 | public CompositeFilterSet(AllFilterConfig allFilterConfig) | |
| 162 | throws ServletException { | |
| 163 | super(allFilterConfig); | |
| 164 | filters = new Filter[getFilterInfoMap().size()]; | |
| 165 | Iterator filterInfo = this.getFilterInfoMap().values().iterator(); | |
| 166 | for (int i = 0; i < filters.length; i++) { | |
| 167 | FilterInfo info = (FilterInfo) filterInfo.next(); | |
| 168 | filters[i] = info.getFilter(); | |
| 169 | } | |
| 170 | } | |
| 171 | ||
| 172 | /** | |
| 173 | * Run the chain for this request. When the chain is complete, the | |
| 174 | * terminator filter is invoked. This terminator filter should invoke the | |
| 175 | * resource at the end of the chain. | |
| 176 | * | |
| 177 | * @param request The request. | |
| 178 | * @param response The response. | |
| 179 | * @param terminator A filter that invokes the final resource invoked once | |
| 180 | * the chain is complete. | |
| 181 | * @throws ServletException For servlet errors. | |
| 182 | * @throws IOException For I/O errors. | |
| 183 | */ | |
| 184 | public void doChain( | |
| 185 | HttpServletRequest request, | |
| 186 | ServletResponse response, | |
| 187 | Filter terminator) | |
| 188 | throws IOException, ServletException { | |
| 189 | ||
| 190 | String uri = request.getRequestURI(); | |
| 191 | FilterChainImpl chain = new FilterChainImpl(this.filters); | |
| 192 | chain.doChain(request, response, terminator); | |
| 193 | } | |
| 194 | } | |
| 195 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |