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