001/**
002 *
003 * Copyright 2018 Paul Schaub, 2021 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.sid.element;
018
019import javax.xml.namespace.QName;
020
021import org.jivesoftware.smack.packet.Message;
022import org.jivesoftware.smack.packet.MessageBuilder;
023import org.jivesoftware.smack.util.XmlStringBuilder;
024
025import org.jivesoftware.smackx.sid.StableUniqueStanzaIdManager;
026
027public class OriginIdElement extends StableAndUniqueIdElement {
028
029    public static final String ELEMENT = "origin-id";
030    public static final String NAMESPACE = StableUniqueStanzaIdManager.NAMESPACE;
031    public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
032
033    public OriginIdElement() {
034        super();
035    }
036
037    public OriginIdElement(String id) {
038        super(id);
039    }
040
041    /**
042     * Add an origin-id element to a message and set the stanzas id to the same id as in the origin-id element.
043     *
044     * @param messageBuilder the message builder to add an origin ID to.
045     * @return the added origin-id element.
046     */
047    public static OriginIdElement addTo(MessageBuilder messageBuilder) {
048        OriginIdElement originId = messageBuilder.getExtension(OriginIdElement.class);
049        if (originId != null) {
050            return originId;
051        }
052
053        originId = new OriginIdElement();
054        messageBuilder.addExtension(originId);
055        // TODO: Find solution to have both the originIds stanzaId and a nice to look at incremental stanzaID.
056        // message.setStanzaId(originId.getId());
057        return originId;
058    }
059
060    /**
061     * Return true, if the message contains a origin-id element.
062     *
063     * @param message message
064     * @return true if the message contains a origin-id, false otherwise.
065     */
066    public static boolean hasOriginId(Message message) {
067        return getOriginId(message) != null;
068    }
069
070    /**
071     * Return the origin-id element of a message or null, if absent.
072     *
073     * @param message message
074     * @return origin-id element
075     */
076    public static OriginIdElement getOriginId(Message message) {
077        return (OriginIdElement) message.getExtensionElement(OriginIdElement.ELEMENT, StableUniqueStanzaIdManager.NAMESPACE);
078    }
079
080    @Override
081    public String getNamespace() {
082        return StableUniqueStanzaIdManager.NAMESPACE;
083    }
084
085    @Override
086    public String getElementName() {
087        return ELEMENT;
088    }
089
090    @Override
091    public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
092        return new XmlStringBuilder(this)
093                .attribute(ATTR_ID, getId())
094                .closeEmptyElement();
095    }
096
097    @Override
098    public boolean equals(Object other) {
099        if (this == other) {
100            return true;
101        }
102        if (other == null) {
103            return false;
104        }
105        if (!(other instanceof OriginIdElement)) {
106            return false;
107        }
108
109        OriginIdElement otherId = (OriginIdElement) other;
110        return getId().equals(otherId.getId());
111    }
112
113    @Override
114    public int hashCode() {
115        return getId().hashCode();
116    }
117}