51 |
* |
* |
52 |
* @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> |
* @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> |
53 |
*/ |
*/ |
54 |
public class SimpleCookieManager implements CookieManager |
public class SimpleCookieManager |
55 |
|
implements CookieManager |
56 |
{ |
{ |
57 |
|
|
58 |
/** |
/** |
64 |
/** |
/** |
65 |
* Constructor. |
* Constructor. |
66 |
*/ |
*/ |
67 |
public SimpleCookieManager () |
public SimpleCookieManager() |
68 |
{ |
{ |
69 |
cookies = new HashMap (); |
cookies = new HashMap(); |
70 |
} |
} |
71 |
|
|
72 |
public void setCookie (Cookie cookie) |
public void setCookie(Cookie cookie) |
73 |
{ |
{ |
74 |
String domain = cookie.getDomain (); |
String domain = cookie.getDomain(); |
75 |
Map map = (Map) cookies.get (domain); |
Map map =(Map) cookies.get(domain); |
76 |
if (map == null) |
if (map == null) |
77 |
{ |
{ |
78 |
map = new HashMap (); |
map = new HashMap(); |
79 |
cookies.put (domain, map); |
cookies.put(domain, map); |
80 |
} |
} |
81 |
String name = cookie.getName (); |
String name = cookie.getName(); |
82 |
map.put (name, cookie); // will replace a cookie of the same name |
map.put(name, cookie); // will replace a cookie of the same name |
83 |
} |
} |
84 |
|
|
85 |
public Cookie[] getCookies (String host, boolean secure, String path) |
public Cookie[] getCookies(String host, boolean secure, String path) |
86 |
{ |
{ |
87 |
List matches = new ArrayList (); |
List matches = new ArrayList(); |
88 |
Date now = new Date (); |
Date now = new Date(); |
89 |
if (Character.isLetter (host.charAt (0))) |
if (Character.isLetter(host.charAt(0))) |
90 |
{ |
{ |
91 |
int di = host.indexOf ('.'); |
int di = host.indexOf('.'); |
92 |
while (di != -1) |
while (di != -1) |
93 |
{ |
{ |
94 |
addCookies (matches, host, secure, path, now); |
addCookies(matches, host, secure, path, now); |
95 |
host = host.substring (di); |
host = host.substring(di); |
96 |
di = host.indexOf ('.', 1); |
di = host.indexOf('.', 1); |
97 |
} |
} |
98 |
} |
} |
99 |
addCookies (matches, host, secure, path, now); |
addCookies(matches, host, secure, path, now); |
100 |
Cookie[] ret = new Cookie[matches.size ()]; |
Cookie[] ret = new Cookie[matches.size()]; |
101 |
matches.toArray (ret); |
matches.toArray(ret); |
102 |
return ret; |
return ret; |
103 |
} |
} |
104 |
|
|
105 |
private void addCookies(List matches, String domain, boolean secure, |
private void addCookies(List matches, String domain, boolean secure, |
106 |
String path, Date now) |
String path, Date now) |
107 |
{ |
{ |
108 |
Map map = (Map) cookies.get (domain); |
Map map = (Map) cookies.get(domain); |
109 |
if (map != null) |
if (map != null) |
110 |
{ |
{ |
111 |
List expired = new ArrayList (); |
List expired = new ArrayList(); |
112 |
for (Iterator i = map.entrySet ().iterator (); i.hasNext (); ) |
for (Iterator i = map.entrySet().iterator(); i.hasNext(); ) |
113 |
{ |
{ |
114 |
Map.Entry entry = (Map.Entry) i.next (); |
Map.Entry entry = (Map.Entry) i.next(); |
115 |
Cookie cookie = (Cookie) entry.getValue (); |
Cookie cookie = (Cookie) entry.getValue(); |
116 |
Date expires = cookie.getExpiryDate (); |
Date expires = cookie.getExpiryDate(); |
117 |
if (expires != null && expires.before (now)) |
if (expires != null && expires.before(now)) |
118 |
{ |
{ |
119 |
expired.add (entry.getKey ()); |
expired.add(entry.getKey()); |
120 |
continue; |
continue; |
121 |
} |
} |
122 |
if (secure && !cookie.isSecure ()) |
if (secure && !cookie.isSecure()) |
123 |
{ |
{ |
124 |
continue; |
continue; |
125 |
} |
} |
126 |
if (path.startsWith (cookie.getPath ())) |
if (path.startsWith(cookie.getPath())) |
127 |
{ |
{ |
128 |
matches.add (cookie); |
matches.add(cookie); |
129 |
} |
} |
130 |
} |
} |
131 |
// Good housekeeping |
// Good housekeeping |
132 |
for (Iterator i = expired.iterator (); i.hasNext (); ) |
for (Iterator i = expired.iterator(); i.hasNext(); ) |
133 |
{ |
{ |
134 |
map.remove (i.next ()); |
map.remove(i.next()); |
135 |
} |
} |
136 |
} |
} |
137 |
} |
} |