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