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; | |
34 | ||
35 | import java.util.HashMap; | |
36 | import java.util.Iterator; | |
37 | import java.util.Map; | |
38 | ||
39 | import org.chwf.converter.Converter; | |
40 | ||
41 | ||
42 | /** | |
43 | * Invocation context that allows the controller to specify view urls and | |
44 | * perform error handling. A new context is created for each invocation of | |
45 | * a controller. As a rule, developers should not directly manipulate the | |
46 | * <code>InvocationContext</code>; its methods are public only to facilitate | |
47 | * out-of-container testing for view redirection. | |
48 | * | |
49 | * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a> | |
50 | */ | |
51 | 134 | public class InvocationContext { |
52 | ||
53 | /** Parameters for view redirection. */ | |
54 | 67 | private final Map parameters = new HashMap(); |
55 | ||
56 | /** Result of invocation. */ | |
57 | private Object result; | |
58 | ||
59 | /** The redirect URL. */ | |
60 | private String viewURL; | |
61 | ||
62 | /** The error page. */ | |
63 | private String errorPage; | |
64 | ||
65 | /** The error. */ | |
66 | private Throwable error; | |
67 | ||
68 | /** | |
69 | * Set the view for the controller. When the invocation is complete, | |
70 | * the system will redirect to this view. The view URL should be a | |
71 | * "context-relative" URL. That is, if the URL begins with a "/", it should | |
72 | * not include the application's context path. The URL will be resolved | |
73 | * relative to the web application root directory. Controllers may call | |
74 | * this method if they must manually control view redirection.<p> | |
75 | * | |
76 | * @param url The URL to redirect to after processing is complete. | |
77 | */ | |
78 | public void setView(String url) { | |
79 | 10 | this.viewURL = url; |
80 | 10 | } |
81 | ||
82 | /** | |
83 | * Set the view and a single parameter value for the controller. This | |
84 | * simplifies the common situation when the controller needs to redirect to | |
85 | * a JSP that requires a single parameter:<p> | |
86 | * | |
87 | * <pre>setView("/showItem.jsp", "itemId", item.getItemId());</pre> | |
88 | * | |
89 | * This results in the redirection URL:<p> | |
90 | * | |
91 | * <pre>/{<i>context-path</i>}/showProject.jsp?itemId={<i>itemId</i>}</pre> | |
92 | * | |
93 | * @param url The URL to redirect to after processing is complete. | |
94 | * @param parameter The parameter name. | |
95 | * @param value The parameter value. This will be converted to a string. | |
96 | */ | |
97 | public void setView(String url, String parameter, Object value) { | |
98 | 1 | setView(url); |
99 | 1 | addViewParameter(parameter, value); |
100 | 1 | } |
101 | ||
102 | /** | |
103 | * Sets a parameter for the view redirection. This parameter will be | |
104 | * appended to the redirection URL. The parameter value will be converted | |
105 | * to a String, and both the name and value will be URL encoded before | |
106 | * redirection. This method may be called multiple times to set multiple | |
107 | * values:<p> | |
108 | * | |
109 | * <pre> | |
110 | * addViewParameter("id", productId); | |
111 | * addViewParameter("showSpecials", "true");</pre> | |
112 | * | |
113 | * @param parameter The parameter name. | |
114 | * @param value The parameter value. This will be converted to a string. | |
115 | */ | |
116 | public void addViewParameter(String parameter, Object value) { | |
117 | 5 | parameters.put(parameter, String.valueOf(value)); |
118 | 5 | } |
119 | ||
120 | /** | |
121 | * Specifies that the system redirect to the default error page and pass | |
122 | * it the specified error. Any specified view will be ignored.<p> | |
123 | * | |
124 | * @param error The error. | |
125 | */ | |
126 | public void handleError(Throwable error) { | |
127 | 23 | this.error = error; |
128 | 23 | } |
129 | ||
130 | /** | |
131 | * Specifies that the system redirect to the error page and pass it the | |
132 | * specified error. Any specified view will be ignored. This method lets | |
133 | * the controller programmatically control error flow, allowing the | |
134 | * controller to use different error pages for different kinds of errors.<p> | |
135 | * | |
136 | * @param errorPage The error page. | |
137 | * @param error The error. | |
138 | */ | |
139 | public void handleError(String errorPage, Throwable error) { | |
140 | ||
141 | 7 | this.errorPage = errorPage; |
142 | 7 | handleError(error); |
143 | 7 | } |
144 | ||
145 | /** | |
146 | * The invoked method's result.<p> | |
147 | * | |
148 | * @return The invoked method's result. | |
149 | */ | |
150 | public Object getResult() { | |
151 | 6 | return this.result; |
152 | } | |
153 | ||
154 | /** | |
155 | * Set the invoked method's result.<p> | |
156 | * | |
157 | * @param result The invoked method's result. | |
158 | */ | |
159 | public void setResult(Object result) { | |
160 | 28 | this.result = result; |
161 | 28 | } |
162 | ||
163 | /** | |
164 | * The view, a URL without the context path.<p> | |
165 | * | |
166 | * @return The view URL. | |
167 | */ | |
168 | public String getView() { | |
169 | 13 | return this.viewURL; |
170 | } | |
171 | ||
172 | /** | |
173 | * The error page, a URL without the context path.<p> | |
174 | * | |
175 | * @return The error page. | |
176 | */ | |
177 | public String getErrorPage() { | |
178 | 7 | return this.errorPage; |
179 | } | |
180 | ||
181 | /** | |
182 | * The exception thrown by the invoked method or <code>null</code> if | |
183 | * the method was successful. It is forwarded to the error page.<p> | |
184 | * | |
185 | * @return The error, or null if there is none. | |
186 | */ | |
187 | public Throwable getError() { | |
188 | 13 | return this.error; |
189 | } | |
190 | ||
191 | /** | |
192 | * True if the invoked method threw an exception.<p> | |
193 | * | |
194 | * @return True if the invoked method threw an exception. | |
195 | */ | |
196 | public boolean hasError() { | |
197 | 57 | return (this.error != null); |
198 | } | |
199 | ||
200 | /** | |
201 | * The redirection URL with parameter values appended.<p> | |
202 | * | |
203 | * @return The redirection URL with parameter values appended. | |
204 | */ | |
205 | public String getRedirectURL() { | |
206 | 9 | String baseURL = getView(); |
207 | 9 | StringBuffer buffer = new StringBuffer(baseURL); |
208 | ||
209 | 9 | Iterator i = parameters.keySet().iterator(); |
210 | 9 | if (i.hasNext()) { |
211 | 4 | buffer.append("?"); |
212 | 4 | encodeNext(buffer, i); |
213 | } | |
214 | 10 | while (i.hasNext()) { |
215 | 1 | buffer.append("&"); |
216 | 1 | encodeNext(buffer, i); |
217 | } | |
218 | 9 | return buffer.toString(); |
219 | } | |
220 | ||
221 | /** | |
222 | * Specify the default view.<p> | |
223 | * | |
224 | * @param url The default view. | |
225 | */ | |
226 | public void setDefaultView(String url) { | |
227 | 42 | this.viewURL = url; |
228 | 42 | } |
229 | ||
230 | /** | |
231 | * Specify the default error page.<p> | |
232 | * | |
233 | * @param url The default error page. | |
234 | */ | |
235 | public void setDefaultErrorPage(String url) { | |
236 | 42 | this.errorPage = url; |
237 | 42 | } |
238 | ||
239 | /** | |
240 | * Encode the next parameter value.<p> | |
241 | * | |
242 | * @param buffer The buffer. | |
243 | * @param i The iterator of parameter entries, pointed to the next value. | |
244 | */ | |
245 | private void encodeNext(StringBuffer buffer, Iterator i) { | |
246 | 5 | String key = i.next().toString(); |
247 | 5 | String value = Converter.convert(parameters.get(key)); |
248 | 5 | buffer.append(ServletUtils.encode(key)); |
249 | 5 | buffer.append("="); |
250 | 5 | buffer.append(ServletUtils.encode(value)); |
251 | 5 | } |
252 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |