BoBDataExtension.java

  1. /**
  2.  *
  3.  * Copyright 2020-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.bob.element;

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

  22. import org.jivesoftware.smackx.bob.BoBData;
  23. import org.jivesoftware.smackx.bob.BoBManager;
  24. import org.jivesoftware.smackx.bob.ContentId;

  25. /**
  26.  * Bits of Binary data extension element.
  27.  *
  28.  * @author Florian Schmaus
  29.  * @see <a href="http://xmpp.org/extensions/xep-0231.html">XEP-0231: Bits of
  30.  *      Binary</a>
  31.  */
  32. public class BoBDataExtension implements ExtensionElement {

  33.     public static final String ELEMENT = "data";
  34.     public static final String NAMESPACE = BoBManager.NAMESPACE;

  35.     private final ContentId cid;
  36.     private final BoBData bobData;

  37.     /**
  38.      * Bits of Binary data extension constructor.
  39.      *
  40.      * @param cid TODO javadoc me please
  41.      * @param bobData TODO javadoc me please
  42.      */
  43.     public BoBDataExtension(ContentId cid, BoBData bobData) {
  44.         this.cid = Objects.requireNonNull(cid);
  45.         this.bobData = Objects.requireNonNull(bobData);
  46.     }

  47.     @Override
  48.     public String getElementName() {
  49.         return ELEMENT;
  50.     }

  51.     @Override
  52.     public String getNamespace() {
  53.         return NAMESPACE;
  54.     }

  55.     /**
  56.      * Get the content ID.
  57.      *
  58.      * @return the content ID.
  59.      * @since 4.4.1
  60.      */
  61.     public final ContentId getContentId() {
  62.         return cid;
  63.     }

  64.     /**
  65.      * Get the Bits of Binary (BOB) data.
  66.      *
  67.      * @return the BoB data.
  68.      * @since 4.4.1
  69.      */
  70.     public final BoBData getBobData() {
  71.         return bobData;
  72.     }

  73.     @Override
  74.     public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  75.         XmlStringBuilder xml = new XmlStringBuilder(this);
  76.         xml.attribute("cid", cid.getCid());
  77.         xml.attribute("type", bobData.getType());
  78.         xml.optAttribute("max-age", bobData.getMaxAge());
  79.         xml.rightAngleBracket();

  80.         xml.append(bobData.getContentBase64Encoded());

  81.         xml.closeElement(this);
  82.         return xml;
  83.     }

  84.     public static BoBDataExtension from(StanzaView stanza) {
  85.         return stanza.getExtension(BoBDataExtension.class);
  86.     }

  87. }