All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.sdk.nio.http.common.SerializableHttpCookie Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
package org.zodiac.sdk.nio.http.common;

import java.io.Serializable;
import java.net.HttpCookie;
import java.util.Objects;

/**
 * @see SerializableHttpCookie.java
 * @author hyq
 *
 */
public class SerializableHttpCookie implements Serializable {

    private static final long serialVersionUID = 5207300198744711233L;

    private String name;

    private String value;

    private String comment;

    private String commentURL;
    private boolean toDiscard;

    private String domain;
    private long maxAge = -1;

    private String path;

    private String portlist;
    private boolean secure;
    private boolean httpOnly;
    private int version = 1;

    public SerializableHttpCookie() {
    }

    public static SerializableHttpCookie of(HttpCookie cookie) {
        SerializableHttpCookie newCookie = new SerializableHttpCookie();
        newCookie.name = cookie.getName();
        newCookie.value = cookie.getValue();
        newCookie.comment = cookie.getComment();
        newCookie.commentURL = cookie.getCommentURL();
        newCookie.toDiscard = cookie.getDiscard();
        newCookie.domain = cookie.getDomain();
        newCookie.maxAge = cookie.getMaxAge();
        newCookie.path = cookie.getPath();
        newCookie.portlist = cookie.getPortlist();
        newCookie.secure = cookie.getSecure();
        newCookie.version = cookie.getVersion();

        /*for Android (API level 24) https://developer.android.com/reference/java/net/HttpCookie.html#isHttpOnly()*/
        try {
            newCookie.httpOnly = cookie.isHttpOnly();
        } catch (NoSuchMethodError e) {
            newCookie.httpOnly = false;
        }

        return newCookie;
    }

    public HttpCookie toHttpCookie() {
        if (this.name == null) {
            throw new IllegalStateException("Illegal cookie name");
        }

        HttpCookie cookie = new HttpCookie(this.name, this.value);
        cookie.setComment(this.comment);
        cookie.setCommentURL(this.commentURL);
        cookie.setDiscard(this.toDiscard);
        cookie.setDomain(this.domain);
        cookie.setMaxAge(this.maxAge);
        cookie.setPath(this.path);
        cookie.setPortlist(this.portlist);
        cookie.setSecure(this.secure);
        cookie.setVersion(this.version);

        /*for Android (API level 24) https://developer.android.com/reference/java/net/HttpCookie.html#setHttpOnly(boolean)*/
        try {
            cookie.setHttpOnly(this.httpOnly);
        } catch (NoSuchMethodError e) {
        }

        return cookie;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public String getCommentURL() {
        return commentURL;
    }

    public void setCommentURL(String commentURL) {
        this.commentURL = commentURL;
    }

    public boolean getDiscard() {
        return toDiscard;
    }

    public void setDiscard(boolean toDiscard) {
        this.toDiscard = toDiscard;
    }

    public String getDomain() {
        return domain;
    }

    public void setDomain(String domain) {
        this.domain = domain;
    }

    public long getMaxAge() {
        return maxAge;
    }

    public void setMaxAge(long maxAge) {
        this.maxAge = maxAge;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getPortlist() {
        return portlist;
    }

    public void setPortlist(String portlist) {
        this.portlist = portlist;
    }

    public boolean getSecure() {
        return secure;
    }

    public void setSecure(boolean secure) {
        this.secure = secure;
    }

    public boolean isHttpOnly() {
        return httpOnly;
    }

    public void setHttpOnly(boolean httpOnly) {
        this.httpOnly = httpOnly;
    }

    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        SerializableHttpCookie other = (SerializableHttpCookie)obj;
        return Objects.equals(name, other.name);
    }

    @Override
    public String toString() {
        return "SerializableHttpCookie [name=" + name + ", value=" + value + ", comment=" + comment + ", commentURL="
            + commentURL + ", toDiscard=" + toDiscard + ", domain=" + domain + ", maxAge=" + maxAge + ", path=" + path
            + ", portlist=" + portlist + ", secure=" + secure + ", httpOnly=" + httpOnly + ", version=" + version + "]";
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy