Shuyi Chen | d7955ce | 2013-05-22 14:51:55 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 2003-2007 Jive Software. |
| 3 | * |
| 4 | * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.jivesoftware.smackx.search; |
| 18 | |
| 19 | import org.jivesoftware.smack.Connection; |
| 20 | import org.jivesoftware.smack.XMPPException; |
| 21 | import org.jivesoftware.smackx.Form; |
| 22 | import org.jivesoftware.smackx.ReportedData; |
| 23 | import org.jivesoftware.smackx.ServiceDiscoveryManager; |
| 24 | import org.jivesoftware.smackx.packet.DiscoverInfo; |
| 25 | import org.jivesoftware.smackx.packet.DiscoverItems; |
| 26 | |
| 27 | import java.util.ArrayList; |
| 28 | import java.util.Collection; |
| 29 | import java.util.Iterator; |
| 30 | import java.util.List; |
| 31 | |
| 32 | /** |
| 33 | * The UserSearchManager is a facade built upon Jabber Search Services (JEP-055) to allow for searching |
| 34 | * repositories on a Jabber Server. This implementation allows for transparency of implementation of |
| 35 | * searching (DataForms or No DataForms), but allows the user to simply use the DataForm model for both |
| 36 | * types of support. |
| 37 | * <pre> |
| 38 | * Connection con = new XMPPConnection("jabber.org"); |
| 39 | * con.login("john", "doe"); |
| 40 | * UserSearchManager search = new UserSearchManager(con, "users.jabber.org"); |
| 41 | * Form searchForm = search.getSearchForm(); |
| 42 | * Form answerForm = searchForm.createAnswerForm(); |
| 43 | * answerForm.setAnswer("last", "DeMoro"); |
| 44 | * ReportedData data = search.getSearchResults(answerForm); |
| 45 | * // Use Returned Data |
| 46 | * </pre> |
| 47 | * |
| 48 | * @author Derek DeMoro |
| 49 | */ |
| 50 | public class UserSearchManager { |
| 51 | |
| 52 | private Connection con; |
| 53 | private UserSearch userSearch; |
| 54 | |
| 55 | /** |
| 56 | * Creates a new UserSearchManager. |
| 57 | * |
| 58 | * @param con the Connection to use. |
| 59 | */ |
| 60 | public UserSearchManager(Connection con) { |
| 61 | this.con = con; |
| 62 | userSearch = new UserSearch(); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Returns the form to fill out to perform a search. |
| 67 | * |
| 68 | * @param searchService the search service to query. |
| 69 | * @return the form to fill out to perform a search. |
| 70 | * @throws XMPPException thrown if a server error has occurred. |
| 71 | */ |
| 72 | public Form getSearchForm(String searchService) throws XMPPException { |
| 73 | return userSearch.getSearchForm(con, searchService); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Submits a search form to the server and returns the resulting information |
| 78 | * in the form of <code>ReportedData</code> |
| 79 | * |
| 80 | * @param searchForm the <code>Form</code> to submit for searching. |
| 81 | * @param searchService the name of the search service to use. |
| 82 | * @return the ReportedData returned by the server. |
| 83 | * @throws XMPPException thrown if a server error has occurred. |
| 84 | */ |
| 85 | public ReportedData getSearchResults(Form searchForm, String searchService) throws XMPPException { |
| 86 | return userSearch.sendSearchForm(con, searchForm, searchService); |
| 87 | } |
| 88 | |
| 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 XMPPException thrown if a server error has occurred. |
| 95 | */ |
| 96 | public Collection<String> getSearchServices() throws XMPPException { |
| 97 | final List<String> searchServices = new ArrayList<String>(); |
| 98 | ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(con); |
| 99 | DiscoverItems items = discoManager.discoverItems(con.getServiceName()); |
| 100 | Iterator<DiscoverItems.Item> iter = items.getItems(); |
| 101 | while (iter.hasNext()) { |
| 102 | DiscoverItems.Item item = iter.next(); |
| 103 | try { |
| 104 | DiscoverInfo info; |
| 105 | try { |
| 106 | info = discoManager.discoverInfo(item.getEntityID()); |
| 107 | } |
| 108 | catch (XMPPException e) { |
| 109 | // Ignore Case |
| 110 | continue; |
| 111 | } |
| 112 | |
| 113 | if (info.containsFeature("jabber:iq:search")) { |
| 114 | searchServices.add(item.getEntityID()); |
| 115 | } |
| 116 | } |
| 117 | catch (Exception e) { |
| 118 | // No info found. |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | return searchServices; |
| 123 | } |
| 124 | } |