KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > controller > ServletControllerRequest


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64 package com.jcorporate.expresso.core.controller;
65
66 import com.jcorporate.expresso.core.controller.session.HTTPPersistentSession;
67 import com.jcorporate.expresso.core.i18n.Messages;
68 import com.jcorporate.expresso.core.jsdkapi.GenericSession;
69 import com.jcorporate.expresso.core.misc.CurrentLogin;
70 import com.jcorporate.expresso.core.misc.StringUtil;
71 import com.jcorporate.expresso.core.misc.URLUTF8Encoder;
72 import com.jcorporate.expresso.core.misc.upload.FileItem;
73 import com.jcorporate.expresso.core.security.User;
74 import org.apache.log4j.Logger;
75 import org.apache.struts.action.ActionForm;
76 import org.apache.struts.action.ActionMapping;
77 import org.apache.struts.upload.MultipartRequestHandler;
78
79 import javax.servlet.Servlet JavaDoc;
80 import javax.servlet.ServletException JavaDoc;
81 import javax.servlet.ServletRequest JavaDoc;
82 import javax.servlet.ServletResponse JavaDoc;
83 import javax.servlet.http.HttpServletRequest JavaDoc;
84 import javax.servlet.http.HttpServletResponse JavaDoc;
85 import javax.servlet.http.HttpSession JavaDoc;
86 import java.util.Enumeration JavaDoc;
87 import java.util.Hashtable JavaDoc;
88 import java.util.Locale JavaDoc;
89 import java.util.StringTokenizer JavaDoc;
90 import java.util.Vector JavaDoc;
91
92
93 /**
94  * ServletControllerRequest is an extension of ControllerRequest that also contains
95  * access to "special" servlet-related items, such as the ServletResponse object.
96  * It should only be used *when absolutely necessary*, as normally a standard ControllerParms
97  * object is better.
98  */

99 public class ServletControllerRequest
100         extends ControllerRequest
101         implements Cloneable JavaDoc {
102     private static Logger log = Logger.getLogger(ServletControllerRequest.class);
103
104     /**
105      * NOTE: The response object below should be used VERY RARELY. It is not good
106      * Controller design to simply send output to this stream for response data
107      * that could be represented as Outputs, Inputs, etc. It is here for those
108      * situations where a controller must take servlet-API specific actions,
109      * such as setting a custom mime type or sending a binary stream of data back to
110      * the client. Don't use it if you don't *need* it.
111      */

112     private ServletResponse JavaDoc myServletResponse = null;
113     private ServletRequest JavaDoc myServletRequest = null;
114     private Servlet JavaDoc myCallingServlet = null;
115     private ActionMapping myMapping = null;
116     private ActionForm myForm = null;
117
118
119     /**
120      * Default constructor
121      */

122     public ServletControllerRequest() {
123
124     } /* ServletControllerRequest() */
125
126     /**
127      * Sets the servlet response object
128      *
129      * @param newResponse the new servlet response
130      * @see #getServletResponse
131      */

132     public synchronized void setServletResponse(ServletResponse JavaDoc newResponse) {
133         myServletResponse = newResponse;
134     }
135
136     /**
137      * Sets the servlet request object
138      *
139      * @param newRequest the new servlet request
140      * @see #getServletRequest
141      */

142     public synchronized void setServletRequest(ServletRequest JavaDoc newRequest) {
143         myServletRequest = newRequest;
144     }
145
146     /**
147      * Sets the calling servlet. For example an ExpressoActionServlet
148      *
149      * @param newServlet the new calling servlet
150      * @see #getCallingServlet
151      */

152     public void setCallingServlet(Servlet JavaDoc newServlet) {
153         myCallingServlet = newServlet;
154     }
155
156     /**
157      * Gets the calling servlet. Used for creation, for example.
158      *
159      * @return javax.servlet.Servlet set previously in setCallingServlet()
160      * @see #getCallingServlet
161      */

162     public Servlet JavaDoc getCallingServlet() {
163         return myCallingServlet;
164     }
165
166     /**
167      * Sets the action mapping as a convenience for pure Struts writer
168      * <p> author Peter Pilgrim
169      *
170      * @param newMapping the ActionMapping associated with the controller request
171      * @see #getMapping
172      */

173     public synchronized void setMapping(ActionMapping newMapping) {
174         this.myMapping = newMapping;
175     }
176
177     /**
178      * Gets the action mapping as a convenience for pure Struts writer.
179      * The base <code>Controller</code> object stores the current
180      * action mapping instance in the <code>ControllerResponse</code>
181      * using this method. Writers who only ever intend there
182      * controller to be used in a web environment can access the
183      * action mapping to do more sophisticated processing.
184      * <p/>
185      * <pre>
186      * public AcmeController extends DBController {
187      * <p/>
188      * protected void runProcessOrderState(
189      * ControllerRequest req,
190      * ControllerRequest res )
191      * {
192      * ServletControllerRequest request =
193      * (ServletControllerRequest)req;
194      * ActionMapping mapping = request.getMapping();
195      * <p/>
196      * }
197      * ...
198      * }
199      * </pre>
200      * <p/>
201      * author Peter Pilgrim
202      *
203      * @return ActionMapping
204      * @see #setMapping
205      */

206     public synchronized ActionMapping getMapping() {
207         return myMapping;
208     }
209
210     /**
211      * Sets the action form as a convenience for pure Struts writer.
212      * <p/>
213      * author Peter Pilgrim Fri Jan 23 23:40:14 GMT 2004
214      *
215      * @param newForm the ActionForm associated with the controller request
216      * @see #getForm
217      */

218     public synchronized void setForm(ActionForm newForm) {
219         this.myForm = newForm;
220     }
221
222     /**
223      * Gets the action form as a convenience for pure Struts writer.
224      * The base <code>Controller</code> object stores the current
225      * action form instance in the <code>ControllerResponse</code>
226      * using this method. Writers who only ever intend there
227      * controller to be used in a web environment can access the
228      * action form to do more sophisticated processing.
229      * <p/>
230      * <pre>
231      * public AcmeController extends DBController {
232      * <p/>
233      * protected void runProcessOrderState(
234      * ControllerRequest req,
235      * ControllerRequest res )
236      * {
237      * ServletControllerRequest request =
238      * (ServletControllerRequest)req;
239      * ActionForm form = request.getForm();
240      * <p/>
241      * }
242      * ...
243      * }
244      * </pre>
245      * <p/>
246      * author Peter Pilgrim
247      *
248      * @return ActionForm
249      * @see #setForm
250      */

251     public synchronized ActionForm getForm() {
252         return myForm;
253     }
254
255     /**
256      * Gets the servlet request object
257      * <p/>
258      * <p/>
259      * Important: A Controller should only use this method if strictly necessary, and not
260      * write output that is otherwise able to be handled with inputs, outputs and transitions.
261      * If you do access the response object, you must also call setCustomOutput(true) in the
262      * ControllResponse object to let the Controller container (ControllerServlet, ControllRun, etc)
263      * know that it should not attempt to handle view processing itself.
264      * Don't use it if you don't *need* it.
265      * </p>
266      *
267      * @return ServletRequest object
268      * @see #getServletResponse
269      * @see #setServletRequest
270      */

271     public ServletRequest JavaDoc getServletRequest() {
272         return myServletRequest;
273     }
274
275     /**
276      * Gets the servlet response object
277      * <p/>
278      * <p/>
279      * Important: A Controller should only use this method if strictly necessary, and not
280      * write output that is otherwise able to be handled with inputs, outputs and transitions.
281      * If you do access the response object, you must also call setCustomOutput(true) in the
282      * ControllResponse object to let the Controller container (ControllerServlet, ControllRun, etc)
283      * know that it should not attempt to handle view processing itself.
284      * Don't use it if you don't *need* it.
285      * </p>
286      *
287      * @return ServletResponse object
288      * @see #getServletRequest
289      * @see #setServletResponse
290      */

291     public ServletResponse JavaDoc getServletResponse() {
292         return myServletResponse;
293     }
294
295     /**
296      * Gets the http servlet response object
297      * <p/>
298      * <p/>
299      * Important: A Controller should only use this method if strictly necessary, and not
300      * write output that is otherwise able to be handled with inputs, outputs and transitions.
301      * If you do access the response object, you must also call setCustomOutput(true) in the
302      * ControllResponse object to let the Controller container (ControllerServlet, ControllRun, etc)
303      * know that it should not attempt to handle view processing itself.
304      * Don't use it if you don't *need* it.
305      * </p>
306      *
307      * @return HttpServletRequest object
308      * @throws ClassCastException if the servlet request cannot be
309      * convert to <code>HttpServletResponse</code>
310      * @see #getHttpServletResponse
311      */

312     public HttpServletRequest JavaDoc getHttpServletRequest() {
313         return (HttpServletRequest JavaDoc) myServletRequest;
314     }
315
316
317     /**
318      * Gets the http servlet response object
319      * <p/>
320      * <p/>
321      * Important: A Controller should only use this method if strictly necessary, and not
322      * write output that is otherwise able to be handled with inputs, outputs and transitions.
323      * If you do access the response object, you must also call setCustomOutput(true) in the
324      * ControllResponse object to let the Controller container (ControllerServlet, ControllRun, etc)
325      * know that it should not attempt to handle view processing itself.
326      * Don't use it if you don't *need* it.
327      * </p>
328      *
329      * @return HttpServletResponse object
330      * @throws ClassCastException if the servlet request cannot be
331      * convert to <code>HttpServletResponse</code>
332      * @see #getHttpServletRequest
333      */

334     public HttpServletResponse JavaDoc getHttpServletResponse() {
335         return (HttpServletResponse JavaDoc) myServletResponse;
336     }
337
338     /**
339      * Return a Hashtable of parameter name/parameter value pairs. If the parameter
340      * is a file upload, then the parameter value is not a string, it's a Vector of
341      * two strings - the first is the value of the parameter as usual, the second
342      * is the file name that the file was uploaded into in the temporary directory.
343      *
344      * @param req the servlet request
345      * @param controller the controller being referenced
346      * @return Hashtable of keys Strings mapped to values (String/String)
347      * @throws ControllerException upon error
348      * @throws IllegalArgumentException if any parameters are null
349      */

350     private static Hashtable JavaDoc parseParamsReq(HttpServletRequest JavaDoc req,
351                                             Controller controller)
352             throws ControllerException {
353         if (controller == null) {
354             throw new IllegalArgumentException JavaDoc("ServletControllerRequest." +
355                     "parseParamsReq(): Parameter 'controller' must not be null");
356         }
357
358         String JavaDoc controllerName = controller.getClass().getName();
359         Hashtable JavaDoc params = new Hashtable JavaDoc();
360         params.put(Controller.CONTROLLER_PARAM_KEY, controllerName);
361
362         if (req == null) {
363             throw new IllegalArgumentException JavaDoc("Request may not be null here");
364         }
365
366         String JavaDoc oneName = null;
367         String JavaDoc oneValue = null;
368         log.debug("Regular query string Parameters:");
369
370         for (Enumeration JavaDoc e = req.getParameterNames(); e.hasMoreElements();) {
371             oneName = (String JavaDoc) e.nextElement();
372             oneValue = StringUtil.notNull(req.getParameter(oneName));
373             params.put(oneName, oneValue);
374
375             if (log.isDebugEnabled()) {
376                 log.debug("Parameter '" + oneName + "', value '" + oneValue +
377                         "'");
378             }
379         } /* for each parameter */
380
381
382         log.debug("End regular query string parameters");
383
384         Hashtable JavaDoc newParams = addButtonParams(params);
385
386         return newParams;
387 // return addDefaultParams(newParams, req, controller);
388
} /* parseParamsReq */
389
390
391     /**
392      * Add any parameters specified by the "button"
393      *
394      * @param params the hashtable of parameters to add
395      * @return Hashtable
396      */

397     private static Hashtable JavaDoc addButtonParams(Hashtable JavaDoc params)
398             throws ControllerException {
399         log.debug("Adding button parameters");
400
401         String JavaDoc oneParamName = null;
402         Object JavaDoc oneParamValue = null;
403         boolean gotControllerFromButton = false;
404         boolean gotStateFromButton = false;
405         Hashtable JavaDoc newParams = new Hashtable JavaDoc();
406
407         for (Enumeration JavaDoc e = params.keys(); e.hasMoreElements();) {
408             oneParamName = (String JavaDoc) e.nextElement();
409             oneParamValue = params.get(oneParamName);
410
411             // This param name/vale should be added to the list as is
412
boolean addThisParam = true;
413
414             // Handle special cases
415
if (oneParamName.equals(Controller.CONTROLLER_PARAM_KEY) && gotControllerFromButton) {
416
417                 // This is a controller name, but we already have one
418
// previously set from a button (xxx_params).
419
addThisParam = false;
420
421             } else if (oneParamName.equals(Controller.STATE_PARAM_KEY) && gotStateFromButton) {
422
423                 // This is a state name, but we already have one
424
// previously set from a button (xxx_params).
425
addThisParam = false;
426             } else if (oneParamName.endsWith("_params")) {
427
428                 //This is a button paramter-list
429
String JavaDoc buttonName = oneParamName.substring(0,
430                         oneParamName.indexOf("_params"));
431
432                 //Check to see if it is already URL-encoded
433
if (!"u".equals(params.get(buttonName + "_encoding"))) {
434
435                     //It's not encoded, so we do it here and add the encoded version
436
newParams.put(oneParamName,
437                             URLUTF8Encoder.encode((String JavaDoc) oneParamValue));
438
439                     // Now indicate that the just added parameter was URL-encoded
440
newParams.put(buttonName + "_encoding", "u");
441
442                     // We added it, no need to add it again....
443
addThisParam = false;
444                 }
445             }
446             // If we passed special-cases check above, then add the param name/value
447
if (addThisParam) {
448                 newParams.put(oneParamName, oneParamValue);
449             }
450             if (oneParamName.startsWith("button_")) {
451
452                 //We add a special parameter to the collection, which
453
//is simply "button"="name_ofButton"
454
String JavaDoc buttonName = oneParamName.substring(oneParamName.indexOf("_") + 1);
455
456                 /**
457                  * button names come in from the browser like
458
459                  button_promptLogin.y
460                  button_promptLogin.x
461
462                  cut off those .x, .y trailers
463                  */

464                 if (buttonName.endsWith(".x")) {
465                     buttonName = buttonName.substring(0, buttonName.length() - 2);
466                 }
467                 if (buttonName.endsWith(".y")) {
468                     buttonName = buttonName.substring(0, buttonName.length() - 2);
469                 }
470
471                 if (log.isDebugEnabled()) {
472                     log.debug("There is a button parameter called '" +
473                             buttonName + "'");
474                 }
475
476                 newParams.put("button", buttonName);
477
478                 /* pick up the corresponding parameter string, if any */
479                 String JavaDoc paramString = (String JavaDoc) params.get(buttonName +
480                         "_params");
481
482                 if (paramString != null) {
483                     log.debug("Button parameters:");
484
485                     //We first check to see if the button parameters string is encoded. If it is, we decode it
486
//before parsing it.
487
String JavaDoc encodeType = (String JavaDoc) params.get(buttonName +
488                             "_encoding");
489
490                     if ("u".equals(encodeType)) {
491
492                         //this param string has been URL encoded
493
paramString = URLUTF8Encoder.decode(paramString);
494                     } /* if the param string for the button was encoded */
495
496
497                     /* parse it into a hashtable */
498                     StringTokenizer JavaDoc stkpm = null;
499                     stkpm = new StringTokenizer JavaDoc(paramString, "&");
500
501                     while (stkpm.hasMoreTokens()) {
502                         String JavaDoc pairValue = stkpm.nextToken();
503
504                         //This decode is paired up with the getParamString() method's encode() in
505
//the Transition class.
506
pairValue = URLUTF8Encoder.decode(pairValue);
507
508                         String JavaDoc paramName = pairValue;
509                         String JavaDoc paramValue = "";
510                         int position = pairValue.indexOf("=");
511                         if (position != -1) {
512                             paramName = pairValue.substring(0, position);
513                             position++; //ignore the '='
514
if (position < pairValue.length()) {
515                                 paramValue = pairValue.substring(position);
516                             }
517                         }
518
519                         newParams.put(paramName, paramValue);
520
521                         if (paramName.equals(Controller.CONTROLLER_PARAM_KEY)) {
522                             gotControllerFromButton = true;
523                         } else if (paramName.equals(Controller.STATE_PARAM_KEY)) {
524                             gotStateFromButton = true;
525                         }
526
527                         if (log.isDebugEnabled()) {
528                             log.debug("Parameter '" + paramName + "', value '"
529                                     + paramValue + "'");
530                         }
531                     } /* while more parameters in the button param string */
532
533
534                     log.debug("End button parameters");
535                 } else { /* if the button param string exists */
536                     throw new ControllerException("Button '" + buttonName +
537                             "' was clicked, but no " +
538                             "button parameters field called '" +
539                             buttonName +
540                             "_params' was found.");
541                 }
542             } /* if this param is a buitton */
543
544         } /* for each of the parameters */
545
546
547         return newParams;
548     }
549
550
551     /**
552      * Static constructor for creating an servlet controller request
553      * with all the <em>acoutrements</em> of the Struts Action execution signature.
554      * <p/>
555      * <p/>
556      * I deliberate add the <code>ActionMapping</code>, <code>ActionForm beans</code>
557      * as conveniences for Struts developers. You can probably sense I am cheesed off like
558      * Simon Cowell int American (Pop) Idol
559      * <p/>
560      * <p/>
561      * Docu byte Peter Pilgrim Fri Jan 23 23:20:41 GMT 2004
562      *
563      * @param mapping The ActionMapping used to select this instance
564      * @param form The optional ActionForm bean for this request (if any)
565      * @param request the Servlet Request
566      * @param response the Servlet Response
567      * @param controller the Controller
568      * @throws ControllerException exception upon controller error
569      * @throws ServletException upon javax.servlet related errors
570      */

571     public static ServletControllerRequest parseParamsMultiPart(MultipartRequestHandler mp,
572                                                                 ActionMapping mapping,
573                                                                 ActionForm form,
574                                                                 HttpServletRequest JavaDoc request,
575                                                                 HttpServletResponse JavaDoc response,
576                                                                 Controller controller)
577             throws ServletException JavaDoc, ControllerException {
578         String JavaDoc controllerName = controller.getClass().getName();
579
580         //set up the required parameters
581
ServletControllerRequest cparams = new ServletControllerRequest();
582         cparams.setMapping(mapping);
583         cparams.setForm(form);
584         cparams.setParameters(null);
585
586         Hashtable JavaDoc params = new Hashtable JavaDoc();
587         params.put(Controller.CONTROLLER_PARAM_KEY, controllerName);
588
589         Hashtable JavaDoc textParams = mp.getTextElements();
590         Hashtable JavaDoc fileParams = mp.getFileElements();
591         String JavaDoc oneKey = null;
592
593         for (Enumeration JavaDoc te = textParams.keys(); te.hasMoreElements();) {
594             oneKey = (String JavaDoc) te.nextElement();
595             params.put(oneKey, textParams.get(oneKey));
596             log.debug("Got '" + oneKey + "', '" +
597                     (String JavaDoc) textParams.get(oneKey) + "'");
598         }
599         for (Enumeration JavaDoc fe = fileParams.keys(); fe.hasMoreElements();) {
600             oneKey = (String JavaDoc) fe.nextElement();
601
602             FileItem oneFileItem = (FileItem) fileParams.get(oneKey);
603             Vector JavaDoc oneFile = new Vector JavaDoc();
604             oneFile.addElement(oneFileItem.getFileName());
605             oneFile.addElement(oneFileItem.getStoreLocation().getPath());
606             params.put(oneKey, oneFile);
607             log.debug("Got file '" + oneKey + "', file '" +
608                     oneFileItem.getFileName() + "'");
609         }
610
611         HttpSession JavaDoc session = request.getSession(false);
612         String JavaDoc userName = null;
613         int uid = 0;
614
615         if (session != null) {
616             Object JavaDoc o = GenericSession.getAttribute(request, "CurrentLogin");
617
618             if (o != null) {
619                 CurrentLogin cl = (CurrentLogin) o;
620                 uid = cl.getUid();
621                 userName = cl.getUserName();
622
623                 if (log.isDebugEnabled()) {
624                     log.debug("Login established as user '" + userName +
625                             "', uid " + uid);
626                 }
627             } else {
628                 log.debug("No CurrentLogin object in session - assuming 'none'");
629             }
630         } else {
631             userName = User.UNKNOWN_USER;
632         }
633
634         cparams.setUser(userName);
635         cparams.setUid(uid);
636
637         /* Hand the controller a place to store it's persistent values */
638         cparams.setSession(new HTTPPersistentSession(request, response));
639         cparams.setServletRequest(request);
640         cparams.setServletResponse(response);
641         cparams.setDataContext(getDBName(request));
642
643 // Hashtable newParams = addButtonParams(params);
644
// Hashtable finalParams = addDefaultParams(newParams, request,
645
// controller);
646
Hashtable JavaDoc finalParams = addButtonParams(params);
647         Object JavaDoc oneParamValue = null;
648         String JavaDoc oneParamName = null;
649
650         for (Enumeration JavaDoc ep = finalParams.keys(); ep.hasMoreElements();) {
651             oneParamName = (String JavaDoc) ep.nextElement();
652             oneParamValue = finalParams.get(oneParamName);
653
654             if (oneParamValue instanceof String JavaDoc) {
655                 cparams.setParameter(oneParamName, (String JavaDoc) oneParamValue);
656             } else if (oneParamValue instanceof Vector JavaDoc) {
657                 Vector JavaDoc v = (Vector JavaDoc) oneParamValue;
658                 Enumeration JavaDoc ee = v.elements();
659                 String JavaDoc clientFileName = (String JavaDoc) ee.nextElement();
660                 String JavaDoc serverFileName = (String JavaDoc) ee.nextElement();
661                 cparams.setFileParameter(oneParamName, clientFileName,
662                         serverFileName);
663             }
664         } /* for each parameter */
665
666
667         return cparams;
668     } /* parseParamsMultiPart */
669
670
671     /**
672      * Parse a regular servlet request for parameters (e.g. not multipart)
673      *
674      * @param mapping The ActionMapping used to select this instance
675      * @param form The optional ActionForm bean for this request (if any)
676      * @param request the Servlet Request
677      * @param response the Servlet Response
678      * @param controller the Controller
679      * @return a constructed ServletControllerRequest object
680      * @throws ControllerException exception upon controller error
681      * @throws ServletException upon javax.servlet related errors
682      */

683     public static ServletControllerRequest parseParams(ActionMapping mapping,
684                                                        ActionForm form,
685                                                        HttpServletRequest JavaDoc request,
686                                                        HttpServletResponse JavaDoc response,
687                                                        Controller controller)
688             throws ServletException JavaDoc, ControllerException {
689         //set up the required parameters
690
Hashtable JavaDoc params = parseParamsReq(request, controller);
691         HttpSession JavaDoc session = request.getSession(false);
692         ServletControllerRequest cparams = new ServletControllerRequest();
693         cparams.setMapping(mapping);
694         cparams.setForm(form);
695         String JavaDoc userName = null;
696         int uid = 0;
697
698         if (session != null) {
699             Object JavaDoc o = GenericSession.getAttribute(request, "CurrentLogin");
700
701             if (o != null) {
702                 CurrentLogin cl = (CurrentLogin) o;
703                 uid = cl.getUid();
704                 userName = cl.getUserName();
705
706                 if (log.isDebugEnabled()) {
707                     log.debug("Login established as user '" + userName +
708                             "', uid " + uid);
709                 }
710             } else {
711                 log.debug("No CurrentLogin object in session - assuming 'none'");
712             }
713         } else {
714             userName = User.UNKNOWN_USER;
715             log.debug("No session available - user is none");
716         }
717
718         cparams.setUser(userName);
719         cparams.setUid(uid);
720
721         Object JavaDoc oneParamValue = null;
722         String JavaDoc oneParamName = null;
723
724         //Fixed on 7/11/2001. The line below was added to ensure that
725
//the params collection is regenerated on each request
726
cparams.setParameters(null);
727         boolean newRouting = (request.getAttribute("usedRouting") == null);
728
729         for (Enumeration JavaDoc ep = params.keys(); ep.hasMoreElements();) {
730             oneParamName = (String JavaDoc) ep.nextElement();
731             oneParamValue = params.get(oneParamName);
732
733
734             //Cleanup controller and state params because they have been consumed already.
735
//Not removing them will cause an unintended rerouting in the Controller.perform() method.
736

737             boolean routingParam = (oneParamName.equals(Controller.CONTROLLER_PARAM_KEY) || oneParamName.equals(
738                     Controller.STATE_PARAM_KEY));
739             if (newRouting || (!newRouting && !routingParam)) {
740                 if (oneParamValue instanceof String JavaDoc) {
741                     cparams.setParameter(oneParamName, (String JavaDoc) oneParamValue);
742                 } else if (oneParamValue instanceof Vector JavaDoc) {
743                     Vector JavaDoc v = (Vector JavaDoc) oneParamValue;
744                     Enumeration JavaDoc ee = v.elements();
745                     String JavaDoc clientFileName = (String JavaDoc) ee.nextElement();
746                     String JavaDoc serverFileName = (String JavaDoc) ee.nextElement();
747                     cparams.setFileParameter(oneParamName, clientFileName,
748                             serverFileName);
749                 }
750             }
751         } /* for each parameter */
752
753
754         /* Hand the controller a place to store it's persistent values */
755         cparams.setSession(new HTTPPersistentSession(request, response));
756         cparams.setServletRequest(request);
757         cparams.setServletResponse(response);
758
759         cparams.setDataContext(getDBName(request));
760
761         return cparams;
762     }
763
764     /**
765      * Convenience method to get the database name out of the session
766      * <p/>
767      * <p/>
768      * <p/>
769      * I deliberate add the <code>ActionMapping</code>, <code>ActionForm beans</code>
770      * as conveniences for Struts developers. You can probably sense I am cheesed off like
771      * Simon Cowell int American (Pop) Idol
772      * <p/>
773      * <p/>
774      * Docu byte Peter Pilgrim Fri Jan 23 23:20:41 GMT 2004
775      *
776      * @param request the servlet request object
777      * @return The name of the specified alternate database
778      */

779     private static String JavaDoc getDBName(HttpServletRequest JavaDoc request) {
780         try {
781             CurrentLogin cl = (CurrentLogin) GenericSession.getAttribute(request, CurrentLogin.LOGIN_KEY);
782
783             if (cl == null) {
784                 return "default";
785             }
786
787             return cl.getDBName();
788         } catch (ServletException JavaDoc se) {
789             log.error(se);
790
791             return "default";
792         }
793     } /* getDBName(HttpServletRequest) */
794
795
796     public Locale JavaDoc getLocale() {
797         Locale JavaDoc l = null;
798
799         try {
800             if (myServletRequest != null) {
801                 Object JavaDoc o = GenericSession.getAttribute((HttpServletRequest JavaDoc) myServletRequest,
802                         Messages.LOCALE_KEY);
803
804                 if (o != null) {
805                     l = (Locale JavaDoc) o;
806                 }
807             }
808         } catch (ServletException JavaDoc se) {
809             log.error(se);
810         }
811         if (l == null) {
812             l = Locale.getDefault();
813         }
814
815         return l;
816     }
817
818     /**
819      * Method that returns a copy of itself with all the fields properly filled
820      * out. Note that all the servletrequest/servletresponse classes are merely
821      * shallow copied since a deep copy is meaningless in a servlet environment
822      *
823      * @return a new instantiated ServletControllerRequest object
824      */

825     public Object JavaDoc clone() {
826         ServletControllerRequest scr = (ServletControllerRequest) super.clone();
827         scr.myCallingServlet = this.myCallingServlet;
828         scr.myServletRequest = this.myServletRequest;
829         scr.myServletResponse = this.myServletResponse;
830         scr.myMapping = this.myMapping;
831
832         return scr;
833     }
834
835     /**
836      * Fetches array of parameter values from underlying HTTP request; use this in a web app to access
837      * the underlying parameters in the HTTP request which have the same name; parameters with the same name are not
838      * reflected in the hashtable of params maintained by ControllerRequest;
839      *
840      * @param paramName key to look for among all parameters
841      * @return an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist.
842      */

843     public String JavaDoc[] getParamValues(String JavaDoc paramName) {
844         HttpServletRequest JavaDoc hreq = (HttpServletRequest JavaDoc) getServletRequest();
845         return hreq.getParameterValues(paramName);
846     }
847
848 } /* ServletControllerRequest */
849
Popular Tags