Base64BinaryChunk.java

  1. /**
  2.  *
  3.  * Copyright 2014 Andriy Tsykholyas, 2015-2021 Florian Schmaus
  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.hoxt.packet;

  18. import javax.xml.namespace.QName;

  19. import org.jivesoftware.smack.packet.ExtensionElement;
  20. import org.jivesoftware.smack.util.Objects;
  21. import org.jivesoftware.smack.util.XmlStringBuilder;

  22. import org.jivesoftware.smackx.hoxt.HOXTManager;

  23. /**
  24.  * Stanza extension for base64 binary chunks.<p>
  25.  * This class is immutable.
  26.  *
  27.  * @author Andriy Tsykholyas
  28.  * @see <a href="http://xmpp.org/extensions/xep-0332.html">XEP-0332: HTTP over XMPP transport</a>
  29.  */
  30. public class Base64BinaryChunk implements ExtensionElement {

  31.     public static final String ELEMENT_CHUNK = "chunk";
  32.     public static final String ATTRIBUTE_STREAM_ID = "streamId";
  33.     public static final String ATTRIBUTE_LAST = "last";
  34.     public static final String ATTRIBUTE_NR = "nr";

  35.     public static final QName QNAME = new QName(HOXTManager.NAMESPACE, ELEMENT_CHUNK);

  36.     private final String streamId;
  37.     private final boolean last;
  38.     private final String text;
  39.     private final int nr;

  40.     /**
  41.      * Creates the extension.
  42.      *
  43.      * @param text     value of text attribute
  44.      * @param streamId value of streamId attribute
  45.      * @param nr       value of nr attribute
  46.      * @param last     value of last attribute
  47.      */
  48.     public Base64BinaryChunk(String text, String streamId, int nr, boolean last) {
  49.         this.text = Objects.requireNonNull(text, "text must not be null");
  50.         this.streamId = Objects.requireNonNull(streamId, "streamId must not be null");
  51.         if (nr < 0) {
  52.             throw new IllegalArgumentException("nr must be a non negative integer");
  53.         }
  54.         this.nr = nr;
  55.         this.last = last;
  56.     }

  57.     /**
  58.      * Creates the extension. Last attribute will be initialized with default value (false).
  59.      *
  60.      * @param text     value of text attribute
  61.      * @param streamId value of streamId attribute
  62.      * @param nr       value of nr attribute
  63.      */
  64.     public Base64BinaryChunk(String text, String streamId, int nr) {
  65.         this(text, streamId, nr, false);
  66.     }

  67.     /**
  68.      * Returns streamId attribute.
  69.      *
  70.      * @return streamId attribute
  71.      */
  72.     public String getStreamId() {
  73.         return streamId;
  74.     }

  75.     /**
  76.      * Returns last attribute.
  77.      *
  78.      * @return last attribute
  79.      */
  80.     public boolean isLast() {
  81.         return last;
  82.     }

  83.     /**
  84.      * Returns text attribute.
  85.      *
  86.      * @return text attribute
  87.      */
  88.     public String getText() {
  89.         return text;
  90.     }

  91.     /**
  92.      * Returns nr attribute.
  93.      *
  94.      * @return nr attribute
  95.      */
  96.     public int getNr() {
  97.         return nr;
  98.     }

  99.     @Override
  100.     public String getElementName() {
  101.         return QNAME.getLocalPart();
  102.     }

  103.     @Override
  104.     public String getNamespace() {
  105.         return QNAME.getNamespaceURI();
  106.     }

  107.     @Override
  108.     public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  109.         XmlStringBuilder xml = new XmlStringBuilder(this);
  110.         xml.attribute("streamId", streamId);
  111.         xml.attribute("nr", nr);
  112.         xml.optBooleanAttribute("last", last);
  113.         xml.rightAngleBracket();
  114.         xml.append(text);
  115.         xml.closeElement(this);
  116.         return xml;
  117.     }
  118. }