Skip to content
This repository was archived by the owner on Jul 31, 2022. It is now read-only.

Commit 41e918d

Browse files
committed
Added documentation
1 parent 9dc7e16 commit 41e918d

File tree

1 file changed

+205
-5
lines changed

1 file changed

+205
-5
lines changed

src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpCookie.java

Lines changed: 205 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
* An HTTP Cookie to be sent in a response header.
88
*
99
* @see SimpleHttpExchange
10+
* @see Builder
1011
* @since 02.00.00
11-
* @version 02.00.00
12+
* @version 02.03.00
1213
* @author Ktt Development
1314
*/
1415
public class SimpleHttpCookie {
@@ -29,7 +30,7 @@ public class SimpleHttpCookie {
2930
/**
3031
* 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.
3132
*
32-
* @deprecated Use {@link Builder} class instead.
33+
* @deprecated Use {@link Builder} class instead. This method will be removed in the future.
3334
*
3435
* @param name name of the cookie
3536
* @param value value of the cookie
@@ -43,7 +44,6 @@ public class SimpleHttpCookie {
4344
*
4445
* @since 02.00.00
4546
* @author Ktt Development
46-
*
4747
*/
4848
@Deprecated
4949
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){
@@ -99,12 +99,22 @@ public final String toString(){
9999
return OUT.toString();
100100
}
101101

102+
private final SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss");
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+
*/
102112
public final String toCookieHeaderString(){
103113
final StringBuilder OUT = new StringBuilder();
104114

105115
OUT.append(name).append("=").append(value);
106116
if(expires != null)
107-
OUT.append("; Expires=").append(new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss").format(expires)).append(" GMT");
117+
OUT.append("; Expires=").append(sdf.format(expires)).append(" GMT");
108118
if(maxAge != null)
109119
OUT.append("; Max-Age=").append(maxAge);
110120
if(domain != null)
@@ -121,6 +131,15 @@ public final String toCookieHeaderString(){
121131
return OUT.toString();
122132
}
123133

134+
/**
135+
* Builder class for {@link SimpleHttpCookie}.
136+
*
137+
* @see SimpleHttpCookie
138+
*
139+
* @since 02.03.00
140+
* @version 02.03.00
141+
* @author Ktt Development
142+
*/
124143
public static class Builder {
125144

126145
private final String name;
@@ -134,77 +153,258 @@ public static class Builder {
134153
private boolean secure = false;
135154
private boolean httpOnly = false;
136155

156+
/**
157+
* Creates an HTTP cookie builder given a key and value.
158+
*
159+
* @param name Name of the cookie
160+
* @param value Value of the cookie
161+
*
162+
* @since 02.03.00
163+
* @author Ktt Development
164+
*/
137165
public Builder(final String name, final String value){
138166
if((this.name = name) == null)
139167
throw new NullPointerException("Cookie name can not be null");
140168
if((this.value = value) == null)
141169
throw new NullPointerException("Cookie value can not be null");
142170
}
143171

172+
/**
173+
* Returns the name of the cookie.
174+
*
175+
* @return cookie name
176+
*
177+
* @since 02.00.00
178+
* @author Ktt Development
179+
*/
144180
public final String getName(){
145181
return name;
146182
}
147183

184+
/**
185+
* Returns the value of the cookie.
186+
*
187+
* @return cookie value
188+
*
189+
* @since 02.03.00
190+
* @author Ktt Development
191+
*/
148192
public final String getValue(){
149193
return value;
150194
}
151195

196+
/**
197+
* Returns the domain to send the cookie to.
198+
*
199+
* @return domain to send the cookie to
200+
*
201+
* @see #setDomain(String)
202+
*
203+
* @since 02.03.00
204+
* @author Ktt Development
205+
*/
152206
public final String getDomain(){
153207
return domain;
154208
}
155209

210+
/**
211+
* Sets the domain of the cookie.
212+
*
213+
* @param domain what domain to send the cookie to
214+
*
215+
* @see #getDomain()
216+
*
217+
* @since 02.03.00
218+
* @author Ktt Development
219+
*/
156220
public final void setDomain(final String domain){
157221
this.domain = domain;
158222
}
159223

224+
/**
225+
* Returns the path to send the cookie to.
226+
*
227+
* @return what path to send the cookie to
228+
*
229+
* @see #setPath(String)
230+
*
231+
* @since 02.03.00
232+
* @author Ktt Development
233+
*/
160234
public final String getPath(){
161235
return path;
162236
}
163237

238+
/**
239+
* Sets the path of the cookie.
240+
*
241+
* @param path what path to send the cookie to
242+
*
243+
* @see #getPath()
244+
*
245+
* @since 02.03.00
246+
* @author Ktt Development
247+
*/
164248
public final void setPath(final String path){
165249
this.path = path;
166250
}
167251

168-
public final String getSameSite(){
252+
/**
253+
* Returns if the cookie should be prevented from being sent cross-site.
254+
*
255+
* @return if the cookie should be prevented from being sent cross-site.
256+
*
257+
* @see #setSameSite(String)
258+
*
259+
* @since 02.03.00
260+
* @author Ktt Development
261+
*/
262+
public final String isSameSite(){
169263
return sameSite;
170264
}
171265

266+
/**
267+
* Sets if the cookie should be prevented from being sent cross-site.
268+
*
269+
* @param sameSite if the cookie should be prevented from being sent cross-site
270+
*
271+
* @see #isSameSite()
272+
*
273+
* @since 02.03.00
274+
* @author Ktt Development
275+
*/
172276
public final void setSameSite(final String sameSite){
173277
this.sameSite = sameSite;
174278
}
175279

280+
/**
281+
* Returns when the cookie should expire.
282+
*
283+
* @return when the cookie should expire.
284+
*
285+
* @see #setExpires(Date)
286+
* @see #getMaxAge()
287+
* @see #setMaxAge(int)
288+
*
289+
* @since 02.03.00
290+
* @author Ktt Development
291+
*/
176292
public final Date getExpires(){
177293
return expires;
178294
}
179295

296+
/**
297+
* Sets when the cookie should expire.
298+
*
299+
* @param expires when the cookie should expire
300+
*
301+
* @see #getExpires()
302+
* @see #getMaxAge()
303+
* @see #setMaxAge(int)
304+
*
305+
* @since 02.03.00
306+
* @author Ktt Development
307+
*/
180308
public final void setExpires(final Date expires){
181309
this.expires = expires;
182310
}
183311

312+
/**
313+
* Returns how long the cookie should exist for.
314+
*
315+
* @return how long the cookie should exist for
316+
*
317+
* @see #getExpires()
318+
* @see #setExpires(Date)
319+
* @see #setMaxAge(int)
320+
*
321+
* @since 02.03.00
322+
* @author Ktt Development
323+
*/
184324
public final int getMaxAge(){
185325
return maxAge;
186326
}
187327

328+
/**
329+
* Sets how long the cookie should exist for.
330+
*
331+
* @param maxAge how long the cookie should exist for
332+
*
333+
* @see #getExpires()
334+
* @see #setExpires(Date)
335+
* @see #getMaxAge()
336+
*
337+
* @since 02.03.00
338+
* @author Ktt Development
339+
*/
188340
public final void setMaxAge(final int maxAge){
189341
this.maxAge = maxAge;
190342
}
191343

344+
/**
345+
* Returns if the cookie must be sent over a secure/HTTPS protocol.
346+
*
347+
* @return if the cookie must be sent over a secure/HTTPS protocol
348+
*
349+
* @see #isSecure()
350+
*
351+
* @since 02.03.00
352+
* @author Ktt Development
353+
*/
192354
public final boolean isSecure(){
193355
return secure;
194356
}
195357

358+
/**
359+
* Sets if the cookie must be sent over a secure/HTTPS protocol.
360+
*
361+
* @param secure if the cookie must be sent over a secure/HTTPS protocol.
362+
*
363+
* @see #setSecure(boolean)
364+
*
365+
* @since 02.03.00
366+
* @author Ktt Development
367+
*/
196368
public final void setSecure(final boolean secure){
197369
this.secure = secure;
198370
}
199371

372+
/**
373+
* Returns if only the server should have access to the cookies.
374+
*
375+
* @return if only the server should have access to the cookies.
376+
*
377+
* @see #setHttpOnly(boolean)
378+
*
379+
* @since 02.03.00
380+
* @author Ktt Development
381+
*/
200382
public final boolean isHttpOnly(){
201383
return httpOnly;
202384
}
203385

386+
/**
387+
* Sets if only the server should have access to the cookies.
388+
*
389+
* @param httpOnly if only the server should have access to the cookies
390+
*
391+
* @see #isHttpOnly()
392+
*
393+
* @since 02.03.00
394+
* @author Ktt Development
395+
*/
204396
public final void setHttpOnly(final boolean httpOnly){
205397
this.httpOnly = httpOnly;
206398
}
207399

400+
/**
401+
* Returns the completed cookie.
402+
*
403+
* @return simple http cookie
404+
*
405+
* @since 02.03.00
406+
* @author Ktt Development
407+
*/
208408
public final SimpleHttpCookie build(){
209409
return new SimpleHttpCookie(name,value,domain,path,sameSite,expires,maxAge,secure,httpOnly);
210410
}

0 commit comments

Comments
 (0)