1 21 22 package org.armedbear.j; 23 24 import java.util.Vector ; 25 import java.net.URL ; 26 27 public final class Cookie 28 { 29 private static Vector cookies; 30 31 private String name; 32 private String value; 33 private String domain; 34 private String path; 35 private String expires; 36 private boolean secure; 37 38 private Cookie() 39 { 40 } 41 42 public static void setCookie(URL url, String s) 43 { 44 Cookie cookie = new Cookie(); 45 String remaining = s.trim(); 46 while (remaining.length() > 0) { 47 int index = remaining.indexOf('='); 48 if (index < 0) { 49 cookie.set(remaining, null); 50 break; 51 } 52 String key = remaining.substring(0, index); 53 remaining = remaining.substring(index + 1); 54 index = remaining.indexOf(';'); 55 String value; 56 if (index < 0) { 57 value = remaining; 58 cookie.set(key, value); 59 break; 60 } else { 61 value = remaining.substring(0, index); 62 cookie.set(key, value); 63 remaining = remaining.substring(index + 1).trim(); 64 } 65 } 66 if (cookie.domain == null) 67 cookie.domain = url.getHost(); 68 if (cookie.path == null) { 69 String file = url.getFile(); 71 int index = file.lastIndexOf('?'); 72 if (index >= 0) 73 cookie.path = file.substring(0, index); 74 else 75 cookie.path = file; 76 } 77 if (cookie.isValid()) 78 addCookie(cookie); 79 } 80 81 public static String getCookie(URL url) 83 { 84 if (cookies == null) 85 return null; 86 String host = url.getHost(); 87 String file = url.getFile(); 89 int index = file.lastIndexOf('?'); 90 String path = index >= 0 ? file.substring(0, index) : file; 91 FastStringBuffer sb = new FastStringBuffer(256); 92 for (int i = cookies.size()-1; i >= 0; i--) { 93 Cookie cookie = (Cookie) cookies.get(i); 94 if (cookie.domain != null && host.endsWith(cookie.domain)) { 95 if (cookie.path != null && path.startsWith(cookie.path)) { 96 if (sb.length() > 0) 97 sb.append("; "); 98 sb.append(cookie.name); 99 sb.append('='); 100 sb.append(cookie.value); 101 } 102 } 103 } 104 if (sb.length() == 0) 105 return null; 106 return sb.toString(); 107 } 108 109 public static void deleteCookies() 110 { 111 cookies = null; 112 } 113 114 private static void addCookie(Cookie cookie) 115 { 116 if (cookies != null) { 117 for (int i = cookies.size()-1; i >= 0; i--) { 118 Cookie c = (Cookie) cookies.get(i); 119 if (c.domain.equals(cookie.domain) && 120 c.path.equals(cookie.path) && 121 c.name.equals(cookie.name)) { 122 c.value = cookie.value; 125 return; 126 } 127 } 128 } else 129 cookies = new Vector (); 130 cookies.add(cookie); 131 } 132 133 private void set(String key, String s) 134 { 135 if (key.equals("domain")) 136 domain = s; 137 else if (key.equals("path")) 138 path = s; 139 else if (key.equals("expires")) 140 expires = s; 141 else if (key.equals("secure")) 142 secure = true; 143 else { 144 name = key; 145 value = s; 146 } 147 } 148 149 private boolean isValid() 150 { 151 if (name != null && name.length() > 0) 152 if (value != null && value.length() > 0) 153 return true; 154 return false; 155 } 156 } 157 | Popular Tags |