Skip to content

Commit

Permalink
Change Context.Cookie into an interface
Browse files Browse the repository at this point in the history
  • Loading branch information
rbygrave committed Nov 24, 2024
1 parent 3aa03c8 commit 0fc9408
Show file tree
Hide file tree
Showing 2 changed files with 216 additions and 109 deletions.
186 changes: 77 additions & 109 deletions avaje-jex/src/main/java/io/avaje/jex/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@

import java.io.InputStream;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -369,135 +366,106 @@ default Context render(String name) {
*/
BasicAuthCredentials basicAuthCredentials();

final class Cookie {
private static final ZonedDateTime EXPIRED = ZonedDateTime.of(LocalDateTime.of(2000, 1, 1, 0, 0, 0), ZoneId.of("GMT"));
private static final DateTimeFormatter RFC_1123_DATE_TIME = DateTimeFormatter.RFC_1123_DATE_TIME;
private static final String PARAM_SEPARATOR = "; ";
private final String name; // NAME= ... "$Name" style is reserved
private final String value; // value of NAME
private String domain; // ;Domain=VALUE ... domain that sees cookie
private ZonedDateTime expires;
private Duration maxAge;// = -1; // ;Max-Age=VALUE ... cookies auto-expire
private String path; // ;Path=VALUE ... URLs that see the cookie
private boolean secure; // ;Secure ... e.g. use SSL
private boolean httpOnly;

private Cookie(String name, String value) {
if (name == null || name.isEmpty()) {
throw new IllegalArgumentException("name required");
}
this.name = name;
this.value = value;
}
interface Cookie {

public static Cookie expired(String name) {
return new Cookie(name, "").expires(EXPIRED);
/**
* Return an expired cookie given the name.
*
* @param name The name of the cookie.
* @return The expired cookie
*/
static Cookie expired(String name) {
return DCookie.expired(name);
}

public static Cookie of(String name, String value) {
return new Cookie(name, value);
/**
* Return a new cookie given the name and value.
*
* @param name The name of the cookie
* @param value The cookie value
* @return The new cookie
*/
static Cookie of(String name, String value) {
return DCookie.of(name, value);
}

public String name() {
return name;
}
/**
* Return the name.
*/
String name();

public String value() {
return value;
}
/**
* Return the value.
*/
String value();

public String domain() {
return domain;
}
/**
* Return the domain.
*/
String domain();

public Cookie domain(String domain) {
this.domain = domain;
return this;
}
/**
* Set the domain.
*/
Cookie domain(String domain);

public Duration maxAge() {
return maxAge;
}
/**
* Return the max age.
*/
Duration maxAge();

public Cookie maxAge(Duration maxAge) {
this.maxAge = maxAge;
return this;
}
/**
* Set the max age.
*/
Cookie maxAge(Duration maxAge);

public ZonedDateTime expires() {
return expires;
}
/**
* Return the cookie expiration.
*/
ZonedDateTime expires();

public Cookie expires(ZonedDateTime expires) {
this.expires = expires;
return this;
}
/**
* Set when the cookie expires.
*/
Cookie expires(ZonedDateTime expires);

public String path() {
return path;
}
/**
* Return the path.
*/
String path();

public Cookie path(String path) {
this.path = path;
return this;
}
/**
* Set the path.
*/
Cookie path(String path);

public boolean secure() {
return secure;
}
/**
* Return the secure attribute of the cookie.
*/
boolean secure();

public Cookie secure(boolean secure) {
this.secure = secure;
return this;
}
/**
* Set the secure attribute of the cookie.
*/
Cookie secure(boolean secure);

public boolean httpOnly() {
return httpOnly;
}
/**
* Return the httpOnly attribute of the cookie.
*/
boolean httpOnly();

public Cookie httpOnly(boolean httpOnly) {
this.httpOnly = httpOnly;
return this;
}
/**
* Set the httpOnly attribute of the cookie.
*/
Cookie httpOnly(boolean httpOnly);

/**
* Returns content of this instance as a 'Set-Cookie:' header value specified
* Returns content of the cookie as a 'Set-Cookie:' header value specified
* by <a href="https://tools.ietf.org/html/rfc6265">RFC6265</a>.
*/
@Override
public String toString() {
StringBuilder result = new StringBuilder(60);
result.append(name).append('=').append(value);
if (expires != null) {
result.append(PARAM_SEPARATOR);
result.append("Expires=");
result.append(expires.format(RFC_1123_DATE_TIME));
}
if ((maxAge != null) && !maxAge.isNegative() && !maxAge.isZero()) {
result.append(PARAM_SEPARATOR);
result.append("Max-Age=");
result.append(maxAge.getSeconds());
}
if (domain != null) {
result.append(PARAM_SEPARATOR);
result.append("Domain=");
result.append(domain);
}
if (path != null) {
result.append(PARAM_SEPARATOR);
result.append("Path=");
result.append(path);
}
if (secure) {
result.append(PARAM_SEPARATOR);
result.append("Secure");
}
if (httpOnly) {
result.append(PARAM_SEPARATOR);
result.append("HttpOnly");
}
return result.toString();
}
String toString();

}

}
139 changes: 139 additions & 0 deletions avaje-jex/src/main/java/io/avaje/jex/DCookie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package io.avaje.jex;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

final class DCookie implements Context.Cookie {

private static final ZonedDateTime EXPIRED = ZonedDateTime.of(LocalDateTime.of(2000, 1, 1, 0, 0, 0), ZoneId.of("GMT"));
private static final DateTimeFormatter RFC_1123_DATE_TIME = DateTimeFormatter.RFC_1123_DATE_TIME;
private static final String PARAM_SEPARATOR = "; ";
private final String name; // NAME= ... "$Name" style is reserved
private final String value; // value of NAME
private String domain; // ;Domain=VALUE ... domain that sees cookie
private ZonedDateTime expires;
private Duration maxAge;// = -1; // ;Max-Age=VALUE ... cookies auto-expire
private String path; // ;Path=VALUE ... URLs that see the cookie
private boolean secure; // ;Secure ... e.g. use SSL
private boolean httpOnly;

private DCookie(String name, String value) {
if (name == null || name.isEmpty()) {
throw new IllegalArgumentException("name required");
}
this.name = name;
this.value = value;
}

static Context.Cookie expired(String name) {
return new DCookie(name, "").expires(EXPIRED);
}

static Context.Cookie of(String name, String value) {
return new DCookie(name, value);
}

@Override
public String name() {
return name;
}

@Override
public String value() {
return value;
}

@Override
public String domain() {
return domain;
}

@Override
public Context.Cookie domain(String domain) {
this.domain = domain;
return this;
}

@Override
public Duration maxAge() {
return maxAge;
}

@Override
public Context.Cookie maxAge(Duration maxAge) {
this.maxAge = maxAge;
return this;
}

@Override
public ZonedDateTime expires() {
return expires;
}

@Override
public Context.Cookie expires(ZonedDateTime expires) {
this.expires = expires;
return this;
}

@Override
public String path() {
return path;
}

@Override
public Context.Cookie path(String path) {
this.path = path;
return this;
}

@Override
public boolean secure() {
return secure;
}

@Override
public Context.Cookie secure(boolean secure) {
this.secure = secure;
return this;
}

@Override
public boolean httpOnly() {
return httpOnly;
}

@Override
public Context.Cookie httpOnly(boolean httpOnly) {
this.httpOnly = httpOnly;
return this;
}

@Override
public String toString() {
StringBuilder result = new StringBuilder(60);
result.append(name).append('=').append(value);
if (expires != null) {
result.append(PARAM_SEPARATOR).append("Expires=").append(expires.format(RFC_1123_DATE_TIME));
}
if ((maxAge != null) && !maxAge.isNegative() && !maxAge.isZero()) {
result.append(PARAM_SEPARATOR).append("Max-Age=").append(maxAge.getSeconds());
}
if (domain != null) {
result.append(PARAM_SEPARATOR).append("Domain=").append(domain);
}
if (path != null) {
result.append(PARAM_SEPARATOR).append("Path=").append(path);
}
if (secure) {
result.append(PARAM_SEPARATOR).append("Secure");
}
if (httpOnly) {
result.append(PARAM_SEPARATOR).append("HttpOnly");
}
return result.toString();
}
}

0 comments on commit 0fc9408

Please sign in to comment.