org.zodiac.sdk.nio.http.common.HttpHeaderCookie Maven / Gradle / Ivy
Show all versions of zodiac-sdk-nio Show documentation
package org.zodiac.sdk.nio.http.common;
import org.zodiac.sdk.toolkit.util.StringUtil;
public class HttpHeaderCookie {
private final String name;
private final String value;
private String expires;
private String domain;
private String path = "/";
/**
* True
if the browser is sending cookies only over a secure protocol, or false
if the
* browser can send cookies using any protocol.
*
*/
private boolean secure;
/**
* Version 1 complies with RFC 2109, and version 0 complies with the original cookie specification drafted by
* Netscape. Cookies provided by a browser use and identify the browser's cookie version.
* 0 if the cookie complies with the original Netscape specification; 1 if the cookie complies with RFC 2109.
*/
private int version = 0;
/**
* Marks or unmarks this Cookie as HttpOnly.
*
*
* If isHttpOnly is set to true, this cookie is marked as HttpOnly, by adding the
* HttpOnly attribute to it.
*
*
* HttpOnly cookies are not supposed to be exposed to client-side scripting code, and may therefore help
* mitigate certain kinds of cross-site scripting attacks.
*
* True if this cookie is to be marked as HttpOnly, false otherwise.
*
*/
private boolean httpOnly = false;
public HttpHeaderCookie(String name, String value) {
if (StringUtil.isBlank(name)) {
throw new IllegalArgumentException("'name' is required and must not be empty.");
}
this.name = name;
this.value = (value != null ? value : "");
}
public HttpHeaderCookie(String name, String value, String expires, String domain, String path, boolean secure,
int version, boolean httpOnly) {
this.name = name;
this.value = value;
this.expires = expires;
this.domain = domain;
this.path = path;
this.secure = secure;
this.version = version;
this.httpOnly = httpOnly;
}
public String getName() {
return this.name;
}
public String getValue() {
return this.value;
}
public String getExpires() {
return expires;
}
public String getDomain() {
return domain;
}
public String getPath() {
return path;
}
public boolean isSecure() {
return secure;
}
public int getVersion() {
return version;
}
public boolean isHttpOnly() {
return httpOnly;
}
public HttpHeaderCookie setExpires(String expires) {
this.expires = expires;
return this;
}
public HttpHeaderCookie setPath(String path) {
this.path = path;
return this;
}
public HttpHeaderCookie setSecure(boolean secure) {
this.secure = secure;
return this;
}
public HttpHeaderCookie setVersion(int version) {
this.version = version;
return this;
}
public HttpHeaderCookie setHttpOnly(boolean httpOnly) {
this.httpOnly = httpOnly;
return this;
}
public HttpHeaderCookie setDomain(String domain) {
this.domain = domain;
return this;
}
@Override
public int hashCode() {
return this.name.hashCode();
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof HttpHeaderCookie)) {
return false;
}
HttpHeaderCookie otherCookie = (HttpHeaderCookie) other;
return (this.name.equalsIgnoreCase(otherCookie.getName()));
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(this.name);
sb.append("=");
sb.append(this.value);
sb.append(";");
if (null != this.expires) {
sb.append("expires=");
sb.append(this.expires);
sb.append(";");
}
if (null != this.domain) {
sb.append("domain=");
sb.append(this.domain);
sb.append(";");
}
if (null != this.path) {
sb.append("path=");
sb.append(this.path);
sb.append(";");
}
sb.append("secure=");
sb.append(this.secure);sb.append(";");
sb.append("httpOnly=");
sb.append(this.httpOnly);
sb.append(";");
sb.append("version=");
sb.append(this.version);
sb.append(";");
return sb.toString();
}
public static void main(String[] args) {
HttpHeaderCookie cookie1 = new HttpHeaderCookie("cookie-name1", "cookie-value1");
cookie1.setDomain("www.mytest.com");
HttpHeaderCookie cookie2 = new HttpHeaderCookie("cookie-name2", "cookie-value2");
cookie2.setPath("/path1/path2/mypath").setSecure(true);
HttpHeaderCookie cookie3 = new HttpHeaderCookie("cookie-name3", "cookie-value3");
cookie3.setVersion(1000).setHttpOnly(true).setExpires("44444");
HttpHeaderCookie cookie4 = new HttpHeaderCookie("cookie-name4", "cookie-value4");
cookie4.setDomain("*.mydomain.io").setVersion(4).setSecure(true).setExpires("44444");
System.out.println(cookie1);
System.out.println(cookie2);
System.out.println(cookie3);
System.out.println(cookie4);
}
}