001/**
002 *
003 * Copyright 2018 Paul Schaub
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 message message.
045     * @return the added origin-id element.
046     * @deprecated use {@link #addTo(MessageBuilder)} instead.
047     */
048    @Deprecated
049    // TODO: Remove in Smack 4.5.
050    public static OriginIdElement addOriginId(Message message) {
051        OriginIdElement originId = new OriginIdElement();
052        message.addExtension(originId);
053        // TODO: Find solution to have both the originIds stanzaId and a nice to look at incremental stanzaID.
054        // message.setStanzaId(originId.getId());
055        return originId;
056    }
057
058    /**
059     * Add an origin-id element to a message and set the stanzas id to the same id as in the origin-id element.
060     *
061     * @param messageBuilder the message builder to add an origin ID to.
062     * @return the added origin-id element.
063     */
064    public static OriginIdElement addTo(MessageBuilder messageBuilder) {
065        OriginIdElement originId = new OriginIdElement();
066        messageBuilder.addExtension(originId);
067        // TODO: Find solution to have both the originIds stanzaId and a nice to look at incremental stanzaID.
068        // message.setStanzaId(originId.getId());
069        return originId;
070    }
071
072    /**
073     * Return true, if the message contains a origin-id element.
074     *
075     * @param message message
076     * @return true if the message contains a origin-id, false otherwise.
077     */
078    public static boolean hasOriginId(Message message) {
079        return getOriginId(message) != null;
080    }
081
082    /**
083     * Return the origin-id element of a message or null, if absent.
084     *
085     * @param message message
086     * @return origin-id element
087     */
088    public static OriginIdElement getOriginId(Message message) {
089        return (OriginIdElement) message.getExtensionElement(OriginIdElement.ELEMENT, StableUniqueStanzaIdManager.NAMESPACE);
090    }
091
092    @Override
093    public String getNamespace() {
094        return StableUniqueStanzaIdManager.NAMESPACE;
095    }
096
097    @Override
098    public String getElementName() {
099        return ELEMENT;
100    }
101
102    @Override
103    public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
104        return new XmlStringBuilder(this)
105                .attribute(ATTR_ID, getId())
106                .closeEmptyElement();
107    }
108
109    @Override
110    public boolean equals(Object other) {
111        if (this == other) {
112            return true;
113        }
114        if (other == null) {
115            return false;
116        }
117        if (!(other instanceof OriginIdElement)) {
118            return false;
119        }
120
121        OriginIdElement otherId = (OriginIdElement) other;
122        return getId().equals(otherId.getId());
123    }
124
125    @Override
126    public int hashCode() {
127        return getId().hashCode();
128    }
129}