Class Packet

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

@NotThreadSafe public abstract class Packet extends 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:

  • Message -- used to send data between users.
  • Presence -- contains user presence information or is used to manage presence subscriptions.
  • IQ -- exchange information and perform queries using a request/response protocol.
Author:
Matt Tucker
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected static final org.dom4j.DocumentFactory
     
    protected org.dom4j.Element
     
    protected JID
     
    protected JID
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    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

    Modifier and Type
    Method
    Description
    void
    Adds the element contained in the PacketExtension to the element of this packet.
    abstract Packet
    Creates a deep copy of this packet.
    boolean
    deleteExtension(String name, String namespace)
    Deletes the first element whose element name and namespace matches the specified element name and namespace.
    org.dom4j.Element
    Returns the DOM4J Element that backs the packet.
    Returns the packet error, or null if there is no packet error.
    getExtension(String name, 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.
    Returns the XMPP address (JID) that the packet is from, or null if the "from" attribute is not set.
    Returns the packet ID, or null if the packet does not have an ID.
    Returns the XMPP address (JID) that the packet is addressed to, or null if the "to" attribute is not set.
    void
    Sets the packet error.
    void
    Sets the packet error using the specified condition.
    void
    Sets the XMPP address (JID) that the packet comes from.
    void
    setFrom(JID from)
    Sets the XMPP address (JID) that the packet comes from.
    void
    Sets the packet ID.
    void
    Sets the XMPP address (JID) that the packet is addressed to.
    void
    setTo(JID to)
    Sets the XMPP address (JID) that the packet is address to.
     
    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 Details

    • docFactory

      protected static final org.dom4j.DocumentFactory docFactory
    • element

      protected org.dom4j.Element element
    • toJID

      protected JID toJID
    • fromJID

      protected JID 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 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 Details

    • getID

      public 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(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(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(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(String name, 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(String name, 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 String toXML()
      Returns the textual XML representation of this packet.
      Returns:
      the textual XML representation of this packet.
    • toString

      public String toString()
      Overrides:
      toString in class Object