KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > dispatcher > RequestMap


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.dispatcher;
6
7 import javax.servlet.http.HttpServletRequest JavaDoc;
8 import java.io.Serializable JavaDoc;
9 import java.util.AbstractMap JavaDoc;
10 import java.util.Enumeration JavaDoc;
11 import java.util.HashSet JavaDoc;
12 import java.util.Set JavaDoc;
13
14
15 /**
16  * A simple implementation of the {@link java.util.Map} interface to handle a collection of request attributes.
17  *
18  * @author Patrick Lightbody
19  * @author Bill Lynch (docs)
20  */

21 public class RequestMap extends AbstractMap JavaDoc implements Serializable JavaDoc {
22     //~ Instance fields ////////////////////////////////////////////////////////
23

24     Set JavaDoc entries;
25     private HttpServletRequest JavaDoc request;
26
27     //~ Constructors ///////////////////////////////////////////////////////////
28

29     /**
30      * Saves the request to use as the backing for getting and setting values
31      *
32      * @param request the http servlet request.
33      */

34     public RequestMap(final HttpServletRequest JavaDoc request) {
35         this.request = request;
36     }
37
38     //~ Methods ////////////////////////////////////////////////////////////////
39

40     /**
41      * Removes all attributes from the request as well as clears entries in this map.
42      */

43     public void clear() {
44         Enumeration JavaDoc keys = request.getAttributeNames();
45
46         while (keys.hasMoreElements()) {
47             String JavaDoc key = (String JavaDoc) keys.nextElement();
48             request.removeAttribute(key);
49         }
50     }
51
52     /**
53      * Returns a Set of attributes from the http request.
54      *
55      * @return a Set of attributes from the http request.
56      */

57     public Set JavaDoc entrySet() {
58         if (entries == null) {
59             entries = new HashSet JavaDoc();
60
61             Enumeration JavaDoc enumeration = request.getAttributeNames();
62
63             while (enumeration.hasMoreElements()) {
64                 final String JavaDoc key = enumeration.nextElement().toString();
65                 final Object JavaDoc value = request.getAttribute(key);
66                 entries.add(new Entry() {
67                     public boolean equals(Object JavaDoc obj) {
68                         Entry entry = (Entry) obj;
69
70                         return ((key == null) ? (entry.getKey() == null) : key.equals(entry.getKey())) && ((value == null) ? (entry.getValue() == null) : value.equals(entry.getValue()));
71                     }
72
73                     public int hashCode() {
74                         return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode());
75                     }
76
77                     public Object JavaDoc getKey() {
78                         return key;
79                     }
80
81                     public Object JavaDoc getValue() {
82                         return value;
83                     }
84
85                     public Object JavaDoc setValue(Object JavaDoc obj) {
86                         request.setAttribute(key.toString(), obj);
87
88                         return value;
89                     }
90                 });
91             }
92         }
93
94         return entries;
95     }
96
97     /**
98      * Returns the request attribute associated with the given key or <tt>null</tt> if it doesn't exist.
99      *
100      * @param key the name of the request attribute.
101      * @return the request attribute or <tt>null</tt> if it doesn't exist.
102      */

103     public Object JavaDoc get(Object JavaDoc key) {
104         return request.getAttribute(key.toString());
105     }
106
107     /**
108      * Saves an attribute in the request.
109      *
110      * @param key the name of the request attribute.
111      * @param value the value to set.
112      * @return the object that was just set.
113      */

114     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
115         entries = null;
116         request.setAttribute(key.toString(), value);
117
118         return get(key);
119     }
120
121     /**
122      * Removes the specified request attribute.
123      *
124      * @param key the name of the attribute to remove.
125      * @return the value that was removed or <tt>null</tt> if the value was not found (and hence, not removed).
126      */

127     public Object JavaDoc remove(Object JavaDoc key) {
128         entries = null;
129
130         Object JavaDoc value = get(key);
131         request.removeAttribute(key.toString());
132
133         return value;
134     }
135 }
136
Popular Tags