/**
* Slim XMPP Stream reader. This reader only understands enough XMPP
- * to handle logins and recv messages.
+ * to handle logins and recv messages. It's implemented as a StAX parser.
* @author Bill Erickson, Georgia Public Library Systems
*/
public class XMPPReader implements Runnable {
* @param inStream the inbound XML stream
*/
public XMPPReader(InputStream inStream) {
- msgQueue = new ConcurrentLinkedQueue();
+ msgQueue = new ConcurrentLinkedQueue<XMPPMessage>();
this.inStream = inStream;
resetBuffers();
xmlState = XMLState.IN_NOTHING;
}
- /** Thread kickoff point */
+
+ /** Kickoff the thread */
public void run() {
read();
}
public void read() {
try {
+
XMLInputFactory factory = XMLInputFactory.newInstance();
- /** disable as many features as possible to speed up the parsing */
+ /** disable as many unused features as possible to speed up the parsing */
factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE);
factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
factory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE);
String name = reader.getName().toString();
- if("stream:stream".equals(name)) {
- setXMPPStreamState(XMPPStreamState.CONNECT_RECV);
- return;
- }
-
- if("iq".equals(name)) {
- if("result".equals(reader.getAttributeValue(null, "type")))
- setXMPPStreamState(XMPPStreamState.CONNECTED);
- return;
- }
-
if("message".equals(name)) {
xmlState = XMLState.IN_BODY;
+
/** add a special case for the opensrf "router_from" attribute */
String rf = reader.getAttributeValue(null, "router_from");
if( rf != null )
return;
}
+ if("body".equals(name)) {
+ xmlState = XMLState.IN_BODY;
+ return;
+ }
+
if("thread".equals(name)) {
xmlState = XMLState.IN_THREAD;
return;
}
+ if("stream:stream".equals(name)) {
+ setXMPPStreamState(XMPPStreamState.CONNECT_RECV);
+ return;
+ }
+
+ if("iq".equals(name)) {
+ if("result".equals(reader.getAttributeValue(null, "type")))
+ setXMPPStreamState(XMPPStreamState.CONNECTED);
+ return;
+ }
+
if("status".equals(name)) {
xmlState = XMLState.IN_STATUS;
return;
public class TestXMPP {
+ /**
+ * Connects to the jabber server and waits for inbound messages.
+ * If a recipient is provided, a small message is sent to the recipient.
+ */
public static void main(String args[]) throws Exception {
String host;
resource = args[4];
} catch(ArrayIndexOutOfBoundsException e) {
- System.err.println("usage: org.opensrf.test.TestXMPP <host> <port> <username> <password> <resource>");
+ System.err.println("usage: org.opensrf.test.TestXMPP <host> <port> <username> <password> <resource> [<recipient>]");
return;
}
XMPPMessage msg;
if( args.length == 6 ) {
+
/** they specified a recipient */
+
recipient = args[5];
msg = new XMPPMessage();
msg.setTo(recipient);
while(true) {
System.out.println("waiting for message...");
- msg = session.recv(-1);
+ msg = session.recv(-1); /* wait forever for a message to arrive */
System.out.println("got message: " + msg.toXML());
}
}