StanzaIdElement.java

  1. /**
  2.  *
  3.  * Copyright 2018 Paul Schaub
  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.sid.element;

  18. import javax.xml.namespace.QName;

  19. import org.jivesoftware.smack.packet.Message;
  20. import org.jivesoftware.smack.util.XmlStringBuilder;

  21. import org.jivesoftware.smackx.sid.StableUniqueStanzaIdManager;

  22. public class StanzaIdElement extends StableAndUniqueIdElement {

  23.     public static final String ELEMENT = "stanza-id";
  24.     public static final String NAMESPACE = StableUniqueStanzaIdManager.NAMESPACE;
  25.     public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
  26.     public static final String ATTR_BY = "by";

  27.     private final String by;

  28.     public StanzaIdElement(String by) {
  29.         super();
  30.         this.by = by;
  31.     }

  32.     public StanzaIdElement(String id, String by) {
  33.         super(id);
  34.         this.by = by;
  35.     }

  36.     /**
  37.      * Return true, if a message contains a stanza-id element.
  38.      *
  39.      * @param message message
  40.      * @return true if message contains stanza-id element, otherwise false.
  41.      */
  42.     public static boolean hasStanzaId(Message message) {
  43.         return getStanzaId(message) != null;
  44.     }

  45.     /**
  46.      * Return the stanza-id element of a message.
  47.      *
  48.      * @param message message
  49.      * @return stanza-id element of a jid, or null if absent.
  50.      */
  51.     public static StanzaIdElement getStanzaId(Message message) {
  52.         return message.getExtension(StanzaIdElement.class);
  53.     }

  54.     public String getBy() {
  55.         return by;
  56.     }

  57.     @Override
  58.     public String getNamespace() {
  59.         return StableUniqueStanzaIdManager.NAMESPACE;
  60.     }

  61.     @Override
  62.     public String getElementName() {
  63.         return ELEMENT;
  64.     }

  65.     @Override
  66.     public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  67.         return new XmlStringBuilder(this)
  68.                 .attribute(ATTR_ID, getId())
  69.                 .attribute(ATTR_BY, getBy())
  70.                 .closeEmptyElement();
  71.     }
  72. }