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