KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > helper > state > ParamPersister


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: ParamPersister.java,v 1.6 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.helper.state;
21
22 import java.util.*;
23 import javax.servlet.*;
24 import javax.servlet.http.*;
25
26 import org.enhydra.barracuda.core.helper.servlet.*;
27 import org.enhydra.barracuda.plankton.data.*;
28 import org.enhydra.barracuda.core.util.http.*;
29
30 /**
31  * <p>Utility methods used for saving servlet request param State
32  * and then reconstituting them later.
33  *
34  * TODO - there is a potential problem with this class, in that
35  * if you have multiple requests running at the same time, which
36  * both need to save values in the session, they are going to stomp
37  * on one another...
38  */

39 public class ParamPersister {
40
41     static final String PARAM_LIST = "ParamPersister.PARAM_LIST";
42
43     /**
44      * This method takes Request parameters and saves them in a
45      * users session. End users should never need
46      *
47      * @param req the ServletRequest object
48      */

49     public static void persistReqParamState(HttpServletRequest req) {
50         //eliminate the obvious
51
if (req==null) return;
52
53         //first see if there are any param values even associated
54
//with this request
55
HttpSession session = null;
56         List paramList = null;
57
58         //iterate through the list
59
Enumeration enum = req.getParameterNames();
60         while (enum.hasMoreElements()) {
61             //get the session obj and create a list to store the values in
62
if (session==null) {
63                 session = SessionServices.getSession(req);
64                 paramList = new ArrayList();
65                 session.setAttribute(PARAM_LIST, paramList);
66             }
67
68             //now get all the values associated with a given parameter
69
//and save them in the state
70
String key = (String) enum.nextElement();
71             String[] values = req.getParameterValues(key);
72             for (int i=0, max=values.length; i<max; i++) {
73                 paramList.add(new Param(key, values[i]));
74             }
75         }
76     }
77
78     /**
79      * This method reconstitutes Request parameters from a users session
80      *
81      * @param req the wrapper ServletRequest object
82      */

83     public static void reconstituteReqParamState(BarracudaServletRequestWrapper req) {
84         //eliminate the obvious
85
if (req==null) return;
86
87         //see if the user even has a session. If not, there won't be any state
88
HttpSession session = SessionServices.getSession(req, false);
89         if (session==null) return;
90         
91         //now see if the session has any param state
92
List paramList = (List) session.getAttribute(PARAM_LIST);
93         if (paramList!=null) {
94             //iterate through the list, adding them back into the req object
95
Iterator it = paramList.iterator();
96             while (it.hasNext()) {
97                 Param p = (Param) it.next();
98                 req.addParameter(p.getKey(), p.getValue());
99             }
100         
101             //finally make sure we clear out the param state
102
session.removeAttribute(PARAM_LIST);
103         }
104     }
105 }
106
Popular Tags