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 java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Locale;
34
35 import org.apache.http.HeaderElement;
36 import org.apache.http.NameValuePair;
37 import org.apache.http.annotation.Contract;
38 import org.apache.http.annotation.ThreadingBehavior;
39 import org.apache.http.cookie.CommonCookieAttributeHandler;
40 import org.apache.http.cookie.Cookie;
41 import org.apache.http.cookie.CookieAttributeHandler;
42 import org.apache.http.cookie.CookieOrigin;
43 import org.apache.http.cookie.MalformedCookieException;
44 import org.apache.http.util.Args;
45
46
47
48
49
50
51 @Contract(threading = ThreadingBehavior.SAFE)
52 public abstract class CookieSpecBase extends AbstractCookieSpec {
53
54 public CookieSpecBase() {
55 super();
56 }
57
58
59
60
61 protected CookieSpecBase(final HashMap<String, CookieAttributeHandler> map) {
62 super(map);
63 }
64
65
66
67
68 protected CookieSpecBase(final CommonCookieAttributeHandler... handlers) {
69 super(handlers);
70 }
71
72 protected static String getDefaultPath(final CookieOrigin origin) {
73 String defaultPath = origin.getPath();
74 int lastSlashIndex = defaultPath.lastIndexOf('/');
75 if (lastSlashIndex >= 0) {
76 if (lastSlashIndex == 0) {
77
78 lastSlashIndex = 1;
79 }
80 defaultPath = defaultPath.substring(0, lastSlashIndex);
81 }
82 return defaultPath;
83 }
84
85 protected static String getDefaultDomain(final CookieOrigin origin) {
86 return origin.getHost();
87 }
88
89 protected List<Cookie> parse(final HeaderElement[] elems, final CookieOrigin origin)
90 throws MalformedCookieException {
91 final List<Cookie> cookies = new ArrayList<Cookie>(elems.length);
92 for (final HeaderElement headerelement : elems) {
93 final String name = headerelement.getName();
94 final String value = headerelement.getValue();
95 if (name == null || name.isEmpty()) {
96 continue;
97 }
98
99 final BasicClientCookieentCookie.html#BasicClientCookie">BasicClientCookie cookie = new BasicClientCookie(name, value);
100 cookie.setPath(getDefaultPath(origin));
101 cookie.setDomain(getDefaultDomain(origin));
102
103
104 final NameValuePair[] attribs = headerelement.getParameters();
105 for (int j = attribs.length - 1; j >= 0; j--) {
106 final NameValuePair attrib = attribs[j];
107 final String s = attrib.getName().toLowerCase(Locale.ROOT);
108
109 cookie.setAttribute(s, attrib.getValue());
110
111 final CookieAttributeHandler handler = findAttribHandler(s);
112 if (handler != null) {
113 handler.parse(cookie, attrib.getValue());
114 }
115 }
116 cookies.add(cookie);
117 }
118 return cookies;
119 }
120
121 @Override
122 public void validate(final Cookie cookie, final CookieOrigin origin)
123 throws MalformedCookieException {
124 Args.notNull(cookie, "Cookie");
125 Args.notNull(origin, "Cookie origin");
126 for (final CookieAttributeHandler handler: getAttribHandlers()) {
127 handler.validate(cookie, origin);
128 }
129 }
130
131 @Override
132 public boolean match(final Cookie cookie, final CookieOrigin origin) {
133 Args.notNull(cookie, "Cookie");
134 Args.notNull(origin, "Cookie origin");
135 for (final CookieAttributeHandler handler: getAttribHandlers()) {
136 if (!handler.match(cookie, origin)) {
137 return false;
138 }
139 }
140 return true;
141 }
142
143 }