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.PacketExtension;
020import org.jivesoftware.smackx.hoxt.HOXTManager;
021
022/**
023 * 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 PacketExtension {
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
035    private final String streamId;
036    private final boolean last;
037    private final String text;
038
039    /**
040     * Creates the extension.
041     *
042     * @param text     value of text attribute
043     * @param streamId value of streamId attribute
044     * @param last     value of last attribute
045     */
046    public Base64BinaryChunk(String text, String streamId, boolean last) {
047        this.text = text;
048        this.streamId = streamId;
049        this.last = last;
050    }
051
052    /**
053     * Creates the extension. Last attribute will be initialized with default value (false).
054     *
055     * @param text     value of text attribute
056     * @param streamId value of streamId attribute
057     */
058    public Base64BinaryChunk(String text, String streamId) {
059        this(text, streamId, false);
060    }
061
062    /**
063     * Returns streamId attribute.
064     *
065     * @return streamId attribute
066     */
067    public String getStreamId() {
068        return streamId;
069    }
070
071    /**
072     * Returns last attribute.
073     *
074     * @return last attribute
075     */
076    public boolean isLast() {
077        return last;
078    }
079
080    /**
081     * Returns text attribute.
082     *
083     * @return text attribute
084     */
085    public String getText() {
086        return text;
087    }
088
089    @Override
090    public String getElementName() {
091        return ELEMENT_CHUNK;
092    }
093
094    @Override
095    public String getNamespace() {
096        return HOXTManager.NAMESPACE;
097    }
098
099    @Override
100    public String toXML() {
101        StringBuilder builder = new StringBuilder();
102        builder.append("<chunk xmlns='urn:xmpp:http' streamId='");
103        builder.append(streamId);
104        builder.append("' last='");
105        builder.append(Boolean.toString(last));
106        builder.append("'>");
107        builder.append(text);
108        builder.append("</chunk>");
109        return builder.toString();
110    }
111}