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