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 java.util.List;

  19. import org.jivesoftware.smack.SmackException.NoResponseException;
  20. import org.jivesoftware.smack.SmackException.NotConnectedException;
  21. import org.jivesoftware.smack.XMPPConnection;
  22. import org.jivesoftware.smack.XMPPException.XMPPErrorException;

  23. import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
  24. import org.jivesoftware.smackx.xdata.form.FillableForm;
  25. import org.jivesoftware.smackx.xdata.form.Form;
  26. import org.jivesoftware.smackx.xdata.packet.DataForm;

  27. import org.jxmpp.jid.DomainBareJid;

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

  48.     private final XMPPConnection con;
  49.     private final UserSearch userSearch;

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

  59.     /**
  60.      * Returns the form to fill out to perform a search.
  61.      *
  62.      * @param searchService the search service to query.
  63.      * @return the form to fill out to perform a search.
  64.      * @throws XMPPErrorException if there was an XMPP error returned.
  65.      * @throws NoResponseException if there was no response from the remote entity.
  66.      * @throws NotConnectedException if the XMPP connection is not connected.
  67.      * @throws InterruptedException if the calling thread was interrupted.
  68.      */
  69.     public Form getSearchForm(DomainBareJid searchService) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException  {
  70.         DataForm dataForm = userSearch.getSearchForm(con, searchService);
  71.         return new Form(dataForm);
  72.     }

  73.     /**
  74.      * Submits a search form to the server and returns the resulting information
  75.      * in the form of <code>ReportedData</code>.
  76.      *
  77.      * @param searchForm    the <code>Form</code> to submit for searching.
  78.      * @param searchService the name of the search service to use.
  79.      * @return the ReportedData returned by the server.
  80.      * @throws XMPPErrorException if there was an XMPP error returned.
  81.      * @throws NoResponseException if there was no response from the remote entity.
  82.      * @throws NotConnectedException if the XMPP connection is not connected.
  83.      * @throws InterruptedException if the calling thread was interrupted.
  84.      */
  85.     public ReportedData getSearchResults(FillableForm searchForm, DomainBareJid searchService)
  86.                     throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  87.         DataForm dataForm = searchForm.getDataFormToSubmit();
  88.         return userSearch.sendSearchForm(con, dataForm, searchService);
  89.     }

  90.     /**
  91.      * Returns a collection of search services found on the server.
  92.      *
  93.      * @return a Collection of search services found on the server.
  94.      * @throws XMPPErrorException if there was an XMPP error returned.
  95.      * @throws NoResponseException if there was no response from the remote entity.
  96.      * @throws NotConnectedException if the XMPP connection is not connected.
  97.      * @throws InterruptedException if the calling thread was interrupted.
  98.      */
  99.     public List<DomainBareJid> getSearchServices() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException  {
  100.         ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(con);
  101.         return discoManager.findServices(UserSearch.NAMESPACE, false, false);
  102.     }
  103. }