diff --git a/build/resources/META-INF/smack.providers b/build/resources/META-INF/smack.providers index dd189456d..e40e0c896 100644 --- a/build/resources/META-INF/smack.providers +++ b/build/resources/META-INF/smack.providers @@ -139,7 +139,7 @@ query jabber:iq:search - org.jivesoftware.smackx.packet.UserSearch$Provider + org.jivesoftware.smackx.search.UserSearch$Provider \ No newline at end of file diff --git a/source/org/jivesoftware/smackx/search/SimpleUserSearch.java b/source/org/jivesoftware/smackx/search/SimpleUserSearch.java new file mode 100644 index 000000000..1469bbcd4 --- /dev/null +++ b/source/org/jivesoftware/smackx/search/SimpleUserSearch.java @@ -0,0 +1,68 @@ +/** + * $RCSfile: ,v $ + * $Revision: $ + * $Date: $ + * + * Copyright (C) 1999-2005 Jive Software. All rights reserved. + * + * This software is the proprietary information of Jive Software. + * Use is subject to license terms. + */ +package org.jivesoftware.smackx.search; + +import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smackx.Form; +import org.jivesoftware.smackx.FormField; + +import java.util.Iterator; + +public class SimpleUserSearch extends IQ { + + private Form form; + + public Form getForm() { + return form; + } + + public void setForm(Form form) { + this.form = form; + } + + public String getChildElementXML() { + StringBuffer buf = new StringBuffer(); + buf.append(""); + buf.append(getConvertedPacket()); + buf.append(""); + System.out.println(buf.toString()); + return buf.toString(); + } + + public String getConvertedPacket() { + StringBuffer buf = new StringBuffer(); + + if (form == null) { + form = Form.getFormFrom(this); + } + Iterator fields = form.getFields(); + while (fields.hasNext()) { + FormField field = (FormField) fields.next(); + String name = field.getVariable(); + String value = getValue(field); + if (value.trim().length() > 0) { + buf.append("<").append(name).append(">").append(value).append(""); + } + } + + return buf.toString(); + } + + public String getValue(FormField formField) { + Iterator values = formField.getValues(); + while (values.hasNext()) { + return (String) values.next(); + } + return ""; + } + + +} diff --git a/source/org/jivesoftware/smackx/packet/UserSearch.java b/source/org/jivesoftware/smackx/search/UserSearch.java similarity index 64% rename from source/org/jivesoftware/smackx/packet/UserSearch.java rename to source/org/jivesoftware/smackx/search/UserSearch.java index ee54a8b5d..ea2aaa6fa 100644 --- a/source/org/jivesoftware/smackx/packet/UserSearch.java +++ b/source/org/jivesoftware/smackx/search/UserSearch.java @@ -1,15 +1,14 @@ /** * $RCSfile: ,v $ - * $Revision: 1.0 $ - * $Date: 2005/05/25 04:20:03 $ + * $Revision: $ + * $Date: $ * * Copyright (C) 1999-2005 Jive Software. All rights reserved. * - * This software is the proprietary information of Jive Software. Use is - subject to license terms. + * This software is the proprietary information of Jive Software. + * Use is subject to license terms. */ - -package org.jivesoftware.smackx.packet; +package org.jivesoftware.smackx.search; import org.jivesoftware.smack.PacketCollector; import org.jivesoftware.smack.SmackConfiguration; @@ -20,7 +19,9 @@ import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.provider.IQProvider; import org.jivesoftware.smack.util.PacketParserUtils; import org.jivesoftware.smackx.Form; +import org.jivesoftware.smackx.FormField; import org.jivesoftware.smackx.ReportedData; +import org.jivesoftware.smackx.packet.DataForm; import org.xmlpull.v1.XmlPullParser; /** @@ -39,7 +40,6 @@ public class UserSearch extends IQ { } - public String getChildElementXML() { StringBuffer buf = new StringBuffer(); buf.append(""); @@ -48,7 +48,6 @@ public class UserSearch extends IQ { return buf.toString(); } - /** * Returns the form for all search fields supported by the search service. * @@ -100,6 +99,39 @@ public class UserSearch extends IQ { IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); + // Cancel the collector. + collector.cancel(); + if (response == null) { + throw new XMPPException("No response from server on status set."); + } + if (response.getError() != null) { + return sendSimpleSearchForm(con, searchForm, searchService); + } + return ReportedData.getReportedDataFrom(response); + } + + /** + * Sends the filled out answer form to be sent and queried by the search service. + * + * @param con the current XMPPConnection. + * @param searchForm the Form to send for querying. + * @param searchService the search service to use. (ex. search.jivesoftware.com) + * @return ReportedData the data found from the query. + * @throws org.jivesoftware.smack.XMPPException + * thrown if a server error has occured. + */ + public ReportedData sendSimpleSearchForm(XMPPConnection con, Form searchForm, String searchService) throws XMPPException { + SimpleUserSearch search = new SimpleUserSearch(); + search.setForm(searchForm); + search.setType(IQ.Type.SET); + search.setTo(searchService); + + PacketCollector collector = con.createPacketCollector(new PacketIDFilter(search.getPacketID())); + + con.sendPacket(search); + + IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); + // Cancel the collector. collector.cancel(); if (response == null) { @@ -122,10 +154,16 @@ public class UserSearch extends IQ { public IQ parseIQ(XmlPullParser parser) throws Exception { UserSearch search = new UserSearch(); + SimpleUserSearch simpleUserSearch = new SimpleUserSearch(); + boolean done = false; while (!done) { int eventType = parser.next(); - if (eventType == XmlPullParser.START_TAG) { + if (eventType == XmlPullParser.START_TAG && parser.getName().equals("instructions")) { + buildDataForm(simpleUserSearch, parser.getText(), parser); + return simpleUserSearch; + } + else if (eventType == XmlPullParser.START_TAG) { // Otherwise, it must be a packet extension. search.addExtension(PacketParserUtils.parsePacketExtension(parser.getName(), parser.getNamespace(), parser)); @@ -142,4 +180,29 @@ public class UserSearch extends IQ { } } + private static void buildDataForm(SimpleUserSearch search, String instructions, XmlPullParser parser) throws Exception { + DataForm dataForm = new DataForm(Form.TYPE_FORM); + boolean done = false; + while (!done) { + int eventType = parser.next(); + + dataForm.setTitle("User Search"); + dataForm.addInstruction(instructions); + if (eventType == XmlPullParser.START_TAG) { + String name = parser.getName(); + FormField field = new FormField(name); + field.setType(FormField.TYPE_TEXT_SINGLE); + dataForm.addField(field); + } + else if (eventType == XmlPullParser.END_TAG) { + if (parser.getName().equals("query")) { + done = true; + } + } + } + + search.addExtension(dataForm); + } + + } diff --git a/source/org/jivesoftware/smackx/search/UserSearchManager.java b/source/org/jivesoftware/smackx/search/UserSearchManager.java new file mode 100644 index 000000000..8e6904ad5 --- /dev/null +++ b/source/org/jivesoftware/smackx/search/UserSearchManager.java @@ -0,0 +1,70 @@ +/** + * $RCSfile: ,v $ + * $Revision: $ + * $Date: $ + * + * Copyright (C) 1999-2005 Jive Software. All rights reserved. + * + * This software is the proprietary information of Jive Software. + * Use is subject to license terms. + */ +package org.jivesoftware.smackx.search; + +import org.jivesoftware.smack.XMPPConnection; +import org.jivesoftware.smack.XMPPException; +import org.jivesoftware.smackx.Form; +import org.jivesoftware.smackx.ReportedData; + +public class UserSearchManager { + + private XMPPConnection con; + private String serviceName; + private UserSearch userSearch; + + public UserSearchManager(XMPPConnection con, String serviceName) { + this.con = con; + this.serviceName = serviceName; + + userSearch = new UserSearch(); + } + + public Form getSearchForm() { + try { + return userSearch.getSearchForm(con, serviceName); + } + catch (XMPPException e) { + e.printStackTrace(); + } + return null; + } + + public ReportedData sendSearchForm(Form searchForm) { + ReportedData data = null; + try { + data = userSearch.sendSearchForm(con, searchForm, serviceName); + } + catch (XMPPException e) { + e.printStackTrace(); + } + + return data; + } + + public static void main(String args[]) throws Exception { + XMPPConnection.DEBUG_ENABLED = true; + XMPPConnection con = new XMPPConnection("jabber.org"); + con.login("ddman", "whocares"); + + UserSearchManager manager = new UserSearchManager(con, "users.jabber.org"); + Form f = manager.getSearchForm(); + + Form answers = f.createAnswerForm(); + answers.setAnswer("first", "Matt"); + + ReportedData data = manager.sendSearchForm(answers); + System.out.println(data); + + } + + +}