Package org.xmpp.packet
Class Packet
java.lang.Object
org.xmpp.packet.Packet
An XMPP packet (also referred to as a stanza). Each packet is backed by a
DOM4J Element. A set of convenience methods allows easy manipulation of
the Element, or the Element can be accessed directly and manipulated.
There are three core packet types:
- Author:
- Matt Tucker
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddExtension(PacketExtension extension) Adds the element contained in the PacketExtension to the element of this packet.abstract PacketCreates a deep copy of this packet.booleandeleteExtension(String name, String namespace) Deletes the first element whose element name and namespace matches the specified element name and namespace.org.dom4j.ElementReturns the DOM4J Element that backs the packet.getError()Returns the packet error, ornullif there is no packet error.getExtension(String name, String namespace) Returns aPacketExtensionon the first element found in this packet for the specifiednameandnamespaceornullif none was found.getFrom()Returns the XMPP address (JID) that the packet is from, ornullif the "from" attribute is not set.getID()Returns the packet ID, ornullif the packet does not have an ID.getTo()Returns the XMPP address (JID) that the packet is addressed to, ornullif the "to" attribute is not set.voidsetError(PacketError error) Sets the packet error.voidsetError(PacketError.Condition condition) Sets the packet error using the specified condition.voidSets the XMPP address (JID) that the packet comes from.voidSets the XMPP address (JID) that the packet comes from.voidSets the packet ID.voidSets the XMPP address (JID) that the packet is addressed to.voidSets the XMPP address (JID) that the packet is address to.toString()toXML()Returns the textual XML representation of this packet.
-
Field Details
-
docFactory
protected static final org.dom4j.DocumentFactory docFactory -
element
protected org.dom4j.Element element -
toJID
-
fromJID
-
-
Constructor Details
-
Packet
public Packet(org.dom4j.Element element) Constructs a new Packet. The TO address contained in the XML Element will only be validated. In other words, stringprep operations will only be performed on the TO JID to verify that it is well-formed. The FROM address is assigned by the server so there is no need to verify it.- Parameters:
element- the XML Element that contains the packet contents.
-
Packet
public Packet(org.dom4j.Element element, boolean skipValidation) Constructs a new Packet. The JID address contained in the XML Element may not be validated. When validation can be skipped then stringprep operations will not be performed on the JIDs to verify that addresses are well-formed. However, when validation cannot be skipped thenonlythe TO address will be verified. The FROM address is assigned by the server so there is no need to verify it.- Parameters:
element- the XML Element that contains the packet contents.skipValidation- true if stringprep should not be applied to the TO address.
-
Packet
protected Packet()Constructs a new Packet with no element data. This method is used by extensions of this class that require a more optimized path for creating new packets.
-
-
Method Details
-
getID
Returns the packet ID, ornullif the packet does not have an ID. Packet ID's are optional, except for IQ packets.- Returns:
- the packet ID.
-
setID
Sets the packet ID. Packet ID's are optional, except for IQ packets.- Parameters:
ID- the packet ID.
-
getTo
Returns the XMPP address (JID) that the packet is addressed to, ornullif the "to" attribute is not set. The XMPP protocol often makes the "to" attribute optional, so it does not always need to be set.- Returns:
- the XMPP address (JID) that the packet is addressed to, or
nullif not set.
-
setTo
Sets the XMPP address (JID) that the packet is addressed to. The XMPP protocol often makes the "to" attribute optional, so it does not always need to be set.- Parameters:
to- the XMPP address (JID) that the packet is addressed to.
-
setTo
Sets the XMPP address (JID) that the packet is address to. The XMPP protocol often makes the "to" attribute optional, so it does not always need to be set.- Parameters:
to- the XMPP address (JID) that the packet is addressed to.
-
getFrom
Returns the XMPP address (JID) that the packet is from, ornullif the "from" attribute is not set. The XMPP protocol often makes the "from" attribute optional, so it does not always need to be set.- Returns:
- the XMPP address that the packet is from, or
nullif not set.
-
setFrom
Sets the XMPP address (JID) that the packet comes from. The XMPP protocol often makes the "from" attribute optional, so it does not always need to be set.- Parameters:
from- the XMPP address (JID) that the packet comes from.
-
setFrom
Sets the XMPP address (JID) that the packet comes from. The XMPP protocol often makes the "from" attribute optional, so it does not always need to be set.- Parameters:
from- the XMPP address (JID) that the packet comes from.
-
addExtension
Adds the element contained in the PacketExtension to the element of this packet. It is important that this is the first and last time the element contained in PacketExtension is added to another Packet. Otherwise, a runtime error will be thrown when trying to add the PacketExtension's element to the Packet's element. Future modifications to the PacketExtension will be reflected in this Packet.- Parameters:
extension- the PacketExtension whose element will be added to this Packet's element.
-
getExtension
Returns aPacketExtensionon the first element found in this packet for the specifiednameandnamespaceornullif none was found.- Parameters:
name- the child element name.namespace- the child element namespace.- Returns:
- a PacketExtension on the first element found in this packet for the specified name and namespace or null if none was found.
-
deleteExtension
Deletes the first element whose element name and namespace matches the specified element name and namespace.Notice that this method may remove any child element that matches the specified element name and namespace even if that element was not added to the Packet using a
PacketExtension.- Parameters:
name- the child element name.namespace- the child element namespace.- Returns:
- true if a child element was removed.
-
getError
Returns the packet error, ornullif there is no packet error.- Returns:
- the packet error.
-
setError
Sets the packet error. Calling this method will automatically set the packet "type" attribute to "error".- Parameters:
error- the packet error.
-
setError
Sets the packet error using the specified condition. Calling this method will automatically set the packet "type" attribute to "error". This is a convenience method equivalent to calling:setError(new PacketError(condition));- Parameters:
condition- the error condition.
-
createCopy
Creates a deep copy of this packet.- Returns:
- a deep copy of this packet.
-
getElement
public org.dom4j.Element getElement()Returns the DOM4J Element that backs the packet. The element is the definitive representation of the packet and can be manipulated directly to change packet contents.- Returns:
- the DOM4J Element that represents the packet.
-
toXML
Returns the textual XML representation of this packet.- Returns:
- the textual XML representation of this packet.
-
toString
-