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