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.http.impl.cookie;
29
30 import org.apache.http.annotation.Immutable;
31
32 import org.apache.http.cookie.ClientCookie;
33 import org.apache.http.cookie.Cookie;
34 import org.apache.http.cookie.CookieAttributeHandler;
35 import org.apache.http.cookie.CookieOrigin;
36 import org.apache.http.cookie.CookieRestrictionViolationException;
37 import org.apache.http.cookie.MalformedCookieException;
38 import org.apache.http.cookie.SetCookie;
39 import org.apache.http.cookie.SetCookie2;
40
41
42
43
44
45
46 @Immutable
47 public class RFC2965VersionAttributeHandler implements CookieAttributeHandler {
48
49 public RFC2965VersionAttributeHandler() {
50 super();
51 }
52
53
54
55
56 public void parse(final SetCookie cookie, final String value)
57 throws MalformedCookieException {
58 if (cookie == null) {
59 throw new IllegalArgumentException("Cookie may not be null");
60 }
61 if (value == null) {
62 throw new MalformedCookieException(
63 "Missing value for version attribute");
64 }
65 int version = -1;
66 try {
67 version = Integer.parseInt(value);
68 } catch (NumberFormatException e) {
69 version = -1;
70 }
71 if (version < 0) {
72 throw new MalformedCookieException("Invalid cookie version.");
73 }
74 cookie.setVersion(version);
75 }
76
77
78
79
80 public void validate(final Cookie cookie, final CookieOrigin origin)
81 throws MalformedCookieException {
82 if (cookie == null) {
83 throw new IllegalArgumentException("Cookie may not be null");
84 }
85 if (cookie instanceof SetCookie2) {
86 if (cookie instanceof ClientCookie
87 && !((ClientCookie) cookie).containsAttribute(ClientCookie.VERSION_ATTR)) {
88 throw new CookieRestrictionViolationException(
89 "Violates RFC 2965. Version attribute is required.");
90 }
91 }
92 }
93
94 public boolean match(final Cookie cookie, final CookieOrigin origin) {
95 return true;
96 }
97
98 }