mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-26 16:22:06 +01:00
1) Added SearchField and DataForm support for JEP 55.
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@2839 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
ab474ea063
commit
61590449b7
5 changed files with 274 additions and 75 deletions
|
@ -91,6 +91,28 @@ public class ReportedData {
|
|||
this.title = dataForm.getTitle();
|
||||
}
|
||||
|
||||
|
||||
public ReportedData(){
|
||||
// Allow for model creation of ReportedData.
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new <code>Row</code>.
|
||||
* @param row the new row to add.
|
||||
*/
|
||||
public void addRow(Row row){
|
||||
rows.add(row);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new <code>Column</code>
|
||||
* @param column the column to add.
|
||||
*/
|
||||
public void addColumn(Column column){
|
||||
columns.add(column);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns an Iterator for the rows returned from a search.
|
||||
*
|
||||
|
@ -138,7 +160,7 @@ public class ReportedData {
|
|||
* @param variable the variable name of the column.
|
||||
* @param type the format for the returned data.
|
||||
*/
|
||||
private Column(String label, String variable, String type) {
|
||||
public Column(String label, String variable, String type) {
|
||||
this.label = label;
|
||||
this.variable = variable;
|
||||
this.type = type;
|
||||
|
@ -195,7 +217,7 @@ public class ReportedData {
|
|||
public static class Row {
|
||||
private List fields = new ArrayList();
|
||||
|
||||
private Row(List fields) {
|
||||
public Row(List fields) {
|
||||
this.fields = fields;
|
||||
}
|
||||
|
||||
|
@ -225,11 +247,11 @@ public class ReportedData {
|
|||
}
|
||||
}
|
||||
|
||||
private static class Field {
|
||||
public static class Field {
|
||||
private String variable;
|
||||
private List values;
|
||||
|
||||
private Field(String variable, List values) {
|
||||
public Field(String variable, List values) {
|
||||
this.variable = variable;
|
||||
this.values = values;
|
||||
}
|
||||
|
|
|
@ -13,41 +13,57 @@ package org.jivesoftware.smackx.search;
|
|||
import org.jivesoftware.smack.packet.IQ;
|
||||
import org.jivesoftware.smackx.Form;
|
||||
import org.jivesoftware.smackx.FormField;
|
||||
import org.jivesoftware.smackx.ReportedData;
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class SimpleUserSearch extends IQ {
|
||||
|
||||
/**
|
||||
* SimpleUserSearch is used to support the non-dataform type of JEP 55. This provides
|
||||
* the mechanism for allowing always type ReportedData to be returned by any search result,
|
||||
* regardless of the form of the data returned from the server.
|
||||
*
|
||||
* @author Derek DeMoro
|
||||
*/
|
||||
class SimpleUserSearch extends IQ {
|
||||
private Form form;
|
||||
|
||||
public Form getForm() {
|
||||
return form;
|
||||
}
|
||||
private ReportedData data;
|
||||
|
||||
public void setForm(Form form) {
|
||||
this.form = form;
|
||||
}
|
||||
|
||||
public ReportedData getReportedData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
public String getChildElementXML() {
|
||||
StringBuffer buf = new StringBuffer();
|
||||
buf.append("<query xmlns=\"jabber:iq:search\">");
|
||||
buf.append(getConvertedPacket());
|
||||
buf.append(getItemsToSearch());
|
||||
buf.append("</query>");
|
||||
System.out.println(buf.toString());
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
public String getConvertedPacket() {
|
||||
private String getItemsToSearch() {
|
||||
StringBuffer buf = new StringBuffer();
|
||||
|
||||
if (form == null) {
|
||||
form = Form.getFormFrom(this);
|
||||
}
|
||||
|
||||
if(form == null){
|
||||
return "";
|
||||
}
|
||||
|
||||
Iterator fields = form.getFields();
|
||||
while (fields.hasNext()) {
|
||||
FormField field = (FormField) fields.next();
|
||||
String name = field.getVariable();
|
||||
String value = getValue(field);
|
||||
String value = getSingleValue(field);
|
||||
if (value.trim().length() > 0) {
|
||||
buf.append("<").append(name).append(">").append(value).append("</").append(name).append(">");
|
||||
}
|
||||
|
@ -56,7 +72,7 @@ public class SimpleUserSearch extends IQ {
|
|||
return buf.toString();
|
||||
}
|
||||
|
||||
public String getValue(FormField formField) {
|
||||
private static String getSingleValue(FormField formField) {
|
||||
Iterator values = formField.getValues();
|
||||
while (values.hasNext()) {
|
||||
return (String) values.next();
|
||||
|
@ -64,5 +80,52 @@ public class SimpleUserSearch extends IQ {
|
|||
return "";
|
||||
}
|
||||
|
||||
protected void parseItems(XmlPullParser parser) throws Exception {
|
||||
ReportedData data = new ReportedData();
|
||||
|
||||
boolean done = false;
|
||||
|
||||
List fields = new ArrayList();
|
||||
while (!done) {
|
||||
if(parser.getAttributeCount() > 0){
|
||||
String jid = parser.getAttributeValue("", "jid");
|
||||
List valueList = new ArrayList();
|
||||
valueList.add(jid);
|
||||
ReportedData.Field field = new ReportedData.Field("jid", valueList);
|
||||
fields.add(field);
|
||||
}
|
||||
int eventType = parser.next();
|
||||
|
||||
if (eventType == XmlPullParser.START_TAG && parser.getName().equals("item")) {
|
||||
fields = new ArrayList();
|
||||
}
|
||||
else if (eventType == XmlPullParser.END_TAG && parser.getName().equals("item")) {
|
||||
ReportedData.Row row = new ReportedData.Row(fields);
|
||||
data.addRow(row);
|
||||
}
|
||||
else if (eventType == XmlPullParser.START_TAG) {
|
||||
String name = parser.getName();
|
||||
String value = parser.nextText();
|
||||
|
||||
List valueList = new ArrayList();
|
||||
valueList.add(value);
|
||||
ReportedData.Field field = new ReportedData.Field(name, valueList);
|
||||
fields.add(field);
|
||||
|
||||
// Column name should be the same
|
||||
ReportedData.Column column = new ReportedData.Column(name, name, "text-single");
|
||||
data.addColumn(column);
|
||||
}
|
||||
else if (eventType == XmlPullParser.END_TAG) {
|
||||
if (parser.getName().equals("query")) {
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
37
source/org/jivesoftware/smackx/search/Test.java
Normal file
37
source/org/jivesoftware/smackx/search/Test.java
Normal file
|
@ -0,0 +1,37 @@
|
|||
/**
|
||||
* $RCSfile: ,v $
|
||||
* $Revision: 1.0 $
|
||||
* $Date: 2005/05/25 04:20:03 $
|
||||
*
|
||||
* 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.smackx.Form;
|
||||
import org.jivesoftware.smackx.ReportedData;
|
||||
|
||||
public class Test {
|
||||
|
||||
public static void main(String args[]) throws Exception {
|
||||
XMPPConnection.DEBUG_ENABLED = true;
|
||||
XMPPConnection con = new XMPPConnection("jabber.org");
|
||||
con.login("ddman", "whocares");
|
||||
|
||||
UserSearchManager search = new UserSearchManager(con, "users.jabber.org");
|
||||
Form searchForm = search.getSearchForm();
|
||||
|
||||
Form answerForm = searchForm.createAnswerForm();
|
||||
answerForm.setAnswer("last", "Lynch");
|
||||
|
||||
ReportedData data = search.getSearchResults(answerForm);
|
||||
|
||||
|
||||
System.out.println(data);
|
||||
|
||||
}
|
||||
}
|
|
@ -36,8 +36,10 @@ import org.xmlpull.v1.XmlPullParser;
|
|||
*/
|
||||
public class UserSearch extends IQ {
|
||||
|
||||
/**
|
||||
* Creates a new instance of UserSearch.
|
||||
*/
|
||||
public UserSearch() {
|
||||
|
||||
}
|
||||
|
||||
public String getChildElementXML() {
|
||||
|
@ -53,8 +55,9 @@ public class UserSearch extends IQ {
|
|||
*
|
||||
* @param con the current XMPPConnection.
|
||||
* @param searchService the search service to use. (ex. search.jivesoftware.com)
|
||||
* @return the search form received by the server.
|
||||
* @throws org.jivesoftware.smack.XMPPException
|
||||
* thrown if a server error has occured.
|
||||
* thrown if a server error has occurred.
|
||||
*/
|
||||
public Form getSearchForm(XMPPConnection con, String searchService) throws XMPPException {
|
||||
UserSearch search = new UserSearch();
|
||||
|
@ -85,7 +88,7 @@ public class UserSearch extends IQ {
|
|||
* @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.
|
||||
* thrown if a server error has occurred.
|
||||
*/
|
||||
public ReportedData sendSearchForm(XMPPConnection con, Form searchForm, String searchService) throws XMPPException {
|
||||
UserSearch search = new UserSearch();
|
||||
|
@ -107,6 +110,8 @@ public class UserSearch extends IQ {
|
|||
if (response.getError() != null) {
|
||||
return sendSimpleSearchForm(con, searchForm, searchService);
|
||||
}
|
||||
|
||||
|
||||
return ReportedData.getReportedDataFrom(response);
|
||||
}
|
||||
|
||||
|
@ -118,7 +123,7 @@ public class UserSearch extends IQ {
|
|||
* @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.
|
||||
* thrown if a server error has occurred.
|
||||
*/
|
||||
public ReportedData sendSimpleSearchForm(XMPPConnection con, Form searchForm, String searchService) throws XMPPException {
|
||||
SimpleUserSearch search = new SimpleUserSearch();
|
||||
|
@ -140,7 +145,11 @@ public class UserSearch extends IQ {
|
|||
if (response.getError() != null) {
|
||||
throw new XMPPException(response.getError());
|
||||
}
|
||||
return ReportedData.getReportedDataFrom(response);
|
||||
|
||||
if (response instanceof SimpleUserSearch) {
|
||||
return ((SimpleUserSearch) response).getReportedData();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -148,12 +157,15 @@ public class UserSearch extends IQ {
|
|||
*/
|
||||
public static class Provider implements IQProvider {
|
||||
|
||||
/**
|
||||
* Provider Constructor.
|
||||
*/
|
||||
public Provider() {
|
||||
super();
|
||||
}
|
||||
|
||||
public IQ parseIQ(XmlPullParser parser) throws Exception {
|
||||
UserSearch search = new UserSearch();
|
||||
UserSearch search = null;
|
||||
SimpleUserSearch simpleUserSearch = new SimpleUserSearch();
|
||||
|
||||
boolean done = false;
|
||||
|
@ -163,8 +175,13 @@ public class UserSearch extends IQ {
|
|||
buildDataForm(simpleUserSearch, parser.getText(), parser);
|
||||
return simpleUserSearch;
|
||||
}
|
||||
else if (eventType == XmlPullParser.START_TAG) {
|
||||
else if (eventType == XmlPullParser.START_TAG && parser.getName().equals("item")) {
|
||||
simpleUserSearch.parseItems(parser);
|
||||
return simpleUserSearch;
|
||||
}
|
||||
else if (eventType == XmlPullParser.START_TAG && parser.getNamespace().equals("jabber:x:data")) {
|
||||
// Otherwise, it must be a packet extension.
|
||||
search = new UserSearch();
|
||||
search.addExtension(PacketParserUtils.parsePacketExtension(parser.getName(),
|
||||
parser.getNamespace(), parser));
|
||||
|
||||
|
@ -176,7 +193,10 @@ public class UserSearch extends IQ {
|
|||
}
|
||||
}
|
||||
|
||||
return search;
|
||||
if (search != null) {
|
||||
return search;
|
||||
}
|
||||
return simpleUserSearch;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -188,7 +208,7 @@ public class UserSearch extends IQ {
|
|||
|
||||
dataForm.setTitle("User Search");
|
||||
dataForm.addInstruction(instructions);
|
||||
if (eventType == XmlPullParser.START_TAG) {
|
||||
if (eventType == XmlPullParser.START_TAG && !parser.getNamespace().equals("jabber:x:data")) {
|
||||
String name = parser.getName();
|
||||
FormField field = new FormField(name);
|
||||
field.setType(FormField.TYPE_TEXT_SINGLE);
|
||||
|
@ -199,9 +219,15 @@ public class UserSearch extends IQ {
|
|||
done = true;
|
||||
}
|
||||
}
|
||||
else if (eventType == XmlPullParser.START_TAG && parser.getNamespace().equals("jabber:x:data")) {
|
||||
search.addExtension(PacketParserUtils.parsePacketExtension(parser.getName(),
|
||||
parser.getNamespace(), parser));
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
if (search.getExtension("x", "jabber:x:data") == null) {
|
||||
search.addExtension(dataForm);
|
||||
}
|
||||
|
||||
search.addExtension(dataForm);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -14,39 +14,90 @@ import org.jivesoftware.smack.XMPPConnection;
|
|||
import org.jivesoftware.smack.XMPPException;
|
||||
import org.jivesoftware.smackx.Form;
|
||||
import org.jivesoftware.smackx.ReportedData;
|
||||
import org.jivesoftware.smackx.ServiceDiscoveryManager;
|
||||
import org.jivesoftware.smackx.packet.DiscoverInfo;
|
||||
import org.jivesoftware.smackx.packet.DiscoverItems;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The UserSearchManager is a facade built upon Jabber Search Services (JEP-055) to allow for searching
|
||||
* repositories on a Jabber Server. This implementation allows for transparency of implementation of
|
||||
* searching (DataForms or No DataForms), but allows the user to simply use the DataForm model for both
|
||||
* types of support.
|
||||
* <pre>
|
||||
* XMPPConnection con = new XMPPConnection("jabber.org");
|
||||
* con.login("john", "doe");
|
||||
* UserSearchManager search = new UserSearchManager(con, "users.jabber.org");
|
||||
* Form searchForm = search.getSearchForm();
|
||||
* Form answerForm = searchForm.createAnswerForm();
|
||||
* answerForm.setAnswer("last", "DeMoro");
|
||||
* ReportedData data = search.getSearchResults(answerForm);
|
||||
* // Use Returned Data
|
||||
* </pre>
|
||||
*
|
||||
* @author Derek DeMoro
|
||||
*/
|
||||
public class UserSearchManager {
|
||||
|
||||
private XMPPConnection con;
|
||||
private String serviceName;
|
||||
private String searchService;
|
||||
private UserSearch userSearch;
|
||||
|
||||
public UserSearchManager(XMPPConnection con, String serviceName) {
|
||||
/**
|
||||
* Creates a new UserSearchManager.
|
||||
*
|
||||
* @param con the XMPPConnection to use.
|
||||
* @param searchService the name of the search service. (ex. search.jivesoftware.com)
|
||||
*/
|
||||
public UserSearchManager(XMPPConnection con, String searchService) {
|
||||
this.con = con;
|
||||
this.serviceName = serviceName;
|
||||
|
||||
this.searchService = searchService;
|
||||
userSearch = new UserSearch();
|
||||
}
|
||||
|
||||
public Form getSearchForm() {
|
||||
try {
|
||||
return userSearch.getSearchForm(con, serviceName);
|
||||
}
|
||||
catch (XMPPException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
/**
|
||||
* Returns the form to fill out to perform a search.
|
||||
*
|
||||
* @return the form to fill out to perform a search.
|
||||
* @throws XMPPException thrown if a server error has occurred.
|
||||
*/
|
||||
public Form getSearchForm() throws XMPPException {
|
||||
return userSearch.getSearchForm(con, searchService);
|
||||
}
|
||||
|
||||
public ReportedData sendSearchForm(Form searchForm) {
|
||||
ReportedData data = null;
|
||||
try {
|
||||
data = userSearch.sendSearchForm(con, searchForm, serviceName);
|
||||
}
|
||||
catch (XMPPException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
/**
|
||||
* Submits a search form to the server and returns the resulting information
|
||||
* in the form of <code>ReportedData</code>
|
||||
*
|
||||
* @param searchForm the <code>Form</code> to submit for searching.
|
||||
* @return the ReportedData returned by the server.
|
||||
* @throws XMPPException thrown if a server error has occurred.
|
||||
*/
|
||||
public ReportedData getSearchResults(Form searchForm) throws XMPPException {
|
||||
return userSearch.sendSearchForm(con, searchForm, searchService);
|
||||
}
|
||||
|
||||
return data;
|
||||
/**
|
||||
* Returns a collection of search services found on the server.
|
||||
*
|
||||
* @return a Collection of search services found on the server.
|
||||
* @throws XMPPException thrown if a server error has occurred.
|
||||
*/
|
||||
public Collection getSearchServices() throws XMPPException {
|
||||
List searchServices = new ArrayList();
|
||||
ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(con);
|
||||
DiscoverItems items = discoManager.discoverItems(con.getServiceName());
|
||||
for (Iterator it = items.getItems(); it.hasNext();) {
|
||||
DiscoverItems.Item item = (DiscoverItems.Item) it.next();
|
||||
DiscoverInfo info = discoManager.discoverInfo(item.getEntityID());
|
||||
if (info.containsFeature("jabber:iq:search")) {
|
||||
searchServices.add(item.getEntityID());
|
||||
}
|
||||
}
|
||||
return searchServices;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue