001/**
002 *
003 * Copyright 2020-2021 Florian Schmaus
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.bob.element;
018
019import org.jivesoftware.smack.packet.ExtensionElement;
020import org.jivesoftware.smack.packet.StanzaView;
021import org.jivesoftware.smack.util.Objects;
022import org.jivesoftware.smack.util.XmlStringBuilder;
023import org.jivesoftware.smackx.bob.BoBData;
024import org.jivesoftware.smackx.bob.BoBManager;
025import org.jivesoftware.smackx.bob.ContentId;
026
027/**
028 * Bits of Binary data extension element.
029 *
030 * @author Florian Schmaus
031 * @see <a href="http://xmpp.org/extensions/xep-0231.html">XEP-0231: Bits of
032 *      Binary</a>
033 */
034public class BoBDataExtension implements ExtensionElement {
035
036    public static final String ELEMENT = "data";
037    public static final String NAMESPACE = BoBManager.NAMESPACE;
038
039    private final ContentId cid;
040    private final BoBData bobData;
041
042    /**
043     * Bits of Binary data extension constructor.
044     *
045     * @param cid TODO javadoc me please
046     * @param bobData TODO javadoc me please
047     */
048    public BoBDataExtension(ContentId cid, BoBData bobData) {
049        this.cid = Objects.requireNonNull(cid);
050        this.bobData = Objects.requireNonNull(bobData);
051    }
052
053    @Override
054    public String getElementName() {
055        return ELEMENT;
056    }
057
058    @Override
059    public String getNamespace() {
060        return NAMESPACE;
061    }
062
063    /**
064     * Get the content ID.
065     *
066     * @return the content ID.
067     * @since 4.4.1
068     */
069    public final ContentId getContentId() {
070        return cid;
071    }
072
073    /**
074     * Get the Bits of Binary (BOB) data.
075     *
076     * @return the BoB data.
077     * @since 4.4.1
078     */
079    public final BoBData getBobData() {
080        return bobData;
081    }
082
083    @Override
084    public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
085        XmlStringBuilder xml = new XmlStringBuilder(this);
086        xml.attribute("cid", cid.getCid());
087        xml.attribute("type", bobData.getType());
088        xml.optAttribute("max-age", bobData.getMaxAge());
089        xml.rightAngleBracket();
090
091        xml.append(bobData.getContentBase64Encoded());
092
093        xml.closeElement(this);
094        return xml;
095    }
096
097    public static BoBDataExtension from(StanzaView stanza) {
098        return stanza.getExtension(BoBDataExtension.class);
099    }
100
101}