001/**
002 *
003 * Copyright the original author or authors
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.bytestreams.ibb.packet;
018
019import java.util.Locale;
020
021import org.jivesoftware.smack.packet.IQ;
022
023import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager.StanzaType;
024
025/**
026 * Represents a request to open an In-Band Bytestream.
027 *
028 * @author Henning Staib
029 */
030public class Open extends IQ {
031
032    public static final String ELEMENT = "open";
033    public static final String NAMESPACE = DataPacketExtension.NAMESPACE;
034
035    /* unique session ID identifying this In-Band Bytestream */
036    private final String sessionID;
037
038    /* block size in which the data will be fragmented */
039    private final int blockSize;
040
041    /* stanza type used to encapsulate the data */
042    private final StanzaType stanza;
043
044    /**
045     * Creates a new In-Band Bytestream open request packet.
046     * <p>
047     * The data sent over this In-Band Bytestream will be fragmented in blocks
048     * with the given block size. The block size should not be greater than
049     * 65535. A recommended default value is 4096.
050     * <p>
051     * The data can be sent using IQ stanzas or message stanzas.
052     *
053     * @param sessionID unique session ID identifying this In-Band Bytestream
054     * @param blockSize block size in which the data will be fragmented
055     * @param stanza stanza type used to encapsulate the data
056     */
057    @SuppressWarnings("this-escape")
058    public Open(String sessionID, int blockSize, StanzaType stanza) {
059        super(ELEMENT, NAMESPACE);
060        if (sessionID == null || "".equals(sessionID)) {
061            throw new IllegalArgumentException("Session ID must not be null or empty");
062        }
063        if (blockSize <= 0) {
064            throw new IllegalArgumentException("Block size must be greater than zero");
065        }
066
067        this.sessionID = sessionID;
068        this.blockSize = blockSize;
069        this.stanza = stanza;
070        setType(Type.set);
071    }
072
073    /**
074     * Creates a new In-Band Bytestream open request packet.
075     * <p>
076     * The data sent over this In-Band Bytestream will be fragmented in blocks
077     * with the given block size. The block size should not be greater than
078     * 65535. A recommended default value is 4096.
079     * <p>
080     * The data will be sent using IQ stanzas.
081     *
082     * @param sessionID unique session ID identifying this In-Band Bytestream
083     * @param blockSize block size in which the data will be fragmented
084     */
085    public Open(String sessionID, int blockSize) {
086        this(sessionID, blockSize, StanzaType.IQ);
087    }
088
089    /**
090     * Returns the unique session ID identifying this In-Band Bytestream.
091     *
092     * @return the unique session ID identifying this In-Band Bytestream
093     */
094    public String getSessionID() {
095        return sessionID;
096    }
097
098    /**
099     * Returns the block size in which the data will be fragmented.
100     *
101     * @return the block size in which the data will be fragmented
102     */
103    public int getBlockSize() {
104        return blockSize;
105    }
106
107    /**
108     * Returns the stanza type used to encapsulate the data.
109     *
110     * @return the stanza type used to encapsulate the data
111     */
112    public StanzaType getStanza() {
113        return stanza;
114    }
115
116    @Override
117    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
118        xml.attribute("block-size", Integer.toString(blockSize));
119        xml.attribute("sid", sessionID);
120        xml.attribute("stanza", stanza.toString().toLowerCase(Locale.US));
121        xml.setEmptyElement();
122        return xml;
123    }
124
125}