KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > plankton > data > ServletRequestParameterStateMap


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: ServletRequestParameterStateMap.java,v 1.5 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.plankton.data;
21
22 import java.io.*;
23 import java.util.*;
24 import javax.servlet.*;
25 import javax.servlet.http.*;
26
27 /**
28  * The implementation provides a StateMap bridge to a ServletRequest
29  * object's paramter values. By this, we mean that this class allows
30  * you to access ServletRequest paramters through the StateMap interface.
31  * Unlike the ServletRequest parameters, this class will handle null keys,
32  * values. Note however, that it's read-only. Put requests are silently ignored.
33  */

34 public class ServletRequestParameterStateMap implements StateMap {
35
36     //private vars
37
protected ServletRequest request = null;
38     private String JavaDoc NULL = "~Null~";
39     
40     /**
41      * Public constructor.
42      *
43      * @param irequest the underlying servlet request structure
44      */

45     public ServletRequestParameterStateMap(ServletRequest irequest) {
46         request = irequest;
47     }
48     
49     /**
50      * set a property in this StateMap - not implemented because
51      * the servlet API doesn't allow you to set parameters
52      *
53      * @param key the key object
54      * @param val the value object
55      */

56     public void putState(Object JavaDoc key, Object JavaDoc val) {
57         throw new UnsupportedOperationException JavaDoc(); //csc_052803_2
58
}
59     
60     /**
61      * get a property in this StateMap. Since the underlying structure is
62      * an HttpRequest parameter we have to handle the case where there are
63      * multiple values for a key. In this case, a List will be returned.
64      *
65      * @param key the key object
66      * @return the value for the given key
67      */

68     public Object JavaDoc getState(Object JavaDoc key) {
69         if (key==null) key = NULL;
70         Object JavaDoc[] vals = request.getParameterValues(key.toString());
71         if (vals==null) return null;
72         else if (vals.length==1) return (vals[0].equals(NULL) ? null : vals[0]);
73         else {
74             List list = new ArrayList();
75             for (int i=0, max=vals.length; i<max; i++) {
76                 list.add(vals[i]);
77             }
78             return list;
79         }
80     }
81     
82     /**
83      * remove a property in this StateMap - not implemented because
84      * the servlet API doesn't allow you to set parameters
85      *
86      * @param key the key object
87      * @return the object which was removed
88      */

89     public Object JavaDoc removeState(Object JavaDoc key) {
90 //csc_052803_2 return null;
91
throw new UnsupportedOperationException JavaDoc(); //csc_052803_2
92
}
93     
94     /**
95      * get a List of the keys for this StateMap (implementation
96      * is an ArrayList)
97      *
98      * @return a List the keys for this StateMap
99      */

100     public List getStateKeys() {
101         Enumeration e = request.getParameterNames();
102         List list = new ArrayList();
103         while (e.hasMoreElements()) {
104             list.add(e.nextElement());
105         }
106         return list;
107     }
108     
109     /**
110      * get a copy of the underlying Map (in this case, we
111      * map all the attributes of the ServletRequest structure
112      * into a Map and return that)
113      *
114      * @return a copy of the underlying state Map
115      */

116     public Map getStateValues() {
117         Map map = new HashMap();
118 // Map map = new TreeMap();
119
List keys = getStateKeys();
120         Iterator it = keys.iterator();
121         while (it.hasNext()) {
122             Object JavaDoc key = it.next();
123             Object JavaDoc val = getState(key);
124             map.put(key,val);
125         }
126         return map;
127     }
128
129     //csc_052803_2 - added
130
/**
131      * clear all state information
132      */

133     public void clearState() {
134         throw new UnsupportedOperationException JavaDoc();
135     }
136
137     /**
138      * get a reference to the underlying ServletRequest
139      *
140      * @return a reference to the underlying ServletRequest
141      */

142     public ServletRequest getRequest() {
143         return request;
144     }
145 }
146
Popular Tags