001/** 002 * 003 * Copyright 2020 Paul Schaub 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smackx.urldata.http.element; 018 019import org.jivesoftware.smack.packet.XmlEnvironment; 020import org.jivesoftware.smack.util.EqualsUtil; 021import org.jivesoftware.smack.util.HashCode; 022import org.jivesoftware.smack.util.XmlStringBuilder; 023 024public class CookieElement extends NameValuePairElement { 025 026 public static final String ELEMENT = "cookie"; 027 public static final String PREFIX = "http"; 028 public static final String ATTR_DOMAIN = "domain"; 029 public static final String ATTR_MAX_AGE = "max-age"; 030 public static final String ATTR_PATH = "path"; 031 public static final String ATTR_COMMENT = "comment"; 032 public static final String ATTR_VERSION = "version"; 033 public static final String ATTR_SECURE = "secure"; 034 035 private final String domain; 036 private final Integer maxAge; 037 private final String path; 038 private final String comment; 039 private final String version; 040 private final Boolean secure; 041 042 public CookieElement(String name, String value) { 043 this(name, value, null, null, null, null, null, null); 044 } 045 046 public CookieElement(String name, String value, String domain, Integer maxAge, String path, String comment, String version, Boolean secure) { 047 super(name, value); 048 this.domain = domain; 049 this.maxAge = maxAge; 050 this.path = path; 051 this.comment = comment; 052 this.version = version; 053 this.secure = secure; 054 } 055 056 public String getPath() { 057 return path; 058 } 059 060 public int getMaxAge() { 061 return maxAge == null ? 0 : maxAge; 062 } 063 064 public String getDomain() { 065 return domain; 066 } 067 068 public String getComment() { 069 return comment; 070 } 071 072 public String getVersion() { 073 return version == null ? "1.0" : version; 074 } 075 076 public boolean isSecure() { 077 return secure != null && secure; 078 } 079 080 @Override 081 public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) { 082 XmlStringBuilder sb = addCommonXml(new XmlStringBuilder(this)) 083 .optAttribute(ATTR_DOMAIN, domain) 084 .optAttribute(ATTR_MAX_AGE, maxAge) 085 .optAttribute(ATTR_PATH, path) 086 .optAttribute(ATTR_COMMENT, comment) 087 .optAttribute(ATTR_VERSION, version); 088 if (secure != null) { 089 sb.attribute(ATTR_SECURE, secure); 090 } 091 return sb.closeEmptyElement(); 092 } 093 094 @Override 095 public String getElementName() { 096 return PREFIX + ':' + ELEMENT; 097 } 098 099 @Override 100 public int hashCode() { 101 return HashCode.builder() 102 .append(getElementName()) 103 .append(getName()) 104 .append(getValue()) 105 .append(getDomain()) 106 .append(getMaxAge()) 107 .append(getPath()) 108 .append(getComment()) 109 .append(getVersion()) 110 .append(isSecure()) 111 .build(); 112 } 113 114 @Override 115 public boolean equals(Object obj) { 116 return EqualsUtil.equals(this, obj, (equalsBuilder, other) -> 117 equalsBuilder 118 .append(getElementName(), other.getElementName()) 119 .append(getName(), other.getName()) 120 .append(getValue(), other.getValue()) 121 .append(getDomain(), other.getDomain()) 122 .append(getMaxAge(), other.getMaxAge()) 123 .append(getPath(), other.getPath()) 124 .append(getComment(), other.getComment()) 125 .append(getVersion(), other.getVersion()) 126 .append(isSecure(), other.isSecure())); 127 } 128}