2005-09-16 18:35:15 +02:00
|
|
|
/**
|
|
|
|
* $RCSfile: ,v $
|
2005-09-17 01:04:30 +02:00
|
|
|
* $Revision: $
|
|
|
|
* $Date: $
|
2005-09-16 18:35:15 +02:00
|
|
|
*
|
|
|
|
* Copyright (C) 1999-2005 Jive Software. All rights reserved.
|
|
|
|
*
|
2005-09-17 01:04:30 +02:00
|
|
|
* This software is the proprietary information of Jive Software.
|
|
|
|
* Use is subject to license terms.
|
2005-09-16 18:35:15 +02:00
|
|
|
*/
|
2005-09-17 01:04:30 +02:00
|
|
|
package org.jivesoftware.smackx.search;
|
2005-09-16 18:35:15 +02:00
|
|
|
|
|
|
|
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.util.PacketParserUtils;
|
|
|
|
import org.jivesoftware.smackx.Form;
|
2005-09-17 01:04:30 +02:00
|
|
|
import org.jivesoftware.smackx.FormField;
|
2005-09-16 18:35:15 +02:00
|
|
|
import org.jivesoftware.smackx.ReportedData;
|
2005-09-17 01:04:30 +02:00
|
|
|
import org.jivesoftware.smackx.packet.DataForm;
|
2005-09-16 18:35:15 +02:00
|
|
|
import org.xmlpull.v1.XmlPullParser;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements the protocol currently used to search information repositories on the Jabber network. To date, the jabber:iq:search protocol
|
|
|
|
* has been used mainly to search for people who have registered with user directories (e.g., the "Jabber User Directory" hosted at users.jabber.org).
|
|
|
|
* However, the jabber:iq:search protocol is not limited to user directories, and could be used to search other Jabber information repositories
|
|
|
|
* (such as chatroom directories) or even to provide a Jabber interface to conventional search engines.
|
|
|
|
* <p/>
|
|
|
|
* The basic functionality is to query an information repository regarding the possible search fields, to send a search query, and to receive search results.
|
|
|
|
*
|
|
|
|
* @author Derek DeMoro
|
|
|
|
*/
|
|
|
|
public class UserSearch extends IQ {
|
|
|
|
|
2005-09-19 06:06:59 +02:00
|
|
|
/**
|
|
|
|
* Creates a new instance of UserSearch.
|
|
|
|
*/
|
2005-09-16 18:35:15 +02:00
|
|
|
public UserSearch() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getChildElementXML() {
|
2006-07-18 07:14:33 +02:00
|
|
|
StringBuilder buf = new StringBuilder();
|
2005-09-16 18:35:15 +02:00
|
|
|
buf.append("<query xmlns=\"jabber:iq:search\">");
|
|
|
|
buf.append(getExtensionsXML());
|
|
|
|
buf.append("</query>");
|
|
|
|
return buf.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-09-16 18:50:05 +02:00
|
|
|
* Returns the form for all search fields supported by the search service.
|
2005-09-16 18:35:15 +02:00
|
|
|
*
|
|
|
|
* @param con the current XMPPConnection.
|
|
|
|
* @param searchService the search service to use. (ex. search.jivesoftware.com)
|
2005-09-19 06:06:59 +02:00
|
|
|
* @return the search form received by the server.
|
2005-09-16 18:35:15 +02:00
|
|
|
* @throws org.jivesoftware.smack.XMPPException
|
2005-09-19 06:06:59 +02:00
|
|
|
* thrown if a server error has occurred.
|
2005-09-16 18:35:15 +02:00
|
|
|
*/
|
|
|
|
public Form getSearchForm(XMPPConnection con, String searchService) throws XMPPException {
|
|
|
|
UserSearch search = new UserSearch();
|
|
|
|
search.setType(IQ.Type.GET);
|
|
|
|
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) {
|
|
|
|
throw new XMPPException("No response from server on status set.");
|
|
|
|
}
|
|
|
|
if (response.getError() != null) {
|
|
|
|
throw new XMPPException(response.getError());
|
|
|
|
}
|
|
|
|
return Form.getFormFrom(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-09-16 18:50:05 +02:00
|
|
|
* Sends the filled out answer form to be sent and queried by the search service.
|
2005-09-16 18:35:15 +02:00
|
|
|
*
|
|
|
|
* @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)
|
2005-09-16 18:50:05 +02:00
|
|
|
* @return ReportedData the data found from the query.
|
2005-09-16 18:35:15 +02:00
|
|
|
* @throws org.jivesoftware.smack.XMPPException
|
2005-09-19 06:06:59 +02:00
|
|
|
* thrown if a server error has occurred.
|
2005-09-16 18:35:15 +02:00
|
|
|
*/
|
|
|
|
public ReportedData sendSearchForm(XMPPConnection con, Form searchForm, String searchService) throws XMPPException {
|
|
|
|
UserSearch search = new UserSearch();
|
|
|
|
search.setType(IQ.Type.SET);
|
|
|
|
search.setTo(searchService);
|
|
|
|
search.addExtension(searchForm.getDataFormToSend());
|
|
|
|
|
|
|
|
PacketCollector collector = con.createPacketCollector(new PacketIDFilter(search.getPacketID()));
|
|
|
|
|
|
|
|
con.sendPacket(search);
|
|
|
|
|
|
|
|
IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
|
|
|
|
|
2005-09-17 01:04:30 +02:00
|
|
|
// 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);
|
|
|
|
}
|
2005-09-19 06:06:59 +02:00
|
|
|
|
|
|
|
|
2005-09-17 01:04:30 +02:00
|
|
|
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
|
2005-09-19 06:06:59 +02:00
|
|
|
* thrown if a server error has occurred.
|
2005-09-17 01:04:30 +02:00
|
|
|
*/
|
|
|
|
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());
|
|
|
|
|
2005-09-16 18:35:15 +02:00
|
|
|
// Cancel the collector.
|
|
|
|
collector.cancel();
|
|
|
|
if (response == null) {
|
|
|
|
throw new XMPPException("No response from server on status set.");
|
|
|
|
}
|
|
|
|
if (response.getError() != null) {
|
|
|
|
throw new XMPPException(response.getError());
|
|
|
|
}
|
2005-09-19 06:06:59 +02:00
|
|
|
|
|
|
|
if (response instanceof SimpleUserSearch) {
|
|
|
|
return ((SimpleUserSearch) response).getReportedData();
|
|
|
|
}
|
|
|
|
return null;
|
2005-09-16 18:35:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal Search service Provider.
|
|
|
|
*/
|
|
|
|
public static class Provider implements IQProvider {
|
|
|
|
|
2005-09-19 06:06:59 +02:00
|
|
|
/**
|
|
|
|
* Provider Constructor.
|
|
|
|
*/
|
2005-09-16 18:35:15 +02:00
|
|
|
public Provider() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
public IQ parseIQ(XmlPullParser parser) throws Exception {
|
2005-09-19 06:06:59 +02:00
|
|
|
UserSearch search = null;
|
2005-09-17 01:04:30 +02:00
|
|
|
SimpleUserSearch simpleUserSearch = new SimpleUserSearch();
|
|
|
|
|
2005-09-16 18:35:15 +02:00
|
|
|
boolean done = false;
|
|
|
|
while (!done) {
|
|
|
|
int eventType = parser.next();
|
2005-09-17 01:04:30 +02:00
|
|
|
if (eventType == XmlPullParser.START_TAG && parser.getName().equals("instructions")) {
|
2005-09-19 16:44:02 +02:00
|
|
|
buildDataForm(simpleUserSearch, parser.nextText(), parser);
|
2005-09-17 01:04:30 +02:00
|
|
|
return simpleUserSearch;
|
|
|
|
}
|
2005-09-19 06:06:59 +02:00
|
|
|
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")) {
|
2005-09-16 18:35:15 +02:00
|
|
|
// Otherwise, it must be a packet extension.
|
2005-09-19 06:06:59 +02:00
|
|
|
search = new UserSearch();
|
2005-09-16 18:35:15 +02:00
|
|
|
search.addExtension(PacketParserUtils.parsePacketExtension(parser.getName(),
|
|
|
|
parser.getNamespace(), parser));
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (eventType == XmlPullParser.END_TAG) {
|
|
|
|
if (parser.getName().equals("query")) {
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-19 06:06:59 +02:00
|
|
|
if (search != null) {
|
|
|
|
return search;
|
|
|
|
}
|
|
|
|
return simpleUserSearch;
|
2005-09-16 18:35:15 +02:00
|
|
|
}
|
|
|
|
}
|
2005-09-16 18:50:05 +02:00
|
|
|
|
2005-09-17 01:04:30 +02:00
|
|
|
private static void buildDataForm(SimpleUserSearch search, String instructions, XmlPullParser parser) throws Exception {
|
|
|
|
DataForm dataForm = new DataForm(Form.TYPE_FORM);
|
|
|
|
boolean done = false;
|
2005-09-19 16:44:02 +02:00
|
|
|
dataForm.setTitle("User Search");
|
|
|
|
dataForm.addInstruction(instructions);
|
2005-09-17 01:04:30 +02:00
|
|
|
while (!done) {
|
|
|
|
int eventType = parser.next();
|
|
|
|
|
2005-09-19 06:06:59 +02:00
|
|
|
if (eventType == XmlPullParser.START_TAG && !parser.getNamespace().equals("jabber:x:data")) {
|
2005-09-17 01:04:30 +02:00
|
|
|
String name = parser.getName();
|
|
|
|
FormField field = new FormField(name);
|
2005-09-20 23:02:30 +02:00
|
|
|
|
|
|
|
// Handle hard coded values.
|
|
|
|
if(name.equals("first")){
|
|
|
|
field.setLabel("First Name");
|
|
|
|
}
|
|
|
|
else if(name.equals("last")){
|
|
|
|
field.setLabel("Last Name");
|
|
|
|
}
|
|
|
|
else if(name.equals("email")){
|
|
|
|
field.setLabel("Email Address");
|
|
|
|
}
|
|
|
|
else if(name.equals("nick")){
|
|
|
|
field.setLabel("Nickname");
|
|
|
|
}
|
|
|
|
|
2005-09-17 01:04:30 +02:00
|
|
|
field.setType(FormField.TYPE_TEXT_SINGLE);
|
|
|
|
dataForm.addField(field);
|
|
|
|
}
|
|
|
|
else if (eventType == XmlPullParser.END_TAG) {
|
|
|
|
if (parser.getName().equals("query")) {
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
2005-09-19 06:06:59 +02:00
|
|
|
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);
|
2005-09-17 01:04:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-09-16 18:35:15 +02:00
|
|
|
}
|