001/**
002 *
003 * Copyright 2005 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.sharedgroups;
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;
025import org.jivesoftware.smack.packet.IQ;
026
027import org.jivesoftware.smackx.sharedgroups.packet.SharedGroupsInfo;
028
029/**
030 * A SharedGroupManager provides services for discovering the shared groups where a user belongs.<p>
031 *
032 * Important note: This functionality is not part of the XMPP spec and it will only work
033 * with Wildfire.
034 *
035 * @author Gaston Dombiak
036 */
037public class SharedGroupManager {
038
039    /**
040     * Returns the collection that will contain the name of the shared groups where the user
041     * logged in with the specified session belongs.
042     *
043     * @param connection connection to use to get the user's shared groups.
044     * @return collection with the shared groups' name of the logged user.
045     * @throws XMPPErrorException if there was an XMPP error returned.
046     * @throws NoResponseException if there was no response from the remote entity.
047     * @throws NotConnectedException if the XMPP connection is not connected.
048     * @throws InterruptedException if the calling thread was interrupted.
049     */
050    public static List<String> getSharedGroups(XMPPConnection connection) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
051        // Discover the shared groups of the logged user
052        SharedGroupsInfo info = new SharedGroupsInfo();
053        info.setType(IQ.Type.get);
054
055        SharedGroupsInfo result = connection.createStanzaCollectorAndSend(info).nextResultOrThrow();
056        return result.getGroups();
057    }
058}