Home | History | Annotate | Download | only in smackx
      1 /**
      2  * $RCSfile$
      3  * $Revision$
      4  * $Date$
      5  *
      6  * Copyright 2005 Jive Software.
      7  *
      8  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
      9  * you may not use this file except in compliance with the License.
     10  * You may obtain a copy of the License at
     11  *
     12  *     http://www.apache.org/licenses/LICENSE-2.0
     13  *
     14  * Unless required by applicable law or agreed to in writing, software
     15  * distributed under the License is distributed on an "AS IS" BASIS,
     16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     17  * See the License for the specific language governing permissions and
     18  * limitations under the License.
     19  */
     20 package org.jivesoftware.smackx;
     21 
     22 import org.jivesoftware.smack.Connection;
     23 import org.jivesoftware.smack.PacketCollector;
     24 import org.jivesoftware.smack.SmackConfiguration;
     25 import org.jivesoftware.smack.XMPPException;
     26 import org.jivesoftware.smack.filter.PacketIDFilter;
     27 import org.jivesoftware.smack.packet.IQ;
     28 import org.jivesoftware.smackx.packet.SharedGroupsInfo;
     29 
     30 import java.util.List;
     31 
     32 /**
     33  * A SharedGroupManager provides services for discovering the shared groups where a user belongs.<p>
     34  *
     35  * Important note: This functionality is not part of the XMPP spec and it will only work
     36  * with Wildfire.
     37  *
     38  * @author Gaston Dombiak
     39  */
     40 public class SharedGroupManager {
     41 
     42     /**
     43      * Returns the collection that will contain the name of the shared groups where the user
     44      * logged in with the specified session belongs.
     45      *
     46      * @param connection connection to use to get the user's shared groups.
     47      * @return collection with the shared groups' name of the logged user.
     48      */
     49     public static List<String> getSharedGroups(Connection connection) throws XMPPException {
     50         // Discover the shared groups of the logged user
     51         SharedGroupsInfo info = new SharedGroupsInfo();
     52         info.setType(IQ.Type.GET);
     53 
     54         // Create a packet collector to listen for a response.
     55         PacketCollector collector =
     56             connection.createPacketCollector(new PacketIDFilter(info.getPacketID()));
     57 
     58         connection.sendPacket(info);
     59 
     60         // Wait up to 5 seconds for a result.
     61         IQ result = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
     62         // Stop queuing results
     63         collector.cancel();
     64         if (result == null) {
     65             throw new XMPPException("No response from the server.");
     66         }
     67         if (result.getType() == IQ.Type.ERROR) {
     68             throw new XMPPException(result.getError());
     69         }
     70         return ((SharedGroupsInfo) result).getGroups();
     71     }
     72 }
     73