package io.undertow.server.handlers;
import java.util.Arrays;
import java.util.Date;
import io.undertow.UndertowLogger;
import io.undertow.UndertowMessages;
public class CookieImpl implements Cookie {
private final String name;
private String value;
private String path;
private String domain;
private Integer maxAge;
private Date expires;
private boolean discard;
private boolean secure;
private boolean httpOnly;
private int version = 0;
private String comment;
private boolean sameSite;
private String sameSiteMode;
public CookieImpl(final String name, final String value) {
this.name = name;
this.value = value;
}
public CookieImpl(final String name) {
this.name = name;
}
public String getName() {
return name;
}
public String getValue() {
return value;
}
public CookieImpl setValue(final String value) {
this.value = value;
return this;
}
public String getPath() {
return path;
}
public CookieImpl setPath(final String path) {
this.path = path;
return this;
}
public String getDomain() {
return domain;
}
public CookieImpl setDomain(final String domain) {
this.domain = domain;
return this;
}
public Integer getMaxAge() {
return maxAge;
}
public CookieImpl setMaxAge(final Integer maxAge) {
this.maxAge = maxAge;
return this;
}
public boolean isDiscard() {
return discard;
}
public CookieImpl setDiscard(final boolean discard) {
this.discard = discard;
return this;
}
public boolean isSecure() {
return secure;
}
public CookieImpl setSecure(final boolean secure) {
this.secure = secure;
return this;
}
public int getVersion() {
return version;
}
public CookieImpl setVersion(final int version) {
this.version = version;
return this;
}
public boolean isHttpOnly() {
return httpOnly;
}
public CookieImpl setHttpOnly(final boolean httpOnly) {
this.httpOnly = httpOnly;
return this;
}
public Date getExpires() {
return expires;
}
public CookieImpl setExpires(final Date expires) {
this.expires = expires;
return this;
}
public String getComment() {
return comment;
}
public Cookie setComment(final String comment) {
this.comment = comment;
return this;
}
@Override
public boolean isSameSite() {
return sameSite;
}
@Override
public Cookie setSameSite(final boolean sameSite) {
this.sameSite = sameSite;
return this;
}
@Override
public String getSameSiteMode() {
return sameSiteMode;
}
@Override
public Cookie setSameSiteMode(final String mode) {
final String m = CookieSameSiteMode.lookupModeString(mode);
if (m != null) {
UndertowLogger.REQUEST_LOGGER.tracef("Setting SameSite mode to [%s] for cookie [%s]", m, this.name);
this.sameSiteMode = m;
this.setSameSite(true);
} else {
UndertowLogger.REQUEST_LOGGER.warnf(UndertowMessages.MESSAGES.invalidSameSiteMode(mode, Arrays.toString(CookieSameSiteMode.values())), "Ignoring specified SameSite mode [%s] for cookie [%s]", mode, this.name);
}
return this;
}
@Override
public final int hashCode() {
int result = 17;
result = 37 * result + (getName() == null ? 0 : getName().hashCode());
result = 37 * result + (getPath() == null ? 0 : getPath().hashCode());
result = 37 * result + (getDomain() == null ? 0 : getDomain().hashCode());
return result;
}
@Override
public final boolean equals(final Object other) {
if (other == this) return true;
if (!(other instanceof Cookie)) return false;
final Cookie o = (Cookie) other;
if (getName() == null && o.getName() != null) return false;
if (getName() != null && !getName().equals(o.getName())) return false;
if (getPath() == null && o.getPath() != null) return false;
if (getPath() != null && !getPath().equals(o.getPath())) return false;
if (getDomain() == null && o.getDomain() != null) return false;
if (getDomain() != null && !getDomain().equals(o.getDomain())) return false;
return true;
}
@Override
public final int compareTo(final Object other) {
return Cookie.super.compareTo(other);
}
@Override
public final String toString() {
return "{CookieImpl@" + System.identityHashCode(this) + " name=" + getName() + " path=" + getPath() + " domain=" + getDomain() + "}";
}
}