KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > module > sessionInfo


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.module;
11
12 import java.util.*;
13
14 import org.mmbase.module.core.*;
15 import org.mmbase.util.logging.*;
16
17 /**
18  * The module which provides access to a filesystem residing in a database.
19  *
20  * @application SCAN
21  * @rename SessionInfo
22  * @author Daniel Ockeloen
23  */

24 public class sessionInfo {
25
26     private static Logger log = Logging.getLoggerInstance(sessionInfo.class.getName());
27
28     private String JavaDoc hostname;
29     private String JavaDoc cookie;
30     private MMObjectNode node;
31
32     Hashtable values = new Hashtable();
33     Hashtable setvalues = new Hashtable();
34
35     public void setNode(MMObjectNode node) {
36         this.node=node;
37     }
38
39     public MMObjectNode getNode() {
40         return(node);
41     }
42
43     public String JavaDoc getCookie() {
44         return(cookie);
45     }
46
47     public String JavaDoc getValue(String JavaDoc wanted) {
48         return((String JavaDoc)values.get(wanted));
49     }
50
51     public String JavaDoc setValue(String JavaDoc key,String JavaDoc value) {
52         if (isSecure(value)) {
53             return((String JavaDoc)values.put(key,value));
54         } else {
55             log.error("ERROR: Illegal input, action blocked");
56             return("illegal input,see error log");
57         }
58     }
59
60     /**
61      * Removes (clears) a value from a session.
62      * @param key the key of the attribute to clear
63      * @return the original value of the attribute
64      */

65     public String JavaDoc removeValue(String JavaDoc key) {
66         return((String JavaDoc)values.remove(key));
67     }
68
69     /**
70     * adds a value to a set, no duplicates are allowed.
71     */

72     public void addSetValue(String JavaDoc key,String JavaDoc value) {
73
74         log.debug("addSetValue("+key+","+value+")");
75
76         Vector v=(Vector)setvalues.get(key);
77         if (v==null) {
78             // not found so create it
79
v=new Vector();
80             if (isSecure(value)) {
81                 v.addElement(value);
82                 setvalues.put(key,v);
83             } else {
84                 log.error("ERROR: Illegal input, action blocked");
85             }
86             log.debug("sessionset="+v.toString());
87         } else {
88             if (!v.contains(value)) {
89                 v.addElement(value);
90                 log.debug("sessionset="+v.toString());
91             }
92         }
93         log.debug("addSetValue() -> getSetString("+key+"): " +getSetString(key));
94     }
95
96
97     /**
98     * add a value to a set, duplicates are allowed.
99     */

100     public void putSetValue(String JavaDoc key,String JavaDoc value) {
101
102         log.debug("putSetValue("+key+","+value+")");
103
104         Vector v=(Vector)setvalues.get(key);
105         if (v==null) {
106             // not found so create it
107
v=new Vector();
108             if (isSecure(value)) {
109                 v.addElement(value);
110                 setvalues.put(key,v);
111             } else {
112                 log.error("ERROR: Illegal input, action blocked");
113             }
114             log.debug("sessionset="+v.toString());
115         } else {
116             if (isSecure(value)) {
117                 v.addElement(value);
118             } else {
119                 log.error("ERROR: Illegal input, action blocked");
120             }
121             log.debug("sessionset="+v.toString());
122         }
123     }
124
125
126     /**
127     * deletes a value from the SESSION set.
128     */

129     public void delSetValue(String JavaDoc key,String JavaDoc value) {
130         Vector v=(Vector)setvalues.get(key);
131         if (v!=null) {
132             if (v.contains(value)) {
133                 v.removeElement(value);
134                 log.debug("sessionset="+v.toString());
135             }
136         }
137     }
138
139
140     /**
141     * does this set contain the value ?
142     */

143     public String JavaDoc containsSetValue(String JavaDoc key,String JavaDoc value) {
144         Vector v=(Vector)setvalues.get(key);
145         if (v!=null) {
146             if (v.contains(value)) {
147                 return("YES");
148             }
149         }
150         return("NO");
151     }
152
153
154     /**
155     * delete the values belonging to the key
156     */

157     public String JavaDoc clearSet(String JavaDoc key) {
158         log.debug("sessionset="+key);
159         Vector v=(Vector)setvalues.get(key);
160         if (v!=null) {
161             v=new Vector();
162             setvalues.put(key,v);
163             log.debug("sessionset="+v.toString());
164         }
165         return("");
166     }
167
168
169     /**
170     * returns the session variable values comma separaterd
171     * @param key the name of the session variable
172     */

173     public String JavaDoc getSetString(String JavaDoc key) {
174
175         log.debug("getSetString("+key+")");
176
177         Vector v=(Vector)setvalues.get(key);
178         if (v!=null) {
179             String JavaDoc result="";
180             Enumeration res=v.elements();
181             while (res.hasMoreElements()) {
182                 String JavaDoc tmp=(String JavaDoc)res.nextElement();
183                 if (result.equals("")) {
184                     result=tmp;
185                 } else {
186                     result+=","+tmp;
187                 }
188             }
189             return(result);
190         } else {
191             log.error("getSetString("+key+"): ERROR: this key is non-existent!");
192             return(null);
193         }
194     }
195
196     /**
197     * return the number of values contained by a session variable
198     */

199     public String JavaDoc getSetCount(String JavaDoc key) {
200
201         Vector v=(Vector)setvalues.get(key);
202         if (v!=null) {
203             return(""+v.size());
204         } else {
205             return(null);
206         }
207     }
208
209
210     /**
211     * return the average of a set of numbers
212     */

213     public String JavaDoc getAvgSet(String JavaDoc key) {
214         Vector v=(Vector)setvalues.get(key);
215         if (v!=null) {
216             int total=0;
217             int count=0;
218             Enumeration res=v.elements();
219             while (res.hasMoreElements()) {
220                 try {
221                     String JavaDoc tmp=(String JavaDoc)res.nextElement();
222                     int tmpi=Integer.parseInt(tmp);
223                     total+=tmpi;
224                     count++;
225                 } catch(Exception JavaDoc e) {}
226             }
227             int res1=total/count;
228             return(""+res1);
229         } else {
230             return(null);
231         }
232     }
233
234     /**
235      * returns the hostname of a user
236      */

237     public String JavaDoc getHostName() {
238         return(hostname);
239     }
240
241     public sessionInfo(String JavaDoc hostname, String JavaDoc cookie) {
242         this.hostname=hostname;
243         this.cookie=cookie;
244     }
245
246     public sessionInfo(String JavaDoc hostname) {
247         this.hostname=hostname;
248     }
249
250     public String JavaDoc toString() {
251         return("sessionInfo="+values.toString());
252     }
253
254
255     private boolean isSecure(String JavaDoc value) {
256
257         Vector words=new Vector();
258         words.addElement("<transaction");
259         words.addElement("<TRANSACTION");
260         words.addElement("<create");
261         words.addElement("<createObject");
262         words.addElement("<delete");
263         words.addElement("<mark");
264         words.addElement("<setField");
265         words.addElement("</setField>");
266         words.addElement("<DO");
267         Enumeration e = words.elements();
268         while (e.hasMoreElements()) {
269             String JavaDoc check=(String JavaDoc)e.nextElement();
270             if (value.indexOf(check)!=-1) {
271                 log.error(check+" found in session variable");
272                 return(false);
273             }
274         }
275         return(true);
276     }
277 }
278
279
Popular Tags