/*
* Gamers Own Instant Messenger
* Copyright (C) 2005-2006 Herbert Poul (kahless@sphene.net)
* http://goim.sphene.net
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
package net.sphene.goim.rcp.xmpp;
import java.util.ArrayList;
import java.util.List;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.provider.IQProvider;
import org.jivesoftware.smack.provider.ProviderManager;
import org.xmlpull.v1.XmlPullParser;
public class BrowseIQ extends IQ {
List<String> namespaces = new ArrayList<String>();
public String type;
public String jid;
public String name;
@Override
public String getChildElementXML() {
return "<query xmlns=\"jabber:iq:browse\" />";
}
public String[] getNamespaces() { return namespaces.toArray(new String[namespaces.size()]); }
public static class BrowseIQProvider implements IQProvider {
static {
ProviderManager.addIQProvider("service","jabber:iq:browse",new BrowseIQProvider());
}
public IQ parseIQ(XmlPullParser parser) throws Exception {
BrowseIQ iq = new BrowseIQ();
iq.type = parser.getAttributeValue("","type");
iq.jid = parser.getAttributeValue("","jid");
iq.name = parser.getAttributeValue("","name");
while(parser.next() != XmlPullParser.END_TAG) {
if(parser.getEventType() == XmlPullParser.START_TAG) {
if(parser.getName().equals("ns")) {
String ns = parser.nextText();
if(ns != null && !ns.equals(""))
iq.namespaces.add(ns);
while(parser.getEventType() != XmlPullParser.END_TAG) parser.next();
}
}
}
return iq;
}
public static BrowseIQ retrieveBrowseRequest(XMPPConnection conn, String to) throws XMPPException {
BrowseIQ iq = new BrowseIQ();
iq.setType(IQ.Type.GET);
iq.setTo(to);
PacketCollector collector =
conn.createPacketCollector(new PacketIDFilter(iq.getPacketID()));
conn.sendPacket(iq);
// Wait up to 5 seconds for a result.
IQ result = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
// Stop queuing results
collector.cancel();
if (result == null) {
throw new XMPPException("No response from the server.");
}
if (result.getType() == IQ.Type.ERROR) {
throw new XMPPException(result.getError());
}
return (BrowseIQ)result;
}
}
}