KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > Cookie


1 /*
2  * Cookie.java
3  *
4  * Copyright (C) 1998-2002 Peter Graves
5  * $Id: Cookie.java,v 1.1.1.1 2002/09/24 16:09:17 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.util.Vector JavaDoc;
25 import java.net.URL JavaDoc;
26
27 public final class Cookie
28 {
29     private static Vector JavaDoc cookies;
30
31     private String JavaDoc name;
32     private String JavaDoc value;
33     private String JavaDoc domain;
34     private String JavaDoc path;
35     private String JavaDoc expires;
36     private boolean secure;
37
38     private Cookie()
39     {
40     }
41
42     public static void setCookie(URL JavaDoc url, String JavaDoc s)
43     {
44         Cookie cookie = new Cookie();
45         String JavaDoc 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 JavaDoc key = remaining.substring(0, index);
53             remaining = remaining.substring(index + 1);
54             index = remaining.indexOf(';');
55             String JavaDoc 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             // URL.getPath() is only available in Java 1.3!
70
String JavaDoc 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     // BUG! Cookies with more specific path mappings should be sent first.
82
public static String JavaDoc getCookie(URL JavaDoc url)
83     {
84         if (cookies == null)
85             return null;
86         String JavaDoc host = url.getHost();
87         // URL.getPath() is only available in Java 1.3!
88
String JavaDoc file = url.getFile();
89         int index = file.lastIndexOf('?');
90         String JavaDoc 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                     // BUG! Should delete cookie here if new cookie's
123
// expiration time is in the past.
124
c.value = cookie.value;
125                     return;
126                 }
127             }
128         } else
129             cookies = new Vector JavaDoc();
130         cookies.add(cookie);
131     }
132
133     private void set(String JavaDoc key, String JavaDoc 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