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.engine; | |
34 | ||
35 | import java.io.IOException; | |
36 | ||
37 | import javax.servlet.FilterChain; | |
38 | import javax.servlet.ServletException; | |
39 | import javax.servlet.ServletRequest; | |
40 | import javax.servlet.ServletResponse; | |
41 | ||
42 | import org.chwf.servlet.ControllerException; | |
43 | import org.chwf.servlet.InvocationContext; | |
44 | import org.chwf.servlet.filter.FilterSupport; | |
45 | ||
46 | /** | |
47 | * A filter that invokes a controller, and stores the InvocationContext in | |
48 | * the request for post-processing. | |
49 | * | |
50 | * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a> | |
51 | */ | |
52 | 12 | public class InvokerFilter extends FilterSupport { |
53 | ||
54 | /** Attribute name for storing the invocation context in the request. */ | |
55 | 1 | private static final String INVOCATION_CONTEXT = |
56 | InvokerFilter.class.getName() + ".INVOCATION_CONTEXT"; | |
57 | ||
58 | /** | |
59 | * Retrieve the invocation context from the request. This method is only | |
60 | * valid after the controller has been invoked. In particular, this method | |
61 | * can only be used during post-processing in other filters: | |
62 | * | |
63 | * <pre> | |
64 | * public void doFilter(...) { | |
65 | * // Should not call getContext() for pre-processing. | |
66 | * chain.doFilter(request, response); | |
67 | * // Valid to call getContext() for post-processing. | |
68 | * InvocationContext context = InvokerFilter.getContext(request); | |
69 | * // Work with context ... | |
70 | * }</pre> | |
71 | * | |
72 | * @param request The request. | |
73 | * @return The context, or <code>null</code> if it has not been initialized. | |
74 | */ | |
75 | public static InvocationContext getContext(ServletRequest request) { | |
76 | 6 | return (InvocationContext) request.getAttribute(INVOCATION_CONTEXT); |
77 | } | |
78 | ||
79 | /** | |
80 | * Cache the invocation context in the request. | |
81 | * | |
82 | * @param context The context. | |
83 | * @param request The request. | |
84 | */ | |
85 | private static void cacheContext( | |
86 | InvocationContext context, | |
87 | ServletRequest request) { | |
88 | 6 | request.setAttribute(INVOCATION_CONTEXT, context); |
89 | 6 | } |
90 | ||
91 | /** | |
92 | * Process filter. | |
93 | * | |
94 | * @param request The request. | |
95 | * @param response The response. | |
96 | * @param chain The filter chain. | |
97 | * @throws ServletException For servlet exceptions. | |
98 | * @throws IOException For IO exceptions. | |
99 | */ | |
100 | public void doFilter( | |
101 | ServletRequest request, | |
102 | ServletResponse response, | |
103 | FilterChain chain) | |
104 | throws IOException, ServletException { | |
105 | ||
106 | try { | |
107 | 7 | cacheContext(ControllerMapper.invoke(request), request); |
108 | 6 | } catch (ControllerException ex) { |
109 | 1 | throw new ServletException(ex); |
110 | } | |
111 | 6 | } |
112 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |