Home | History | Annotate | Download | only in packet
      1 /**
      2  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
      3  * you may not use this file except in compliance with the License.
      4  * You may obtain a copy of the License at
      5  *
      6  *     http://www.apache.org/licenses/LICENSE-2.0
      7  *
      8  * Unless required by applicable law or agreed to in writing, software
      9  * distributed under the License is distributed on an "AS IS" BASIS,
     10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     11  * See the License for the specific language governing permissions and
     12  * limitations under the License.
     13  */
     14 package org.jivesoftware.smackx.bytestreams.ibb.packet;
     15 
     16 import org.jivesoftware.smack.packet.IQ;
     17 import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager;
     18 import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager.StanzaType;
     19 
     20 /**
     21  * Represents a request to open an In-Band Bytestream.
     22  *
     23  * @author Henning Staib
     24  */
     25 public class Open extends IQ {
     26 
     27     /* unique session ID identifying this In-Band Bytestream */
     28     private final String sessionID;
     29 
     30     /* block size in which the data will be fragmented */
     31     private final int blockSize;
     32 
     33     /* stanza type used to encapsulate the data */
     34     private final StanzaType stanza;
     35 
     36     /**
     37      * Creates a new In-Band Bytestream open request packet.
     38      * <p>
     39      * The data sent over this In-Band Bytestream will be fragmented in blocks
     40      * with the given block size. The block size should not be greater than
     41      * 65535. A recommended default value is 4096.
     42      * <p>
     43      * The data can be sent using IQ stanzas or message stanzas.
     44      *
     45      * @param sessionID unique session ID identifying this In-Band Bytestream
     46      * @param blockSize block size in which the data will be fragmented
     47      * @param stanza stanza type used to encapsulate the data
     48      */
     49     public Open(String sessionID, int blockSize, StanzaType stanza) {
     50         if (sessionID == null || "".equals(sessionID)) {
     51             throw new IllegalArgumentException("Session ID must not be null or empty");
     52         }
     53         if (blockSize <= 0) {
     54             throw new IllegalArgumentException("Block size must be greater than zero");
     55         }
     56 
     57         this.sessionID = sessionID;
     58         this.blockSize = blockSize;
     59         this.stanza = stanza;
     60         setType(Type.SET);
     61     }
     62 
     63     /**
     64      * Creates a new In-Band Bytestream open request packet.
     65      * <p>
     66      * The data sent over this In-Band Bytestream will be fragmented in blocks
     67      * with the given block size. The block size should not be greater than
     68      * 65535. A recommended default value is 4096.
     69      * <p>
     70      * The data will be sent using IQ stanzas.
     71      *
     72      * @param sessionID unique session ID identifying this In-Band Bytestream
     73      * @param blockSize block size in which the data will be fragmented
     74      */
     75     public Open(String sessionID, int blockSize) {
     76         this(sessionID, blockSize, StanzaType.IQ);
     77     }
     78 
     79     /**
     80      * Returns the unique session ID identifying this In-Band Bytestream.
     81      *
     82      * @return the unique session ID identifying this In-Band Bytestream
     83      */
     84     public String getSessionID() {
     85         return sessionID;
     86     }
     87 
     88     /**
     89      * Returns the block size in which the data will be fragmented.
     90      *
     91      * @return the block size in which the data will be fragmented
     92      */
     93     public int getBlockSize() {
     94         return blockSize;
     95     }
     96 
     97     /**
     98      * Returns the stanza type used to encapsulate the data.
     99      *
    100      * @return the stanza type used to encapsulate the data
    101      */
    102     public StanzaType getStanza() {
    103         return stanza;
    104     }
    105 
    106     @Override
    107     public String getChildElementXML() {
    108         StringBuilder buf = new StringBuilder();
    109         buf.append("<open ");
    110         buf.append("xmlns=\"");
    111         buf.append(InBandBytestreamManager.NAMESPACE);
    112         buf.append("\" ");
    113         buf.append("block-size=\"");
    114         buf.append(blockSize);
    115         buf.append("\" ");
    116         buf.append("sid=\"");
    117         buf.append(sessionID);
    118         buf.append("\" ");
    119         buf.append("stanza=\"");
    120         buf.append(stanza.toString().toLowerCase());
    121         buf.append("\"");
    122         buf.append("/>");
    123         return buf.toString();
    124     }
    125 
    126 }
    127