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 org.jivesoftware.smack.packet.Message; 020import org.jivesoftware.smack.util.XmlStringBuilder; 021import org.jivesoftware.smackx.sid.StableUniqueStanzaIdManager; 022 023public class StanzaIdElement extends StableAndUniqueIdElement { 024 025 public static final String ELEMENT = "stanza-id"; 026 public static final String ATTR_BY = "by"; 027 028 private final String by; 029 030 public StanzaIdElement(String by) { 031 super(); 032 this.by = by; 033 } 034 035 public StanzaIdElement(String id, String by) { 036 super(id); 037 this.by = by; 038 } 039 040 /** 041 * Return true, if a message contains a stanza-id element. 042 * 043 * @param message message 044 * @return true if message contains stanza-id element, otherwise false. 045 */ 046 public static boolean hasStanzaId(Message message) { 047 return getStanzaId(message) != null; 048 } 049 050 /** 051 * Return the stanza-id element of a message. 052 * 053 * @param message message 054 * @return stanza-id element of a jid, or null if absent. 055 */ 056 public static StanzaIdElement getStanzaId(Message message) { 057 return message.getExtension(StanzaIdElement.ELEMENT, StableUniqueStanzaIdManager.NAMESPACE); 058 } 059 060 public String getBy() { 061 return by; 062 } 063 064 @Override 065 public String getNamespace() { 066 return StableUniqueStanzaIdManager.NAMESPACE; 067 } 068 069 @Override 070 public String getElementName() { 071 return ELEMENT; 072 } 073 074 @Override 075 public XmlStringBuilder toXML(String enclosingNamespace) { 076 return new XmlStringBuilder(this) 077 .attribute(ATTR_ID, getId()) 078 .attribute(ATTR_BY, getBy()) 079 .closeEmptyElement(); 080 } 081}