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 org.jivesoftware.smack.SmackException.NoResponseException;
020import org.jivesoftware.smack.SmackException.NotConnectedException;
021import org.jivesoftware.smack.XMPPConnection;
022import org.jivesoftware.smack.XMPPException.XMPPErrorException;
023import org.jivesoftware.smack.packet.IQ;
024import org.jivesoftware.smackx.sharedgroups.packet.SharedGroupsInfo;
025
026import java.util.List;
027
028/**
029 * A SharedGroupManager provides services for discovering the shared groups where a user belongs.<p>
030 *
031 * Important note: This functionality is not part of the XMPP spec and it will only work
032 * with Wildfire.
033 *
034 * @author Gaston Dombiak
035 */
036public class SharedGroupManager {
037
038    /**
039     * Returns the collection that will contain the name of the shared groups where the user
040     * logged in with the specified session belongs.
041     *
042     * @param connection connection to use to get the user's shared groups.
043     * @return collection with the shared groups' name of the logged user.
044     * @throws XMPPErrorException 
045     * @throws NoResponseException 
046     * @throws NotConnectedException 
047     */
048    public static List<String> getSharedGroups(XMPPConnection connection) throws NoResponseException, XMPPErrorException, NotConnectedException {
049        // Discover the shared groups of the logged user
050        SharedGroupsInfo info = new SharedGroupsInfo();
051        info.setType(IQ.Type.GET);
052
053        SharedGroupsInfo result = (SharedGroupsInfo) connection.createPacketCollectorAndSend(info).nextResultOrThrow();
054        return result.getGroups();
055    }
056}