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.io.IOException;
020
021import org.jivesoftware.smack.SmackException.NoResponseException;
022import org.jivesoftware.smack.SmackException.NotConnectedException;
023import org.jivesoftware.smack.XMPPConnection;
024import org.jivesoftware.smack.XMPPException.XMPPErrorException;
025import org.jivesoftware.smack.packet.IQ;
026import org.jivesoftware.smack.packet.IqData;
027import org.jivesoftware.smack.packet.SimpleIQ;
028import org.jivesoftware.smack.packet.XmlEnvironment;
029import org.jivesoftware.smack.parsing.SmackParsingException;
030import org.jivesoftware.smack.provider.IqProvider;
031import org.jivesoftware.smack.util.PacketParserUtils;
032import org.jivesoftware.smack.xml.XmlPullParser;
033import org.jivesoftware.smack.xml.XmlPullParserException;
034
035import org.jivesoftware.smackx.xdata.packet.DataForm;
036
037import org.jxmpp.jid.DomainBareJid;
038
039/**
040 * Implements the protocol currently used to search information repositories on the Jabber network. To date, the jabber:iq:search protocol
041 * has been used mainly to search for people who have registered with user directories (e.g., the "Jabber User Directory" hosted at users.jabber.org).
042 * However, the jabber:iq:search protocol is not limited to user directories, and could be used to search other Jabber information repositories
043 * (such as chatroom directories) or even to provide a Jabber interface to conventional search engines.
044 *
045 * The basic functionality is to query an information repository regarding the possible search fields, to send a search query, and to receive search results.
046 *
047 * @author Derek DeMoro
048 */
049public class UserSearch extends SimpleIQ {
050
051    public static final String ELEMENT = QUERY_ELEMENT;
052    public static final String NAMESPACE = "jabber:iq:search";
053
054    /**
055     * Creates a new instance of UserSearch.
056     */
057    public UserSearch() {
058        super(ELEMENT, NAMESPACE);
059    }
060
061    /**
062     * Returns the form for all search fields supported by the search service.
063     *
064     * @param con           the current XMPPConnection.
065     * @param searchService the search service to use. (ex. search.jivesoftware.com)
066     * @return the search form received by the server.
067     * @throws XMPPErrorException if there was an XMPP error returned.
068     * @throws NoResponseException if there was no response from the remote entity.
069     * @throws NotConnectedException if the XMPP connection is not connected.
070     * @throws InterruptedException if the calling thread was interrupted.
071     */
072    public DataForm getSearchForm(XMPPConnection con, DomainBareJid searchService) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
073        UserSearch search = new UserSearch();
074        search.setType(IQ.Type.get);
075        search.setTo(searchService);
076
077        IQ response = con.sendIqRequestAndWaitForResponse(search);
078        return DataForm.from(response, NAMESPACE);
079    }
080
081    /**
082     * Sends the filled out answer form to be sent and queried by the search service.
083     *
084     * @param con           the current XMPPConnection.
085     * @param searchForm    the <code>Form</code> to send for querying.
086     * @param searchService the search service to use. (ex. search.jivesoftware.com)
087     * @return ReportedData the data found from the query.
088     * @throws XMPPErrorException if there was an XMPP error returned.
089     * @throws NoResponseException if there was no response from the remote entity.
090     * @throws NotConnectedException if the XMPP connection is not connected.
091     * @throws InterruptedException if the calling thread was interrupted.
092     */
093    public ReportedData sendSearchForm(XMPPConnection con, DataForm searchForm, DomainBareJid searchService) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
094        UserSearch search = new UserSearch();
095        search.setType(IQ.Type.set);
096        search.setTo(searchService);
097        search.addExtension(searchForm);
098
099        IQ response = con.sendIqRequestAndWaitForResponse(search);
100        return ReportedData.getReportedDataFrom(response);
101    }
102
103    /**
104     * Sends the filled out answer form to be sent and queried by the search service.
105     *
106     * @param con           the current XMPPConnection.
107     * @param searchForm    the <code>Form</code> to send for querying.
108     * @param searchService the search service to use. (ex. search.jivesoftware.com)
109     * @return ReportedData the data found from the query.
110     * @throws XMPPErrorException if there was an XMPP error returned.
111     * @throws NoResponseException if there was no response from the remote entity.
112     * @throws NotConnectedException if the XMPP connection is not connected.
113     * @throws InterruptedException if the calling thread was interrupted.
114     */
115    public ReportedData sendSimpleSearchForm(XMPPConnection con, DataForm searchForm, DomainBareJid searchService) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
116        SimpleUserSearch search = new SimpleUserSearch();
117        search.setForm(searchForm);
118        search.setType(IQ.Type.set);
119        search.setTo(searchService);
120
121        SimpleUserSearch response = con.sendIqRequestAndWaitForResponse(search);
122        return response.getReportedData();
123    }
124
125    /**
126     * Internal Search service Provider.
127     */
128    public static class Provider extends IqProvider<IQ> {
129
130        // FIXME this provider does return two different types of IQs
131        @Override
132        public IQ parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
133            UserSearch search = null;
134            SimpleUserSearch simpleUserSearch = new SimpleUserSearch();
135
136            boolean done = false;
137            while (!done) {
138                XmlPullParser.Event eventType = parser.next();
139                if (eventType == XmlPullParser.Event.START_ELEMENT && parser.getName().equals("item")) {
140                    simpleUserSearch.parseItems(parser);
141                    return simpleUserSearch;
142                }
143                else if (eventType == XmlPullParser.Event.START_ELEMENT && parser.getNamespace().equals("jabber:x:data")) {
144                    // Otherwise, it must be a packet extension.
145                    search = new UserSearch();
146                    PacketParserUtils.addExtensionElement(search, parser, xmlEnvironment);
147                }
148                else if (eventType == XmlPullParser.Event.END_ELEMENT) {
149                    if (parser.getName().equals("query")) {
150                        done = true;
151                    }
152                }
153            }
154
155            if (search != null) {
156                return search;
157            }
158            return simpleUserSearch;
159        }
160    }
161
162}