diff --git a/pom.xml b/pom.xml index 2075ef1..bd45f9d 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.kttdevelopment simplehttpserver - 03.02.00 + 03.03.00 jar https://github.com/Ktt-Development/simplehttpserver diff --git a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpCookie.java b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpCookie.java index b8eb12e..5d41a6d 100644 --- a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpCookie.java +++ b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpCookie.java @@ -7,8 +7,9 @@ * An HTTP Cookie to be sent in a response header. * * @see SimpleHttpExchange + * @see Builder * @since 02.00.00 - * @version 02.00.00 + * @version 02.03.00 * @author Ktt Development */ public class SimpleHttpCookie { @@ -22,13 +23,15 @@ public class SimpleHttpCookie { private final Date expires; private final Integer maxAge; - private final boolean + private final Boolean secure, httpOnly; /** * Creates an HTTP cookie. All fields except for name, secure, httpOnly, and value can be set to null if unused. * + * @deprecated Use {@link Builder} class instead. This method will be removed in the future. + * * @param name name of the cookie * @param value value of the cookie * @param domain what domain to send the cookie to @@ -42,7 +45,8 @@ public class SimpleHttpCookie { * @since 02.00.00 * @author Ktt Development */ - 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){ + @Deprecated + 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){ if(name == null) throw new NullPointerException("Cookie name can not be null"); else @@ -63,6 +67,8 @@ public SimpleHttpCookie(final String name, final String value, final String doma /** * Converts the cookie to a readable string for the cookie header. * + * @deprecated Use {@link #toCookieHeaderString()} instead. + * * @return cookie header * * @see SimpleHttpExchange#setCookie(SimpleHttpCookie) @@ -70,10 +76,41 @@ public SimpleHttpCookie(final String name, final String value, final String doma * @author Ktt Development */ @SuppressWarnings({"ConstantConditions", "SpellCheckingInspection"}) - @Override + @Override @Deprecated public final String toString(){ - StringBuilder OUT = new StringBuilder(); + final StringBuilder OUT = new StringBuilder(); + + OUT.append(name).append("=").append(value); + if(expires != null) + OUT.append("; Expires=").append(new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss").format(expires)).append(" GMT"); + if(maxAge != null) + OUT.append("; Max-Age=").append(maxAge); + if(domain != null) + OUT.append("; Domain=").append(domain); + if(path != null) + OUT.append("; Path=").append(path); + if(secure != null && secure) + OUT.append("; Secure=").append(secure); + if(httpOnly != null && httpOnly) + OUT.append("; HttpOnly=").append(httpOnly); + if(sameSite != null) + OUT.append("; SameSite=").append(sameSite); + + return OUT.toString(); + } + + private final SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss"); + /** + * Converts the cookie to a readable string for a response header. + * + * @return cookie as a header string + * + * @since 02.03.00 + * @author Ktt Development + */ + public final String toCookieHeaderString(){ + final StringBuilder OUT = new StringBuilder(); OUT.append(name).append("=").append(value); if(expires != null) OUT.append("; Expires=").append(new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss").format(expires)).append(" GMT"); @@ -83,9 +120,9 @@ public final String toString(){ OUT.append("; Domain=").append(domain); if(path != null) OUT.append("; Path=").append(path); - if(secure) + if(secure != null && secure) OUT.append("; Secure=").append(secure); - if(httpOnly) + if(httpOnly != null && httpOnly) OUT.append("; HttpOnly=").append(httpOnly); if(sameSite != null) OUT.append("; SameSite=").append(sameSite); @@ -93,4 +130,283 @@ public final String toString(){ return OUT.toString(); } + /** + * Builder class for {@link SimpleHttpCookie}. + * + * @see SimpleHttpCookie + * @since 02.03.00 + * @version 02.03.00 + * @author Ktt Development + */ + public static class Builder { + + private final String name; + private final String value; + + private String domain; + private String path; + private String sameSite; + private Date expires; + private Integer maxAge; + private Boolean secure; + private Boolean httpOnly; + + /** + * Creates an HTTP cookie builder given a key and value. + * + * @param name Name of the cookie + * @param value Value of the cookie + * + * @since 02.03.00 + * @author Ktt Development + */ + public Builder(final String name, final String value){ + if((this.name = name) == null) + throw new NullPointerException("Cookie name can not be null"); + if((this.value = value) == null) + throw new NullPointerException("Cookie value can not be null"); + } + + /** + * Returns the name of the cookie. + * + * @return cookie name + * + * @since 02.00.00 + * @author Ktt Development + */ + public final String getName(){ + return name; + } + + /** + * Returns the value of the cookie. + * + * @return cookie value + * + * @since 02.03.00 + * @author Ktt Development + */ + public final String getValue(){ + return value; + } + + /** + * Returns the domain to send the cookie to. + * + * @return domain to send the cookie to + * + * @see #setDomain(String) + * @since 02.03.00 + * @author Ktt Development + */ + public final String getDomain(){ + return domain; + } + + /** + * Sets the domain of the cookie. + * + * @param domain what domain to send the cookie to + * @return cookie builder + * + * @see #getDomain() + * @since 02.03.00 + * @author Ktt Development + */ + public final Builder setDomain(final String domain){ + this.domain = domain; + return this; + } + + /** + * Returns the path to send the cookie to. + * + * @return what path to send the cookie to + * + * @see #setPath(String) + * @since 02.03.00 + * @author Ktt Development + */ + public final String getPath(){ + return path; + } + + /** + * Sets the path of the cookie. + * + * @param path what path to send the cookie to + * @return cookie builder + * + * @see #getPath() + * @since 02.03.00 + * @author Ktt Development + */ + public final Builder setPath(final String path){ + this.path = path; + return this; + } + + /** + * Returns if the cookie should be prevented from being sent cross-site. + * + * @return if the cookie should be prevented from being sent cross-site. + * + * @see #setSameSite(String) + * @since 02.03.00 + * @author Ktt Development + */ + public final String isSameSite(){ + return sameSite; + } + + /** + * Sets if the cookie should be prevented from being sent cross-site. + * + * @param sameSite if the cookie should be prevented from being sent cross-site + * @return cookie builder + * + * @see #isSameSite() + * @since 02.03.00 + * @author Ktt Development + */ + public final Builder setSameSite(final String sameSite){ + this.sameSite = sameSite; + return this; + } + + /** + * Returns when the cookie should expire. + * + * @return when the cookie should expire. + * + * @see #setExpires(Date) + * @see #getMaxAge() + * @see #setMaxAge(int) + * @since 02.03.00 + * @author Ktt Development + */ + public final Date getExpires(){ + return expires; + } + + /** + * Sets when the cookie should expire. + * + * @param expires when the cookie should expire + * @return cookie builder + * + * @see #getExpires() + * @see #getMaxAge() + * @see #setMaxAge(int) + * @since 02.03.00 + * @author Ktt Development + */ + public final Builder setExpires(final Date expires){ + this.expires = expires; + return this; + } + + /** + * Returns how long the cookie should exist for. + * + * @return how long the cookie should exist for + * + * @see #getExpires() + * @see #setExpires(Date) + * @see #setMaxAge(int) + * @since 02.03.00 + * @author Ktt Development + */ + public final int getMaxAge(){ + return maxAge; + } + + /** + * Sets how long the cookie should exist for. + * + * @param maxAge how long the cookie should exist for + * @return cookie builder + * + * @see #getExpires() + * @see #setExpires(Date) + * @see #getMaxAge() + * @since 02.03.00 + * @author Ktt Development + */ + public final Builder setMaxAge(final int maxAge){ + this.maxAge = maxAge; + return this; + } + + /** + * Returns if the cookie must be sent over a secure/HTTPS protocol. + * + * @return if the cookie must be sent over a secure/HTTPS protocol + * + * @see #isSecure() + * @since 02.03.00 + * @author Ktt Development + */ + public final boolean isSecure(){ + return secure; + } + + /** + * Sets if the cookie must be sent over a secure/HTTPS protocol. + * + * @param secure if the cookie must be sent over a secure/HTTPS protocol. + * @return cookie builder + * + * @see #setSecure(boolean) + * @since 02.03.00 + * @author Ktt Development + */ + public final Builder setSecure(final boolean secure){ + this.secure = secure; + return this; + } + + /** + * Returns if only the server should have access to the cookies. + * + * @return if only the server should have access to the cookies. + * + * @see #setHttpOnly(boolean) + * @since 02.03.00 + * @author Ktt Development + */ + public final boolean isHttpOnly(){ + return httpOnly; + } + + /** + * Sets if only the server should have access to the cookies. + * + * @param httpOnly if only the server should have access to the cookies + * @return cookie builder + * + * @see #isHttpOnly() + * @since 02.03.00 + * @author Ktt Development + */ + public final Builder setHttpOnly(final boolean httpOnly){ + this.httpOnly = httpOnly; + return this; + } + + /** + * Returns the completed cookie. + * + * @return simple http cookie + * + * @since 02.03.00 + * @author Ktt Development + */ + public final SimpleHttpCookie build(){ + return new SimpleHttpCookie(name,value,domain,path,sameSite,expires,maxAge,secure,httpOnly); + } + + } + } diff --git a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchange.java b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchange.java index e54b489..236c038 100644 --- a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchange.java +++ b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchange.java @@ -304,21 +304,40 @@ static SimpleHttpExchange create(final HttpExchange exchange){ // /** - * Returns the client's existing cookies. + * Returns the client's existing cookies as a map of keys and values. * * @return client's cookies * + * @see #setCookie(String, String) + * @see #setCookie(SimpleHttpCookie) * @since 02.00.00 * @author Ktt Development */ public abstract HashMap getCookies(); + /** + * Sets a cookie in the response header. + * + * @param key name of cookie to set + * @param value value of cookie + * + * @see SimpleHttpCookie + * @see #setCookie(SimpleHttpCookie) + * @see #getCookies() + * @see #getResponseHeaders() + * @since 02.03.00 + * @author Ktt Development + */ + public abstract void setCookie(final String key, final String value); + /** * Sets a cookie in the response header. * * @param cookie cookie to set * * @see SimpleHttpCookie + * @see #setCookie(String, String) + * @see #getCookies() * @see #getResponseHeaders() * @since 02.00.00 * @author Ktt Development diff --git a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchangeImpl.java b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchangeImpl.java index 39130db..40771ff 100644 --- a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchangeImpl.java +++ b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchangeImpl.java @@ -17,7 +17,7 @@ * * @see SimpleHttpExchange * @since 02.00.00 - * @version 02.00.00 + * @version 02.03.00 * @author Ktt Development */ @SuppressWarnings("SpellCheckingInspection") @@ -309,9 +309,14 @@ public final HashMap getCookies(){ return new HashMap<>(cookies); } + @Override + public synchronized final void setCookie(final String key, final String value){ + setCookie(new SimpleHttpCookie.Builder(key,value).build()); + } + @Override public synchronized final void setCookie(final SimpleHttpCookie cookie){ - getResponseHeaders().add("Set-Cookie",cookie.toString()); + getResponseHeaders().add("Set-Cookie",cookie.toCookieHeaderString()); } // @@ -322,7 +327,9 @@ public synchronized final HttpSession getHttpSession(){ final HttpSession session; if((sessionId = cookies.get("__session-id")) == null || !HttpSession.sessions.containsKey(sessionId)){ session = HttpSession.create(); - setCookie(new SimpleHttpCookie("__session-id", session.getSessionID(), null, null, null, null, null, false, true)); + setCookie( + new SimpleHttpCookie.Builder("__session-id",session.getSessionID()).build() + ); }else{ session = HttpSession.sessions.get(sessionId); }