StanzaView.java

  1. /**
  2.  *
  3.  * Copyright 2019-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.smack.packet;

  18. import java.util.List;

  19. import javax.xml.namespace.QName;

  20. import org.jivesoftware.smack.util.XmppElementUtil;

  21. import org.jxmpp.jid.Jid;

  22. public interface StanzaView extends XmlLangElement {

  23.     /**
  24.      * Returns the unique ID of the stanza. The returned value could be <code>null</code>.
  25.      *
  26.      * @return the packet's unique ID or <code>null</code> if the id is not available.
  27.      */
  28.     String getStanzaId();

  29.     /**
  30.      * Returns who the stanza is being sent "to", or <code>null</code> if
  31.      * the value is not set. The XMPP protocol often makes the "to"
  32.      * attribute optional, so it does not always need to be set.<p>
  33.      *
  34.      * @return who the stanza is being sent to, or <code>null</code> if the
  35.      *      value has not been set.
  36.      */
  37.     Jid getTo();

  38.     /**
  39.      * Returns who the stanza is being sent "from" or <code>null</code> if
  40.      * the value is not set. The XMPP protocol often makes the "from"
  41.      * attribute optional, so it does not always need to be set.<p>
  42.      *
  43.      * @return who the stanza is being sent from, or <code>null</code> if the
  44.      *      value has not been set.
  45.      */
  46.     Jid getFrom();

  47.     /**
  48.      * Returns the error associated with this packet, or <code>null</code> if there are
  49.      * no errors.
  50.      *
  51.      * @return the error sub-packet or <code>null</code> if there isn't an error.
  52.      */
  53.     StanzaError getError();

  54.     XmlElement getExtension(QName qname);

  55.     default boolean hasExtension(QName qname) {
  56.         return getExtension(qname) != null;
  57.     }

  58.     default boolean hasExtension(Class<? extends ExtensionElement> extensionElementClass) {
  59.         return getExtension(extensionElementClass) != null;
  60.     }

  61.     /**
  62.      * Check if a extension element with the given namespace exists.
  63.      *
  64.      * @param namespace the namespace of the extension element to check for.
  65.      * @return true if a stanza extension exists, false otherwise.
  66.      */
  67.     default boolean hasExtension(String namespace) {
  68.         for (XmlElement packetExtension : getExtensions()) {
  69.             if (packetExtension.getNamespace().equals(namespace)) {
  70.                 return true;
  71.             }
  72.         }

  73.         return false;
  74.     }

  75.     default <E extends ExtensionElement> E getExtension(Class<E> extensionElementClass) {
  76.         QName qname = XmppElementUtil.getQNameFor(extensionElementClass);
  77.         XmlElement extensionElement = getExtension(qname);

  78.         if (extensionElement == null) {
  79.             return null;
  80.         }

  81.         return XmppElementUtil.castOrThrow(extensionElement, extensionElementClass);
  82.     }

  83.     /**
  84.      * Returns a list of all extension elements of this stanza.
  85.      *
  86.      * @return a list of all extension elements of this stanza.
  87.      */
  88.     List<XmlElement> getExtensions();

  89.     List<XmlElement> getExtensions(QName qname);

  90.     /**
  91.      * Return all extension elements of the given type. Returns the empty list if there a none.
  92.      *
  93.      * @param <E> the type of extension elements.
  94.      * @param extensionElementClass the class of the type of extension elements.
  95.      * @return a list of extension elements of that type, which may be empty.
  96.      */
  97.     <E extends ExtensionElement> List<E> getExtensions(Class<E> extensionElementClass);
  98. }