1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.dispatcher;
23
24 import java.io.Serializable;
25 import java.util.AbstractMap;
26 import java.util.Enumeration;
27 import java.util.HashSet;
28 import java.util.Set;
29
30 import javax.servlet.http.HttpServletRequest;
31
32
33 /***
34 * A simple implementation of the {@link java.util.Map} interface to handle a collection of request attributes.
35 */
36 public class RequestMap extends AbstractMap implements Serializable {
37
38 private static final long serialVersionUID = -7675640869293787926L;
39
40 private Set<Object> entries;
41 private HttpServletRequest request;
42
43
44 /***
45 * Saves the request to use as the backing for getting and setting values
46 *
47 * @param request the http servlet request.
48 */
49 public RequestMap(final HttpServletRequest request) {
50 this.request = request;
51 }
52
53
54 /***
55 * Removes all attributes from the request as well as clears entries in this map.
56 */
57 public void clear() {
58 entries = null;
59 Enumeration keys = request.getAttributeNames();
60
61 while (keys.hasMoreElements()) {
62 String key = (String) keys.nextElement();
63 request.removeAttribute(key);
64 }
65 }
66
67 /***
68 * Returns a Set of attributes from the http request.
69 *
70 * @return a Set of attributes from the http request.
71 */
72 public Set entrySet() {
73 if (entries == null) {
74 entries = new HashSet<Object>();
75
76 Enumeration enumeration = request.getAttributeNames();
77
78 while (enumeration.hasMoreElements()) {
79 final String key = enumeration.nextElement().toString();
80 final Object value = request.getAttribute(key);
81 entries.add(new Entry() {
82 public boolean equals(Object obj) {
83 Entry entry = (Entry) obj;
84
85 return ((key == null) ? (entry.getKey() == null) : key.equals(entry.getKey())) && ((value == null) ? (entry.getValue() == null) : value.equals(entry.getValue()));
86 }
87
88 public int hashCode() {
89 return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode());
90 }
91
92 public Object getKey() {
93 return key;
94 }
95
96 public Object getValue() {
97 return value;
98 }
99
100 public Object setValue(Object obj) {
101 request.setAttribute(key, obj);
102
103 return value;
104 }
105 });
106 }
107 }
108
109 return entries;
110 }
111
112 /***
113 * Returns the request attribute associated with the given key or <tt>null</tt> if it doesn't exist.
114 *
115 * @param key the name of the request attribute.
116 * @return the request attribute or <tt>null</tt> if it doesn't exist.
117 */
118 public Object get(Object key) {
119 return request.getAttribute(key.toString());
120 }
121
122 /***
123 * Saves an attribute in the request.
124 *
125 * @param key the name of the request attribute.
126 * @param value the value to set.
127 * @return the object that was just set.
128 */
129 public Object put(Object key, Object value) {
130 entries = null;
131 request.setAttribute(key.toString(), value);
132
133 return get(key);
134 }
135
136 /***
137 * Removes the specified request attribute.
138 *
139 * @param key the name of the attribute to remove.
140 * @return the value that was removed or <tt>null</tt> if the value was not found (and hence, not removed).
141 */
142 public Object remove(Object key) {
143 entries = null;
144
145 Object value = get(key);
146 request.removeAttribute(key.toString());
147
148 return value;
149 }
150 }