author | chegar |
Tue, 04 Oct 2011 13:48:41 +0100 | |
changeset 10698 | b2da906f8080 |
parent 5506 | 202f599c92aa |
child 12192 | dd8301ce22b6 |
permissions | -rw-r--r-- |
3858 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved. |
3858 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
3858 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
3858 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
3858 | 24 |
*/ |
25 |
||
26 |
package java.net; |
|
27 |
||
28 |
import java.net.URI; |
|
29 |
import java.net.CookieStore; |
|
30 |
import java.net.HttpCookie; |
|
31 |
import java.net.URISyntaxException; |
|
32 |
import java.util.List; |
|
33 |
import java.util.Map; |
|
34 |
import java.util.ArrayList; |
|
35 |
import java.util.HashMap; |
|
36 |
import java.util.Collections; |
|
37 |
import java.util.Iterator; |
|
38 |
import java.util.concurrent.locks.ReentrantLock; |
|
39 |
||
40 |
/** |
|
41 |
* A simple in-memory java.net.CookieStore implementation |
|
42 |
* |
|
43 |
* @author Edward Wang |
|
44 |
* @since 1.6 |
|
45 |
*/ |
|
46 |
class InMemoryCookieStore implements CookieStore { |
|
47 |
// the in-memory representation of cookies |
|
48 |
private List<HttpCookie> cookieJar = null; |
|
49 |
||
50 |
// the cookies are indexed by its domain and associated uri (if present) |
|
51 |
// CAUTION: when a cookie removed from main data structure (i.e. cookieJar), |
|
52 |
// it won't be cleared in domainIndex & uriIndex. Double-check the |
|
53 |
// presence of cookie when retrieve one form index store. |
|
54 |
private Map<String, List<HttpCookie>> domainIndex = null; |
|
55 |
private Map<URI, List<HttpCookie>> uriIndex = null; |
|
56 |
||
57 |
// use ReentrantLock instead of syncronized for scalability |
|
58 |
private ReentrantLock lock = null; |
|
59 |
||
60 |
||
61 |
/** |
|
62 |
* The default ctor |
|
63 |
*/ |
|
64 |
public InMemoryCookieStore() { |
|
65 |
cookieJar = new ArrayList<HttpCookie>(); |
|
66 |
domainIndex = new HashMap<String, List<HttpCookie>>(); |
|
67 |
uriIndex = new HashMap<URI, List<HttpCookie>>(); |
|
68 |
||
69 |
lock = new ReentrantLock(false); |
|
70 |
} |
|
71 |
||
72 |
/** |
|
73 |
* Add one cookie into cookie store. |
|
74 |
*/ |
|
75 |
public void add(URI uri, HttpCookie cookie) { |
|
76 |
// pre-condition : argument can't be null |
|
77 |
if (cookie == null) { |
|
78 |
throw new NullPointerException("cookie is null"); |
|
79 |
} |
|
80 |
||
81 |
||
82 |
lock.lock(); |
|
83 |
try { |
|
84 |
// remove the ole cookie if there has had one |
|
85 |
cookieJar.remove(cookie); |
|
86 |
||
87 |
// add new cookie if it has a non-zero max-age |
|
88 |
if (cookie.getMaxAge() != 0) { |
|
89 |
cookieJar.add(cookie); |
|
90 |
// and add it to domain index |
|
91 |
if (cookie.getDomain() != null) { |
|
92 |
addIndex(domainIndex, cookie.getDomain(), cookie); |
|
93 |
} |
|
10698
b2da906f8080
6953455: CookieStore.add() cannot handle null URI parameter, contrary to the API
chegar
parents:
5506
diff
changeset
|
94 |
if (uri != null) { |
b2da906f8080
6953455: CookieStore.add() cannot handle null URI parameter, contrary to the API
chegar
parents:
5506
diff
changeset
|
95 |
// add it to uri index, too |
b2da906f8080
6953455: CookieStore.add() cannot handle null URI parameter, contrary to the API
chegar
parents:
5506
diff
changeset
|
96 |
addIndex(uriIndex, getEffectiveURI(uri), cookie); |
b2da906f8080
6953455: CookieStore.add() cannot handle null URI parameter, contrary to the API
chegar
parents:
5506
diff
changeset
|
97 |
} |
3858 | 98 |
} |
99 |
} finally { |
|
100 |
lock.unlock(); |
|
101 |
} |
|
102 |
} |
|
103 |
||
104 |
||
105 |
/** |
|
106 |
* Get all cookies, which: |
|
107 |
* 1) given uri domain-matches with, or, associated with |
|
108 |
* given uri when added to the cookie store. |
|
109 |
* 3) not expired. |
|
110 |
* See RFC 2965 sec. 3.3.4 for more detail. |
|
111 |
*/ |
|
112 |
public List<HttpCookie> get(URI uri) { |
|
113 |
// argument can't be null |
|
114 |
if (uri == null) { |
|
115 |
throw new NullPointerException("uri is null"); |
|
116 |
} |
|
117 |
||
118 |
List<HttpCookie> cookies = new ArrayList<HttpCookie>(); |
|
119 |
boolean secureLink = "https".equalsIgnoreCase(uri.getScheme()); |
|
120 |
lock.lock(); |
|
121 |
try { |
|
122 |
// check domainIndex first |
|
123 |
getInternal1(cookies, domainIndex, uri.getHost(), secureLink); |
|
124 |
// check uriIndex then |
|
125 |
getInternal2(cookies, uriIndex, getEffectiveURI(uri), secureLink); |
|
126 |
} finally { |
|
127 |
lock.unlock(); |
|
128 |
} |
|
129 |
||
130 |
return cookies; |
|
131 |
} |
|
132 |
||
133 |
/** |
|
134 |
* Get all cookies in cookie store, except those have expired |
|
135 |
*/ |
|
136 |
public List<HttpCookie> getCookies() { |
|
137 |
List<HttpCookie> rt; |
|
138 |
||
139 |
lock.lock(); |
|
140 |
try { |
|
141 |
Iterator<HttpCookie> it = cookieJar.iterator(); |
|
142 |
while (it.hasNext()) { |
|
143 |
if (it.next().hasExpired()) { |
|
144 |
it.remove(); |
|
145 |
} |
|
146 |
} |
|
147 |
} finally { |
|
148 |
rt = Collections.unmodifiableList(cookieJar); |
|
149 |
lock.unlock(); |
|
150 |
} |
|
151 |
||
152 |
return rt; |
|
153 |
} |
|
154 |
||
155 |
/** |
|
156 |
* Get all URIs, which are associated with at least one cookie |
|
157 |
* of this cookie store. |
|
158 |
*/ |
|
159 |
public List<URI> getURIs() { |
|
160 |
List<URI> uris = new ArrayList<URI>(); |
|
161 |
||
162 |
lock.lock(); |
|
163 |
try { |
|
164 |
Iterator<URI> it = uriIndex.keySet().iterator(); |
|
165 |
while (it.hasNext()) { |
|
166 |
URI uri = it.next(); |
|
167 |
List<HttpCookie> cookies = uriIndex.get(uri); |
|
168 |
if (cookies == null || cookies.size() == 0) { |
|
169 |
// no cookies list or an empty list associated with |
|
170 |
// this uri entry, delete it |
|
171 |
it.remove(); |
|
172 |
} |
|
173 |
} |
|
174 |
} finally { |
|
175 |
uris.addAll(uriIndex.keySet()); |
|
176 |
lock.unlock(); |
|
177 |
} |
|
178 |
||
179 |
return uris; |
|
180 |
} |
|
181 |
||
182 |
||
183 |
/** |
|
184 |
* Remove a cookie from store |
|
185 |
*/ |
|
186 |
public boolean remove(URI uri, HttpCookie ck) { |
|
187 |
// argument can't be null |
|
188 |
if (ck == null) { |
|
189 |
throw new NullPointerException("cookie is null"); |
|
190 |
} |
|
191 |
||
192 |
boolean modified = false; |
|
193 |
lock.lock(); |
|
194 |
try { |
|
195 |
modified = cookieJar.remove(ck); |
|
196 |
} finally { |
|
197 |
lock.unlock(); |
|
198 |
} |
|
199 |
||
200 |
return modified; |
|
201 |
} |
|
202 |
||
203 |
||
204 |
/** |
|
205 |
* Remove all cookies in this cookie store. |
|
206 |
*/ |
|
207 |
public boolean removeAll() { |
|
208 |
lock.lock(); |
|
209 |
try { |
|
210 |
cookieJar.clear(); |
|
211 |
domainIndex.clear(); |
|
212 |
uriIndex.clear(); |
|
213 |
} finally { |
|
214 |
lock.unlock(); |
|
215 |
} |
|
216 |
||
217 |
return true; |
|
218 |
} |
|
219 |
||
220 |
||
221 |
/* ---------------- Private operations -------------- */ |
|
222 |
||
223 |
||
224 |
/* |
|
225 |
* This is almost the same as HttpCookie.domainMatches except for |
|
226 |
* one difference: It won't reject cookies when the 'H' part of the |
|
227 |
* domain contains a dot ('.'). |
|
228 |
* I.E.: RFC 2965 section 3.3.2 says that if host is x.y.domain.com |
|
229 |
* and the cookie domain is .domain.com, then it should be rejected. |
|
230 |
* However that's not how the real world works. Browsers don't reject and |
|
231 |
* some sites, like yahoo.com do actually expect these cookies to be |
|
232 |
* passed along. |
|
233 |
* And should be used for 'old' style cookies (aka Netscape type of cookies) |
|
234 |
*/ |
|
235 |
private boolean netscapeDomainMatches(String domain, String host) |
|
236 |
{ |
|
237 |
if (domain == null || host == null) { |
|
238 |
return false; |
|
239 |
} |
|
240 |
||
241 |
// if there's no embedded dot in domain and domain is not .local |
|
242 |
boolean isLocalDomain = ".local".equalsIgnoreCase(domain); |
|
243 |
int embeddedDotInDomain = domain.indexOf('.'); |
|
244 |
if (embeddedDotInDomain == 0) { |
|
245 |
embeddedDotInDomain = domain.indexOf('.', 1); |
|
246 |
} |
|
247 |
if (!isLocalDomain && (embeddedDotInDomain == -1 || embeddedDotInDomain == domain.length() - 1)) { |
|
248 |
return false; |
|
249 |
} |
|
250 |
||
251 |
// if the host name contains no dot and the domain name is .local |
|
252 |
int firstDotInHost = host.indexOf('.'); |
|
253 |
if (firstDotInHost == -1 && isLocalDomain) { |
|
254 |
return true; |
|
255 |
} |
|
256 |
||
257 |
int domainLength = domain.length(); |
|
258 |
int lengthDiff = host.length() - domainLength; |
|
259 |
if (lengthDiff == 0) { |
|
260 |
// if the host name and the domain name are just string-compare euqal |
|
261 |
return host.equalsIgnoreCase(domain); |
|
262 |
} else if (lengthDiff > 0) { |
|
263 |
// need to check H & D component |
|
264 |
String H = host.substring(0, lengthDiff); |
|
265 |
String D = host.substring(lengthDiff); |
|
266 |
||
267 |
return (D.equalsIgnoreCase(domain)); |
|
268 |
} else if (lengthDiff == -1) { |
|
269 |
// if domain is actually .host |
|
270 |
return (domain.charAt(0) == '.' && |
|
271 |
host.equalsIgnoreCase(domain.substring(1))); |
|
272 |
} |
|
273 |
||
274 |
return false; |
|
275 |
} |
|
276 |
||
277 |
private void getInternal1(List<HttpCookie> cookies, Map<String, List<HttpCookie>> cookieIndex, |
|
278 |
String host, boolean secureLink) { |
|
279 |
// Use a separate list to handle cookies that need to be removed so |
|
280 |
// that there is no conflict with iterators. |
|
281 |
ArrayList<HttpCookie> toRemove = new ArrayList<HttpCookie>(); |
|
282 |
for (Map.Entry<String, List<HttpCookie>> entry : cookieIndex.entrySet()) { |
|
283 |
String domain = entry.getKey(); |
|
284 |
List<HttpCookie> lst = entry.getValue(); |
|
285 |
for (HttpCookie c : lst) { |
|
286 |
if ((c.getVersion() == 0 && netscapeDomainMatches(domain, host)) || |
|
287 |
(c.getVersion() == 1 && HttpCookie.domainMatches(domain, host))) { |
|
288 |
if ((cookieJar.indexOf(c) != -1)) { |
|
289 |
// the cookie still in main cookie store |
|
290 |
if (!c.hasExpired()) { |
|
291 |
// don't add twice and make sure it's the proper |
|
292 |
// security level |
|
293 |
if ((secureLink || !c.getSecure()) && |
|
294 |
!cookies.contains(c)) { |
|
295 |
cookies.add(c); |
|
296 |
} |
|
297 |
} else { |
|
298 |
toRemove.add(c); |
|
299 |
} |
|
300 |
} else { |
|
301 |
// the cookie has beed removed from main store, |
|
302 |
// so also remove it from domain indexed store |
|
303 |
toRemove.add(c); |
|
304 |
} |
|
305 |
} |
|
306 |
} |
|
307 |
// Clear up the cookies that need to be removed |
|
308 |
for (HttpCookie c : toRemove) { |
|
309 |
lst.remove(c); |
|
310 |
cookieJar.remove(c); |
|
311 |
||
312 |
} |
|
313 |
toRemove.clear(); |
|
314 |
} |
|
315 |
} |
|
316 |
||
317 |
// @param cookies [OUT] contains the found cookies |
|
318 |
// @param cookieIndex the index |
|
319 |
// @param comparator the prediction to decide whether or not |
|
320 |
// a cookie in index should be returned |
|
321 |
private <T> void getInternal2(List<HttpCookie> cookies, |
|
322 |
Map<T, List<HttpCookie>> cookieIndex, |
|
323 |
Comparable<T> comparator, boolean secureLink) |
|
324 |
{ |
|
325 |
for (T index : cookieIndex.keySet()) { |
|
326 |
if (comparator.compareTo(index) == 0) { |
|
327 |
List<HttpCookie> indexedCookies = cookieIndex.get(index); |
|
328 |
// check the list of cookies associated with this domain |
|
329 |
if (indexedCookies != null) { |
|
330 |
Iterator<HttpCookie> it = indexedCookies.iterator(); |
|
331 |
while (it.hasNext()) { |
|
332 |
HttpCookie ck = it.next(); |
|
333 |
if (cookieJar.indexOf(ck) != -1) { |
|
334 |
// the cookie still in main cookie store |
|
335 |
if (!ck.hasExpired()) { |
|
336 |
// don't add twice |
|
337 |
if ((secureLink || !ck.getSecure()) && |
|
338 |
!cookies.contains(ck)) |
|
339 |
cookies.add(ck); |
|
340 |
} else { |
|
341 |
it.remove(); |
|
342 |
cookieJar.remove(ck); |
|
343 |
} |
|
344 |
} else { |
|
345 |
// the cookie has beed removed from main store, |
|
346 |
// so also remove it from domain indexed store |
|
347 |
it.remove(); |
|
348 |
} |
|
349 |
} |
|
350 |
} // end of indexedCookies != null |
|
351 |
} // end of comparator.compareTo(index) == 0 |
|
352 |
} // end of cookieIndex iteration |
|
353 |
} |
|
354 |
||
355 |
// add 'cookie' indexed by 'index' into 'indexStore' |
|
356 |
private <T> void addIndex(Map<T, List<HttpCookie>> indexStore, |
|
357 |
T index, |
|
358 |
HttpCookie cookie) |
|
359 |
{ |
|
360 |
if (index != null) { |
|
361 |
List<HttpCookie> cookies = indexStore.get(index); |
|
362 |
if (cookies != null) { |
|
363 |
// there may already have the same cookie, so remove it first |
|
364 |
cookies.remove(cookie); |
|
365 |
||
366 |
cookies.add(cookie); |
|
367 |
} else { |
|
368 |
cookies = new ArrayList<HttpCookie>(); |
|
369 |
cookies.add(cookie); |
|
370 |
indexStore.put(index, cookies); |
|
371 |
} |
|
372 |
} |
|
373 |
} |
|
374 |
||
375 |
||
376 |
// |
|
377 |
// for cookie purpose, the effective uri should only be http://host |
|
378 |
// the path will be taken into account when path-match algorithm applied |
|
379 |
// |
|
380 |
private URI getEffectiveURI(URI uri) { |
|
381 |
URI effectiveURI = null; |
|
382 |
try { |
|
383 |
effectiveURI = new URI("http", |
|
384 |
uri.getHost(), |
|
385 |
null, // path component |
|
386 |
null, // query component |
|
387 |
null // fragment component |
|
388 |
); |
|
389 |
} catch (URISyntaxException ignored) { |
|
390 |
effectiveURI = uri; |
|
391 |
} |
|
392 |
||
393 |
return effectiveURI; |
|
394 |
} |
|
395 |
} |