1 2 24 25 26 27 28 29 package com.lutris.http; 30 import java.util.Enumeration ; 31 import java.util.Vector ; 32 33 import javax.servlet.http.Cookie ; 34 35 43 public class HttpCookieManager { 44 45 48 private Vector cookies; 49 50 53 public HttpCookieManager () { 54 cookies = new Vector (); 55 } 56 57 67 public String getRequestHeader () { 68 return getRequestHeader(null, null); 69 } 70 71 89 public String getRequestHeader(String path, String domain) { 90 String header = null; 92 Enumeration e = cookies.elements(); 93 while (e.hasMoreElements()) { 94 Cookie c = (Cookie )e.nextElement(); 95 boolean matchPath = true; 96 boolean matchDomain = true; 97 if (path != null) { 98 if (c.getPath() != null) 99 matchPath = c.getPath().startsWith(path); 100 else 101 matchPath = true; 102 } 103 if (domain != null) { 104 if (c.getDomain() != null) 106 matchDomain = c.getDomain().endsWith(domain); 107 else 108 matchDomain = true; 109 } 110 if (matchPath && matchDomain) { 111 if (header == null) 112 header = c.getName() + "=" + c.getValue(); 113 else 114 header += "; " + c.getName() + "=" + c.getValue(); 115 } 116 } 117 return header; 118 } 119 120 135 public String getResponseHeader() { 136 String header = null; 137 Enumeration e = cookies.elements(); 138 while (e.hasMoreElements()) { 139 Cookie c = (Cookie )e.nextElement(); 140 if (header == null) 141 header = c.toString(); 142 else 143 header += ", " + c.toString(); 144 } 145 return header; 146 } 147 148 154 public synchronized void mergeHeader(String header) { 155 if (header == null) { 156 return; 157 } 158 Vector newCookies = HttpCookieParser.parseResponseHeader(header); 159 Cookie [] merge = new Cookie [cookies.size()]; 160 Cookie [] mergeNew = new Cookie [newCookies.size()]; 161 cookies.copyInto(merge); 162 newCookies.copyInto(mergeNew); 163 164 for (int idx=0; idx<merge.length; idx++) { 169 for (int idxNew=0; idxNew<mergeNew.length; idxNew++) { 170 if (testEquality(merge[idx], mergeNew[idxNew])) { 171 merge[idx] = null; 172 break; 173 } 174 } 175 } 176 177 cookies = new Vector (); 181 for (int idx=0; idx<merge.length; idx++) { 182 if (merge[idx] != null) 183 cookies.addElement(merge[idx]); 184 } 185 for (int idx=0; idx<mergeNew.length; idx++) { 186 cookies.addElement(mergeNew[idx]); 187 } 188 } 189 190 197 public Cookie [] getCookies() { 198 Cookie [] cookieTab = new Cookie [cookies.size()]; 199 cookies.copyInto(cookieTab); 200 return cookieTab; 201 } 202 203 206 public synchronized void clear() { 207 cookies.setSize(0); 208 } 209 210 219 public String toString() { 220 return getResponseHeader(); 221 } 222 223 229 230 private boolean testEquality(Cookie c1, Cookie c2) { 231 String name1 = c1.getName(); 232 String name2 = c2.getName(); 233 if ((name1 == null) && (name2 != null)) 234 return false; 235 if ((name1 != null) && (name2 == null)) 236 return false; 237 if ((name1 != null) && (name2 != null) && !name1.equals(name2)) 238 return false; 239 240 String path1 = c1.getPath(); 241 String path2 = c2.getPath(); 242 if ((path1 == null) && (path2 != null)) 243 return false; 244 if ((path1 != null) && (path2 == null)) 245 return false; 246 if ((path1 != null) && (path2 != null) && !path1.equals(path2)) 247 return false; 248 249 String domain1 = c1.getDomain(); 250 String domain2 = c2.getDomain(); 251 if ((domain1 == null) && (domain2 != null)) 252 return false; 253 if ((domain1 != null) && (domain2 == null)) 254 return false; 255 if ((domain1 != null) && (domain2 != null) && !domain1.equals(domain2)) 256 return false; 257 258 return true; 259 } 260 } 261 | Popular Tags |