001/**
002 *
003 * Copyright 2020 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.Message;
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    @Override
064    public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
065        XmlStringBuilder xml = new XmlStringBuilder(this);
066        xml.attribute("cid", cid.getCid());
067        xml.attribute("type", bobData.getType());
068        xml.optAttribute("max-age", bobData.getMaxAge());
069        xml.rightAngleBracket();
070
071        xml.append(bobData.getContentBase64Encoded());
072
073        xml.closeElement(this);
074        return xml;
075    }
076
077    public static BoBDataExtension from(Message message) {
078        return message.getExtension(BoBDataExtension.class);
079    }
080
081}