1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.util;
16
17 import java.io.Serializable;
18 import java.util.Date;
19
20 import org.apache.commons.lang3.builder.EqualsBuilder;
21 import org.apache.commons.lang3.builder.HashCodeBuilder;
22 import org.apache.http.cookie.ClientCookie;
23 import org.apache.http.impl.cookie.BasicClientCookie;
24
25
26
27
28
29
30
31
32
33 public class Cookie implements Serializable {
34
35 private final ClientCookie httpClientCookie_;
36
37
38
39
40
41
42
43
44 public Cookie(final String domain, final String name, final String value) {
45 this(domain, name, value, null, null, false);
46 }
47
48
49
50
51
52
53
54
55
56
57
58 public Cookie(final String domain, final String name, final String value, final String path, final Date expires,
59 final boolean secure) {
60 this(domain, name, value, path, expires, secure, false, null);
61 }
62
63
64
65
66
67
68
69
70
71
72
73
74 public Cookie(final String domain, final String name, final String value, final String path, final Date expires,
75 final boolean secure, final boolean httpOnly) {
76 this(domain, name, value, path, expires, secure, httpOnly, null);
77 }
78
79
80
81
82
83
84
85
86
87
88
89
90
91 public Cookie(final String domain, final String name, final String value, final String path, final Date expires,
92 final boolean secure, final boolean httpOnly, final String sameSite) {
93 if (domain == null) {
94 throw new IllegalArgumentException("Cookie domain must be specified");
95 }
96
97 final BasicClientCookie cookie = new BasicClientCookie(name, value == null ? "" : value);
98
99 cookie.setDomain(domain);
100
101 cookie.setAttribute(ClientCookie.DOMAIN_ATTR, domain);
102
103 cookie.setPath(path);
104 if (expires != null) {
105 cookie.setExpiryDate(expires);
106 }
107 cookie.setSecure(secure);
108 if (httpOnly) {
109 cookie.setAttribute("httponly", "true");
110 }
111
112 if (sameSite != null) {
113 cookie.setAttribute("samesite", sameSite);
114 }
115
116 httpClientCookie_ = cookie;
117 }
118
119
120
121
122
123 public Cookie(final ClientCookie clientCookie) {
124 httpClientCookie_ = clientCookie;
125 }
126
127
128
129
130
131
132
133
134
135
136
137
138 public Cookie(final String domain, final String name, final String value, final String path, final int maxAge,
139 final boolean secure) {
140 this(domain, name, value, path, convertToExpiryDate(maxAge), secure);
141 }
142
143 private static Date convertToExpiryDate(final int maxAge) {
144 if (maxAge < -1) {
145 throw new IllegalArgumentException("invalid max age: " + maxAge);
146 }
147
148 if (maxAge >= 0) {
149 return new Date(System.currentTimeMillis() + (maxAge * 1000L));
150 }
151
152 return null;
153 }
154
155
156
157
158
159 public String getName() {
160 return httpClientCookie_.getName();
161 }
162
163
164
165
166
167 public String getValue() {
168 return httpClientCookie_.getValue();
169 }
170
171
172
173
174
175 public String getDomain() {
176 return httpClientCookie_.getDomain();
177 }
178
179
180
181
182
183 public String getPath() {
184 return httpClientCookie_.getPath();
185 }
186
187
188
189
190
191 public Date getExpires() {
192 return httpClientCookie_.getExpiryDate();
193 }
194
195
196
197
198
199 public boolean isSecure() {
200 return httpClientCookie_.isSecure();
201 }
202
203
204
205
206
207
208 public boolean isHttpOnly() {
209 return httpClientCookie_.getAttribute("httponly") != null;
210 }
211
212
213
214
215 public String getSameSite() {
216 return httpClientCookie_.getAttribute("samesite");
217 }
218
219
220
221
222 @Override
223 public String toString() {
224 return getName() + "=" + getValue()
225 + (getDomain() == null ? "" : ";domain=" + getDomain())
226 + (getPath() == null ? "" : ";path=" + getPath())
227 + (getExpires() == null ? "" : ";expires=" + getExpires())
228 + (isSecure() ? ";secure" : "")
229 + (isHttpOnly() ? ";httpOnly" : "")
230 + (getSameSite() == null ? "" : ";sameSite=" + getSameSite());
231 }
232
233
234
235
236 @Override
237 public boolean equals(final Object o) {
238 if (!(o instanceof Cookie)) {
239 return false;
240 }
241 final Cookie other = (Cookie) o;
242 final String path = getPath() == null ? "/" : getPath();
243 final String otherPath = other.getPath() == null ? "/" : other.getPath();
244 return new EqualsBuilder()
245 .append(getName(), other.getName())
246 .append(getDomain(), other.getDomain())
247 .append(path, otherPath)
248 .isEquals();
249 }
250
251
252
253
254 @Override
255 public int hashCode() {
256 final String path = getPath() == null ? "/" : getPath();
257 return new HashCodeBuilder()
258 .append(getName())
259 .append(getDomain())
260 .append(path)
261 .toHashCode();
262 }
263
264
265
266
267
268 public org.apache.http.cookie.Cookie toHttpClient() {
269 return httpClientCookie_;
270 }
271 }