001/**
002 *
003 * Copyright 2014 Andriy Tsykholyas, 2015 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.hoxt.packet;
018
019import org.jivesoftware.smack.packet.ExtensionElement;
020import org.jivesoftware.smack.util.Objects;
021import org.jivesoftware.smack.util.XmlStringBuilder;
022
023import org.jivesoftware.smackx.hoxt.HOXTManager;
024
025/**
026 * Stanza extension for base64 binary chunks.<p>
027 * This class is immutable.
028 *
029 * @author Andriy Tsykholyas
030 * @see <a href="http://xmpp.org/extensions/xep-0332.html">XEP-0332: HTTP over XMPP transport</a>
031 */
032public class Base64BinaryChunk implements ExtensionElement {
033
034    public static final String ELEMENT_CHUNK = "chunk";
035    public static final String ATTRIBUTE_STREAM_ID = "streamId";
036    public static final String ATTRIBUTE_LAST = "last";
037    public static final String ATTRIBUTE_NR = "nr";
038
039    private final String streamId;
040    private final boolean last;
041    private final String text;
042    private final int nr;
043
044    /**
045     * Creates the extension.
046     *
047     * @param text     value of text attribute
048     * @param streamId value of streamId attribute
049     * @param nr       value of nr attribute
050     * @param last     value of last attribute
051     */
052    public Base64BinaryChunk(String text, String streamId, int nr, boolean last) {
053        this.text = Objects.requireNonNull(text, "text must not be null");
054        this.streamId = Objects.requireNonNull(streamId, "streamId must not be null");
055        if (nr < 0) {
056            throw new IllegalArgumentException("nr must be a non negative integer");
057        }
058        this.nr = nr;
059        this.last = last;
060    }
061
062    /**
063     * Creates the extension. Last attribute will be initialized with default value (false).
064     *
065     * @param text     value of text attribute
066     * @param streamId value of streamId attribute
067     * @param nr       value of nr attribute
068     */
069    public Base64BinaryChunk(String text, String streamId, int nr) {
070        this(text, streamId, nr, false);
071    }
072
073    /**
074     * Returns streamId attribute.
075     *
076     * @return streamId attribute
077     */
078    public String getStreamId() {
079        return streamId;
080    }
081
082    /**
083     * Returns last attribute.
084     *
085     * @return last attribute
086     */
087    public boolean isLast() {
088        return last;
089    }
090
091    /**
092     * Returns text attribute.
093     *
094     * @return text attribute
095     */
096    public String getText() {
097        return text;
098    }
099
100    /**
101     * Returns nr attribute.
102     *
103     * @return nr attribute
104     */
105    public int getNr() {
106        return nr;
107    }
108
109    @Override
110    public String getElementName() {
111        return ELEMENT_CHUNK;
112    }
113
114    @Override
115    public String getNamespace() {
116        return HOXTManager.NAMESPACE;
117    }
118
119    @Override
120    public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
121        XmlStringBuilder xml = new XmlStringBuilder(this);
122        xml.attribute("streamId", streamId);
123        xml.attribute("nr", nr);
124        xml.optBooleanAttribute("last", last);
125        xml.rightAngleBracket();
126        xml.append(text);
127        xml.closeElement(this);
128        return xml;
129    }
130}