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

Commit 6521ce4

Browse files
authored
Added Http Cookie Builder
Http Cookie Builder
2 parents 8c8cd99 + 5e02232 commit 6521ce4

File tree

4 files changed

+354
-12
lines changed

4 files changed

+354
-12
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.kttdevelopment</groupId>
88
<artifactId>simplehttpserver</artifactId>
9-
<version>03.02.00</version>
9+
<version>03.03.00</version>
1010
<packaging>jar</packaging>
1111

1212
<url>https://github.com/Ktt-Development/simplehttpserver</url>

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

Lines changed: 323 additions & 7 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 {
@@ -22,13 +23,15 @@ public class SimpleHttpCookie {
2223

2324
private final Date expires;
2425
private final Integer maxAge;
25-
private final boolean
26+
private final Boolean
2627
secure,
2728
httpOnly;
2829

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
*
33+
* @deprecated Use {@link Builder} class instead. This method will be removed in the future.
34+
*
3235
* @param name name of the cookie
3336
* @param value value of the cookie
3437
* @param domain what domain to send the cookie to
@@ -42,7 +45,8 @@ public class SimpleHttpCookie {
4245
* @since 02.00.00
4346
* @author Ktt Development
4447
*/
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){
4650
if(name == null)
4751
throw new NullPointerException("Cookie name can not be null");
4852
else
@@ -63,17 +67,50 @@ public SimpleHttpCookie(final String name, final String value, final String doma
6367
/**
6468
* Converts the cookie to a readable string for the cookie header.
6569
*
70+
* @deprecated Use {@link #toCookieHeaderString()} instead.
71+
*
6672
* @return cookie header
6773
*
6874
* @see SimpleHttpExchange#setCookie(SimpleHttpCookie)
6975
* @since 02.00.00
7076
* @author Ktt Development
7177
*/
7278
@SuppressWarnings({"ConstantConditions", "SpellCheckingInspection"})
73-
@Override
79+
@Override @Deprecated
7480
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");
76103

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();
77114
OUT.append(name).append("=").append(value);
78115
if(expires != null)
79116
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(){
83120
OUT.append("; Domain=").append(domain);
84121
if(path != null)
85122
OUT.append("; Path=").append(path);
86-
if(secure)
123+
if(secure != null && secure)
87124
OUT.append("; Secure=").append(secure);
88-
if(httpOnly)
125+
if(httpOnly != null && httpOnly)
89126
OUT.append("; HttpOnly=").append(httpOnly);
90127
if(sameSite != null)
91128
OUT.append("; SameSite=").append(sameSite);
92129

93130
return OUT.toString();
94131
}
95132

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+
96412
}

0 commit comments

Comments
 (0)