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.request.collection; |
22 | |
|
23 | |
import static org.apache.tiles.request.collection.CollectionUtil.*; |
24 | |
|
25 | |
import java.util.ArrayList; |
26 | |
import java.util.Collection; |
27 | |
import java.util.Enumeration; |
28 | |
import java.util.HashSet; |
29 | |
import java.util.Iterator; |
30 | |
import java.util.List; |
31 | |
import java.util.Map; |
32 | |
import java.util.Set; |
33 | |
|
34 | |
import org.apache.tiles.request.attribute.EnumeratedValuesExtractor; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 26 | public class HeaderValuesMap implements Map<String, String[]> { |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
private EnumeratedValuesExtractor request; |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | 46 | public HeaderValuesMap(EnumeratedValuesExtractor request) { |
55 | 46 | this.request = request; |
56 | 46 | } |
57 | |
|
58 | |
|
59 | |
|
60 | |
public void clear() { |
61 | 1 | throw new UnsupportedOperationException(); |
62 | |
} |
63 | |
|
64 | |
|
65 | |
|
66 | |
public boolean containsKey(Object key) { |
67 | 2 | return (request.getValue(key(key)) != null); |
68 | |
} |
69 | |
|
70 | |
|
71 | |
|
72 | |
public boolean containsValue(Object value) { |
73 | 9 | if (!(value instanceof String[])) { |
74 | 2 | return (false); |
75 | |
} |
76 | 7 | String[] test = (String[]) value; |
77 | 7 | Enumeration<String> names = request.getKeys(); |
78 | 16 | while (names.hasMoreElements()) { |
79 | 13 | String name = names.nextElement(); |
80 | 13 | if (compareHeaders(name, array2set(test))) { |
81 | 4 | return true; |
82 | |
} |
83 | 9 | } |
84 | 3 | return false; |
85 | |
} |
86 | |
|
87 | |
|
88 | |
|
89 | |
public Set<Map.Entry<String, String[]>> entrySet() { |
90 | 15 | return new HeadersEntrySet(); |
91 | |
} |
92 | |
|
93 | |
|
94 | |
|
95 | |
@Override |
96 | |
public boolean equals(Object o) { |
97 | 1 | EnumeratedValuesExtractor otherRequest = ((HeaderValuesMap) o).request; |
98 | 1 | boolean retValue = true; |
99 | 1 | for (Enumeration<String> attribs = request.getKeys(); attribs |
100 | |
.hasMoreElements() |
101 | 3 | && retValue;) { |
102 | 2 | String parameterName = attribs.nextElement(); |
103 | 2 | Set<String> valueSet = enumeration2set(otherRequest.getValues(parameterName)); |
104 | 2 | retValue = compareHeaders(parameterName, valueSet); |
105 | 2 | } |
106 | |
|
107 | 1 | return retValue; |
108 | |
} |
109 | |
|
110 | |
|
111 | |
|
112 | |
public String[] get(Object key) { |
113 | 1 | return getHeaderValues(key(key)); |
114 | |
} |
115 | |
|
116 | |
|
117 | |
@Override |
118 | |
public int hashCode() { |
119 | 1 | int retValue = 0; |
120 | 1 | for (Enumeration<String> attribs = request.getKeys(); attribs |
121 | 3 | .hasMoreElements();) { |
122 | 2 | String parameterName = attribs.nextElement(); |
123 | 2 | Enumeration<String> values = request.getValues(parameterName); |
124 | 2 | int valueHash = 0; |
125 | 5 | while (values.hasMoreElements()) { |
126 | 3 | valueHash += values.nextElement().hashCode(); |
127 | |
} |
128 | 2 | retValue += parameterName.hashCode() ^ valueHash; |
129 | 2 | } |
130 | 1 | return retValue; |
131 | |
} |
132 | |
|
133 | |
|
134 | |
|
135 | |
public boolean isEmpty() { |
136 | 4 | return !request.getKeys().hasMoreElements(); |
137 | |
} |
138 | |
|
139 | |
|
140 | |
|
141 | |
public Set<String> keySet() { |
142 | 1 | return new KeySet(request); |
143 | |
} |
144 | |
|
145 | |
|
146 | |
|
147 | |
public String[] put(String key, String[] value) { |
148 | 1 | throw new UnsupportedOperationException(); |
149 | |
} |
150 | |
|
151 | |
|
152 | |
|
153 | |
public void putAll(Map<? extends String, ? extends String[]> map) { |
154 | 1 | throw new UnsupportedOperationException(); |
155 | |
} |
156 | |
|
157 | |
|
158 | |
|
159 | |
public String[] remove(Object key) { |
160 | 1 | throw new UnsupportedOperationException(); |
161 | |
} |
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
public int size() { |
167 | 3 | return enumerationSize(request.getKeys()); |
168 | |
} |
169 | |
|
170 | |
|
171 | |
|
172 | |
public Collection<String[]> values() { |
173 | 16 | return new HeaderValuesCollection(); |
174 | |
} |
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
private String[] getHeaderValues(String key) { |
183 | 6 | List<String> list = new ArrayList<String>(); |
184 | 6 | Enumeration<String> values = request.getValues(key); |
185 | 16 | while (values.hasMoreElements()) { |
186 | 10 | list.add(values.nextElement()); |
187 | |
} |
188 | 6 | String[] retValue = list.toArray(new String[list.size()]); |
189 | 6 | return retValue; |
190 | |
} |
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
private Set<String> enumeration2set(Enumeration<String> enumeration) { |
199 | 2 | Set<String> retValue = new HashSet<String>(); |
200 | 5 | while (enumeration.hasMoreElements()) { |
201 | 3 | retValue.add(enumeration.nextElement()); |
202 | |
} |
203 | 2 | return retValue; |
204 | |
} |
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
private Set<String> array2set(String[] valueArray) { |
213 | 17 | Set<String> values = new HashSet<String>(); |
214 | 46 | for (int i = 0; i < valueArray.length; i++) { |
215 | 29 | values.add(valueArray[i]); |
216 | |
} |
217 | 17 | return values; |
218 | |
} |
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
private boolean compareHeaders(String name, Set<String> testSet) { |
230 | 15 | Enumeration<String> values = request.getValues(name); |
231 | 15 | boolean matched = true; |
232 | 36 | while (values.hasMoreElements() && matched) { |
233 | 21 | String currentValue = values.nextElement(); |
234 | 21 | matched = testSet.remove(currentValue); |
235 | 21 | } |
236 | 15 | matched = matched && testSet.isEmpty(); |
237 | 15 | return matched; |
238 | |
} |
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | 32 | private class HeadersEntrySet implements Set<Map.Entry<String, String[]>> { |
244 | |
|
245 | |
@Override |
246 | |
public boolean add(java.util.Map.Entry<String, String[]> e) { |
247 | 1 | throw new UnsupportedOperationException(); |
248 | |
} |
249 | |
|
250 | |
@Override |
251 | |
public boolean addAll( |
252 | |
Collection<? extends java.util.Map.Entry<String, String[]>> c) { |
253 | 1 | throw new UnsupportedOperationException(); |
254 | |
} |
255 | |
|
256 | |
@Override |
257 | |
public void clear() { |
258 | 1 | throw new UnsupportedOperationException(); |
259 | |
} |
260 | |
|
261 | |
@SuppressWarnings("unchecked") |
262 | |
@Override |
263 | |
public boolean contains(Object o) { |
264 | 1 | return containsEntry((java.util.Map.Entry<String, String[]>) o); |
265 | |
} |
266 | |
|
267 | |
@SuppressWarnings("unchecked") |
268 | |
@Override |
269 | |
public boolean containsAll(Collection<?> c) { |
270 | 2 | Collection<Map.Entry<String, String[]>> realCollection = |
271 | |
(Collection<Map.Entry<String, String[]>>) c; |
272 | 2 | for (Map.Entry<String, String[]> entry : realCollection) { |
273 | 3 | if (!containsEntry(entry)) { |
274 | 1 | return false; |
275 | |
} |
276 | 2 | } |
277 | 1 | return true; |
278 | |
} |
279 | |
|
280 | |
@Override |
281 | |
public boolean isEmpty() { |
282 | 1 | return HeaderValuesMap.this.isEmpty(); |
283 | |
} |
284 | |
|
285 | |
@Override |
286 | |
public Iterator<java.util.Map.Entry<String, String[]>> iterator() { |
287 | 2 | return new HeadersEntrySetIterator(); |
288 | |
} |
289 | |
|
290 | |
@Override |
291 | |
public boolean remove(Object o) { |
292 | 1 | throw new UnsupportedOperationException(); |
293 | |
} |
294 | |
|
295 | |
@Override |
296 | |
public boolean removeAll(Collection<?> c) { |
297 | 1 | throw new UnsupportedOperationException(); |
298 | |
} |
299 | |
|
300 | |
@Override |
301 | |
public boolean retainAll(Collection<?> c) { |
302 | 1 | throw new UnsupportedOperationException(); |
303 | |
} |
304 | |
|
305 | |
@Override |
306 | |
public int size() { |
307 | 1 | return HeaderValuesMap.this.size(); |
308 | |
} |
309 | |
|
310 | |
@Override |
311 | |
public Object[] toArray() { |
312 | 1 | return toList().toArray(); |
313 | |
} |
314 | |
|
315 | |
@Override |
316 | |
public <T> T[] toArray(T[] a) { |
317 | 1 | return toList().toArray(a); |
318 | |
} |
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
private boolean containsEntry(Map.Entry<String, String[]> entry) { |
327 | 4 | Enumeration<String> entryValues = request.getValues(key(entry.getKey())); |
328 | 4 | String[] valueArray = entry.getValue(); |
329 | 4 | Set<String> values = array2set(valueArray); |
330 | 9 | while (entryValues.hasMoreElements()) { |
331 | 6 | if (!values.remove(entryValues.nextElement())) { |
332 | 1 | return false; |
333 | |
} |
334 | |
} |
335 | 3 | return values.isEmpty(); |
336 | |
} |
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | |
|
342 | |
|
343 | |
private List<Map.Entry<String, String[]>> toList() { |
344 | 2 | List<Map.Entry<String, String[]>> entries = new ArrayList<Map.Entry<String, String[]>>(); |
345 | 2 | Enumeration<String> names = request.getKeys(); |
346 | 6 | while (names.hasMoreElements()) { |
347 | 4 | entries.add(extractNextEntry(names)); |
348 | |
} |
349 | 2 | return entries; |
350 | |
} |
351 | |
|
352 | |
|
353 | |
|
354 | |
|
355 | |
|
356 | |
|
357 | |
|
358 | |
private MapEntry<String, String[]> extractNextEntry( |
359 | |
Enumeration<String> names) { |
360 | 5 | String name = names.nextElement(); |
361 | 5 | return new MapEntryArrayValues<String, String>(name, getHeaderValues(name), false); |
362 | |
} |
363 | |
|
364 | |
|
365 | |
|
366 | |
|
367 | 5 | private class HeadersEntrySetIterator implements Iterator<Map.Entry<String, String[]>> { |
368 | |
|
369 | |
|
370 | |
|
371 | |
|
372 | 2 | private Enumeration<String> namesEnumeration = request.getKeys(); |
373 | |
|
374 | |
@Override |
375 | |
public boolean hasNext() { |
376 | 1 | return namesEnumeration.hasMoreElements(); |
377 | |
} |
378 | |
|
379 | |
@Override |
380 | |
public Map.Entry<String, String[]> next() { |
381 | 1 | return extractNextEntry(namesEnumeration); |
382 | |
} |
383 | |
|
384 | |
@Override |
385 | |
public void remove() { |
386 | 1 | throw new UnsupportedOperationException(); |
387 | |
} |
388 | |
|
389 | |
} |
390 | |
} |
391 | |
|
392 | |
|
393 | |
|
394 | |
|
395 | |
|
396 | 34 | private class HeaderValuesCollection implements Collection<String[]> { |
397 | |
|
398 | |
@Override |
399 | |
public boolean add(String[] e) { |
400 | 1 | throw new UnsupportedOperationException(); |
401 | |
} |
402 | |
|
403 | |
@Override |
404 | |
public boolean addAll(Collection<? extends String[]> c) { |
405 | 1 | throw new UnsupportedOperationException(); |
406 | |
} |
407 | |
|
408 | |
@Override |
409 | |
public void clear() { |
410 | 1 | throw new UnsupportedOperationException(); |
411 | |
} |
412 | |
|
413 | |
@Override |
414 | |
public boolean contains(Object o) { |
415 | 2 | return containsValue(o); |
416 | |
} |
417 | |
|
418 | |
@SuppressWarnings("unchecked") |
419 | |
@Override |
420 | |
public boolean containsAll(Collection<?> c) { |
421 | 2 | Collection<String[]> realCollection = (Collection<String[]>) c; |
422 | 2 | for (String[] value : realCollection) { |
423 | 3 | if (!containsValue(value)) { |
424 | 1 | return false; |
425 | |
} |
426 | 2 | } |
427 | 1 | return true; |
428 | |
} |
429 | |
|
430 | |
@Override |
431 | |
public boolean isEmpty() { |
432 | 1 | return HeaderValuesMap.this.isEmpty(); |
433 | |
} |
434 | |
|
435 | |
@Override |
436 | |
public Iterator<String[]> iterator() { |
437 | 2 | return new HeaderValuesCollectionIterator(); |
438 | |
} |
439 | |
|
440 | |
@Override |
441 | |
public boolean remove(Object o) { |
442 | 1 | throw new UnsupportedOperationException(); |
443 | |
} |
444 | |
|
445 | |
@Override |
446 | |
public boolean removeAll(Collection<?> c) { |
447 | 1 | throw new UnsupportedOperationException(); |
448 | |
} |
449 | |
|
450 | |
@Override |
451 | |
public boolean retainAll(Collection<?> c) { |
452 | 1 | throw new UnsupportedOperationException(); |
453 | |
} |
454 | |
|
455 | |
@Override |
456 | |
public int size() { |
457 | 1 | return HeaderValuesMap.this.size(); |
458 | |
} |
459 | |
|
460 | |
@Override |
461 | |
public Object[] toArray() { |
462 | 1 | return toList().toArray(); |
463 | |
} |
464 | |
|
465 | |
@Override |
466 | |
public <T> T[] toArray(T[] a) { |
467 | 1 | return toList().toArray(a); |
468 | |
} |
469 | |
|
470 | |
|
471 | |
|
472 | |
|
473 | |
|
474 | |
|
475 | |
private List<String[]> toList() { |
476 | 2 | List<String[]> entries = new ArrayList<String[]>(); |
477 | 2 | Enumeration<String> names = request.getKeys(); |
478 | 6 | while (names.hasMoreElements()) { |
479 | 4 | entries.add(enumeration2array(request.getValues(names.nextElement()))); |
480 | |
} |
481 | 2 | return entries; |
482 | |
} |
483 | |
|
484 | |
|
485 | |
|
486 | |
|
487 | |
|
488 | |
|
489 | |
|
490 | |
private String[] enumeration2array(Enumeration<String> enumeration) { |
491 | 5 | List<String> list1 = new ArrayList<String>(); |
492 | 13 | while (enumeration.hasMoreElements()) { |
493 | 8 | list1.add(enumeration.nextElement()); |
494 | |
} |
495 | |
|
496 | 5 | return list1.toArray(new String[list1.size()]); |
497 | |
} |
498 | |
|
499 | |
|
500 | |
|
501 | |
|
502 | 16 | private class HeaderValuesCollectionIterator implements Iterator<String[]> { |
503 | |
|
504 | |
|
505 | |
|
506 | |
|
507 | 2 | private Enumeration<String> namesEnumeration = request.getKeys(); |
508 | |
|
509 | |
@Override |
510 | |
public boolean hasNext() { |
511 | 1 | return namesEnumeration.hasMoreElements(); |
512 | |
} |
513 | |
|
514 | |
@Override |
515 | |
public String[] next() { |
516 | 1 | return enumeration2array(request.getValues(namesEnumeration.nextElement())); |
517 | |
} |
518 | |
|
519 | |
@Override |
520 | |
public void remove() { |
521 | 1 | throw new UnsupportedOperationException(); |
522 | |
} |
523 | |
} |
524 | |
} |
525 | |
} |