1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.hc.client5.http.impl.cookie;
29
30 import java.io.Serializable;
31 import java.time.Instant;
32 import java.util.Date;
33 import java.util.HashMap;
34 import java.util.Locale;
35 import java.util.Map;
36
37 import org.apache.hc.client5.http.cookie.SetCookie;
38 import org.apache.hc.client5.http.utils.DateUtils;
39 import org.apache.hc.core5.util.Args;
40
41
42
43
44
45
46 public final class BasicClientCookie implements SetCookie, Cloneable, Serializable {
47
48 private static final long serialVersionUID = -3869795591041535538L;
49
50
51
52
53
54
55
56 public BasicClientCookie(final String name, final String value) {
57 super();
58 Args.notNull(name, "Name");
59 this.name = name;
60 this.attribs = new HashMap<>();
61 this.value = value;
62 }
63
64
65
66
67
68
69 @Override
70 public String getName() {
71 return this.name;
72 }
73
74
75
76
77
78
79 @Override
80 public String getValue() {
81 return this.value;
82 }
83
84
85
86
87
88
89 @Override
90 public void setValue(final String value) {
91 this.value = value;
92 }
93
94
95
96
97
98
99
100
101
102
103
104
105
106 @Deprecated
107 @Override
108 public Date getExpiryDate() {
109 return DateUtils.toDate(cookieExpiryDate);
110 }
111
112
113
114
115 @Override
116 public Instant getExpiryInstant() {
117 return cookieExpiryDate;
118 }
119
120
121
122
123
124
125
126
127
128
129
130
131 @Deprecated
132 @Override
133 public void setExpiryDate (final Date expiryDate) {
134 cookieExpiryDate = DateUtils.toInstant(expiryDate);
135 }
136
137
138
139
140
141
142
143
144
145
146
147 @Override
148 public void setExpiryDate (final Instant expiryInstant) {
149 cookieExpiryDate = expiryInstant;
150 }
151
152
153
154
155
156
157
158
159
160 @Override
161 public boolean isPersistent() {
162 return (null != cookieExpiryDate);
163 }
164
165
166
167
168
169
170
171
172
173 @Override
174 public String getDomain() {
175 return cookieDomain;
176 }
177
178
179
180
181
182
183
184
185 @Override
186 public void setDomain(final String domain) {
187 if (domain != null) {
188 cookieDomain = domain.toLowerCase(Locale.ROOT);
189 } else {
190 cookieDomain = null;
191 }
192 }
193
194
195
196
197
198
199
200
201
202 @Override
203 public String getPath() {
204 return cookiePath;
205 }
206
207
208
209
210
211
212
213
214
215 @Override
216 public void setPath(final String path) {
217 cookiePath = path;
218 }
219
220
221
222
223
224 @Override
225 public boolean isSecure() {
226 return isSecure;
227 }
228
229
230
231
232
233
234
235
236
237
238
239
240
241 @Override
242 public void setSecure (final boolean secure) {
243 isSecure = secure;
244 }
245
246
247
248
249
250
251
252
253
254 @Override
255 public void setHttpOnly(final boolean httpOnly) {
256 this.httpOnly = httpOnly;
257 }
258
259
260
261
262
263
264
265
266
267 @Deprecated
268 @Override
269 public boolean isExpired(final Date date) {
270 Args.notNull(date, "Date");
271 return (cookieExpiryDate != null
272 && cookieExpiryDate.compareTo(DateUtils.toInstant(date)) <= 0);
273 }
274
275
276
277
278
279
280
281 @Override
282 public boolean isExpired(final Instant instant) {
283 Args.notNull(instant, "Instant");
284 return (cookieExpiryDate != null
285 && cookieExpiryDate.compareTo(instant) <= 0);
286 }
287
288
289
290
291
292
293 @Deprecated
294 @Override
295 public Date getCreationDate() {
296 return DateUtils.toDate(creationDate);
297 }
298
299
300
301
302 @Override
303 public Instant getCreationInstant() {
304 return creationDate;
305 }
306
307
308
309
310
311
312 @Override
313 public boolean isHttpOnly() {
314 return httpOnly;
315 }
316
317
318
319
320
321 @Deprecated
322 public void setCreationDate(final Date creationDate) {
323 this.creationDate = DateUtils.toInstant(creationDate);
324 }
325
326
327
328
329 public void setCreationDate(final Instant creationDate) {
330 this.creationDate = creationDate;
331 }
332
333 public void setAttribute(final String name, final String value) {
334 this.attribs.put(name, value);
335 }
336
337 @Override
338 public String getAttribute(final String name) {
339 return this.attribs.get(name);
340 }
341
342 @Override
343 public boolean containsAttribute(final String name) {
344 return this.attribs.containsKey(name);
345 }
346
347
348
349
350 public boolean removeAttribute(final String name) {
351 return this.attribs.remove(name) != null;
352 }
353
354 @Override
355 public Object clone() throws CloneNotSupportedException {
356 final BasicClientCookie clone = (BasicClientCookie) super.clone();
357 clone.attribs = new HashMap<>(this.attribs);
358 return clone;
359 }
360
361 @Override
362 public String toString() {
363 final StringBuilder buffer = new StringBuilder();
364 buffer.append("[name: ");
365 buffer.append(this.name);
366 buffer.append("; ");
367 buffer.append("value: ");
368 buffer.append(this.value);
369 buffer.append("; ");
370 buffer.append("domain: ");
371 buffer.append(this.cookieDomain);
372 buffer.append("; ");
373 buffer.append("path: ");
374 buffer.append(this.cookiePath);
375 buffer.append("; ");
376 buffer.append("expiry: ");
377 buffer.append(this.cookieExpiryDate);
378 buffer.append("]");
379 return buffer.toString();
380 }
381
382
383
384
385 private final String name;
386
387
388 private Map<String, String> attribs;
389
390
391 private String value;
392
393
394 private String cookieDomain;
395
396
397 private Instant cookieExpiryDate;
398
399
400 private String cookiePath;
401
402
403 private boolean isSecure;
404
405 private Instant creationDate;
406
407
408 private boolean httpOnly;
409
410 }
411