Open.java

  1. /**
  2.  *
  3.  * Copyright the original author or authors
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.bytestreams.ibb.packet;

  18. import java.util.Locale;

  19. import org.jivesoftware.smack.packet.IQ;

  20. import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager.StanzaType;

  21. /**
  22.  * Represents a request to open an In-Band Bytestream.
  23.  *
  24.  * @author Henning Staib
  25.  */
  26. public class Open extends IQ {

  27.     public static final String ELEMENT = "open";
  28.     public static final String NAMESPACE = DataPacketExtension.NAMESPACE;

  29.     /* unique session ID identifying this In-Band Bytestream */
  30.     private final String sessionID;

  31.     /* block size in which the data will be fragmented */
  32.     private final int blockSize;

  33.     /* stanza type used to encapsulate the data */
  34.     private final StanzaType stanza;

  35.     /**
  36.      * Creates a new In-Band Bytestream open request packet.
  37.      * <p>
  38.      * The data sent over this In-Band Bytestream will be fragmented in blocks
  39.      * with the given block size. The block size should not be greater than
  40.      * 65535. A recommended default value is 4096.
  41.      * <p>
  42.      * The data can be sent using IQ stanzas or message stanzas.
  43.      *
  44.      * @param sessionID unique session ID identifying this In-Band Bytestream
  45.      * @param blockSize block size in which the data will be fragmented
  46.      * @param stanza stanza type used to encapsulate the data
  47.      */
  48.     public Open(String sessionID, int blockSize, StanzaType stanza) {
  49.         super(ELEMENT, NAMESPACE);
  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.         this.sessionID = sessionID;
  57.         this.blockSize = blockSize;
  58.         this.stanza = stanza;
  59.         setType(Type.set);
  60.     }

  61.     /**
  62.      * Creates a new In-Band Bytestream open request packet.
  63.      * <p>
  64.      * The data sent over this In-Band Bytestream will be fragmented in blocks
  65.      * with the given block size. The block size should not be greater than
  66.      * 65535. A recommended default value is 4096.
  67.      * <p>
  68.      * The data will be sent using IQ stanzas.
  69.      *
  70.      * @param sessionID unique session ID identifying this In-Band Bytestream
  71.      * @param blockSize block size in which the data will be fragmented
  72.      */
  73.     public Open(String sessionID, int blockSize) {
  74.         this(sessionID, blockSize, StanzaType.IQ);
  75.     }

  76.     /**
  77.      * Returns the unique session ID identifying this In-Band Bytestream.
  78.      *
  79.      * @return the unique session ID identifying this In-Band Bytestream
  80.      */
  81.     public String getSessionID() {
  82.         return sessionID;
  83.     }

  84.     /**
  85.      * Returns the block size in which the data will be fragmented.
  86.      *
  87.      * @return the block size in which the data will be fragmented
  88.      */
  89.     public int getBlockSize() {
  90.         return blockSize;
  91.     }

  92.     /**
  93.      * Returns the stanza type used to encapsulate the data.
  94.      *
  95.      * @return the stanza type used to encapsulate the data
  96.      */
  97.     public StanzaType getStanza() {
  98.         return stanza;
  99.     }

  100.     @Override
  101.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
  102.         xml.attribute("block-size", Integer.toString(blockSize));
  103.         xml.attribute("sid", sessionID);
  104.         xml.attribute("stanza", stanza.toString().toLowerCase(Locale.US));
  105.         xml.setEmptyElement();
  106.         return xml;
  107.     }

  108. }