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 package org.apache.hc.client5.http.impl.cookie;
28
29 import java.util.Date;
30
31 import org.apache.hc.client5.http.cookie.CommonCookieAttributeHandler;
32 import org.apache.hc.client5.http.cookie.Cookie;
33 import org.apache.hc.client5.http.cookie.MalformedCookieException;
34 import org.apache.hc.client5.http.cookie.SetCookie;
35 import org.apache.hc.core5.annotation.Contract;
36 import org.apache.hc.core5.annotation.ThreadingBehavior;
37 import org.apache.hc.core5.util.Args;
38
39
40
41
42
43
44 @Contract(threading = ThreadingBehavior.STATELESS)
45 public class BasicMaxAgeHandler extends AbstractCookieAttributeHandler implements CommonCookieAttributeHandler {
46
47 public BasicMaxAgeHandler() {
48 super();
49 }
50
51 @Override
52 public void parse(final SetCookie cookie, final String value)
53 throws MalformedCookieException {
54 Args.notNull(cookie, "Cookie");
55 if (value == null) {
56 throw new MalformedCookieException("Missing value for 'max-age' attribute");
57 }
58 final int age;
59 try {
60 age = Integer.parseInt(value);
61 } catch (final NumberFormatException e) {
62 throw new MalformedCookieException ("Invalid 'max-age' attribute: "
63 + value);
64 }
65 if (age < 0) {
66 throw new MalformedCookieException ("Negative 'max-age' attribute: "
67 + value);
68 }
69 cookie.setExpiryDate(new Date(System.currentTimeMillis() + age * 1000L));
70 }
71
72 @Override
73 public String getAttributeName() {
74 return Cookie.MAX_AGE_ATTR;
75 }
76
77 }