2005-09-17 01:04:30 +02:00
|
|
|
/**
|
2007-09-27 01:14:51 +02:00
|
|
|
* Copyright 2003-2007 Jive Software.
|
2005-09-17 01:04:30 +02:00
|
|
|
*
|
2007-09-27 01:14:51 +02:00
|
|
|
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2005-09-17 01:04:30 +02:00
|
|
|
*
|
2007-09-27 01:14:51 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2005-09-17 01:04:30 +02:00
|
|
|
*/
|
2007-09-27 01:14:51 +02:00
|
|
|
|
2005-09-17 01:04:30 +02:00
|
|
|
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;
|
2005-09-19 06:06:59 +02:00
|
|
|
import org.jivesoftware.smackx.ServiceDiscoveryManager;
|
|
|
|
import org.jivesoftware.smackx.packet.DiscoverInfo;
|
|
|
|
import org.jivesoftware.smackx.packet.DiscoverItems;
|
2005-09-17 01:04:30 +02:00
|
|
|
|
2005-09-19 06:06:59 +02:00
|
|
|
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
|
|
|
|
*/
|
2005-09-17 01:04:30 +02:00
|
|
|
public class UserSearchManager {
|
|
|
|
|
|
|
|
private XMPPConnection con;
|
|
|
|
private UserSearch userSearch;
|
|
|
|
|
2005-09-19 06:06:59 +02:00
|
|
|
/**
|
|
|
|
* Creates a new UserSearchManager.
|
|
|
|
*
|
2006-09-14 17:19:39 +02:00
|
|
|
* @param con the XMPPConnection to use.
|
2005-09-19 06:06:59 +02:00
|
|
|
*/
|
2005-09-20 23:02:30 +02:00
|
|
|
public UserSearchManager(XMPPConnection con) {
|
2005-09-17 01:04:30 +02:00
|
|
|
this.con = con;
|
|
|
|
userSearch = new UserSearch();
|
|
|
|
}
|
|
|
|
|
2005-09-19 06:06:59 +02:00
|
|
|
/**
|
|
|
|
* Returns the form to fill out to perform a search.
|
|
|
|
*
|
2005-09-20 23:02:30 +02:00
|
|
|
* @param searchService the search service to query.
|
2005-09-19 06:06:59 +02:00
|
|
|
* @return the form to fill out to perform a search.
|
|
|
|
* @throws XMPPException thrown if a server error has occurred.
|
|
|
|
*/
|
2005-09-20 23:02:30 +02:00
|
|
|
public Form getSearchForm(String searchService) throws XMPPException {
|
2005-09-19 06:06:59 +02:00
|
|
|
return userSearch.getSearchForm(con, searchService);
|
2005-09-17 01:04:30 +02:00
|
|
|
}
|
|
|
|
|
2005-09-19 06:06:59 +02:00
|
|
|
/**
|
|
|
|
* Submits a search form to the server and returns the resulting information
|
|
|
|
* in the form of <code>ReportedData</code>
|
|
|
|
*
|
2006-09-14 17:19:39 +02:00
|
|
|
* @param searchForm the <code>Form</code> to submit for searching.
|
2005-09-20 23:02:30 +02:00
|
|
|
* @param searchService the name of the search service to use.
|
2005-09-19 06:06:59 +02:00
|
|
|
* @return the ReportedData returned by the server.
|
|
|
|
* @throws XMPPException thrown if a server error has occurred.
|
|
|
|
*/
|
2005-09-20 23:02:30 +02:00
|
|
|
public ReportedData getSearchResults(Form searchForm, String searchService) throws XMPPException {
|
2005-09-19 06:06:59 +02:00
|
|
|
return userSearch.sendSearchForm(con, searchForm, searchService);
|
|
|
|
}
|
2005-09-17 01:04:30 +02:00
|
|
|
|
2005-09-20 23:02:30 +02:00
|
|
|
|
2005-09-19 06:06:59 +02:00
|
|
|
/**
|
|
|
|
* 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 {
|
2006-09-14 17:19:39 +02:00
|
|
|
final List<String> searchServices = new ArrayList<String>();
|
2005-09-19 06:06:59 +02:00
|
|
|
ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(con);
|
|
|
|
DiscoverItems items = discoManager.discoverItems(con.getServiceName());
|
2006-09-16 00:42:06 +02:00
|
|
|
Iterator<DiscoverItems.Item> iter = items.getItems();
|
2006-09-14 17:19:39 +02:00
|
|
|
while (iter.hasNext()) {
|
2006-09-16 00:42:06 +02:00
|
|
|
DiscoverItems.Item item = iter.next();
|
2005-10-30 02:39:49 +02:00
|
|
|
try {
|
2006-09-16 00:42:06 +02:00
|
|
|
DiscoverInfo info;
|
2006-09-14 17:19:39 +02:00
|
|
|
try {
|
|
|
|
info = discoManager.discoverInfo(item.getEntityID());
|
|
|
|
}
|
|
|
|
catch (XMPPException e) {
|
|
|
|
// Ignore Case
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2005-10-30 02:39:49 +02:00
|
|
|
if (info.containsFeature("jabber:iq:search")) {
|
|
|
|
searchServices.add(item.getEntityID());
|
|
|
|
}
|
|
|
|
}
|
2006-09-14 17:19:39 +02:00
|
|
|
catch (Exception e) {
|
2005-10-30 02:39:49 +02:00
|
|
|
// No info found.
|
|
|
|
break;
|
2005-09-19 06:06:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return searchServices;
|
2005-09-17 01:04:30 +02:00
|
|
|
}
|
|
|
|
}
|