7
7
* An HTTP Cookie to be sent in a response header.
8
8
*
9
9
* @see SimpleHttpExchange
10
+ * @see Builder
10
11
* @since 02.00.00
11
- * @version 02.00 .00
12
+ * @version 02.03 .00
12
13
* @author Ktt Development
13
14
*/
14
15
public class SimpleHttpCookie {
@@ -22,13 +23,15 @@ public class SimpleHttpCookie {
22
23
23
24
private final Date expires ;
24
25
private final Integer maxAge ;
25
- private final boolean
26
+ private final Boolean
26
27
secure ,
27
28
httpOnly ;
28
29
29
30
/**
30
31
* Creates an HTTP cookie. All fields except for <code>name</code>, <code>secure</code>, <code>httpOnly</code>, and <code>value</code> can be set to null if unused.
31
32
*
33
+ * @deprecated Use {@link Builder} class instead. This method will be removed in the future.
34
+ *
32
35
* @param name name of the cookie
33
36
* @param value value of the cookie
34
37
* @param domain what domain to send the cookie to
@@ -42,7 +45,8 @@ public class SimpleHttpCookie {
42
45
* @since 02.00.00
43
46
* @author Ktt Development
44
47
*/
45
- public SimpleHttpCookie (final String name , final String value , final String domain , final String path , final String sameSite , final Date expires , final Integer maxAge , final boolean secure , final boolean httpOnly ){
48
+ @ Deprecated
49
+ public SimpleHttpCookie (final String name , final String value , final String domain , final String path , final String sameSite , final Date expires , final Integer maxAge , final Boolean secure , final Boolean httpOnly ){
46
50
if (name == null )
47
51
throw new NullPointerException ("Cookie name can not be null" );
48
52
else
@@ -63,17 +67,50 @@ public SimpleHttpCookie(final String name, final String value, final String doma
63
67
/**
64
68
* Converts the cookie to a readable string for the cookie header.
65
69
*
70
+ * @deprecated Use {@link #toCookieHeaderString()} instead.
71
+ *
66
72
* @return cookie header
67
73
*
68
74
* @see SimpleHttpExchange#setCookie(SimpleHttpCookie)
69
75
* @since 02.00.00
70
76
* @author Ktt Development
71
77
*/
72
78
@ SuppressWarnings ({"ConstantConditions" , "SpellCheckingInspection" })
73
- @ Override
79
+ @ Override @ Deprecated
74
80
public final String toString (){
75
- StringBuilder OUT = new StringBuilder ();
81
+ final StringBuilder OUT = new StringBuilder ();
82
+
83
+ OUT .append (name ).append ("=" ).append (value );
84
+ if (expires != null )
85
+ OUT .append ("; Expires=" ).append (new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss" ).format (expires )).append (" GMT" );
86
+ if (maxAge != null )
87
+ OUT .append ("; Max-Age=" ).append (maxAge );
88
+ if (domain != null )
89
+ OUT .append ("; Domain=" ).append (domain );
90
+ if (path != null )
91
+ OUT .append ("; Path=" ).append (path );
92
+ if (secure != null && secure )
93
+ OUT .append ("; Secure=" ).append (secure );
94
+ if (httpOnly != null && httpOnly )
95
+ OUT .append ("; HttpOnly=" ).append (httpOnly );
96
+ if (sameSite != null )
97
+ OUT .append ("; SameSite=" ).append (sameSite );
98
+
99
+ return OUT .toString ();
100
+ }
101
+
102
+ private final SimpleDateFormat sdf = new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss" );
76
103
104
+ /**
105
+ * Converts the cookie to a readable string for a response header.
106
+ *
107
+ * @return cookie as a header string
108
+ *
109
+ * @since 02.03.00
110
+ * @author Ktt Development
111
+ */
112
+ public final String toCookieHeaderString (){
113
+ final StringBuilder OUT = new StringBuilder ();
77
114
OUT .append (name ).append ("=" ).append (value );
78
115
if (expires != null )
79
116
OUT .append ("; Expires=" ).append (new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss" ).format (expires )).append (" GMT" );
@@ -83,14 +120,293 @@ public final String toString(){
83
120
OUT .append ("; Domain=" ).append (domain );
84
121
if (path != null )
85
122
OUT .append ("; Path=" ).append (path );
86
- if (secure )
123
+ if (secure != null && secure )
87
124
OUT .append ("; Secure=" ).append (secure );
88
- if (httpOnly )
125
+ if (httpOnly != null && httpOnly )
89
126
OUT .append ("; HttpOnly=" ).append (httpOnly );
90
127
if (sameSite != null )
91
128
OUT .append ("; SameSite=" ).append (sameSite );
92
129
93
130
return OUT .toString ();
94
131
}
95
132
133
+ /**
134
+ * Builder class for {@link SimpleHttpCookie}.
135
+ *
136
+ * @see SimpleHttpCookie
137
+ * @since 02.03.00
138
+ * @version 02.03.00
139
+ * @author Ktt Development
140
+ */
141
+ public static class Builder {
142
+
143
+ private final String name ;
144
+ private final String value ;
145
+
146
+ private String domain ;
147
+ private String path ;
148
+ private String sameSite ;
149
+ private Date expires ;
150
+ private Integer maxAge ;
151
+ private Boolean secure ;
152
+ private Boolean httpOnly ;
153
+
154
+ /**
155
+ * Creates an HTTP cookie builder given a key and value.
156
+ *
157
+ * @param name Name of the cookie
158
+ * @param value Value of the cookie
159
+ *
160
+ * @since 02.03.00
161
+ * @author Ktt Development
162
+ */
163
+ public Builder (final String name , final String value ){
164
+ if ((this .name = name ) == null )
165
+ throw new NullPointerException ("Cookie name can not be null" );
166
+ if ((this .value = value ) == null )
167
+ throw new NullPointerException ("Cookie value can not be null" );
168
+ }
169
+
170
+ /**
171
+ * Returns the name of the cookie.
172
+ *
173
+ * @return cookie name
174
+ *
175
+ * @since 02.00.00
176
+ * @author Ktt Development
177
+ */
178
+ public final String getName (){
179
+ return name ;
180
+ }
181
+
182
+ /**
183
+ * Returns the value of the cookie.
184
+ *
185
+ * @return cookie value
186
+ *
187
+ * @since 02.03.00
188
+ * @author Ktt Development
189
+ */
190
+ public final String getValue (){
191
+ return value ;
192
+ }
193
+
194
+ /**
195
+ * Returns the domain to send the cookie to.
196
+ *
197
+ * @return domain to send the cookie to
198
+ *
199
+ * @see #setDomain(String)
200
+ * @since 02.03.00
201
+ * @author Ktt Development
202
+ */
203
+ public final String getDomain (){
204
+ return domain ;
205
+ }
206
+
207
+ /**
208
+ * Sets the domain of the cookie.
209
+ *
210
+ * @param domain what domain to send the cookie to
211
+ * @return cookie builder
212
+ *
213
+ * @see #getDomain()
214
+ * @since 02.03.00
215
+ * @author Ktt Development
216
+ */
217
+ public final Builder setDomain (final String domain ){
218
+ this .domain = domain ;
219
+ return this ;
220
+ }
221
+
222
+ /**
223
+ * Returns the path to send the cookie to.
224
+ *
225
+ * @return what path to send the cookie to
226
+ *
227
+ * @see #setPath(String)
228
+ * @since 02.03.00
229
+ * @author Ktt Development
230
+ */
231
+ public final String getPath (){
232
+ return path ;
233
+ }
234
+
235
+ /**
236
+ * Sets the path of the cookie.
237
+ *
238
+ * @param path what path to send the cookie to
239
+ * @return cookie builder
240
+ *
241
+ * @see #getPath()
242
+ * @since 02.03.00
243
+ * @author Ktt Development
244
+ */
245
+ public final Builder setPath (final String path ){
246
+ this .path = path ;
247
+ return this ;
248
+ }
249
+
250
+ /**
251
+ * Returns if the cookie should be prevented from being sent cross-site.
252
+ *
253
+ * @return if the cookie should be prevented from being sent cross-site.
254
+ *
255
+ * @see #setSameSite(String)
256
+ * @since 02.03.00
257
+ * @author Ktt Development
258
+ */
259
+ public final String isSameSite (){
260
+ return sameSite ;
261
+ }
262
+
263
+ /**
264
+ * Sets if the cookie should be prevented from being sent cross-site.
265
+ *
266
+ * @param sameSite if the cookie should be prevented from being sent cross-site
267
+ * @return cookie builder
268
+ *
269
+ * @see #isSameSite()
270
+ * @since 02.03.00
271
+ * @author Ktt Development
272
+ */
273
+ public final Builder setSameSite (final String sameSite ){
274
+ this .sameSite = sameSite ;
275
+ return this ;
276
+ }
277
+
278
+ /**
279
+ * Returns when the cookie should expire.
280
+ *
281
+ * @return when the cookie should expire.
282
+ *
283
+ * @see #setExpires(Date)
284
+ * @see #getMaxAge()
285
+ * @see #setMaxAge(int)
286
+ * @since 02.03.00
287
+ * @author Ktt Development
288
+ */
289
+ public final Date getExpires (){
290
+ return expires ;
291
+ }
292
+
293
+ /**
294
+ * Sets when the cookie should expire.
295
+ *
296
+ * @param expires when the cookie should expire
297
+ * @return cookie builder
298
+ *
299
+ * @see #getExpires()
300
+ * @see #getMaxAge()
301
+ * @see #setMaxAge(int)
302
+ * @since 02.03.00
303
+ * @author Ktt Development
304
+ */
305
+ public final Builder setExpires (final Date expires ){
306
+ this .expires = expires ;
307
+ return this ;
308
+ }
309
+
310
+ /**
311
+ * Returns how long the cookie should exist for.
312
+ *
313
+ * @return how long the cookie should exist for
314
+ *
315
+ * @see #getExpires()
316
+ * @see #setExpires(Date)
317
+ * @see #setMaxAge(int)
318
+ * @since 02.03.00
319
+ * @author Ktt Development
320
+ */
321
+ public final int getMaxAge (){
322
+ return maxAge ;
323
+ }
324
+
325
+ /**
326
+ * Sets how long the cookie should exist for.
327
+ *
328
+ * @param maxAge how long the cookie should exist for
329
+ * @return cookie builder
330
+ *
331
+ * @see #getExpires()
332
+ * @see #setExpires(Date)
333
+ * @see #getMaxAge()
334
+ * @since 02.03.00
335
+ * @author Ktt Development
336
+ */
337
+ public final Builder setMaxAge (final int maxAge ){
338
+ this .maxAge = maxAge ;
339
+ return this ;
340
+ }
341
+
342
+ /**
343
+ * Returns if the cookie must be sent over a secure/HTTPS protocol.
344
+ *
345
+ * @return if the cookie must be sent over a secure/HTTPS protocol
346
+ *
347
+ * @see #isSecure()
348
+ * @since 02.03.00
349
+ * @author Ktt Development
350
+ */
351
+ public final boolean isSecure (){
352
+ return secure ;
353
+ }
354
+
355
+ /**
356
+ * Sets if the cookie must be sent over a secure/HTTPS protocol.
357
+ *
358
+ * @param secure if the cookie must be sent over a secure/HTTPS protocol.
359
+ * @return cookie builder
360
+ *
361
+ * @see #setSecure(boolean)
362
+ * @since 02.03.00
363
+ * @author Ktt Development
364
+ */
365
+ public final Builder setSecure (final boolean secure ){
366
+ this .secure = secure ;
367
+ return this ;
368
+ }
369
+
370
+ /**
371
+ * Returns if only the server should have access to the cookies.
372
+ *
373
+ * @return if only the server should have access to the cookies.
374
+ *
375
+ * @see #setHttpOnly(boolean)
376
+ * @since 02.03.00
377
+ * @author Ktt Development
378
+ */
379
+ public final boolean isHttpOnly (){
380
+ return httpOnly ;
381
+ }
382
+
383
+ /**
384
+ * Sets if only the server should have access to the cookies.
385
+ *
386
+ * @param httpOnly if only the server should have access to the cookies
387
+ * @return cookie builder
388
+ *
389
+ * @see #isHttpOnly()
390
+ * @since 02.03.00
391
+ * @author Ktt Development
392
+ */
393
+ public final Builder setHttpOnly (final boolean httpOnly ){
394
+ this .httpOnly = httpOnly ;
395
+ return this ;
396
+ }
397
+
398
+ /**
399
+ * Returns the completed cookie.
400
+ *
401
+ * @return simple http cookie
402
+ *
403
+ * @since 02.03.00
404
+ * @author Ktt Development
405
+ */
406
+ public final SimpleHttpCookie build (){
407
+ return new SimpleHttpCookie (name ,value ,domain ,path ,sameSite ,expires ,maxAge ,secure ,httpOnly );
408
+ }
409
+
410
+ }
411
+
96
412
}
0 commit comments