CookieElement.java

  1. /**
  2.  *
  3.  * Copyright 2020 Paul Schaub
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.urldata.http.element;

  18. import org.jivesoftware.smack.packet.XmlEnvironment;
  19. import org.jivesoftware.smack.util.EqualsUtil;
  20. import org.jivesoftware.smack.util.HashCode;
  21. import org.jivesoftware.smack.util.XmlStringBuilder;

  22. public class CookieElement extends NameValuePairElement {

  23.     public static final String ELEMENT = "cookie";
  24.     public static final String PREFIX = "http";
  25.     public static final String ATTR_DOMAIN = "domain";
  26.     public static final String ATTR_MAX_AGE = "max-age";
  27.     public static final String ATTR_PATH = "path";
  28.     public static final String ATTR_COMMENT = "comment";
  29.     public static final String ATTR_VERSION = "version";
  30.     public static final String ATTR_SECURE = "secure";

  31.     private final String domain;
  32.     private final Integer maxAge;
  33.     private final String path;
  34.     private final String comment;
  35.     private final String version;
  36.     private final Boolean secure;

  37.     public CookieElement(String name, String value) {
  38.         this(name, value, null, null, null, null, null, null);
  39.     }

  40.     public CookieElement(String name, String value, String domain, Integer maxAge, String path, String comment, String version, Boolean secure) {
  41.         super(name, value);
  42.         this.domain = domain;
  43.         this.maxAge = maxAge;
  44.         this.path = path;
  45.         this.comment = comment;
  46.         this.version = version;
  47.         this.secure = secure;
  48.     }

  49.     public String getPath() {
  50.         return path;
  51.     }

  52.     public int getMaxAge() {
  53.         return maxAge == null ? 0 : maxAge;
  54.     }

  55.     public String getDomain() {
  56.         return domain;
  57.     }

  58.     public String getComment() {
  59.         return comment;
  60.     }

  61.     public String getVersion() {
  62.         return version == null ? "1.0" : version;
  63.     }

  64.     public boolean isSecure() {
  65.         return secure != null && secure;
  66.     }

  67.     @Override
  68.     public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
  69.         XmlStringBuilder sb = addCommonXml(new XmlStringBuilder(this))
  70.                 .optAttribute(ATTR_DOMAIN, domain)
  71.                 .optAttribute(ATTR_MAX_AGE, maxAge)
  72.                 .optAttribute(ATTR_PATH, path)
  73.                 .optAttribute(ATTR_COMMENT, comment)
  74.                 .optAttribute(ATTR_VERSION, version);
  75.         if (secure != null) {
  76.             sb.attribute(ATTR_SECURE, secure);
  77.         }
  78.         return sb.closeEmptyElement();
  79.     }

  80.     @Override
  81.     public String getElementName() {
  82.         return PREFIX + ':' + ELEMENT;
  83.     }

  84.     @Override
  85.     public int hashCode() {
  86.         return HashCode.builder()
  87.                 .append(getElementName())
  88.                 .append(getName())
  89.                 .append(getValue())
  90.                 .append(getDomain())
  91.                 .append(getMaxAge())
  92.                 .append(getPath())
  93.                 .append(getComment())
  94.                 .append(getVersion())
  95.                 .append(isSecure())
  96.                 .build();
  97.     }

  98.     @Override
  99.     public boolean equals(Object obj) {
  100.         return EqualsUtil.equals(this, obj, (equalsBuilder, other) ->
  101.                 equalsBuilder
  102.                         .append(getElementName(), other.getElementName())
  103.                         .append(getName(), other.getName())
  104.                         .append(getValue(), other.getValue())
  105.                         .append(getDomain(), other.getDomain())
  106.                         .append(getMaxAge(), other.getMaxAge())
  107.                         .append(getPath(), other.getPath())
  108.                         .append(getComment(), other.getComment())
  109.                         .append(getVersion(), other.getVersion())
  110.                         .append(isSecure(), other.isSecure()));
  111.     }
  112. }