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;
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    /* unique session ID identifying this In-Band Bytestream */
033    private final String sessionID;
034
035    /* block size in which the data will be fragmented */
036    private final int blockSize;
037
038    /* stanza type used to encapsulate the data */
039    private final StanzaType stanza;
040
041    /**
042     * Creates a new In-Band Bytestream open request packet.
043     * <p>
044     * The data sent over this In-Band Bytestream will be fragmented in blocks
045     * with the given block size. The block size should not be greater than
046     * 65535. A recommended default value is 4096.
047     * <p>
048     * The data can be sent using IQ stanzas or message stanzas.
049     * 
050     * @param sessionID unique session ID identifying this In-Band Bytestream
051     * @param blockSize block size in which the data will be fragmented
052     * @param stanza stanza type used to encapsulate the data
053     */
054    public Open(String sessionID, int blockSize, StanzaType stanza) {
055        if (sessionID == null || "".equals(sessionID)) {
056            throw new IllegalArgumentException("Session ID must not be null or empty");
057        }
058        if (blockSize <= 0) {
059            throw new IllegalArgumentException("Block size must be greater than zero");
060        }
061
062        this.sessionID = sessionID;
063        this.blockSize = blockSize;
064        this.stanza = stanza;
065        setType(Type.SET);
066    }
067
068    /**
069     * Creates a new In-Band Bytestream open request packet.
070     * <p>
071     * The data sent over this In-Band Bytestream will be fragmented in blocks
072     * with the given block size. The block size should not be greater than
073     * 65535. A recommended default value is 4096.
074     * <p>
075     * The data will be sent using IQ stanzas.
076     * 
077     * @param sessionID unique session ID identifying this In-Band Bytestream
078     * @param blockSize block size in which the data will be fragmented
079     */
080    public Open(String sessionID, int blockSize) {
081        this(sessionID, blockSize, StanzaType.IQ);
082    }
083
084    /**
085     * Returns the unique session ID identifying this In-Band Bytestream.
086     * 
087     * @return the unique session ID identifying this In-Band Bytestream
088     */
089    public String getSessionID() {
090        return sessionID;
091    }
092
093    /**
094     * Returns the block size in which the data will be fragmented.
095     * 
096     * @return the block size in which the data will be fragmented
097     */
098    public int getBlockSize() {
099        return blockSize;
100    }
101
102    /**
103     * Returns the stanza type used to encapsulate the data.
104     * 
105     * @return the stanza type used to encapsulate the data
106     */
107    public StanzaType getStanza() {
108        return stanza;
109    }
110
111    @Override
112    public String getChildElementXML() {
113        StringBuilder buf = new StringBuilder();
114        buf.append("<open ");
115        buf.append("xmlns=\"");
116        buf.append(InBandBytestreamManager.NAMESPACE);
117        buf.append("\" ");
118        buf.append("block-size=\"");
119        buf.append(blockSize);
120        buf.append("\" ");
121        buf.append("sid=\"");
122        buf.append(sessionID);
123        buf.append("\" ");
124        buf.append("stanza=\"");
125        buf.append(stanza.toString().toLowerCase(Locale.US));
126        buf.append("\"");
127        buf.append("/>");
128        return buf.toString();
129    }
130
131}