OriginIdElement.java

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

  18. import javax.xml.namespace.QName;

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

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

  23. public class OriginIdElement extends StableAndUniqueIdElement {

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

  27.     public OriginIdElement() {
  28.         super();
  29.     }

  30.     public OriginIdElement(String id) {
  31.         super(id);
  32.     }

  33.     /**
  34.      * Add an origin-id element to a message and set the stanzas id to the same id as in the origin-id element.
  35.      *
  36.      * @param messageBuilder the message builder to add an origin ID to.
  37.      * @return the added origin-id element.
  38.      */
  39.     public static OriginIdElement addTo(MessageBuilder messageBuilder) {
  40.         OriginIdElement originId = messageBuilder.getExtension(OriginIdElement.class);
  41.         if (originId != null) {
  42.             return originId;
  43.         }

  44.         originId = new OriginIdElement();
  45.         messageBuilder.addExtension(originId);
  46.         // TODO: Find solution to have both the originIds stanzaId and a nice to look at incremental stanzaID.
  47.         // message.setStanzaId(originId.getId());
  48.         return originId;
  49.     }

  50.     /**
  51.      * Return true, if the message contains a origin-id element.
  52.      *
  53.      * @param message message
  54.      * @return true if the message contains a origin-id, false otherwise.
  55.      */
  56.     public static boolean hasOriginId(Message message) {
  57.         return getOriginId(message) != null;
  58.     }

  59.     /**
  60.      * Return the origin-id element of a message or null, if absent.
  61.      *
  62.      * @param message message
  63.      * @return origin-id element
  64.      */
  65.     public static OriginIdElement getOriginId(Message message) {
  66.         return (OriginIdElement) message.getExtensionElement(OriginIdElement.ELEMENT, StableUniqueStanzaIdManager.NAMESPACE);
  67.     }

  68.     @Override
  69.     public String getNamespace() {
  70.         return StableUniqueStanzaIdManager.NAMESPACE;
  71.     }

  72.     @Override
  73.     public String getElementName() {
  74.         return ELEMENT;
  75.     }

  76.     @Override
  77.     public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  78.         return new XmlStringBuilder(this)
  79.                 .attribute(ATTR_ID, getId())
  80.                 .closeEmptyElement();
  81.     }

  82.     @Override
  83.     public boolean equals(Object other) {
  84.         if (this == other) {
  85.             return true;
  86.         }
  87.         if (other == null) {
  88.             return false;
  89.         }
  90.         if (!(other instanceof OriginIdElement)) {
  91.             return false;
  92.         }

  93.         OriginIdElement otherId = (OriginIdElement) other;
  94.         return getId().equals(otherId.getId());
  95.     }

  96.     @Override
  97.     public int hashCode() {
  98.         return getId().hashCode();
  99.     }
  100. }