Class Stanza
- All Implemented Interfaces:
Element,NamedElement,StanzaView,TopLevelStreamElement,XmlElement,XmlLangElement
- Direct Known Subclasses:
IQ,MessageOrPresence
Every stanza has a unique ID (which is automatically generated, but can be overridden). Stanza IDs are required for IQ stanzas and recommended for presence and message stanzas. Optionally, the "to" and "from" fields can be set.
XMPP Stanzas are Message, IQ and Presence. Which therefore subclass this
class. If you think you need to subclass this class, then you are doing something wrong.
Use StanzaBuilder to construct a stanza instance. All instance mutating methods of this
class are deprecated, although not all of them are currently marked as such, and must not be used.
- See Also:
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedStanza()protectedprotectedStanza(StanzaBuilder<?> stanzaBuilder) -
Method Summary
Modifier and TypeMethodDescriptionprotected final voidAdd to, from, and id attributes.final voidaddExtension(XmlElement extension) Adds a stanza extension to the packet.final voidaddExtensions(Collection<? extends XmlElement> extensions) Adds a collection of stanza extensions to the packet.protected final voidAppend an XMPPError is this stanza has one set.static StringReturns the default language used for all messages containing localized content.final StanzaErrorgetError()Returns the error associated with this packet, ornullif there are no errors.final XmlElementgetExtension(String namespace) Returns the first extension of this stanza that has the given namespace.final XmlElementgetExtension(QName qname) final XmlElementgetExtensionElement(String elementName, String namespace) Returns the first extension that matches the specified element name and namespace, ornullif it doesn't exist.final List<XmlElement>Returns a list of all extension elements of this stanza.final <E extends ExtensionElement>
List<E>getExtensions(Class<E> extensionElementClass) Return all extension elements of the given type.final List<XmlElement>getExtensions(String elementName, String namespace) Return a list of all extensions with the given element name and namespace.final List<XmlElement>getExtensions(QName qname) final MultiMap<QName,XmlElement> final org.jxmpp.jid.JidgetFrom()Returns who the stanza is being sent "from" ornullif the value is not set.final StringReturns the xml:lang of this XML element, or null if one has not been set.final StringReturns the root element XML namespace.final StringReturns the unique ID of the stanza.final org.jxmpp.jid.JidgetTo()Returns who the stanza is being sent "to", ornullif the value is not set.final booleanhasExtension(String namespace) Check if a extension element with the given namespace exists.final booleanhasExtension(String elementName, String namespace) Check if a stanza extension with the given element and namespace exists.final booleanCheck if this stanza has an ID set.protected voidfinal XmlElementoverrideExtension(XmlElement extension) Add the given extension and override eventually existing extensions with the same name and namespace.final XmlElementremoveExtension(String elementName, String namespace) Remove the stanza extension with the given elementName and namespace.voidsetError(StanzaError stanzaError) Sets the error for this stanza.voidsetFrom(org.jxmpp.jid.Jid from) Sets who the packet is being sent "from".protected StringEnsure that a stanza ID is set.voidsetStanzaId(String id) Sets the unique ID of the packet.final voidsetTo(org.jxmpp.jid.Jid to) Sets who the packet is being sent "to".final voidThrows anIllegalArgumentExceptionif this stanza has no stanza ID set.abstract StringtoString()Returns a short String describing the Stanza.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.jivesoftware.smack.packet.NamedElement
getElementNameMethods inherited from interface org.jivesoftware.smack.packet.StanzaView
getExtension, hasExtension, hasExtensionMethods inherited from interface org.jivesoftware.smack.packet.XmlElement
getQName
-
Field Details
-
TEXT
- See Also:
-
ITEM
- See Also:
-
DEFAULT_LANGUAGE
-
language
Optional value of the 'xml:lang' attribute of the outermost element of the stanza.Such an attribute is defined for all stanza types. For IQ, see for example XEP-50 3.7: "The requester SHOULD provide its locale information using the "xml:lang " attribute on either the <iq/> (RECOMMENDED) or <command/> element."
-
-
Constructor Details
-
Stanza
protected Stanza() -
Stanza
-
Stanza
-
-
Method Details
-
getStanzaId
Description copied from interface:StanzaViewReturns the unique ID of the stanza. The returned value could benull.- Specified by:
getStanzaIdin interfaceStanzaView- Returns:
- the packet's unique ID or
nullif the id is not available.
-
setStanzaId
Sets the unique ID of the packet. To indicate that a stanza has no id passnullas the packet's id value.- Parameters:
id- the unique ID for the packet.
-
hasStanzaIdSet
public final boolean hasStanzaIdSet()Check if this stanza has an ID set.- Returns:
- true if the stanza ID is set, false otherwise.
- Since:
- 4.1
-
throwIfNoStanzaId
public final void throwIfNoStanzaId()Throws anIllegalArgumentExceptionif this stanza has no stanza ID set.- Throws:
IllegalArgumentException- if this stanza has no stanza ID set.- Since:
- 4.4.
-
setNewStanzaId
Ensure that a stanza ID is set.- Returns:
- the stanza ID.
- Since:
- 4.4
-
getTo
public final org.jxmpp.jid.Jid getTo()Description copied from interface:StanzaViewReturns who the stanza is being sent "to", ornullif the value is not set. The XMPP protocol often makes the "to" attribute optional, so it does not always need to be set.- Specified by:
getToin interfaceStanzaView- Returns:
- who the stanza is being sent to, or
nullif the value has not been set.
-
setTo
public final void setTo(org.jxmpp.jid.Jid to) Sets who the packet is being sent "to". The XMPP protocol often makes the "to" attribute optional, so it does not always need to be set.- Parameters:
to- who the packet is being sent to.
-
getFrom
public final org.jxmpp.jid.Jid getFrom()Description copied from interface:StanzaViewReturns who the stanza is being sent "from" ornullif the value is not set. The XMPP protocol often makes the "from" attribute optional, so it does not always need to be set.- Specified by:
getFromin interfaceStanzaView- Returns:
- who the stanza is being sent from, or
nullif the value has not been set.
-
setFrom
public void setFrom(org.jxmpp.jid.Jid from) Sets who the packet is being sent "from". The XMPP protocol often makes the "from" attribute optional, so it does not always need to be set.- Parameters:
from- who the packet is being sent to.
-
getError
Description copied from interface:StanzaViewReturns the error associated with this packet, ornullif there are no errors.- Specified by:
getErrorin interfaceStanzaView- Returns:
- the error sub-packet or
nullif there isn't an error.
-
setError
Sets the error for this stanza.- Parameters:
stanzaError- the error that this stanza carries and hence signals.
-
getLanguage
Description copied from interface:XmlLangElementReturns the xml:lang of this XML element, or null if one has not been set.- Specified by:
getLanguagein interfaceXmlElement- Specified by:
getLanguagein interfaceXmlLangElement- Returns:
- the xml:lang of this XML element, or null.
-
getExtensions
Description copied from interface:StanzaViewReturns a list of all extension elements of this stanza.- Specified by:
getExtensionsin interfaceStanzaView- Returns:
- a list of all extension elements of this stanza.
-
getExtensionsMap
-
getExtensions
Return a list of all extensions with the given element name and namespace.Changes to the returned set will update the stanza extensions, if the returned set is not the empty set.
- Parameters:
elementName- the element name, must not be null.namespace- the namespace of the element(s), must not be null.- Returns:
- a set of all matching extensions.
- Since:
- 4.1
-
getExtensions
- Specified by:
getExtensionsin interfaceStanzaView
-
getExtensions
Description copied from interface:StanzaViewReturn all extension elements of the given type. Returns the empty list if there a none.- Specified by:
getExtensionsin interfaceStanzaView- Type Parameters:
E- the type of extension elements.- Parameters:
extensionElementClass- the class of the type of extension elements.- Returns:
- a list of extension elements of that type, which may be empty.
-
getExtension
Returns the first extension of this stanza that has the given namespace.When possible, use
getExtensionElement(String, String)instead.- Parameters:
namespace- the namespace of the extension that is desired.- Returns:
- the stanza extension with the given namespace.
-
getExtensionElement
Returns the first extension that matches the specified element name and namespace, ornullif it doesn't exist. Extensions are are arbitrary XML elements in standard XMPP stanzas.Note that it is recommended to use
StanzaView.getExtension(Class)instead of this method. TheStanzaView.getExtension(Class)is more robust, as it is type safe.- Parameters:
elementName- the XML element name of the extension.namespace- the XML element namespace of the extension.- Returns:
- the extension, or
nullif it doesn't exist.
-
getExtension
- Specified by:
getExtensionin interfaceStanzaView
-
addExtension
Adds a stanza extension to the packet. Does nothing if extension is null.Please note that although this method is not yet marked as deprecated, it is recommended to use
StanzaBuilder.addExtension(XmlElement)instead.- Parameters:
extension- a stanza extension.
-
overrideExtension
Add the given extension and override eventually existing extensions with the same name and namespace.Please note that although this method is not yet marked as deprecated, it is recommended to use
StanzaBuilder.overrideExtension(XmlElement)instead.- Parameters:
extension- the extension element to add.- Returns:
- one of the removed extensions or
nullif there are none. - Since:
- 4.1.2
-
addExtensions
Adds a collection of stanza extensions to the packet. Does nothing if extensions is null.- Parameters:
extensions- a collection of stanza extensions
-
hasExtension
Check if a stanza extension with the given element and namespace exists.The argument
elementNamemay be null.- Parameters:
elementName- TODO javadoc me pleasenamespace- TODO javadoc me please- Returns:
- true if a stanza extension exists, false otherwise.
-
hasExtension
Description copied from interface:StanzaViewCheck if a extension element with the given namespace exists.- Specified by:
hasExtensionin interfaceStanzaView- Parameters:
namespace- the namespace of the extension element to check for.- Returns:
- true if a stanza extension exists, false otherwise.
-
removeExtension
Remove the stanza extension with the given elementName and namespace.- Parameters:
elementName- TODO javadoc me pleasenamespace- TODO javadoc me please- Returns:
- the removed stanza extension or null.
-
toString
Returns a short String describing the Stanza. This method is suited for log purposes. -
getNamespace
Description copied from interface:XmlElementReturns the root element XML namespace.- Specified by:
getNamespacein interfaceXmlElement- Returns:
- the namespace.
-
getDefaultLanguage
Returns the default language used for all messages containing localized content.- Returns:
- the default language
-
addCommonAttributes
Add to, from, and id attributes.- Parameters:
xml- theXmlStringBuilder.
-
logCommonAttributes
-
appendErrorIfExists
Append an XMPPError is this stanza has one set.- Parameters:
xml- the XmlStringBuilder to append the error to.
-