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

Commit 9dc7e16

Browse files
committed
Start implementation
1 parent 8c8cd99 commit 9dc7e16

File tree

1 file changed

+119
-1
lines changed

1 file changed

+119
-1
lines changed

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

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public class SimpleHttpCookie {
2929
/**
3030
* 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.
3131
*
32+
* @deprecated Use {@link Builder} class instead.
33+
*
3234
* @param name name of the cookie
3335
* @param value value of the cookie
3436
* @param domain what domain to send the cookie to
@@ -41,7 +43,9 @@ public class SimpleHttpCookie {
4143
*
4244
* @since 02.00.00
4345
* @author Ktt Development
46+
*
4447
*/
48+
@Deprecated
4549
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");
@@ -63,14 +67,16 @@ 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(){
7581
StringBuilder OUT = new StringBuilder();
7682

@@ -93,4 +99,116 @@ public final String toString(){
9399
return OUT.toString();
94100
}
95101

102+
public final String toCookieHeaderString(){
103+
final StringBuilder OUT = new StringBuilder();
104+
105+
OUT.append(name).append("=").append(value);
106+
if(expires != null)
107+
OUT.append("; Expires=").append(new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss").format(expires)).append(" GMT");
108+
if(maxAge != null)
109+
OUT.append("; Max-Age=").append(maxAge);
110+
if(domain != null)
111+
OUT.append("; Domain=").append(domain);
112+
if(path != null)
113+
OUT.append("; Path=").append(path);
114+
if(secure)
115+
OUT.append("; Secure=").append(secure);
116+
if(httpOnly)
117+
OUT.append("; HttpOnly=").append(httpOnly);
118+
if(sameSite != null)
119+
OUT.append("; SameSite=").append(sameSite);
120+
121+
return OUT.toString();
122+
}
123+
124+
public static class Builder {
125+
126+
private final String name;
127+
private final String value;
128+
129+
private String domain;
130+
private String path;
131+
private String sameSite;
132+
private Date expires;
133+
private int maxAge;
134+
private boolean secure = false;
135+
private boolean httpOnly = false;
136+
137+
public Builder(final String name, final String value){
138+
if((this.name = name) == null)
139+
throw new NullPointerException("Cookie name can not be null");
140+
if((this.value = value) == null)
141+
throw new NullPointerException("Cookie value can not be null");
142+
}
143+
144+
public final String getName(){
145+
return name;
146+
}
147+
148+
public final String getValue(){
149+
return value;
150+
}
151+
152+
public final String getDomain(){
153+
return domain;
154+
}
155+
156+
public final void setDomain(final String domain){
157+
this.domain = domain;
158+
}
159+
160+
public final String getPath(){
161+
return path;
162+
}
163+
164+
public final void setPath(final String path){
165+
this.path = path;
166+
}
167+
168+
public final String getSameSite(){
169+
return sameSite;
170+
}
171+
172+
public final void setSameSite(final String sameSite){
173+
this.sameSite = sameSite;
174+
}
175+
176+
public final Date getExpires(){
177+
return expires;
178+
}
179+
180+
public final void setExpires(final Date expires){
181+
this.expires = expires;
182+
}
183+
184+
public final int getMaxAge(){
185+
return maxAge;
186+
}
187+
188+
public final void setMaxAge(final int maxAge){
189+
this.maxAge = maxAge;
190+
}
191+
192+
public final boolean isSecure(){
193+
return secure;
194+
}
195+
196+
public final void setSecure(final boolean secure){
197+
this.secure = secure;
198+
}
199+
200+
public final boolean isHttpOnly(){
201+
return httpOnly;
202+
}
203+
204+
public final void setHttpOnly(final boolean httpOnly){
205+
this.httpOnly = httpOnly;
206+
}
207+
208+
public final SimpleHttpCookie build(){
209+
return new SimpleHttpCookie(name,value,domain,path,sameSite,expires,maxAge,secure,httpOnly);
210+
}
211+
212+
}
213+
96214
}

0 commit comments

Comments
 (0)