Home | History | Annotate | Download | only in packet
      1 /**
      2  * $Revision$
      3  * $Date$
      4  *
      5  * Copyright 2003-2007 Jive Software.
      6  *
      7  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
      8  * you may not use this file except in compliance with the License.
      9  * You may obtain a copy of the License at
     10  *
     11  *     http://www.apache.org/licenses/LICENSE-2.0
     12  *
     13  * Unless required by applicable law or agreed to in writing, software
     14  * distributed under the License is distributed on an "AS IS" BASIS,
     15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16  * See the License for the specific language governing permissions and
     17  * limitations under the License.
     18  */
     19 
     20 package org.jivesoftware.smackx.workgroup.packet;
     21 
     22 import org.jivesoftware.smack.packet.IQ;
     23 import org.jivesoftware.smack.provider.IQProvider;
     24 import org.xmlpull.v1.XmlPullParser;
     25 
     26 import java.text.SimpleDateFormat;
     27 import java.util.*;
     28 
     29 /**
     30  * Packet used for requesting information about occupants of a room or for retrieving information
     31  * such information.
     32  *
     33  * @author Gaston Dombiak
     34  */
     35 public class OccupantsInfo extends IQ {
     36 
     37     private static final SimpleDateFormat UTC_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HH:mm:ss");
     38 
     39     static {
     40         UTC_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT+0"));
     41     }
     42 
     43     /**
     44      * Element name of the packet extension.
     45      */
     46     public static final String ELEMENT_NAME = "occupants-info";
     47 
     48     /**
     49      * Namespace of the packet extension.
     50      */
     51     public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";
     52 
     53     private String roomID;
     54     private final Set<OccupantInfo> occupants;
     55 
     56     public OccupantsInfo(String roomID) {
     57         this.roomID = roomID;
     58         this.occupants = new HashSet<OccupantInfo>();
     59     }
     60 
     61     public String getRoomID() {
     62         return roomID;
     63     }
     64 
     65     public int getOccupantsCount() {
     66         return occupants.size();
     67     }
     68 
     69     public Set<OccupantInfo> getOccupants() {
     70         return Collections.unmodifiableSet(occupants);
     71     }
     72 
     73     public String getChildElementXML() {
     74         StringBuilder buf = new StringBuilder();
     75         buf.append("<").append(ELEMENT_NAME).append(" xmlns=\"").append(NAMESPACE);
     76         buf.append("\" roomID=\"").append(roomID).append("\">");
     77         synchronized (occupants) {
     78             for (OccupantInfo occupant : occupants) {
     79                 buf.append("<occupant>");
     80                 // Add the occupant jid
     81                 buf.append("<jid>");
     82                 buf.append(occupant.getJID());
     83                 buf.append("</jid>");
     84                 // Add the occupant nickname
     85                 buf.append("<name>");
     86                 buf.append(occupant.getNickname());
     87                 buf.append("</name>");
     88                 // Add the date when the occupant joined the room
     89                 buf.append("<joined>");
     90                 buf.append(UTC_FORMAT.format(occupant.getJoined()));
     91                 buf.append("</joined>");
     92                 buf.append("</occupant>");
     93             }
     94         }
     95         buf.append("</").append(ELEMENT_NAME).append("> ");
     96         return buf.toString();
     97     }
     98 
     99     public static class OccupantInfo {
    100 
    101         private String jid;
    102         private String nickname;
    103         private Date joined;
    104 
    105         public OccupantInfo(String jid, String nickname, Date joined) {
    106             this.jid = jid;
    107             this.nickname = nickname;
    108             this.joined = joined;
    109         }
    110 
    111         public String getJID() {
    112             return jid;
    113         }
    114 
    115         public String getNickname() {
    116             return nickname;
    117         }
    118 
    119         public Date getJoined() {
    120             return joined;
    121         }
    122     }
    123 
    124     /**
    125      * Packet extension provider for AgentStatusRequest packets.
    126      */
    127     public static class Provider implements IQProvider {
    128 
    129         public IQ parseIQ(XmlPullParser parser) throws Exception {
    130             if (parser.getEventType() != XmlPullParser.START_TAG) {
    131                 throw new IllegalStateException("Parser not in proper position, or bad XML.");
    132             }
    133             OccupantsInfo occupantsInfo = new OccupantsInfo(parser.getAttributeValue("", "roomID"));
    134 
    135             boolean done = false;
    136             while (!done) {
    137                 int eventType = parser.next();
    138                 if ((eventType == XmlPullParser.START_TAG) &&
    139                         ("occupant".equals(parser.getName()))) {
    140                     occupantsInfo.occupants.add(parseOccupantInfo(parser));
    141                 } else if (eventType == XmlPullParser.END_TAG &&
    142                         ELEMENT_NAME.equals(parser.getName())) {
    143                     done = true;
    144                 }
    145             }
    146             return occupantsInfo;
    147         }
    148 
    149         private OccupantInfo parseOccupantInfo(XmlPullParser parser) throws Exception {
    150 
    151             boolean done = false;
    152             String jid = null;
    153             String nickname = null;
    154             Date joined = null;
    155             while (!done) {
    156                 int eventType = parser.next();
    157                 if ((eventType == XmlPullParser.START_TAG) && ("jid".equals(parser.getName()))) {
    158                     jid = parser.nextText();
    159                 } else if ((eventType == XmlPullParser.START_TAG) &&
    160                         ("nickname".equals(parser.getName()))) {
    161                     nickname = parser.nextText();
    162                 } else if ((eventType == XmlPullParser.START_TAG) &&
    163                         ("joined".equals(parser.getName()))) {
    164                     joined = UTC_FORMAT.parse(parser.nextText());
    165                 } else if (eventType == XmlPullParser.END_TAG &&
    166                         "occupant".equals(parser.getName())) {
    167                     done = true;
    168                 }
    169             }
    170             return new OccupantInfo(jid, nickname, joined);
    171         }
    172     }
    173 }
    174