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    public Open(String sessionID, int blockSize, StanzaType stanza) {
058        super(ELEMENT, NAMESPACE);
059        if (sessionID == null || "".equals(sessionID)) {
060            throw new IllegalArgumentException("Session ID must not be null or empty");
061        }
062        if (blockSize <= 0) {
063            throw new IllegalArgumentException("Block size must be greater than zero");
064        }
065
066        this.sessionID = sessionID;
067        this.blockSize = blockSize;
068        this.stanza = stanza;
069        setType(Type.set);
070    }
071
072    /**
073     * Creates a new In-Band Bytestream open request packet.
074     * <p>
075     * The data sent over this In-Band Bytestream will be fragmented in blocks
076     * with the given block size. The block size should not be greater than
077     * 65535. A recommended default value is 4096.
078     * <p>
079     * The data will be sent using IQ stanzas.
080     *
081     * @param sessionID unique session ID identifying this In-Band Bytestream
082     * @param blockSize block size in which the data will be fragmented
083     */
084    public Open(String sessionID, int blockSize) {
085        this(sessionID, blockSize, StanzaType.IQ);
086    }
087
088    /**
089     * Returns the unique session ID identifying this In-Band Bytestream.
090     *
091     * @return the unique session ID identifying this In-Band Bytestream
092     */
093    public String getSessionID() {
094        return sessionID;
095    }
096
097    /**
098     * Returns the block size in which the data will be fragmented.
099     *
100     * @return the block size in which the data will be fragmented
101     */
102    public int getBlockSize() {
103        return blockSize;
104    }
105
106    /**
107     * Returns the stanza type used to encapsulate the data.
108     *
109     * @return the stanza type used to encapsulate the data
110     */
111    public StanzaType getStanza() {
112        return stanza;
113    }
114
115    @Override
116    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
117        xml.attribute("block-size", Integer.toString(blockSize));
118        xml.attribute("sid", sessionID);
119        xml.attribute("stanza", stanza.toString().toLowerCase(Locale.US));
120        xml.setEmptyElement();
121        return xml;
122    }
123
124}