1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.tiles.servlet.context;
22
23
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Enumeration;
27 import java.util.HashSet;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Set;
32
33 import javax.servlet.http.HttpServletRequest;
34
35 import org.apache.tiles.context.MapEntry;
36
37
38
39
40
41
42
43
44
45 final class ServletHeaderValuesMap implements Map<String, String[]> {
46
47
48
49
50
51
52
53 public ServletHeaderValuesMap(HttpServletRequest request) {
54 this.request = request;
55 }
56
57
58
59
60
61 private HttpServletRequest request = null;
62
63
64
65 public void clear() {
66 throw new UnsupportedOperationException();
67 }
68
69
70
71 public boolean containsKey(Object key) {
72 return (request.getHeader(key(key)) != null);
73 }
74
75
76
77 public boolean containsValue(Object value) {
78 if (!(value instanceof String[])) {
79 return (false);
80 }
81 String[] test = (String[]) value;
82 Iterator<String[]> values = values().iterator();
83 while (values.hasNext()) {
84 String[] actual = values.next();
85 if (test.length == actual.length) {
86 boolean matched = true;
87 for (int i = 0; i < test.length; i++) {
88 if (!test[i].equals(actual[i])) {
89 matched = false;
90 break;
91 }
92 }
93 if (matched) {
94 return (true);
95 }
96 }
97 }
98 return (false);
99 }
100
101
102
103 @SuppressWarnings("unchecked")
104 public Set<Map.Entry<String, String[]>> entrySet() {
105 Set<Map.Entry<String, String[]>> set = new HashSet<Map.Entry<String, String[]>>();
106 Enumeration<String> keys = request.getHeaderNames();
107 String key;
108 while (keys.hasMoreElements()) {
109 key = keys.nextElement();
110 Enumeration<String> headerEnum = request.getHeaders(key);
111 set.add(new MapEntry<String, String[]>(key,
112 enumeration2array(headerEnum), false));
113 }
114 return (set);
115 }
116
117
118
119 @SuppressWarnings("unchecked")
120 public boolean equals(Object o) {
121 HttpServletRequest otherRequest = ((ServletHeaderValuesMap) o).request;
122 boolean retValue = true;
123 synchronized (request) {
124 for (Enumeration<String> attribs = request.getHeaderNames(); attribs
125 .hasMoreElements()
126 && retValue;) {
127 String parameterName = attribs.nextElement();
128 retValue = request.getHeaders(parameterName).equals(
129 otherRequest.getHeaders(parameterName));
130 }
131 }
132
133 return retValue;
134 }
135
136
137
138 @SuppressWarnings("unchecked")
139 public String[] get(Object key) {
140 List<String> list = new ArrayList<String>();
141 Enumeration<String> values = request.getHeaders(key(key));
142 while (values.hasMoreElements()) {
143 list.add(values.nextElement());
144 }
145 return ((list.toArray(new String[list.size()])));
146 }
147
148
149
150 public int hashCode() {
151 return (request.hashCode());
152 }
153
154
155
156 public boolean isEmpty() {
157 return (size() < 1);
158 }
159
160
161
162 @SuppressWarnings("unchecked")
163 public Set<String> keySet() {
164 Set<String> set = new HashSet<String>();
165 Enumeration<String> keys = request.getHeaderNames();
166 while (keys.hasMoreElements()) {
167 set.add(keys.nextElement());
168 }
169 return (set);
170 }
171
172
173
174 public String[] put(String key, String[] value) {
175 throw new UnsupportedOperationException();
176 }
177
178
179
180 public void putAll(Map<? extends String, ? extends String[]> map) {
181 throw new UnsupportedOperationException();
182 }
183
184
185
186 public String[] remove(Object key) {
187 throw new UnsupportedOperationException();
188 }
189
190
191
192
193 @SuppressWarnings("unchecked")
194 public int size() {
195 int n = 0;
196 Enumeration<String> keys = request.getHeaderNames();
197 while (keys.hasMoreElements()) {
198 keys.nextElement();
199 n++;
200 }
201 return (n);
202 }
203
204
205
206 @SuppressWarnings("unchecked")
207 public Collection<String[]> values() {
208 List<String[]> list = new ArrayList<String[]>();
209 Enumeration<String> keys = request.getHeaderNames();
210 while (keys.hasMoreElements()) {
211 String key = keys.nextElement();
212 Enumeration<String> values = request.getHeaders(key);
213 list.add(enumeration2array(values));
214 }
215 return (list);
216 }
217
218
219
220
221
222
223
224
225
226 private String key(Object key) {
227 if (key == null) {
228 throw new IllegalArgumentException();
229 } else if (key instanceof String) {
230 return ((String) key);
231 } else {
232 return (key.toString());
233 }
234 }
235
236
237
238
239
240
241
242 private String[] enumeration2array(Enumeration<String> enumeration) {
243 List<String> list1 = new ArrayList<String>();
244 while (enumeration.hasMoreElements()) {
245 list1.add(enumeration.nextElement());
246 }
247
248 return list1.toArray(new String[list1.size()]);
249 }
250 }