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