mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-12-28 07:07:58 +01:00
Adding multiple IQ for JEP 55 handling.
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@2830 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
3e60826296
commit
23bc043dc1
4 changed files with 211 additions and 10 deletions
|
@ -139,7 +139,7 @@
|
|||
<iqProvider>
|
||||
<elementName>query</elementName>
|
||||
<namespace>jabber:iq:search</namespace>
|
||||
<className>org.jivesoftware.smackx.packet.UserSearch$Provider</className>
|
||||
<className>org.jivesoftware.smackx.search.UserSearch$Provider</className>
|
||||
</iqProvider>
|
||||
|
||||
</smackProviders>
|
68
source/org/jivesoftware/smackx/search/SimpleUserSearch.java
Normal file
68
source/org/jivesoftware/smackx/search/SimpleUserSearch.java
Normal file
|
@ -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("<query xmlns=\"jabber:iq:search\">");
|
||||
buf.append(getConvertedPacket());
|
||||
buf.append("</query>");
|
||||
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("</").append(name).append(">");
|
||||
}
|
||||
}
|
||||
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
public String getValue(FormField formField) {
|
||||
Iterator values = formField.getValues();
|
||||
while (values.hasNext()) {
|
||||
return (String) values.next();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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("<query xmlns=\"jabber:iq:search\">");
|
||||
|
@ -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 <code>Form</code> 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);
|
||||
}
|
||||
|
||||
|
||||
}
|
70
source/org/jivesoftware/smackx/search/UserSearchManager.java
Normal file
70
source/org/jivesoftware/smackx/search/UserSearchManager.java
Normal file
|
@ -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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in a new issue