DataPacketExtension.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 javax.xml.namespace.QName;

  19. import org.jivesoftware.smack.datatypes.UInt16;
  20. import org.jivesoftware.smack.packet.ExtensionElement;
  21. import org.jivesoftware.smack.packet.IQ.IQChildElementXmlStringBuilder;
  22. import org.jivesoftware.smack.util.Objects;
  23. import org.jivesoftware.smack.util.XmlStringBuilder;
  24. import org.jivesoftware.smack.util.stringencoder.Base64;

  25. /**
  26.  * Represents a chunk of data of an In-Band Bytestream within an IQ stanza or a
  27.  * message stanza.
  28.  *
  29.  * @author Henning Staib
  30.  */
  31. public class DataPacketExtension implements ExtensionElement {

  32.     /**
  33.      * The element name of the data stanza extension.
  34.      */
  35.     public static final String ELEMENT = "data";

  36.     /**
  37.      * The XMPP namespace of the In-Band Bytestream.
  38.      */
  39.     public static final String NAMESPACE = "http://jabber.org/protocol/ibb";

  40.     public static final QName QNAME = new QName(NAMESPACE, ELEMENT);

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

  43.     /* sequence of this packet in regard to the other data packets */
  44.     private final UInt16 seq;

  45.     /* the data contained in this packet */
  46.     private final String data;

  47.     private byte[] decodedData;

  48.     /**
  49.      * Creates a new In-Band Bytestream data packet.
  50.      *
  51.      * @param sessionID unique session ID identifying this In-Band Bytestream
  52.      * @param seq sequence of this stanza in regard to the other data packets
  53.      * @param data the base64 encoded data contained in this packet
  54.      * @throws IllegalArgumentException if seq is not within the range [0, 65535].
  55.      */
  56.     public DataPacketExtension(String sessionID, int seq, String data) {
  57.         this(sessionID, UInt16.from(seq), data);
  58.     }

  59.     /**
  60.      * Creates a new In-Band Bytestream data packet.
  61.      *
  62.      * @param sessionID unique session ID identifying this In-Band Bytestream
  63.      * @param seq sequence of this stanza in regard to the other data packets
  64.      * @param data the base64 encoded data contained in this packet
  65.      */
  66.     public DataPacketExtension(String sessionID, UInt16 seq, String data) {
  67.         if (sessionID == null || "".equals(sessionID)) {
  68.             throw new IllegalArgumentException("Session ID must not be null or empty");
  69.         }
  70.         if (data == null) {
  71.             throw new IllegalArgumentException("Data must not be null");
  72.         }
  73.         this.sessionID = sessionID;
  74.         this.seq = Objects.requireNonNull(seq);
  75.         this.data = data;
  76.     }

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

  85.     /**
  86.      * Returns the sequence of this stanza in regard to the other data packets.
  87.      *
  88.      * @return the sequence of this stanza in regard to the other data packets.
  89.      */
  90.     public UInt16 getSeq() {
  91.         return seq;
  92.     }

  93.     /**
  94.      * Returns the data contained in this packet.
  95.      *
  96.      * @return the data contained in this packet.
  97.      */
  98.     public String getData() {
  99.         return data;
  100.     }

  101.     /**
  102.      * Returns the decoded data or null if data could not be decoded.
  103.      * <p>
  104.      * The encoded data is invalid if it contains bad Base64 input characters or
  105.      * if it contains the pad ('=') character on a position other than the last
  106.      * character(s) of the data. See <a
  107.      * href="http://xmpp.org/extensions/xep-0047.html#sec">XEP-0047</a> Section
  108.      * 6.
  109.      *
  110.      * @return the decoded data
  111.      */
  112.     public byte[] getDecodedData() {
  113.         // return cached decoded data
  114.         if (this.decodedData != null) {
  115.             return this.decodedData;
  116.         }

  117.         // data must not contain the pad (=) other than end of data
  118.         if (data.matches(".*={1,2}+.+")) {
  119.             return null;
  120.         }

  121.         // decodeBase64 will return null if bad characters are included
  122.         this.decodedData = Base64.decode(data);
  123.         return this.decodedData;
  124.     }

  125.     @Override
  126.     public String getElementName() {
  127.         return ELEMENT;
  128.     }

  129.     @Override
  130.     public String getNamespace() {
  131.         return NAMESPACE;
  132.     }

  133.     @Override
  134.     public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  135.         XmlStringBuilder xml = getIQChildElementBuilder(new IQChildElementXmlStringBuilder(this, enclosingNamespace));
  136.         xml.closeElement(this);
  137.         return xml;
  138.     }

  139.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
  140.         xml.attribute("seq", seq);
  141.         xml.attribute("sid", sessionID);
  142.         xml.rightAngleBracket();
  143.         xml.append(data);
  144.         return xml;
  145.     }
  146. }