UserSearchManager.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.search;

  18. import org.jivesoftware.smack.SmackException.NoResponseException;
  19. import org.jivesoftware.smack.SmackException.NotConnectedException;
  20. import org.jivesoftware.smack.XMPPConnection;
  21. import org.jivesoftware.smack.XMPPException.XMPPErrorException;
  22. import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
  23. import org.jivesoftware.smackx.xdata.Form;
  24. import org.jxmpp.jid.DomainBareJid;

  25. import java.util.List;

  26. /**
  27.  * The UserSearchManager is a facade built upon Jabber Search Services (XEP-055) to allow for searching
  28.  * repositories on a Jabber Server. This implementation allows for transparency of implementation of
  29.  * searching (DataForms or No DataForms), but allows the user to simply use the DataForm model for both
  30.  * types of support.
  31.  * <pre>
  32.  * XMPPConnection con = new XMPPTCPConnection("jabber.org");
  33.  * con.login("john", "doe");
  34.  * UserSearchManager search = new UserSearchManager(con, "users.jabber.org");
  35.  * Form searchForm = search.getSearchForm();
  36.  * Form answerForm = searchForm.createAnswerForm();
  37.  * answerForm.setAnswer("last", "DeMoro");
  38.  * ReportedData data = search.getSearchResults(answerForm);
  39.  * // Use Returned Data
  40.  * </pre>
  41.  *
  42.  * @author Derek DeMoro
  43.  */
  44. public class UserSearchManager {

  45.     private XMPPConnection con;
  46.     private UserSearch userSearch;

  47.     /**
  48.      * Creates a new UserSearchManager.
  49.      *
  50.      * @param con the XMPPConnection to use.
  51.      */
  52.     public UserSearchManager(XMPPConnection con) {
  53.         this.con = con;
  54.         userSearch = new UserSearch();
  55.     }

  56.     /**
  57.      * Returns the form to fill out to perform a search.
  58.      *
  59.      * @param searchService the search service to query.
  60.      * @return the form to fill out to perform a search.
  61.      * @throws XMPPErrorException
  62.      * @throws NoResponseException
  63.      * @throws NotConnectedException
  64.      * @throws InterruptedException
  65.      */
  66.     public Form getSearchForm(DomainBareJid searchService) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException  {
  67.         return userSearch.getSearchForm(con, searchService);
  68.     }

  69.     /**
  70.      * Submits a search form to the server and returns the resulting information
  71.      * in the form of <code>ReportedData</code>
  72.      *
  73.      * @param searchForm    the <code>Form</code> to submit for searching.
  74.      * @param searchService the name of the search service to use.
  75.      * @return the ReportedData returned by the server.
  76.      * @throws XMPPErrorException
  77.      * @throws NoResponseException
  78.      * @throws NotConnectedException
  79.      * @throws InterruptedException
  80.      */
  81.     public ReportedData getSearchResults(Form searchForm, DomainBareJid searchService) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException  {
  82.         return userSearch.sendSearchForm(con, searchForm, searchService);
  83.     }


  84.     /**
  85.      * Returns a collection of search services found on the server.
  86.      *
  87.      * @return a Collection of search services found on the server.
  88.      * @throws XMPPErrorException
  89.      * @throws NoResponseException
  90.      * @throws NotConnectedException
  91.      * @throws InterruptedException
  92.      */
  93.     public List<DomainBareJid> getSearchServices() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException  {
  94.         ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(con);
  95.         return discoManager.findServices(UserSearch.NAMESPACE, false, false);
  96.     }
  97. }