001/**
002 *
003 * Copyright 2016 Fernando Ramirez
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.IQ;
020import org.jivesoftware.smackx.bob.BoBData;
021import org.jivesoftware.smackx.bob.BoBHash;
022import org.jivesoftware.smackx.bob.BoBManager;
023
024/**
025 * Bits of Binary IQ class.
026 * 
027 * @author Fernando Ramirez
028 * @see <a href="http://xmpp.org/extensions/xep-0231.html">XEP-0231: Bits of
029 *      Binary</a>
030 */
031public class BoBIQ extends IQ {
032
033    /**
034     * data element.
035     */
036    public static final String ELEMENT = "data";
037
038    /**
039     * the IQ NAMESPACE.
040     */
041    public static final String NAMESPACE = BoBManager.NAMESPACE;
042
043    private final BoBHash bobHash;
044    private final BoBData bobData;
045
046    /**
047     * Bits of Binary IQ constructor.
048     * 
049     * @param bobHash
050     * @param bobData
051     */
052    public BoBIQ(BoBHash bobHash, BoBData bobData) {
053        super(ELEMENT, NAMESPACE);
054        this.bobHash = bobHash;
055        this.bobData = bobData;
056    }
057
058    /**
059     * Bits of Binary IQ constructor.
060     * 
061     * @param bobHash
062     */
063    public BoBIQ(BoBHash bobHash) {
064        this(bobHash, null);
065    }
066
067    /**
068     * Get the BoB hash.
069     * 
070     * @return the BoB hash
071     */
072    public BoBHash getBoBHash() {
073        return bobHash;
074    }
075
076    /**
077     * Get the BoB data.
078     * 
079     * @return the BoB data
080     */
081    public BoBData getBoBData() {
082        return bobData;
083    }
084
085    @Override
086    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
087        xml.attribute("cid", bobHash.getCid());
088
089        if (bobData != null) {
090            xml.optIntAttribute("max_age", bobData.getMaxAge());
091            xml.attribute("type", bobData.getType());
092            xml.rightAngleBracket();
093            xml.escape(bobData.getContentBase64Encoded());
094        } else {
095            xml.setEmptyElement();
096        }
097
098        return xml;
099    }
100
101}