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.Date;
020
021import org.jivesoftware.smack.packet.ExtensionElement;
022import org.jivesoftware.smack.util.Objects;
023import org.jivesoftware.smack.util.XmlStringBuilder;
024import org.jivesoftware.smack.util.stringencoder.Base64;
025
026/**
027 * Class representing a pubkey element which is used to transport OpenPGP public keys.
028 *
029 * @see <a href="https://xmpp.org/extensions/xep-0373.html#announcing-pubkey">
030 *     XEP-0373: ยง4.1 The OpenPGP Public-Key Data Node</a>
031 */
032public class PubkeyElement implements ExtensionElement {
033
034    public static final String NAMESPACE = OpenPgpElement.NAMESPACE;
035    public static final String ELEMENT = "pubkey";
036    public static final String ATTR_DATE = "date";
037
038    private final PubkeyDataElement dataElement;
039    private final Date date;
040
041    public PubkeyElement(PubkeyDataElement dataElement, Date date) {
042        this.dataElement = Objects.requireNonNull(dataElement);
043        this.date = date;
044    }
045
046    /**
047     * Return the &lt;data&gt; element containing the base64 encoded public key.
048     *
049     * @return data element
050     */
051    public PubkeyDataElement getDataElement() {
052        return dataElement;
053    }
054
055    /**
056     * Date on which the key was last modified.
057     *
058     * @return last modification date
059     */
060    public Date getDate() {
061        return date;
062    }
063
064    @Override
065    public String getNamespace() {
066        return NAMESPACE;
067    }
068
069    @Override
070    public String getElementName() {
071        return ELEMENT;
072    }
073
074    @Override
075    public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
076        XmlStringBuilder xml = new XmlStringBuilder(this)
077                .optAttribute(ATTR_DATE, date)
078                .rightAngleBracket()
079                .append(getDataElement())
080                .closeElement(this);
081        return xml;
082    }
083
084    /**
085     * Element that contains the base64 encoded public key.
086     */
087    public static class PubkeyDataElement implements ExtensionElement {
088
089        public static final String ELEMENT = "data";
090
091        private final String b64Data;
092
093        public PubkeyDataElement(String b64Data) {
094            this.b64Data = Objects.requireNonNull(b64Data);
095        }
096
097        /**
098         * Base64 encoded public key.
099         *
100         * @return the base64 encoded version of the public key.
101         */
102        public String getB64Data() {
103            return b64Data;
104        }
105
106        private transient byte[] pubKeyBytesCache;
107
108        public byte[] getPubKeyBytes() {
109            if (pubKeyBytesCache == null) {
110                pubKeyBytesCache = Base64.decode(b64Data);
111            }
112            return pubKeyBytesCache.clone();
113        }
114
115        @Override
116        public String getElementName() {
117            return ELEMENT;
118        }
119
120        @Override
121        public String getNamespace() {
122            return NAMESPACE;
123        }
124
125        @Override
126        public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
127            XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace)
128                    .rightAngleBracket()
129                    .append(b64Data)
130                    .closeElement(this);
131            return xml;
132        }
133    }
134}