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