1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.http.cookie;
17
18 import io.netty.handler.codec.http.cookie.CookieHeaderNames.SameSite;
19
20 import static io.netty.handler.codec.http.cookie.CookieUtil.stringBuilder;
21 import static io.netty.handler.codec.http.cookie.CookieUtil.validateAttributeValue;
22 import static io.netty.util.internal.ObjectUtil.checkNotNull;
23 import static io.netty.util.internal.ObjectUtil.checkNonEmptyAfterTrim;
24
25
26
27
28 public class DefaultCookie implements Cookie {
29
30 private final String name;
31 private String value;
32 private boolean wrap;
33 private String domain;
34 private String path;
35 private long maxAge = UNDEFINED_MAX_AGE;
36 private boolean secure;
37 private boolean httpOnly;
38 private SameSite sameSite;
39 private boolean partitioned;
40
41
42
43
44 public DefaultCookie(String name, String value) {
45 this.name = checkNonEmptyAfterTrim(name, "name");
46 setValue(value);
47 }
48
49 @Override
50 public String name() {
51 return name;
52 }
53
54 @Override
55 public String value() {
56 return value;
57 }
58
59 @Override
60 public void setValue(String value) {
61 this.value = checkNotNull(value, "value");
62 }
63
64 @Override
65 public boolean wrap() {
66 return wrap;
67 }
68
69 @Override
70 public void setWrap(boolean wrap) {
71 this.wrap = wrap;
72 }
73
74 @Override
75 public String domain() {
76 return domain;
77 }
78
79 @Override
80 public void setDomain(String domain) {
81 this.domain = validateAttributeValue("domain", domain);
82 }
83
84 @Override
85 public String path() {
86 return path;
87 }
88
89 @Override
90 public void setPath(String path) {
91 this.path = validateAttributeValue("path", path);
92 }
93
94 @Override
95 public long maxAge() {
96 return maxAge;
97 }
98
99 @Override
100 public void setMaxAge(long maxAge) {
101 this.maxAge = maxAge;
102 }
103
104 @Override
105 public boolean isSecure() {
106 return secure;
107 }
108
109 @Override
110 public void setSecure(boolean secure) {
111 this.secure = secure;
112 }
113
114 @Override
115 public boolean isHttpOnly() {
116 return httpOnly;
117 }
118
119 @Override
120 public void setHttpOnly(boolean httpOnly) {
121 this.httpOnly = httpOnly;
122 }
123
124
125
126
127
128
129
130 public SameSite sameSite() {
131 return sameSite;
132 }
133
134
135
136
137
138
139
140 public void setSameSite(SameSite sameSite) {
141 this.sameSite = sameSite;
142 }
143
144
145
146
147
148
149 public boolean isPartitioned() {
150 return partitioned;
151 }
152
153
154
155
156
157
158 public void setPartitioned(boolean partitioned) {
159 this.partitioned = partitioned;
160 }
161
162 @Override
163 public int hashCode() {
164 return name().hashCode();
165 }
166
167 @Override
168 public boolean equals(Object o) {
169 if (this == o) {
170 return true;
171 }
172
173 if (!(o instanceof Cookie)) {
174 return false;
175 }
176
177 Cookie that = (Cookie) o;
178 if (!name().equals(that.name())) {
179 return false;
180 }
181
182 if (path() == null) {
183 if (that.path() != null) {
184 return false;
185 }
186 } else if (that.path() == null) {
187 return false;
188 } else if (!path().equals(that.path())) {
189 return false;
190 }
191
192 if (domain() == null) {
193 if (that.domain() != null) {
194 return false;
195 }
196 } else {
197 return domain().equalsIgnoreCase(that.domain());
198 }
199
200 return true;
201 }
202
203 @Override
204 public int compareTo(Cookie c) {
205 int v = name().compareTo(c.name());
206 if (v != 0) {
207 return v;
208 }
209
210 if (path() == null) {
211 if (c.path() != null) {
212 return -1;
213 }
214 } else if (c.path() == null) {
215 return 1;
216 } else {
217 v = path().compareTo(c.path());
218 if (v != 0) {
219 return v;
220 }
221 }
222
223 if (domain() == null) {
224 if (c.domain() != null) {
225 return -1;
226 }
227 } else if (c.domain() == null) {
228 return 1;
229 } else {
230 v = domain().compareToIgnoreCase(c.domain());
231 return v;
232 }
233
234 return 0;
235 }
236
237
238
239
240
241
242
243
244
245 @Deprecated
246 protected String validateValue(String name, String value) {
247 return validateAttributeValue(name, value);
248 }
249
250 @Override
251 public String toString() {
252 StringBuilder buf = stringBuilder()
253 .append(name())
254 .append('=')
255 .append(value());
256 if (domain() != null) {
257 buf.append(", domain=")
258 .append(domain());
259 }
260 if (path() != null) {
261 buf.append(", path=")
262 .append(path());
263 }
264 if (maxAge() >= 0) {
265 buf.append(", maxAge=")
266 .append(maxAge())
267 .append('s');
268 }
269 if (isSecure()) {
270 buf.append(", secure");
271 }
272 if (isHttpOnly()) {
273 buf.append(", HTTPOnly");
274 }
275 if (sameSite() != null) {
276 buf.append(", SameSite=").append(sameSite());
277 }
278 if (isPartitioned()) {
279 buf.append(", Partitioned");
280 }
281 return buf.toString();
282 }
283 }