KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > MVCRequest


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.mvc;
8
9
10 import java.util.ArrayList JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Map JavaDoc;
14
15 import javax.servlet.http.HttpServletRequest JavaDoc;
16 import javax.servlet.http.HttpServletResponse JavaDoc;
17
18 import com.inversoft.verge.mvc.controller.Action;
19 import com.inversoft.verge.mvc.controller.ControllerMVCInfo;
20 import com.inversoft.verge.mvc.controller.ControllerParser;
21 import com.inversoft.verge.mvc.model.ModelParser;
22 import com.inversoft.verge.mvc.validator.Validator;
23 import com.inversoft.verge.mvc.validator.ValidatorParser;
24 import com.inversoft.verge.util.RequestContext;
25
26
27 /**
28  * <p>
29  * This is a simple struct that stores information about a
30  * request into the MVC Framework. This is used by the {@link
31  * DefaultMVCMediator DefaultMVCMediator} during its mediation
32  * as well as by the parsers, if they need it.
33  * </p>
34  *
35  * @author Brian Pontarelli
36  * @since 2.0
37  * @version 2.0
38  */

39 public class MVCRequest {
40
41     private ControllerParser controllerParser;
42     private ModelParser modelParser;
43     private ValidatorParser validatorParser;
44     
45     private RequestContext requestContext;
46     private HttpServletRequest JavaDoc request;
47     private HttpServletResponse JavaDoc response;
48     private Object JavaDoc configuration;
49
50     private ControllerMVCInfo controllerInfo;
51     private Action action;
52
53     private boolean modelEnabled;
54     private Map JavaDoc modelObjects;
55
56     private boolean validationEnabled;
57     private List JavaDoc validators;
58     private List JavaDoc validatorHandlersToCall;
59
60
61     /**
62      * Constructs a new <code>MVCRequest</code> and sets the model and validation
63      * enabled flags to true.
64      *
65      * @param request The HttpServletRequest
66      * @param response The HttpServletResponse
67      */

68     public MVCRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
69         assert (request != null) : "request == null";
70         assert (response != null) : "response == null";
71         this.request = request;
72         this.response = response;
73
74         requestContext = new RequestContext(request);
75         modelEnabled = true;
76         validationEnabled = true;
77
78         validators = new ArrayList JavaDoc();
79         modelObjects = new HashMap JavaDoc();
80         validatorHandlersToCall = new ArrayList JavaDoc();
81     }
82
83
84     /**
85      * Gets the ControllerParser for this request.
86      *
87      * @return The Controller parser for this request
88      */

89     public ControllerParser getControllerParser() {
90         return controllerParser;
91     }
92     
93     /**
94      * Sets the ControllerParser for this request.
95      *
96      * @param controllerParser The Controller parser for this request
97      */

98     public void setControllerParser(ControllerParser controllerParser) {
99         this.controllerParser = controllerParser;
100     }
101     
102     /**
103      * Gets the ModelParser for this request.
104      *
105      * @return The ModelParser for this request
106      */

107     public ModelParser getModelParser() {
108         return modelParser;
109     }
110     
111     /**
112      * Sets the ModelParser for this request.
113      *
114      * @param modelParser The ModelParser for this request
115      */

116     public void setModelParser(ModelParser modelParser) {
117         this.modelParser = modelParser;
118     }
119     
120     /**
121      * Gets the ValidatorParser for this request.
122      *
123      * @return The ValidatorParser for this request
124      */

125     public ValidatorParser getValidatorParser() {
126         return validatorParser;
127     }
128     
129     /**
130      * Sets the ValidatorParser for this request.
131      *
132      * @param validatorParser The ValidatorParser for this request
133      */

134     public void setValidatorParser(ValidatorParser validatorParser) {
135         this.validatorParser = validatorParser;
136     }
137     
138     /**
139      * Gets the HttpServletRequest for this request
140      *
141      * @return The HttpServletRequest and never null
142      */

143     public HttpServletRequest JavaDoc getRequest() {
144         return request;
145     }
146
147     /**
148      * Gets the HttpServletResponse for this request
149      *
150      * @return The HttpServletResponse and never null
151      */

152     public HttpServletResponse JavaDoc getResponse() {
153         return response;
154     }
155
156     /**
157      * Gets the RequestContext instance for this request. This will never be null
158      * and should not be re-created to save processing time.
159      *
160      * @return The RequestContext for this request
161      */

162     public RequestContext getRequestContext() {
163         return requestContext;
164     }
165
166     /**
167      * Gets the RequestContext instance for this request. This will never be null
168      * and should not be re-created to save processing time.
169      *
170      * @param requestContext The RequestContext for this request
171      */

172     public void setRequestContext(RequestContext requestContext) {
173         assert (requestContext != null) : "requestContext == null";
174         this.requestContext = requestContext;
175     }
176
177     /**
178      * Gets the configuration for the currently executing Controller implementaiton.
179      * This could be any object or an array of configuration objects. This could
180      * also be null.
181      *
182      * @return The configuration for this request, if available
183      */

184     public Object JavaDoc getConfiguration() {
185         return configuration;
186     }
187
188     /**
189      * Sets the configuration for the currently executing Controller implementaiton.
190      * This could be any object or an array of configuration objects. This could
191      * also be null.
192      *
193      * @param configuration (Optional) The configuration for this request, or
194      * null
195      */

196     public void setConfiguration(Object JavaDoc configuration) {
197         this.configuration = configuration;
198     }
199
200     /**
201      * Gets the Controller information for this request. This can be null if
202      * there is no controller information in the HttpServletRequest or the
203      * Controller parser does not support this object.
204      *
205      * @return The information or null
206      */

207     public ControllerMVCInfo getControllerInfo() {
208         return controllerInfo;
209     }
210
211     /**
212      * Sets the Controller information for this request. This can be null if
213      * there is no controller information in the HttpServletRequest or the
214      * Controller parser does not support this object.
215      *
216      * @param controllerInfo (Optional) The information or null
217      */

218     public void setControllerInfo(ControllerMVCInfo controllerInfo) {
219         this.controllerInfo = controllerInfo;
220     }
221
222     /**
223      * Gets the Controller action for this request. This can be null if there is
224      * no action or the Controller parser does not support actions.
225      *
226      * @return The action or null
227      */

228     public Action getAction() {
229         return action;
230     }
231
232     /**
233      * Sets the Controller action for this request. This can be null if there is
234      * no action or the Controller parser does not support actions.
235      *
236      * @param action (Optional) The action or null
237      */

238     public void setAction(Action action) {
239         this.action = action;
240     }
241
242     /**
243      * Sets the model object under the given key
244      *
245      * @param key The key
246      * @param model The model object
247      */

248     public void putModelObject(String JavaDoc key, Object JavaDoc model) {
249         modelObjects.put(key, model);
250     }
251
252     /**
253      * Returns the map of model objects for this request
254      *
255      * @return The map of model objects
256      */

257     public Map JavaDoc getModelObjects() {
258         return modelObjects;
259     }
260     /**
261      * Gets whether or not the model is enabled for this request
262      *
263      * @return True if the model is enabled, false otherwise
264      */

265     public boolean isModelEnabled() {
266         return modelEnabled;
267     }
268
269     /**
270      * Sets whether or not the model is enabled for this request
271      *
272      * @param modelEnabled True if the model is enabled, false otherwise
273      */

274     public void setModelEnabled(boolean modelEnabled) {
275         this.modelEnabled = modelEnabled;
276     }
277
278     /**
279      * Gets whether or not the validation is enabled for this request
280      *
281      * @return True if the validation is enabled, false otherwise
282      */

283     public boolean isValidationEnabled() {
284         return validationEnabled;
285     }
286
287     /**
288      * Sets whether or not the model is enabled for this request
289      *
290      * @param validationEnabled True if the validation is enabled, false otherwise
291      */

292     public void setValidationEnabled(boolean validationEnabled) {
293         this.validationEnabled = validationEnabled;
294     }
295
296     /**
297      * Gets the Validators used for this request. This could be zero or more and
298      * could also be null
299      *
300      * @return The Validators for this request in an array
301      */

302     public List JavaDoc getValidators() {
303         return validators;
304     }
305
306     /**
307      * Adds a list of Validators used for this request. This could be zero or
308      * more and could also be null
309      *
310      * @param validators (Optional) The Validators for this request in an array
311      * or null
312      */

313     public void addValidators(Validator[] validators) {
314         if (validators != null && validators.length > 0) {
315             for (int i = 0; i < validators.length; i++) {
316                 if (validators[i] != null) {
317                     this.validators.add(validators[i]);
318                 }
319             }
320         }
321     }
322
323     /**
324      * Adds a single Validators used for this request. This could be zero or more and
325      * could also be null
326      *
327      * @param validator Another Validator for this request
328      */

329     public void addValidator(Validator validator) {
330         if (validator != null) {
331             this.validators.add(validator);
332         }
333     }
334
335     /**
336      * Empties out the list of validators
337      */

338     public void clearValidators() {
339         validators.clear();
340     }
341
342     /**
343      * Returns true if this request has Validators
344      *
345      * @return True if this request has Validators
346      */

347     public boolean hasValidators() {
348         return (validators.size() > 0);
349     }
350
351     /**
352      * Adds the name of a validator handler that needs to be called when any
353      * validation is happening. These sub systems are instances of the {@link
354      * com.inversoft.verge.mvc.validator.ValidatorHandler ValidatorHandler} interface
355      * and must be registered with the MVCRegistry under the same name as the
356      * handler parameter given.
357      *
358      * @param handler The name of the handler to be called
359      */

360     public void addValidatorHandlerToCall(String JavaDoc handler) {
361         if (MVCRegistry.lookupValidatorHandler(handler) == null) {
362             throw new IllegalArgumentException JavaDoc("ValidatorHandler named: " +
363                 handler + " not registred");
364         }
365
366         validatorHandlersToCall.add(handler);
367     }
368
369     /**
370      * Returns the active list of validator subsystems to call.
371      *
372      * @return The List of names of handlers to call
373      */

374     public List JavaDoc getValidatorHandlersToCall() {
375         return validatorHandlersToCall;
376     }
377 }
Popular Tags