20
20
import java .util .Set ;
21
21
import java .util .TreeSet ;
22
22
23
- public class Cookie {
23
+ public class Cookie implements Comparable < Cookie > {
24
24
private final String domain ;
25
25
private final String name ;
26
26
private final String value ;
27
27
private final String path ;
28
28
private final int maxAge ;
29
29
private final boolean secure ;
30
30
private final int version ;
31
+ private final boolean httpOnly ;
32
+ private final boolean discard ;
33
+ private final String comment ;
34
+ private final String commentUrl ;
35
+
31
36
private Set <Integer > ports = Collections .emptySet ();
32
37
private Set <Integer > unmodifiablePorts = ports ;
33
38
39
+ @ Deprecated
34
40
public Cookie (String domain , String name , String value , String path , int maxAge , boolean secure ) {
35
- this .domain = domain ;
36
- this .name = name ;
37
- this .value = value ;
38
- this .path = path ;
39
- this .maxAge = maxAge ;
40
- this .secure = secure ;
41
- this .version = 1 ;
41
+ this (domain , name , value , path , maxAge , secure , 1 );
42
42
}
43
43
44
+ @ Deprecated
44
45
public Cookie (String domain , String name , String value , String path , int maxAge , boolean secure , int version ) {
45
- this .domain = domain ;
46
+ this (domain , name , value , path , maxAge , secure , version , false , false , null , null , Collections .<Integer > emptySet ());
47
+ }
48
+
49
+ public Cookie (String domain , String name , String value , String path , int maxAge , boolean secure , int version , boolean httpOnly , boolean discard , String comment , String commentUrl , Iterable <Integer > ports ) {
50
+
51
+ if (name == null ) {
52
+ throw new NullPointerException ("name" );
53
+ }
54
+ name = name .trim ();
55
+ if (name .length () == 0 ) {
56
+ throw new IllegalArgumentException ("empty name" );
57
+ }
58
+
59
+ for (int i = 0 ; i < name .length (); i ++) {
60
+ char c = name .charAt (i );
61
+ if (c > 127 ) {
62
+ throw new IllegalArgumentException ("name contains non-ascii character: " + name );
63
+ }
64
+
65
+ // Check prohibited characters.
66
+ switch (c ) {
67
+ case '\t' :
68
+ case '\n' :
69
+ case 0x0b :
70
+ case '\f' :
71
+ case '\r' :
72
+ case ' ' :
73
+ case ',' :
74
+ case ';' :
75
+ case '=' :
76
+ throw new IllegalArgumentException ("name contains one of the following prohibited characters: " + "=,; \\ t\\ r\\ n\\ v\\ f: " + name );
77
+ }
78
+ }
79
+
80
+ if (name .charAt (0 ) == '$' ) {
81
+ throw new IllegalArgumentException ("name starting with '$' not allowed: " + name );
82
+ }
83
+
84
+ if (value == null ) {
85
+ throw new NullPointerException ("value" );
86
+ }
87
+
46
88
this .name = name ;
47
89
this .value = value ;
48
- this .path = path ;
90
+ this .domain = validateValue ("domain" , domain );
91
+ this .path = validateValue ("path" , path );
49
92
this .maxAge = maxAge ;
50
93
this .secure = secure ;
51
94
this .version = version ;
95
+ this .httpOnly = httpOnly ;
96
+
97
+ if (version > 0 ) {
98
+ this .comment = validateValue ("comment" , comment );
99
+ } else {
100
+ this .comment = null ;
101
+ }
102
+ if (version > 1 ) {
103
+ this .discard = discard ;
104
+ this .commentUrl = validateValue ("commentUrl" , commentUrl );
105
+ setPorts (ports );
106
+ } else {
107
+ this .discard = false ;
108
+ this .commentUrl = null ;
109
+ }
52
110
}
53
111
54
112
public String getDomain () {
@@ -79,35 +137,30 @@ public int getVersion() {
79
137
return version ;
80
138
}
81
139
140
+ public String getComment () {
141
+ return this .comment ;
142
+ }
143
+
144
+ public String getCommentUrl () {
145
+ return this .commentUrl ;
146
+ }
147
+
148
+ public boolean isHttpOnly () {
149
+ return httpOnly ;
150
+ }
151
+
152
+ public boolean isDiscard () {
153
+ return discard ;
154
+ }
155
+
82
156
public Set <Integer > getPorts () {
83
157
if (unmodifiablePorts == null ) {
84
158
unmodifiablePorts = Collections .unmodifiableSet (ports );
85
159
}
86
160
return unmodifiablePorts ;
87
161
}
88
162
89
- public void setPorts (int ... ports ) {
90
- if (ports == null ) {
91
- throw new NullPointerException ("ports" );
92
- }
93
-
94
- int [] portsCopy = ports .clone ();
95
- if (portsCopy .length == 0 ) {
96
- unmodifiablePorts = this .ports = Collections .emptySet ();
97
- } else {
98
- Set <Integer > newPorts = new TreeSet <Integer >();
99
- for (int p : portsCopy ) {
100
- if (p <= 0 || p > 65535 ) {
101
- throw new IllegalArgumentException ("port out of range: " + p );
102
- }
103
- newPorts .add (Integer .valueOf (p ));
104
- }
105
- this .ports = newPorts ;
106
- unmodifiablePorts = null ;
107
- }
108
- }
109
-
110
- public void setPorts (Iterable <Integer > ports ) {
163
+ private void setPorts (Iterable <Integer > ports ) {
111
164
Set <Integer > newPorts = new TreeSet <Integer >();
112
165
for (int p : ports ) {
113
166
if (p <= 0 || p > 65535 ) {
@@ -125,7 +178,89 @@ public void setPorts(Iterable<Integer> ports) {
125
178
126
179
@ Override
127
180
public String toString () {
128
- return String .format ("Cookie: domain=%s, name=%s, value=%s, path=%s, maxAge=%d, secure=%s" ,
129
- domain , name , value , path , maxAge , secure );
181
+ StringBuilder buf = new StringBuilder ();
182
+ buf .append (getName ());
183
+ buf .append ('=' );
184
+ buf .append (getValue ());
185
+ if (getDomain () != null ) {
186
+ buf .append ("; domain=" );
187
+ buf .append (getDomain ());
188
+ }
189
+ if (getPath () != null ) {
190
+ buf .append ("; path=" );
191
+ buf .append (getPath ());
192
+ }
193
+ if (getComment () != null ) {
194
+ buf .append ("; comment=" );
195
+ buf .append (getComment ());
196
+ }
197
+ if (getMaxAge () >= 0 ) {
198
+ buf .append ("; maxAge=" );
199
+ buf .append (getMaxAge ());
200
+ buf .append ('s' );
201
+ }
202
+ if (isSecure ()) {
203
+ buf .append ("; secure" );
204
+ }
205
+ if (isHttpOnly ()) {
206
+ buf .append ("; HTTPOnly" );
207
+ }
208
+ return buf .toString ();
209
+ }
210
+
211
+ private String validateValue (String name , String value ) {
212
+ if (value == null ) {
213
+ return null ;
214
+ }
215
+ value = value .trim ();
216
+ if (value .length () == 0 ) {
217
+ return null ;
218
+ }
219
+ for (int i = 0 ; i < value .length (); i ++) {
220
+ char c = value .charAt (i );
221
+ switch (c ) {
222
+ case '\r' :
223
+ case '\n' :
224
+ case '\f' :
225
+ case 0x0b :
226
+ case ';' :
227
+ throw new IllegalArgumentException (name + " contains one of the following prohibited characters: " + ";\\ r\\ n\\ f\\ v (" + value + ')' );
228
+ }
229
+ }
230
+ return value ;
231
+ }
232
+
233
+ public int compareTo (Cookie c ) {
234
+ int v ;
235
+ v = getName ().compareToIgnoreCase (c .getName ());
236
+ if (v != 0 ) {
237
+ return v ;
238
+ }
239
+
240
+ if (getPath () == null ) {
241
+ if (c .getPath () != null ) {
242
+ return -1 ;
243
+ }
244
+ } else if (c .getPath () == null ) {
245
+ return 1 ;
246
+ } else {
247
+ v = getPath ().compareTo (c .getPath ());
248
+ if (v != 0 ) {
249
+ return v ;
250
+ }
251
+ }
252
+
253
+ if (getDomain () == null ) {
254
+ if (c .getDomain () != null ) {
255
+ return -1 ;
256
+ }
257
+ } else if (c .getDomain () == null ) {
258
+ return 1 ;
259
+ } else {
260
+ v = getDomain ().compareToIgnoreCase (c .getDomain ());
261
+ return v ;
262
+ }
263
+
264
+ return 0 ;
130
265
}
131
266
}
0 commit comments