Base64BinaryChunk.java

  1. /**
  2.  *
  3.  * Copyright 2014 Andriy Tsykholyas
  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 org.jivesoftware.smack.packet.ExtensionElement;
  19. import org.jivesoftware.smackx.hoxt.HOXTManager;

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

  28.     public static final String ELEMENT_CHUNK = "chunk";
  29.     public static final String ATTRIBUTE_STREAM_ID = "streamId";
  30.     public static final String ATTRIBUTE_LAST = "last";
  31.     public static final String ATTRIBUTE_NR = "nr";

  32.     private final String streamId;
  33.     private final boolean last;
  34.     private final String text;
  35.     private final int nr;

  36.     /**
  37.      * Creates the extension.
  38.      *
  39.      * @param text     value of text attribute
  40.      * @param streamId value of streamId attribute
  41.      * @param nr       value of nr attribute
  42.      * @param last     value of last attribute
  43.      */
  44.     public Base64BinaryChunk(String text, String streamId, int nr, boolean last) {
  45.         this.text = text;
  46.         this.streamId = streamId;
  47.         this.nr = nr;
  48.         this.last = last;
  49.     }

  50.     /**
  51.      * Creates the extension. Last attribute will be initialized with default value (false).
  52.      *
  53.      * @param text     value of text attribute
  54.      * @param streamId value of streamId attribute
  55.      * @param nr       value of nr attribute
  56.      */
  57.     public Base64BinaryChunk(String text, String streamId, int nr) {
  58.         this(text, streamId, nr, false);
  59.     }

  60.     /**
  61.      * Returns streamId attribute.
  62.      *
  63.      * @return streamId attribute
  64.      */
  65.     public String getStreamId() {
  66.         return streamId;
  67.     }

  68.     /**
  69.      * Returns last attribute.
  70.      *
  71.      * @return last attribute
  72.      */
  73.     public boolean isLast() {
  74.         return last;
  75.     }

  76.     /**
  77.      * Returns text attribute.
  78.      *
  79.      * @return text attribute
  80.      */
  81.     public String getText() {
  82.         return text;
  83.     }

  84.     /**
  85.      * Returns nr attribute.
  86.      *
  87.      * @return nr attribute
  88.      */
  89.     public int getNr() {
  90.         return nr;
  91.     }

  92.     @Override
  93.     public String getElementName() {
  94.         return ELEMENT_CHUNK;
  95.     }

  96.     @Override
  97.     public String getNamespace() {
  98.         return HOXTManager.NAMESPACE;
  99.     }

  100.     @Override
  101.     public String toXML() {
  102.         StringBuilder builder = new StringBuilder();
  103.         builder.append("<chunk xmlns='urn:xmpp:http' streamId='");
  104.         builder.append(streamId);
  105.         builder.append("' nr='");
  106.         builder.append(nr);
  107.         builder.append("' last='");
  108.         builder.append(Boolean.toString(last));
  109.         builder.append("'>");
  110.         builder.append(text);
  111.         builder.append("</chunk>");
  112.         return builder.toString();
  113.     }
  114. }