net.ex_337.gradient.util
Class XMPPUtils

java.lang.Object
  extended bynet.ex_337.gradient.util.XMPPUtils

public class XMPPUtils
extends Object

Utility class designed to work with SMACK XMPP API.

Author:
Ian

Field Summary
static org.jivesoftware.smack.filter.PacketFilter ALL_PACKETS
          Accepts all packets except ERROR and RESULT IQ packets.
 
Constructor Summary
XMPPUtils()
           
 
Method Summary
static String generateThreadID()
          Generates a reasonably unique String for use as a thread ID.
static DirectivePacketExtension getDirectiveExtension(org.jivesoftware.smack.packet.Packet packet)
          Returns the directive set extension from a packet, if it exists.
static IQ getIQResponse(org.jivesoftware.smack.XMPPConnection xmppConnection, IQ request, String remoteJID, long timeout)
          This allows you to treat IQ a bit like RPC.
static Element getResponseElement(org.jivesoftware.smack.XMPPConnection xmppConnection, Element element, IQ.Type type, String JID, long timeout)
          This allows you to treat IQ as if it were RPC, with the element being the call and the response being the response.
static TargetPathPacketExtension getTargetPathExtension(org.jivesoftware.smack.packet.Packet packet)
          Returns the target path extension for a packet, if it exists.
static boolean hasDirectiveExtension(org.jivesoftware.smack.packet.Packet packet)
          Checks for the presence of a directive set extension on the given packet.
static boolean hasTargetPathExtension(org.jivesoftware.smack.packet.Packet packet)
          Checks for the presence of a target path extension on the given packet
static boolean isSubscribedTo(org.jivesoftware.smack.XMPPConnection xmppConnection, String remoteJID)
           
static void logConnectionEvents(org.jivesoftware.smack.XMPPConnection xmppConnection)
          Adds a connectionListener to the given XMPPConnection that logs all connection events to the default Logger, including stack traces on any events triggered by an Exception.
static void logEverything(org.jivesoftware.smack.XMPPConnection xmppConnection)
          Adds a connectionListener to the given XMPPConnection that logs all connection events to System.out, including stack traces on any events triggered by an Exception.
static void logIncomingPackets(org.jivesoftware.smack.XMPPConnection xmppConnection)
          Logs ALL incoming packets for the given XMPPConnection to the default Logger.
static void logOutgoingPackets(org.jivesoftware.smack.XMPPConnection xmppConnection)
          Logs ALL outgoing packets for the given XMPPConnection to the default Logger.
static Element newDocResponseElement()
          Creates a blank document response element for use with a DocResponseIQ.
static void prepareEnvironment()
          Registers all current Gradient PacketExtensionProviderS and IQProviders with the ProviderManager.
static void printPacket(org.jivesoftware.smack.packet.Packet p)
          Utility method for debugging.
static void sendErrorResponse(org.jivesoftware.smack.XMPPConnection xmppConnection, IQ original, int code, String message)
          Sends an IQ ERROR response to an IQ.
static void sendReplyPacket(org.jivesoftware.smack.XMPPConnection xmppConnection, org.jivesoftware.smack.packet.Packet original, org.jivesoftware.smack.packet.Packet response, boolean copyID)
          Sends the response after setting the from and to fields as the inverse of the original.
static void setReply(org.jivesoftware.smack.packet.Packet original, org.jivesoftware.smack.packet.Packet response, boolean copyId)
          Sets the correct details on a response packet, optionally copying the ID too.
static String toString(org.jivesoftware.smack.packet.Packet p)
          Returns a string representation of a packet.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ALL_PACKETS

public static final org.jivesoftware.smack.filter.PacketFilter ALL_PACKETS
Accepts all packets except ERROR and RESULT IQ packets. ERRROR and REUSLT packets are listened for by whatever sent the GET or SET that resulted in these packets.

Constructor Detail

XMPPUtils

public XMPPUtils()
Method Detail

hasDirectiveExtension

public static boolean hasDirectiveExtension(org.jivesoftware.smack.packet.Packet packet)
Checks for the presence of a directive set extension on the given packet.

Parameters:
packet - the packet to test
Returns:
true iff the packet contains an element named DIRECTIVE_SET with namespace GRADIENT_NAMESPACE.

getDirectiveExtension

public static DirectivePacketExtension getDirectiveExtension(org.jivesoftware.smack.packet.Packet packet)
Returns the directive set extension from a packet, if it exists.

Parameters:
packet - the packet enclosing the extension.
Returns:
null if an IQ, or the extension doesn't exist, or a DirectivePacketExtension otherwise.
Throws:
ClassCastException - if the DirectivePacketExtensionProvider hasn't been registered.

hasTargetPathExtension

public static boolean hasTargetPathExtension(org.jivesoftware.smack.packet.Packet packet)
Checks for the presence of a target path extension on the given packet

Parameters:
packet - the packet to test
Returns:
true iff the packet contains an element named TARGET_PATH with namespace GRADIENT_NAMESPACE.

getTargetPathExtension

public static TargetPathPacketExtension getTargetPathExtension(org.jivesoftware.smack.packet.Packet packet)
Returns the target path extension for a packet, if it exists.

Parameters:
packet - the packet enclosing the extension.
Returns:
null if an IQ, or if the extension doesn't exist, or a TargetPathPacketExtension otherwise
Throws:
ClassCastException - if the TargetPathPacketExtensionProvider hasn't been registered.

printPacket

public static void printPacket(org.jivesoftware.smack.packet.Packet p)
Utility method for debugging. Prints all possible details on the given packet to the default Logger.

Parameters:
p - the packet to print.

toString

public static String toString(org.jivesoftware.smack.packet.Packet p)
Returns a string representation of a packet.

Parameters:
p - the packet
Returns:
the string representation of the packet.

logEverything

public static void logEverything(org.jivesoftware.smack.XMPPConnection xmppConnection)
Adds a connectionListener to the given XMPPConnection that logs all connection events to System.out, including stack traces on any events triggered by an Exception.

Parameters:
xmppConnection - the connection for which to log connection events.

logConnectionEvents

public static void logConnectionEvents(org.jivesoftware.smack.XMPPConnection xmppConnection)
Adds a connectionListener to the given XMPPConnection that logs all connection events to the default Logger, including stack traces on any events triggered by an Exception.

Parameters:
xmppConnection - the connection for which to log connection events.

logIncomingPackets

public static void logIncomingPackets(org.jivesoftware.smack.XMPPConnection xmppConnection)
Logs ALL incoming packets for the given XMPPConnection to the default Logger. using the printPacket method on this class. Useful for debugging

Parameters:
xmppConnection - the connection for which to log incoming packets.

logOutgoingPackets

public static void logOutgoingPackets(org.jivesoftware.smack.XMPPConnection xmppConnection)
Logs ALL outgoing packets for the given XMPPConnection to the default Logger. using the printPacket method on this class. Useful for debugging.

Parameters:
xmppConnection - the connection for which to log outgoing packets.

getIQResponse

public static IQ getIQResponse(org.jivesoftware.smack.XMPPConnection xmppConnection,
                               IQ request,
                               String remoteJID,
                               long timeout)
This allows you to treat IQ a bit like RPC. Blocks on the response if timeout is -1, otherwise times out.

Parameters:
xmppConnection - the connection to send the IQ on
request - the request IQ.
timeout - the time to wait for a response. use -1 to have no timeout.
remoteJID - the Jabber ID to send the IQ to.
Returns:
the response IQ, or null if the timeout wasn't -1 and the request timed out.

getResponseElement

public static Element getResponseElement(org.jivesoftware.smack.XMPPConnection xmppConnection,
                                         Element element,
                                         IQ.Type type,
                                         String JID,
                                         long timeout)
This allows you to treat IQ as if it were RPC, with the element being the call and the response being the response. Blocks on the response if timeout is -1, otherwise times out.

Parameters:
xmppConnection - the connection to send the IQ on
element - the element to send
timeout - the time to wait for a response. use -1 to have no timeout.
type - the IQ type (GET or SET) to use when sending the element.
JID - the Jabber ID to send the element to.
Returns:
null if the request times out (if a timeout was set) or if there is no child XML, the child element otherwise.

setReply

public static void setReply(org.jivesoftware.smack.packet.Packet original,
                            org.jivesoftware.smack.packet.Packet response,
                            boolean copyId)
Sets the correct details on a response packet, optionally copying the ID too. You can pass the same packet as both the original and response if you're sending the same thing. Does NOT set the type to RESULT on IQ packets.

Parameters:
original - the original packet
response - the response packet
copyId - if true, copy the packet ID.

sendReplyPacket

public static void sendReplyPacket(org.jivesoftware.smack.XMPPConnection xmppConnection,
                                   org.jivesoftware.smack.packet.Packet original,
                                   org.jivesoftware.smack.packet.Packet response,
                                   boolean copyID)
Sends the response after setting the from and to fields as the inverse of the original. If copyID, copies the ID too.

Parameters:
original - the original incoming packet
response - the outbound packet to send.
copyID - if true, copies the ID from the original to the response. Should only be used for IQs.
xmppConnection - the connection with which to send the reply packet.

sendErrorResponse

public static void sendErrorResponse(org.jivesoftware.smack.XMPPConnection xmppConnection,
                                     IQ original,
                                     int code,
                                     String message)
Sends an IQ ERROR response to an IQ.

Parameters:
xmppConnection - the connection to use.
original - the original IQ to reply to.
code - the error code.
message - the error message.

isSubscribedTo

public static boolean isSubscribedTo(org.jivesoftware.smack.XMPPConnection xmppConnection,
                                     String remoteJID)
Parameters:
xmppConnection - the connection
remoteJID - the XMPP JID of the entity.
Returns:
true if there is a BOTH or TO for this JID.

prepareEnvironment

public static void prepareEnvironment()
Registers all current Gradient PacketExtensionProviderS and IQProviders with the ProviderManager. It's a good idea to call this before using other classes :-)


generateThreadID

public static final String generateThreadID()
Generates a reasonably unique String for use as a thread ID. Uses SHA1PRNG.

Returns:
a 24-byte thread ID.

newDocResponseElement

public static Element newDocResponseElement()
Creates a blank document response element for use with a DocResponseIQ.

Returns:
a blank document response