@@ -45,16 +45,27 @@ public class Cookie {
4545 */
4646 private long maxAge = -1 ;
4747
48+ /**
49+ * Creates a response cookie.
50+ *
51+ * @param name Cookie's name.
52+ * @param value Cookie's value or <code>null</code>.
53+ */
4854 public Cookie (@ Nonnull String name , @ Nullable String value ) {
4955 this .name = name ;
5056 this .value = value ;
5157 }
5258
59+ /**
60+ * Creates a response cookie without a value.
61+ *
62+ * @param name Cookie's name.
63+ */
5364 public Cookie (@ Nonnull String name ) {
5465 this (name , null );
5566 }
5667
57- public Cookie (@ Nonnull Cookie cookie ) {
68+ private Cookie (@ Nonnull Cookie cookie ) {
5869 this .domain = cookie .domain ;
5970 this .value = cookie .value ;
6071 this .name = cookie .name ;
@@ -64,90 +75,220 @@ public Cookie(@Nonnull Cookie cookie) {
6475 this .httpOnly = cookie .httpOnly ;
6576 }
6677
67- public Cookie clone () {
78+ /**
79+ * Copy all state from this cookie and creates a new cookie.
80+ *
81+ * @return New cookie.
82+ */
83+ public @ Nonnull Cookie clone () {
6884 return new Cookie (this );
6985 }
7086
87+ /**
88+ * Cookie's name.
89+ *
90+ * @return Cookie's name.
91+ */
7192 public @ Nonnull String getName () {
7293 return name ;
7394 }
7495
96+ /**
97+ * Set cookie's name.
98+ *
99+ * @param name Cookie's name.
100+ * @return This cookie.
101+ */
75102 public @ Nonnull Cookie setName (@ Nonnull String name ) {
76103 this .name = name ;
77104 return this ;
78105 }
79106
107+ /**
108+ * Cookie's value.
109+ *
110+ * @return Cookie's value.
111+ */
80112 public @ Nullable String getValue () {
81113 return value ;
82114 }
83115
116+ /**
117+ * Set cookie's value.
118+ *
119+ * @param value Cookie's value.
120+ * @return This cookie.
121+ */
84122 public @ Nonnull Cookie setValue (@ Nonnull String value ) {
85123 this .value = value ;
86124 return this ;
87125 }
88126
127+ /**
128+ * Cookie's domain.
129+ *
130+ * @return Cookie's domain.
131+ */
89132 public @ Nullable String getDomain () {
90133 return domain ;
91134 }
92135
136+ /**
137+ * Get cookie's domain.
138+ *
139+ * @param domain Defaults cookie's domain.
140+ * @return Cookie's domain..
141+ */
93142 public @ Nonnull String getDomain (@ Nonnull String domain ) {
94143 return this .domain == null ? domain : domain ;
95144 }
96145
146+ /**
147+ * Set cookie's domain.
148+ *
149+ * @param domain Cookie's domain.
150+ * @return This cookie.
151+ */
97152 public @ Nonnull Cookie setDomain (@ Nonnull String domain ) {
98153 this .domain = domain ;
99154 return this ;
100155 }
101156
157+ /**
158+ * Cookie's path.
159+ *
160+ * @return Cookie's path.
161+ */
102162 public @ Nullable String getPath () {
103163 return path ;
104164 }
105165
166+ /**
167+ * Cookie's path.
168+ *
169+ * @param path Defaults path.
170+ * @return Cookie's path.
171+ */
106172 public @ Nonnull String getPath (@ Nonnull String path ) {
107173 return this .path == null ? path : this .path ;
108174 }
109175
176+ /**
177+ * Set cookie's path.
178+ *
179+ * @param path Cookie's path.
180+ * @return This cookie.
181+ */
110182 public @ Nonnull Cookie setPath (@ Nonnull String path ) {
111183 this .path = path ;
112184 return this ;
113185 }
114186
187+ /**
188+ * Cookie's http-only flag.
189+ *
190+ * @return Htto-only flag.
191+ */
115192 public boolean isHttpOnly () {
116193 return httpOnly ;
117194 }
118195
196+ /**
197+ * Set cookie's http-only.
198+ *
199+ * @param httpOnly Cookie's http-only.
200+ * @return This cookie.
201+ */
119202 public Cookie setHttpOnly (boolean httpOnly ) {
120203 this .httpOnly = httpOnly ;
121204 return this ;
122205 }
123206
207+ /**
208+ * Secure cookie.
209+ *
210+ * @return Secure cookie flag.
211+ */
124212 public boolean isSecure () {
125213 return secure ;
126214 }
127215
216+ /**
217+ * Set cookie secure flag..
218+ *
219+ * @param secure Cookie's secure.
220+ * @return This cookie.
221+ */
128222 public @ Nonnull Cookie setSecure (boolean secure ) {
129223 this .secure = secure ;
130224 return this ;
131225 }
132226
227+ /**
228+ * Max age value:
229+ *
230+ * - <code>-1</code>: indicates a browser session. It is deleted when user closed the browser.
231+ * - <code>0</code>: indicates a cookie has expired and browser must delete the cookie.
232+ * - <code>positive value</code>: indicates the number of seconds from current date, where browser
233+ * must expires the cookie.
234+ *
235+ * @return Max age, in seconds.
236+ */
133237 public long getMaxAge () {
134238 return maxAge ;
135239 }
136240
137- public @ Nonnull Cookie setMaxAge (long maxAge ) {
138- this .maxAge = maxAge ;
139- return this ;
140- }
141-
241+ /**
242+ * Set max age value:
243+ *
244+ * - <code>-1</code>: indicates a browser session. It is deleted when user closed the browser.
245+ * - <code>0</code>: indicates a cookie has expired and browser must delete the cookie.
246+ * - <code>positive value</code>: indicates the number of seconds from current date, where browser
247+ * must expires the cookie.
248+ *
249+ * @param maxAge Cookie max age.
250+ * @return This options.
251+ */
142252 public @ Nonnull Cookie setMaxAge (@ Nonnull Duration maxAge ) {
143253 return setMaxAge (maxAge .getSeconds ());
144254 }
145255
256+ /**
257+ * Set max age value:
258+ *
259+ * - <code>-1</code>: indicates a browser session. It is deleted when user closed the browser.
260+ * - <code>0</code>: indicates a cookie has expired and browser must delete the cookie.
261+ * - <code>positive value</code>: indicates the number of seconds from current date, where browser
262+ * must expires the cookie.
263+ *
264+ * @param maxAge Cookie max age, in seconds.
265+ * @return This options.
266+ */
267+ public @ Nonnull Cookie setMaxAge (long maxAge ) {
268+ if (maxAge >= 0 ) {
269+ this .maxAge = maxAge ;
270+ } else {
271+ this .maxAge = -1 ;
272+ }
273+ return this ;
274+ }
275+
146276 @ Override public String toString () {
147- return name ;
277+ StringBuilder buff = new StringBuilder ();
278+ buff .append (name ).append ("=" );
279+ if (value != null ) {
280+ buff .append (value );
281+ }
282+ return buff .toString ();
148283 }
149284
150- public String toCookieString () {
285+ /**
286+ * Generates a cookie string. This is the value we sent to the client as <code>Set-Cookie</code>
287+ * header.
288+ *
289+ * @return Cookie string.
290+ */
291+ public @ Nonnull String toCookieString () {
151292 StringBuilder sb = new StringBuilder ();
152293
153294 // name = value
0 commit comments