1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.context.servlet; |
20 | |
|
21 | |
import java.util.Enumeration; |
22 | |
import java.util.Map; |
23 | |
import java.util.NoSuchElementException; |
24 | |
|
25 | |
import javax.servlet.http.Cookie; |
26 | |
import javax.servlet.http.HttpServletRequest; |
27 | |
|
28 | |
import org.apache.myfaces.util.AbstractAttributeMap; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | 0 | public final class CookieMap extends AbstractAttributeMap<Cookie> |
38 | |
{ |
39 | 0 | private static final Cookie[] EMPTY_ARRAY = new Cookie[0]; |
40 | |
|
41 | |
private final HttpServletRequest _httpServletRequest; |
42 | |
|
43 | |
CookieMap(final HttpServletRequest httpServletRequest) |
44 | 0 | { |
45 | 0 | _httpServletRequest = httpServletRequest; |
46 | 0 | } |
47 | |
|
48 | |
public void clear() |
49 | |
{ |
50 | 0 | throw new UnsupportedOperationException( |
51 | |
"Cannot clear HttpRequest Cookies"); |
52 | |
} |
53 | |
|
54 | |
@Override |
55 | |
public boolean containsValue(final Object findValue) |
56 | |
{ |
57 | 0 | if (findValue == null) |
58 | |
{ |
59 | 0 | return false; |
60 | |
} |
61 | |
|
62 | 0 | final Cookie[] cookies = _httpServletRequest.getCookies(); |
63 | 0 | if (cookies == null) |
64 | 0 | return false; |
65 | 0 | for (int i = 0, len = cookies.length; i < len; i++) |
66 | |
{ |
67 | 0 | if (findValue.equals(cookies[i])) |
68 | |
{ |
69 | 0 | return true; |
70 | |
} |
71 | |
} |
72 | |
|
73 | 0 | return false; |
74 | |
} |
75 | |
|
76 | |
@Override |
77 | |
public boolean isEmpty() |
78 | |
{ |
79 | 0 | final Cookie[] cookies = _httpServletRequest.getCookies(); |
80 | 0 | return cookies == null || cookies.length == 0; |
81 | |
} |
82 | |
|
83 | |
@Override |
84 | |
public int size() |
85 | |
{ |
86 | 0 | final Cookie[] cookies = _httpServletRequest.getCookies(); |
87 | 0 | return cookies == null ? 0 : cookies.length; |
88 | |
} |
89 | |
|
90 | |
@Override |
91 | |
public void putAll(final Map t) |
92 | |
{ |
93 | 0 | throw new UnsupportedOperationException(); |
94 | |
} |
95 | |
|
96 | |
@Override |
97 | |
protected Cookie getAttribute(final String key) |
98 | |
{ |
99 | 0 | final Cookie[] cookies = _httpServletRequest.getCookies(); |
100 | 0 | if (cookies == null) |
101 | 0 | return null; |
102 | 0 | for (int i = 0, len = cookies.length; i < len; i++) |
103 | |
{ |
104 | 0 | if (cookies[i].getName().equals(key)) |
105 | |
{ |
106 | 0 | return cookies[i]; |
107 | |
} |
108 | |
} |
109 | |
|
110 | 0 | return null; |
111 | |
} |
112 | |
|
113 | |
protected void setAttribute(final String key, final Cookie value) |
114 | |
{ |
115 | 0 | throw new UnsupportedOperationException( |
116 | |
"Cannot set HttpRequest Cookies"); |
117 | |
} |
118 | |
|
119 | |
protected void removeAttribute(final String key) |
120 | |
{ |
121 | 0 | throw new UnsupportedOperationException( |
122 | |
"Cannot remove HttpRequest Cookies"); |
123 | |
} |
124 | |
|
125 | |
protected Enumeration<String> getAttributeNames() |
126 | |
{ |
127 | 0 | final Cookie[] cookies = _httpServletRequest.getCookies(); |
128 | |
|
129 | 0 | return cookies == null ? new CookieNameEnumeration(EMPTY_ARRAY) : new CookieNameEnumeration(cookies); |
130 | |
|
131 | |
} |
132 | |
|
133 | 0 | private static class CookieNameEnumeration implements Enumeration<String> |
134 | |
{ |
135 | |
private final Cookie[] _cookies; |
136 | |
private final int _length; |
137 | |
private int _index; |
138 | |
|
139 | |
public CookieNameEnumeration(final Cookie[] cookies) |
140 | 0 | { |
141 | 0 | _cookies = cookies; |
142 | 0 | _length = cookies.length; |
143 | 0 | } |
144 | |
|
145 | |
public boolean hasMoreElements() |
146 | |
{ |
147 | 0 | return _index < _length; |
148 | |
} |
149 | |
|
150 | |
public String nextElement() |
151 | |
{ |
152 | 0 | if (!hasMoreElements()) |
153 | |
{ |
154 | 0 | throw new NoSuchElementException(); |
155 | |
} |
156 | 0 | return _cookies[_index++].getName(); |
157 | |
} |
158 | |
} |
159 | |
} |