001/** 002 * 003 * Copyright 2003-2007 Jive Software. 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smackx.search; 018 019import java.util.List; 020 021import org.jivesoftware.smack.SmackException.NoResponseException; 022import org.jivesoftware.smack.SmackException.NotConnectedException; 023import org.jivesoftware.smack.XMPPConnection; 024import org.jivesoftware.smack.XMPPException.XMPPErrorException; 025 026import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; 027import org.jivesoftware.smackx.xdata.form.FillableForm; 028import org.jivesoftware.smackx.xdata.form.Form; 029import org.jivesoftware.smackx.xdata.packet.DataForm; 030 031import org.jxmpp.jid.DomainBareJid; 032 033/** 034 * The UserSearchManager is a facade built upon Jabber Search Services (XEP-055) to allow for searching 035 * repositories on a Jabber Server. This implementation allows for transparency of implementation of 036 * searching (DataForms or No DataForms), but allows the user to simply use the DataForm model for both 037 * types of support. 038 * <pre> 039 * XMPPConnection con = new XMPPTCPConnection("jabber.org"); 040 * con.login("john", "doe"); 041 * UserSearchManager search = new UserSearchManager(con, "users.jabber.org"); 042 * Form searchForm = search.getSearchForm(); 043 * FillableForm answerForm = searchForm.getFillableForm() 044 * // Fill out the form. 045 * answerForm.setAnswer("last", "DeMoro"); 046 * ReportedData data = search.getSearchResults(answerForm); 047 * // Use Returned Data 048 * </pre> 049 * 050 * @author Derek DeMoro 051 */ 052public class UserSearchManager { 053 054 private final XMPPConnection con; 055 private final UserSearch userSearch; 056 057 /** 058 * Creates a new UserSearchManager. 059 * 060 * @param con the XMPPConnection to use. 061 */ 062 public UserSearchManager(XMPPConnection con) { 063 this.con = con; 064 userSearch = new UserSearch(); 065 } 066 067 /** 068 * Returns the form to fill out to perform a search. 069 * 070 * @param searchService the search service to query. 071 * @return the form to fill out to perform a search. 072 * @throws XMPPErrorException if there was an XMPP error returned. 073 * @throws NoResponseException if there was no response from the remote entity. 074 * @throws NotConnectedException if the XMPP connection is not connected. 075 * @throws InterruptedException if the calling thread was interrupted. 076 */ 077 public Form getSearchForm(DomainBareJid searchService) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { 078 DataForm dataForm = userSearch.getSearchForm(con, searchService); 079 return new Form(dataForm); 080 } 081 082 /** 083 * Submits a search form to the server and returns the resulting information 084 * in the form of <code>ReportedData</code>. 085 * 086 * @param searchForm the <code>Form</code> to submit for searching. 087 * @param searchService the name of the search service to use. 088 * @return the ReportedData returned by the server. 089 * @throws XMPPErrorException if there was an XMPP error returned. 090 * @throws NoResponseException if there was no response from the remote entity. 091 * @throws NotConnectedException if the XMPP connection is not connected. 092 * @throws InterruptedException if the calling thread was interrupted. 093 */ 094 public ReportedData getSearchResults(FillableForm searchForm, DomainBareJid searchService) 095 throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { 096 DataForm dataForm = searchForm.getDataFormToSubmit(); 097 return userSearch.sendSearchForm(con, dataForm, searchService); 098 } 099 100 /** 101 * Returns a collection of search services found on the server. 102 * 103 * @return a Collection of search services found on the server. 104 * @throws XMPPErrorException if there was an XMPP error returned. 105 * @throws NoResponseException if there was no response from the remote entity. 106 * @throws NotConnectedException if the XMPP connection is not connected. 107 * @throws InterruptedException if the calling thread was interrupted. 108 */ 109 public List<DomainBareJid> getSearchServices() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { 110 ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(con); 111 return discoManager.findServices(UserSearch.NAMESPACE, false, false); 112 } 113}