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.packet.DataForm;
028
029import org.jxmpp.jid.DomainBareJid;
030
031/**
032 * The UserSearchManager is a facade built upon Jabber Search Services (XEP-055) to allow for searching
033 * repositories on a Jabber Server. This implementation allows for transparency of implementation of
034 * searching (DataForms or No DataForms), but allows the user to simply use the DataForm model for both
035 * types of support.
036 * <pre>
037 * XMPPConnection con = new XMPPTCPConnection("jabber.org");
038 * con.login("john", "doe");
039 * UserSearchManager search = new UserSearchManager(con, "users.jabber.org");
040 * Form searchForm = search.getSearchForm();
041 * Form answerForm = searchForm.createAnswerForm();
042 * answerForm.setAnswer("last", "DeMoro");
043 * ReportedData data = search.getSearchResults(answerForm);
044 * // Use Returned Data
045 * </pre>
046 *
047 * @author Derek DeMoro
048 */
049public class UserSearchManager {
050
051    private final XMPPConnection con;
052    private final UserSearch userSearch;
053
054    /**
055     * Creates a new UserSearchManager.
056     *
057     * @param con the XMPPConnection to use.
058     */
059    public UserSearchManager(XMPPConnection con) {
060        this.con = con;
061        userSearch = new UserSearch();
062    }
063
064    /**
065     * Returns the form to fill out to perform a search.
066     *
067     * @param searchService the search service to query.
068     * @return the form to fill out to perform a search.
069     * @throws XMPPErrorException if there was an XMPP error returned.
070     * @throws NoResponseException if there was no response from the remote entity.
071     * @throws NotConnectedException if the XMPP connection is not connected.
072     * @throws InterruptedException if the calling thread was interrupted.
073     */
074    public DataForm getSearchForm(DomainBareJid searchService) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException  {
075        return userSearch.getSearchForm(con, searchService);
076    }
077
078    /**
079     * Submits a search form to the server and returns the resulting information
080     * in the form of <code>ReportedData</code>.
081     *
082     * @param searchForm    the <code>Form</code> to submit for searching.
083     * @param searchService the name of the search service to use.
084     * @return the ReportedData returned by the server.
085     * @throws XMPPErrorException if there was an XMPP error returned.
086     * @throws NoResponseException if there was no response from the remote entity.
087     * @throws NotConnectedException if the XMPP connection is not connected.
088     * @throws InterruptedException if the calling thread was interrupted.
089     */
090    public ReportedData getSearchResults(DataForm searchForm, DomainBareJid searchService) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException  {
091        return userSearch.sendSearchForm(con, searchForm, searchService);
092    }
093
094
095    /**
096     * Returns a collection of search services found on the server.
097     *
098     * @return a Collection of search services found on the server.
099     * @throws XMPPErrorException if there was an XMPP error returned.
100     * @throws NoResponseException if there was no response from the remote entity.
101     * @throws NotConnectedException if the XMPP connection is not connected.
102     * @throws InterruptedException if the calling thread was interrupted.
103     */
104    public List<DomainBareJid> getSearchServices() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException  {
105        ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(con);
106        return discoManager.findServices(UserSearch.NAMESPACE, false, false);
107    }
108}