1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.danube.http.cookie;
14
15 import java.text.ParseException;
16 import java.text.SimpleDateFormat;
17 import java.util.Date;
18
19
20
21
22
23
24 public class Cookie {
25
26
27 public static final SimpleDateFormat cookieFormat = new SimpleDateFormat("EEE dd-MMM-yyyy HH:mm:ss");
28
29
30 protected String name;
31
32
33 protected String value;
34
35
36 protected Date expires;
37
38
39 protected String domain;
40
41
42 protected String path;
43
44
45 protected boolean secure = false;
46
47
48
49
50 public Cookie() {
51 }
52
53
54
55
56
57
58 public Cookie(String header) throws ParseException {
59 parseCookie(header);
60 }
61
62
63
64
65
66 public String getDomain() {
67 return domain;
68 }
69
70
71
72
73
74 public void setDomain(String domain) {
75 this.domain = domain;
76 }
77
78
79
80
81
82 public Date getExpires() {
83 return expires;
84 }
85
86
87
88
89
90 public void setExpires(Date expires) {
91 this.expires = expires;
92 }
93
94
95
96
97
98 public String getName() {
99 return name;
100 }
101
102
103
104
105
106 public void setName(String name) {
107 this.name = name;
108 }
109
110
111
112
113
114 public String getPath() {
115 return path;
116 }
117
118
119
120
121
122 public void setPath(String path) {
123 this.path = path;
124 }
125
126
127
128
129
130 public boolean isSecure() {
131 return secure;
132 }
133
134
135
136
137
138 public void setSecure(boolean secure) {
139 this.secure = secure;
140 }
141
142
143
144
145
146 public String getValue() {
147 return value;
148 }
149
150
151
152
153
154 public void setValue(String value) {
155 this.value = value;
156 }
157
158
159
160
161
162 public String toString() {
163 StringBuffer res = new StringBuffer();
164
165 res.append(name).append('=').append(value);
166
167 if (getExpires() != null) {
168
169
170
171 res.append("; expires=" + cookieFormat.format(getExpires()+" GMT"));
172
173 }
174
175 if (getPath() != null) {
176
177 res.append("; path=" + getPath());
178
179 }
180
181 if (getDomain() != null) {
182
183 res.append("; domain=" + getDomain());
184
185 }
186
187 if (isSecure()) {
188
189 res.append("; secure");
190
191 }
192 return res.toString();
193 }
194
195
196
197
198
199
200
201 protected void parseCookie(String header) throws ParseException {
202 int i = header.indexOf('=');
203 if (i < 0) {
204 throw new ParseException("Missing '='", -1);
205 }
206 setName(header.substring(0, i));
207 setValue(header.substring(i+1).trim());
208 }
209
210 }