23 * questions. |
23 * questions. |
24 */ |
24 */ |
25 |
25 |
26 package sun.net.www.protocol.http; |
26 package sun.net.www.protocol.http; |
27 |
27 |
28 import java.io.IOException; |
|
29 import java.net.URL; |
|
30 import java.util.Hashtable; |
|
31 import java.util.LinkedList; |
28 import java.util.LinkedList; |
32 import java.util.ListIterator; |
29 import java.util.ListIterator; |
33 import java.util.Enumeration; |
|
34 import java.util.HashMap; |
30 import java.util.HashMap; |
35 |
31 |
36 /** |
32 /** |
37 * @author Michael McMahon |
33 * @author Michael McMahon |
38 */ |
34 */ |
39 |
35 |
40 public class AuthCacheImpl implements AuthCache { |
36 public class AuthCacheImpl implements AuthCache { |
41 HashMap hashtable; |
37 HashMap<String,LinkedList<AuthCacheValue>> hashtable; |
42 |
38 |
43 public AuthCacheImpl () { |
39 public AuthCacheImpl () { |
44 hashtable = new HashMap (); |
40 hashtable = new HashMap<String,LinkedList<AuthCacheValue>>(); |
45 } |
41 } |
46 |
42 |
47 public void setMap (HashMap map) { |
43 public void setMap (HashMap<String,LinkedList<AuthCacheValue>> map) { |
48 hashtable = map; |
44 hashtable = map; |
49 } |
45 } |
50 |
46 |
51 // put a value in map according to primary key + secondary key which |
47 // put a value in map according to primary key + secondary key which |
52 // is the path field of AuthenticationInfo |
48 // is the path field of AuthenticationInfo |
53 |
49 |
54 public synchronized void put (String pkey, AuthCacheValue value) { |
50 public synchronized void put (String pkey, AuthCacheValue value) { |
55 LinkedList list = (LinkedList) hashtable.get (pkey); |
51 LinkedList<AuthCacheValue> list = hashtable.get (pkey); |
56 String skey = value.getPath(); |
52 String skey = value.getPath(); |
57 if (list == null) { |
53 if (list == null) { |
58 list = new LinkedList (); |
54 list = new LinkedList<AuthCacheValue>(); |
59 hashtable.put (pkey, list); |
55 hashtable.put(pkey, list); |
60 } |
56 } |
61 // Check if the path already exists or a super-set of it exists |
57 // Check if the path already exists or a super-set of it exists |
62 ListIterator iter = list.listIterator(); |
58 ListIterator<AuthCacheValue> iter = list.listIterator(); |
63 while (iter.hasNext()) { |
59 while (iter.hasNext()) { |
64 AuthenticationInfo inf = (AuthenticationInfo)iter.next(); |
60 AuthenticationInfo inf = (AuthenticationInfo)iter.next(); |
65 if (inf.path == null || inf.path.startsWith (skey)) { |
61 if (inf.path == null || inf.path.startsWith (skey)) { |
66 iter.remove (); |
62 iter.remove (); |
67 } |
63 } |
68 } |
64 } |
69 iter.add (value); |
65 iter.add(value); |
70 } |
66 } |
71 |
67 |
72 // get a value from map checking both primary |
68 // get a value from map checking both primary |
73 // and secondary (urlpath) key |
69 // and secondary (urlpath) key |
74 |
70 |
75 public synchronized AuthCacheValue get (String pkey, String skey) { |
71 public synchronized AuthCacheValue get (String pkey, String skey) { |
76 AuthenticationInfo result = null; |
72 AuthenticationInfo result = null; |
77 LinkedList list = (LinkedList) hashtable.get (pkey); |
73 LinkedList<AuthCacheValue> list = hashtable.get (pkey); |
78 if (list == null || list.size() == 0) { |
74 if (list == null || list.size() == 0) { |
79 return null; |
75 return null; |
80 } |
76 } |
81 if (skey == null) { |
77 if (skey == null) { |
82 // list should contain only one element |
78 // list should contain only one element |
83 return (AuthenticationInfo)list.get (0); |
79 return (AuthenticationInfo)list.get (0); |
84 } |
80 } |
85 ListIterator iter = list.listIterator(); |
81 ListIterator<AuthCacheValue> iter = list.listIterator(); |
86 while (iter.hasNext()) { |
82 while (iter.hasNext()) { |
87 AuthenticationInfo inf = (AuthenticationInfo)iter.next(); |
83 AuthenticationInfo inf = (AuthenticationInfo)iter.next(); |
88 if (skey.startsWith (inf.path)) { |
84 if (skey.startsWith (inf.path)) { |
89 return inf; |
85 return inf; |
90 } |
86 } |
91 } |
87 } |
92 return null; |
88 return null; |
93 } |
89 } |
94 |
90 |
95 public synchronized void remove (String pkey, AuthCacheValue entry) { |
91 public synchronized void remove (String pkey, AuthCacheValue entry) { |
96 LinkedList list = (LinkedList) hashtable.get (pkey); |
92 LinkedList<AuthCacheValue> list = hashtable.get (pkey); |
97 if (list == null) { |
93 if (list == null) { |
98 return; |
94 return; |
99 } |
95 } |
100 if (entry == null) { |
96 if (entry == null) { |
101 list.clear(); |
97 list.clear(); |
102 return; |
98 return; |
103 } |
99 } |
104 ListIterator iter = list.listIterator (); |
100 ListIterator<AuthCacheValue> iter = list.listIterator (); |
105 while (iter.hasNext()) { |
101 while (iter.hasNext()) { |
106 AuthenticationInfo inf = (AuthenticationInfo)iter.next(); |
102 AuthenticationInfo inf = (AuthenticationInfo)iter.next(); |
107 if (entry.equals(inf)) { |
103 if (entry.equals(inf)) { |
108 iter.remove (); |
104 iter.remove (); |
109 } |
105 } |