KickJava   Java API By Example, From Geeks To Geeks.

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


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: ServletRequestStateMap.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. By this, we mean that this class allows you to treat an
30  * ServletRequest as a StateMap. Unlike the ServletRequest, this class
31  * will handle null keys, values.
32  */

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

44     public ServletRequestStateMap(ServletRequest irequest) {
45         request = irequest;
46     }
47     
48     /**
49      * set a property in this StateMap
50      *
51      * @param key the key object
52      * @param val the value object
53      */

54     public void putState(Object JavaDoc key, Object JavaDoc val) {
55         if (key==null) key = NULL;
56         if (val==null) val = NULL;
57         request.setAttribute(key.toString(),val);
58     }
59     
60     /**
61      * get a property in this StateMap
62      *
63      * @param key the key object
64      * @return the value for the given key
65      */

66     public Object JavaDoc getState(Object JavaDoc key) {
67         if (key==null) key = NULL;
68         Object JavaDoc val = request.getAttribute(key.toString());
69         return (val.equals(NULL) ? null : val);
70     }
71     
72     /**
73      * remove a property in this StateMap
74      *
75      * @param key the key object
76      * @return the object which was removed
77      */

78     public Object JavaDoc removeState(Object JavaDoc key) {
79         if (key==null) key = NULL;
80         Object JavaDoc val = request.getAttribute(key.toString());
81         request.removeAttribute(key.toString());
82         return (val.equals(NULL) ? null : val);
83     }
84     
85     /**
86      * get a List of the keys for this StateMap (implementation
87      * is an ArrayList)
88      *
89      * @return a List the keys for this StateMap
90      */

91     public List getStateKeys() {
92         Enumeration e = request.getAttributeNames();
93         List list = new ArrayList();
94         while (e.hasMoreElements()) {
95             list.add(e.nextElement());
96         }
97         return list;
98     }
99     
100     /**
101      * get a copy of the underlying Map (in this case, we
102      * map all the attributes of the ServletRequest structure
103      * into a Map and return that)
104      *
105      * @return a copy of the underlying state Map
106      */

107     public Map getStateValues() {
108 // Map map = new HashMap();
109
Map map = new TreeMap();
110         List keys = getStateKeys();
111         Iterator it = keys.iterator();
112         while (it.hasNext()) {
113             Object JavaDoc key = it.next();
114             Object JavaDoc val = request.getAttribute(key.toString());
115             map.put(key,val);
116         }
117         return map;
118     }
119
120     //csc_052803_2 - added
121
/**
122      * clear all state information
123      */

124     public void clearState() {
125         List keys = getStateKeys();
126         Iterator it = keys.iterator();
127         while (it.hasNext()) {
128             Object JavaDoc key = it.next();
129             removeState(key);
130         }
131     }
132
133     /**
134      * get a reference to the underlying ServletRequest
135      *
136      * @return a reference to the underlying ServletRequest
137      */

138     public ServletRequest getRequest() {
139         return request;
140     }
141 }
142
Popular Tags