KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > http > Cookie


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.http;
21
22 import java.text.ParseException JavaDoc;
23 import java.text.SimpleDateFormat JavaDoc;
24 import java.util.Date JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26
27 /**
28  *
29  * @author Lee David Painter <a HREF="mailto:lee@3sp.com">&lt;lee@3sp.com&gt;</a>
30  */

31 public class Cookie {
32
33     String JavaDoc name;
34     String JavaDoc value;
35     String JavaDoc path = ""; //$NON-NLS-1$
36
Date JavaDoc expires = null;
37     String JavaDoc domain = ""; //$NON-NLS-1$
38
boolean secure = false;
39
40     static SimpleDateFormat JavaDoc format = new SimpleDateFormat JavaDoc("EEE, DD-MMM-yyyy HH:mm:ss z"); //$NON-NLS-1$
41

42     public Cookie(String JavaDoc setCookieHeaderValue) {
43
44         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(setCookieHeaderValue, ";"); //$NON-NLS-1$
45
while (tokens.hasMoreTokens()) {
46             String JavaDoc pair = tokens.nextToken();
47             int idx = pair.indexOf('=');
48             if (idx > -1) {
49                 String JavaDoc name = pair.substring(0, idx).trim();
50                 String JavaDoc value = pair.substring(idx + 1).trim();
51
52                 if (name.equalsIgnoreCase("expires")) { //$NON-NLS-1$
53
try {
54                         Integer.parseInt(value);
55                     } catch (NumberFormatException JavaDoc ex) {
56                         try {
57                             expires = format.parse(value);
58                         } catch (ParseException JavaDoc ex2) {
59
60                         }
61                     }
62                 } else if (name.equalsIgnoreCase("path")) { //$NON-NLS-1$
63
this.path = value;
64                 } else if (name.equalsIgnoreCase("domain")) { //$NON-NLS-1$
65
this.domain = path;
66                 } else {
67                     this.name = name;
68                     this.value = value;
69                 }
70             } else if (pair.trim().equalsIgnoreCase("secure")) //$NON-NLS-1$
71
secure = true;
72         }
73
74     }
75
76     public String JavaDoc getPath() {
77         return path;
78     }
79
80     public String JavaDoc getDomain() {
81         return domain;
82     }
83
84     public boolean isSecure() {
85         return secure;
86     }
87
88     public String JavaDoc getName() {
89         return name;
90     }
91
92     public String JavaDoc getValue() {
93         return value;
94     }
95
96     public Date JavaDoc getExpires() {
97         return expires;
98     }
99
100     public String JavaDoc toString() {
101         return name + "=" + value; //$NON-NLS-1$
102

103         /*
104          * + (expires!=null ? format.format(expires) + "; " : "") + (path!=null ?
105          * "path=" + path + "; " : "") + (domain!=null ? "domain=" + domain + "; " :
106          * "") + (secure ? "secure" : "");
107          */

108     }
109 }
110
Popular Tags