org.xmpp.packet
Class Packet

java.lang.Object
  extended by org.xmpp.packet.Packet
Direct Known Subclasses:
IQ, Message, Presence

@NotThreadSafe
public abstract class Packet
extends java.lang.Object

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
protected static org.dom4j.DocumentFactory docFactory
           
protected  org.dom4j.Element element
           
protected  JID fromJID
           
protected  JID toJID
           
 
Constructor Summary
protected Packet()
          Constructs a new Packet with no element data.
  Packet(org.dom4j.Element element)
          Constructs a new Packet.
  Packet(org.dom4j.Element element, boolean skipValidation)
          Constructs a new Packet.
 
Method Summary
 void addExtension(PacketExtension extension)
          Adds the element contained in the PacketExtension to the element of this packet.
abstract  Packet createCopy()
          Creates a deep copy of this packet.
 boolean deleteExtension(java.lang.String name, java.lang.String namespace)
          Deletes the first element whose element name and namespace matches the specified element name and namespace.
 org.dom4j.Element getElement()
          Returns the DOM4J Element that backs the packet.
 PacketError getError()
          Returns the packet error, or null if there is no packet error.
 PacketExtension getExtension(java.lang.String name, java.lang.String namespace)
          Returns a PacketExtension on the first element found in this packet for the specified name and namespace or null if none was found.
 JID getFrom()
          Returns the XMPP address (JID) that the packet is from, or null if the "from" attribute is not set.
 java.lang.String getID()
          Returns the packet ID, or null if the packet does not have an ID.
 JID getTo()
          Returns the XMPP address (JID) that the packet is addressed to, or null if the "to" attribute is not set.
 void setError(PacketError.Condition condition)
          Sets the packet error using the specified condition.
 void setError(PacketError error)
          Sets the packet error.
 void setFrom(JID from)
          Sets the XMPP address (JID) that the packet comes from.
 void setFrom(java.lang.String from)
          Sets the XMPP address (JID) that the packet comes from.
 void setID(java.lang.String ID)
          Sets the packet ID.
 void setTo(JID to)
          Sets the XMPP address (JID) that the packet is address to.
 void setTo(java.lang.String to)
          Sets the XMPP address (JID) that the packet is addressed to.
 java.lang.String toString()
           
 java.lang.String toXML()
          Returns the textual XML representation of this packet.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

docFactory

protected static final org.dom4j.DocumentFactory docFactory

element

protected org.dom4j.Element element

toJID

protected JID toJID

fromJID

protected JID fromJID
Constructor Detail

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 then only the 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 Detail

getID

public java.lang.String getID()
Returns the packet ID, or null if the packet does not have an ID. Packet ID's are optional, except for IQ packets.

Returns:
the packet ID.

setID

public void setID(java.lang.String ID)
Sets the packet ID. Packet ID's are optional, except for IQ packets.

Parameters:
ID - the packet ID.

getTo

public JID getTo()
Returns the XMPP address (JID) that the packet is addressed to, or null if 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 null if not set.

setTo

public void setTo(java.lang.String to)
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

public void setTo(JID to)
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

public JID getFrom()
Returns the XMPP address (JID) that the packet is from, or null if 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 null if not set.

setFrom

public void setFrom(java.lang.String from)
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

public void setFrom(JID from)
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

public void addExtension(PacketExtension extension)
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

public PacketExtension getExtension(java.lang.String name,
                                    java.lang.String namespace)
Returns a PacketExtension on the first element found in this packet for the specified name and namespace or null if 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

public boolean deleteExtension(java.lang.String name,
                               java.lang.String namespace)
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

public PacketError getError()
Returns the packet error, or null if there is no packet error.

Returns:
the packet error.

setError

public void setError(PacketError error)
Sets the packet error. Calling this method will automatically set the packet "type" attribute to "error".

Parameters:
error - the packet error.

setError

public void setError(PacketError.Condition condition)
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

public abstract Packet 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

public java.lang.String toXML()
Returns the textual XML representation of this packet.

Returns:
the textual XML representation of this packet.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object


Copyright © 2009-2010 Ignite Realtime. All Rights Reserved.