001/**
002 *
003 * Copyright 2018 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.ox.element;
018
019import java.util.Collections;
020import java.util.Date;
021import java.util.Map;
022import java.util.TreeMap;
023
024import org.jivesoftware.smack.packet.ExtensionElement;
025import org.jivesoftware.smack.packet.NamedElement;
026import org.jivesoftware.smack.util.Objects;
027import org.jivesoftware.smack.util.XmlStringBuilder;
028
029import org.pgpainless.key.OpenPgpV4Fingerprint;
030
031/**
032 * Class that represents a public key list which was announced to a users metadata node.
033 *
034 * @see <a href="https://xmpp.org/extensions/xep-0373.html#announcing-pubkey-list">
035 *     XEP-0373: ยง4.2 The OpenPGP Public Key Metadata Node</a>
036 */
037public final class PublicKeysListElement implements ExtensionElement {
038
039    public static final String NAMESPACE = OpenPgpElement.NAMESPACE;
040    public static final String ELEMENT = "public-keys-list";
041
042    private final Map<OpenPgpV4Fingerprint, PubkeyMetadataElement> metadata;
043
044    private PublicKeysListElement(TreeMap<OpenPgpV4Fingerprint, PubkeyMetadataElement> metadata) {
045        this.metadata = Collections.unmodifiableMap(Objects.requireNonNull(metadata));
046    }
047
048    public static Builder builder() {
049        return new Builder();
050    }
051
052    public TreeMap<OpenPgpV4Fingerprint, PubkeyMetadataElement> getMetadata() {
053        return new TreeMap<>(metadata);
054    }
055
056    @Override
057    public String getNamespace() {
058        return NAMESPACE;
059    }
060
061    @Override
062    public String getElementName() {
063        return ELEMENT;
064    }
065
066    @Override
067    public XmlStringBuilder toXML(String enclosingNamespace) {
068        XmlStringBuilder xml = new XmlStringBuilder(this).rightAngleBracket();
069        for (PubkeyMetadataElement metadataElement : metadata.values()) {
070            xml.element(metadataElement);
071        }
072        xml.closeElement(this);
073        return xml;
074    }
075
076    public static final class Builder {
077
078        private final TreeMap<OpenPgpV4Fingerprint, PubkeyMetadataElement> metadata = new TreeMap<>();
079
080        private Builder() {
081            // Empty
082        }
083
084        public Builder addMetadata(PubkeyMetadataElement key) {
085            Objects.requireNonNull(key);
086            metadata.put(key.getV4Fingerprint(), key);
087            return this;
088        }
089
090        public PublicKeysListElement build() {
091            return new PublicKeysListElement(metadata);
092        }
093    }
094
095    public static class PubkeyMetadataElement implements NamedElement {
096
097        public static final String ELEMENT = "pubkey-metadata";
098        public static final String ATTR_V4_FINGERPRINT = "v4-fingerprint";
099        public static final String ATTR_DATE = "date";
100
101        private final OpenPgpV4Fingerprint v4_fingerprint;
102        private final Date date;
103
104        public PubkeyMetadataElement(OpenPgpV4Fingerprint v4_fingerprint, Date date) {
105            this.v4_fingerprint = Objects.requireNonNull(v4_fingerprint);
106            this.date = Objects.requireNonNull(date);
107
108            if (v4_fingerprint.length() != 40) {
109                throw new IllegalArgumentException("OpenPGP v4 fingerprint must be 40 characters long.");
110            }
111        }
112
113        public OpenPgpV4Fingerprint getV4Fingerprint() {
114            return v4_fingerprint;
115        }
116
117        public Date getDate() {
118            return date;
119        }
120
121        @Override
122        public String getElementName() {
123            return ELEMENT;
124        }
125
126        @Override
127        public XmlStringBuilder toXML(String enclosingNamespace) {
128            XmlStringBuilder xml = new XmlStringBuilder(this)
129                    .attribute(ATTR_V4_FINGERPRINT, getV4Fingerprint())
130                    .attribute(ATTR_DATE, date).closeEmptyElement();
131            return xml;
132        }
133
134        @Override
135        public int hashCode() {
136            return getV4Fingerprint().hashCode() + 3 * getDate().hashCode();
137        }
138
139        @Override
140        public boolean equals(Object o) {
141            if (o == null) {
142                return false;
143            }
144
145            if (!(o instanceof PubkeyMetadataElement)) {
146                return false;
147            }
148
149            if (o == this) {
150                return true;
151            }
152
153            return hashCode() == o.hashCode();
154        }
155    }
156}